From 1d1f46892fd01a23dbd0df4286fc70e5ff784709 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 27 Jun 2022 23:17:10 +0800 Subject: [PATCH] :bug: fix https://github.com/siyuan-note/siyuan/issues/5304 --- app/src/protyle/wysiwyg/keydown.ts | 25 ++++++++++++++----------- app/src/search/index.ts | 10 ++++++++-- app/src/search/spread.ts | 10 ++++++++-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index b08f8efe3..a0281d20a 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -536,7 +536,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { nodeElement.contains(firstEditElement) ) || (!firstEditElement && nodeElement.isSameNode(protyle.wysiwyg.element.firstElementChild))) { - if (nodeElement.textContent.substr(0, getSelectionOffset(nodeElement, protyle.wysiwyg.element, range).end).indexOf("\n") === -1) { + if (nodeElement.textContent.substr(0, position.end).indexOf("\n") === -1) { if (protyle.title && (protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") === "true" || protyle.contentElement.scrollTop === 0)) { protyle.title.editElement.focus(); @@ -546,16 +546,19 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { } } } else { - let previousElement: HTMLElement = getPreviousBlock(nodeElement) as HTMLElement; - if (previousElement) { - previousElement = getLastBlock(previousElement) as HTMLElement; + // 遇到折叠块 + if (nodeElement.textContent.substr(0, position.end).indexOf("\n") === -1) { + let previousElement: HTMLElement = getPreviousBlock(nodeElement) as HTMLElement; if (previousElement) { - previousElement = hasClosestByAttribute(previousElement, "fold", "1") as HTMLElement; - if (previousElement && previousElement.getAttribute("data-type") !== "NodeListItem") { - // 遇到折叠块 - focusBlock(previousElement, undefined, true); - event.stopPropagation(); - event.preventDefault(); + previousElement = getLastBlock(previousElement) as HTMLElement; + if (previousElement) { + previousElement = hasClosestByAttribute(previousElement, "fold", "1") as HTMLElement; + if (previousElement && previousElement.getAttribute("data-type") !== "NodeListItem") { + previousElement.scrollTop = 0; + focusBlock(previousElement, undefined, true); + event.stopPropagation(); + event.preventDefault(); + } } } } @@ -1325,7 +1328,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { event.preventDefault(); const selectsElement: HTMLElement[] = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select")); if (selectsElement.length < 2) { - return; + return; } turnsIntoTransaction({ protyle, selectsElement, diff --git a/app/src/search/index.ts b/app/src/search/index.ts index 4c038df9f..a938efe4a 100644 --- a/app/src/search/index.ts +++ b/app/src/search/index.ts @@ -189,7 +189,10 @@ export class Search extends Model { size: foldResponse.data ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, }, getResponse => { onGet(getResponse, this.protyle.protyle, foldResponse.data ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL]); - this.protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${id}"] span[data-type="search-mark"]`).scrollIntoView(); + const matchElement = this.protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${id}"] span[data-type="search-mark"]`) + if (matchElement) { + matchElement.scrollIntoView(); + } }); } else { this.protyle = new Protyle(this.element.querySelector("#searchPreview") as HTMLElement, { @@ -201,7 +204,10 @@ export class Search extends Model { breadcrumbDocName: true, }, after: () => { - this.protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${id}"] span[data-type="search-mark"]`).scrollIntoView(); + const matchElement = this.protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${id}"] span[data-type="search-mark"]`) + if (matchElement) { + matchElement.scrollIntoView(); + } } }); } diff --git a/app/src/search/spread.ts b/app/src/search/spread.ts index b4d10a03a..1023370a6 100644 --- a/app/src/search/spread.ts +++ b/app/src/search/spread.ts @@ -648,7 +648,10 @@ const getArticle = (options: { breadcrumbDocName: true }, after: () => { - protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${options.id}"] span[data-type="search-mark"]`).scrollIntoView(); + const matchElement = protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${options.id}"] span[data-type="search-mark"]`) + if (matchElement) { + matchElement.scrollIntoView(); + } } }); } else { @@ -661,7 +664,10 @@ const getArticle = (options: { size: foldResponse.data ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, }, getResponse => { onGet(getResponse, protyle.protyle, foldResponse.data ? [Constants.CB_GET_ALL] : [Constants.CB_GET_HL]); - protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${options.id}"] span[data-type="search-mark"]`).scrollIntoView(); + const matchElement = protyle.protyle.wysiwyg.element.querySelector(`div[data-node-id="${options.id}"] span[data-type="search-mark"]`) + if (matchElement) { + matchElement.scrollIntoView(); + } }); } });