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