From b509d8f436663fd228c81d9c7144cb336bff37a9 Mon Sep 17 00:00:00 2001 From: rlx Date: Thu, 24 Jan 2019 17:00:47 +0530 Subject: [PATCH] cover more exotic selections --- txt.js/txt.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/txt.js/txt.js b/txt.js/txt.js index 5ebabc9..24ae37f 100644 --- a/txt.js/txt.js +++ b/txt.js/txt.js @@ -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) { //
index++