cover more exotic selections

This commit is contained in:
rlx 2019-01-24 17:00:47 +05:30
parent c4cad4985d
commit b509d8f436

View file

@ -1,7 +1,12 @@
let txtjs = {}
Ox.load({UI: {loadCSS: false}}, function() {
// ...
Ox.$parent.bindMessage(function(data, event) {
console.log(event, data)
if (event == 'selectAnnotation') {
}
})
})
txtjs.open = function(url) {
@ -21,15 +26,6 @@ txtjs.mark = function(notes) {
txtjs.notes = []
;(function() {
let listener = addEventListener || attachEvent
let event = addEventListener ? 'message' : 'onmessage'
listener(event, function(e) {
let message = JSON.parse(e.message || e.data)
txtjs.onMessage(message.action, message.data)
})
}())
txtjs.addNoteFromSelection = function() {
let note = txtjs.getNoteFromSelection()
if (!note || txtjs.noteExists(note)) {
@ -134,17 +130,21 @@ txtjs.getNoteFromSelection = function() {
txtjs.getPosition = function(range) {
let container = document.querySelector('#txt')
let nodes = Array.from(container.childNodes)
let startNodeIndex = nodes.indexOf(range.startContainer)
let endNodeIndex = nodes.indexOf(range.endContainer)
let startNodeIndex = range.startContainer == container
? range.startOffset : nodes.indexOf(range.startContainer)
let endNodeIndex = range.endContainer == container
? range.endOffset : nodes.indexOf(range.endContainer)
let startOffset = range.startContainer == container ? 0 : range.startOffset
let endOffset = range.endContainer == container ? 0 : range.endOffset
let index = 0
let start = 0
let end = 0
for (let i = 0; i <= endNodeIndex; i++) {
if (i == startNodeIndex) {
start = index + range.startOffset
start = index + startOffset
}
if (i == endNodeIndex) {
end = index + range.endOffset
end = index + endOffset
}
if (nodes[i].nodeType == 1) { // <br>
index++