From 7c66352b7c588056ef69c18de582d5f758736b7c Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Fri, 6 May 2022 00:37:19 +0800 Subject: [PATCH] fix: scroll bug --- src/views.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/views.ts b/src/views.ts index 9893320..762f8e5 100644 --- a/src/views.ts +++ b/src/views.ts @@ -158,13 +158,36 @@ class AddonViews extends AddonBase { async scrollToLine(instance: EditorInstance, lineIndex: number) { await instance._initPromise; let editor = this.getEditor(instance._iframeWindow.document); - if (lineIndex > editor.children.length) { - lineIndex = editor.children.length - 1; - } else if (lineIndex < 0) { + const indexMap = []; + const diveTagNames = ["OL", "UL", "LI"]; + + function dive(e: Element, targetIndex: string) { + if (diveTagNames.indexOf(e.tagName) !== -1) { + if (e.tagName === "LI") { + indexMap.push(targetIndex); + } + for (let i in e.children) { + dive(e.children[i], targetIndex); + } + } + } + for (let i in editor.children) { + const ele = editor.children[i]; + if (diveTagNames.indexOf(ele.tagName) !== -1) { + dive(ele, i); + } else { + indexMap.push(i); + } + } + if (lineIndex >= indexMap.length) { + lineIndex = indexMap.length - 1; + } else if (indexMap.length < 0) { lineIndex = 0; } + const mappedIndex = indexMap[lineIndex]; + // @ts-ignore - editor.parentNode.scrollTo(0, editor.children[lineIndex].offsetTop); + editor.parentNode.scrollTo(0, editor.children[mappedIndex].offsetTop); } addNewKnowledgeButton() {