diff --git a/src/events.ts b/src/events.ts index 769a399..d973889 100644 --- a/src/events.ts +++ b/src/events.ts @@ -469,14 +469,14 @@ class AddonEvents extends AddonBase { Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") ) { // Update current line index - let focusNode = - message.content.editorInstance._iframeWindow.document.getSelection() - .focusNode; + let selection = + message.content.editorInstance._iframeWindow.document.getSelection(); + let focusNode = selection.focusNode as XUL.Element; if (!focusNode) { return; } - function getChildIndex(node: Node) { + function getChildIndex(node) { return Array.prototype.indexOf.call(node.parentNode.childNodes, node); } @@ -486,15 +486,39 @@ class AddonEvents extends AddonBase { !focusNode.parentElement.className || focusNode.parentElement.className.indexOf("primary-editor") === -1 ) { - focusNode = focusNode.parentNode; + focusNode = focusNode.parentNode as XUL.Element; } } catch (e) { return; } let currentLineIndex = getChildIndex(focusNode); - this._Addon.knowledge.currentLine = currentLineIndex; + + const tableElements = Array.prototype.filter.call( + Array.prototype.slice.call( + focusNode.parentElement.childNodes, + 0, + currentLineIndex + ), + (e) => { + return e.tagName === "UL" || e.tagName === "OL"; + } + ); + + for (const tableElement of tableElements) { + currentLineIndex += tableElement.childElementCount - 1; + } + + const tagName = focusNode.tagName; + if (tagName === "UL" || tagName === "OL") { + let liElement = selection.focusNode as XUL.Element; + while (liElement.tagName !== "LI") { + liElement = liElement.parentElement; + } + currentLineIndex += getChildIndex(liElement); + } Zotero.debug(`Knowledge4Zotero: line ${currentLineIndex} selected.`); + this._Addon.knowledge.currentLine = currentLineIndex; } } else if (message.type === "export") { /* diff --git a/src/knowledge.ts b/src/knowledge.ts index 85a8ec7..c1c53da 100644 --- a/src/knowledge.ts +++ b/src/knowledge.ts @@ -132,8 +132,50 @@ class Knowledge extends AddonBase { noteText.length - "".length ); } - let noteLines: string[] = noteText.split("\n").filter((s) => s); - return noteLines; + let noteLines: string[] = noteText.split("\n"); + + const cacheStart = [/
    /g, /