From f6e41ac4d99cb316773418922730c92f57c31473 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Fri, 6 May 2022 00:09:26 +0800 Subject: [PATCH] fix: add link bugs --- src/events.ts | 36 ++++++++++++++++++---- src/knowledge.ts | 77 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 93 insertions(+), 20 deletions(-) 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, /