From 0dfc7db2bb531a99f6be6ada9b6100689e064b0b Mon Sep 17 00:00:00 2001 From: j Date: Fri, 1 Feb 2019 14:02:10 +0530 Subject: [PATCH 1/2] avoid select loop --- txt.js/txt.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/txt.js/txt.js b/txt.js/txt.js index 3ce17c1..4e9881d 100644 --- a/txt.js/txt.js +++ b/txt.js/txt.js @@ -219,7 +219,10 @@ txtjs.noteExists = function(note) { txtjs.onMessage = function(data, event) { if (event == 'selectAnnotation') { - txtjs.selectNote(data.id) + let selected = txtjs.getSelectedNote() + if (!selected || selected.id != data.id) { + txtjs.selectNote(data.id) + } } else if (event == 'addAnnotations') { data.annotations.forEach(function(note) { txtjs.renderNote(note) From d5282b064a7b9936fcf1d3ed2563e54f00c4cc64 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 1 Feb 2019 14:18:17 +0530 Subject: [PATCH 2/2] avoid selectNote loop, handle addAnnotation event --- txt.js/txt.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/txt.js/txt.js b/txt.js/txt.js index 4e9881d..3381906 100644 --- a/txt.js/txt.js +++ b/txt.js/txt.js @@ -219,10 +219,9 @@ txtjs.noteExists = function(note) { txtjs.onMessage = function(data, event) { if (event == 'selectAnnotation') { - let selected = txtjs.getSelectedNote() - if (!selected || selected.id != data.id) { - txtjs.selectNote(data.id) - } + txtjs.selectNote(data.id, false) + } else if (event == 'addAnnotation') { + txtjs.addNoteFromSelection() } else if (event == 'addAnnotations') { data.annotations.forEach(function(note) { txtjs.renderNote(note) @@ -446,8 +445,9 @@ txtjs.renderText = function(text) { let note = txtjs.getNoteFromSelection() if (!note || txtjs.noteExists(note)) { txtjs.postMessage('selectText', null) + } else { + txtjs.postMessage('selectText', note) } - txtjs.postMessage('selectText', note) } function onResize() { factor = scrollTextElement.clientHeight / textElement.clientHeight @@ -479,7 +479,7 @@ txtjs.selectNextNote = function(direction) { txtjs.selectNote(ids[Ox.mod(ids.indexOf(id) + direction, ids.length)]) } -txtjs.selectNote = function(id) { +txtjs.selectNote = function(id, trigger) { let selected = txtjs.getSelectedNote() if (selected) { selected.elements.forEach(function(element) { @@ -501,5 +501,7 @@ txtjs.selectNote = function(id) { // elements[0].scrollIntoView() // window.scrollTo(0, elements[0].offsetTop) } - txtjs.postMessage('selectNote', {id: id}) + if (trigger !== false) { + txtjs.postMessage('selectNote', {id: id}) + } }