From a1f8339d4bcbc8ce8c780c931561eca84ee107e4 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sat, 20 May 2023 11:18:58 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/8315 --- app/src/menus/protyle.ts | 29 ++++++++++++++++------------- app/src/protyle/toolbar/Link.ts | 14 +++++++++----- app/src/protyle/toolbar/index.ts | 8 ++++++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts index 6d66ee6a7..9d37df202 100644 --- a/app/src/menus/protyle.ts +++ b/app/src/menus/protyle.ts @@ -59,17 +59,6 @@ export const refMenu = (app: App, protyle: IProtyle, element: HTMLElement) => { bind(menuItemElement) { const inputElement = menuItemElement.querySelector("input"); inputElement.value = element.getAttribute("data-subtype") === "d" ? "" : element.textContent; - inputElement.addEventListener("blur", (event) => { - if (nodeElement.outerHTML !== oldHTML) { - nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); - updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML); - oldHTML = nodeElement.outerHTML; - } - protyle.toolbar.range.selectNodeContents(element); - protyle.toolbar.range.collapse(false); - focusByRange(protyle.toolbar.range); - event.stopPropagation(); - }); inputElement.addEventListener("input", () => { if (inputElement.value) { // 不能使用 textContent,否则 < 会变为 < @@ -290,6 +279,19 @@ export const refMenu = (app: App, protyle: IProtyle, element: HTMLElement) => { h: 26 }); window.siyuan.menus.menu.element.querySelector("input").select(); + window.siyuan.menus.menu.removeCB = () => { + if (nodeElement.outerHTML !== oldHTML) { + nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); + updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML); + oldHTML = nodeElement.outerHTML; + } + const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0); + if (currentRange && !protyle.element.contains(currentRange.startContainer)) { + protyle.toolbar.range.selectNodeContents(element); + protyle.toolbar.range.collapse(false); + focusByRange(protyle.toolbar.range); + } + }; }; export const contentMenu = (protyle: IProtyle, nodeElement: Element) => { @@ -890,9 +892,10 @@ export const linkMenu = (app: App, protyle: IProtyle, linkElement: HTMLElement, linkElement.removeAttribute("data-title"); } linkElement.setAttribute("data-href", Lute.EscapeHTMLStr(textElements[0].value.replace(/\n|\r\n|\r|\u2028|\u2029/g, ""))); + const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0); if (linkElement.textContent === "" || linkElement.textContent === Constants.ZWSP) { - removeLink(linkElement, protyle.toolbar.range); - } else { + removeLink(linkElement, (currentRange && !protyle.element.contains(currentRange.startContainer)) ? protyle.toolbar.range : undefined); + } else if (currentRange && !protyle.element.contains(currentRange.startContainer)) { protyle.toolbar.range.selectNodeContents(linkElement); protyle.toolbar.range.collapse(false); focusByRange(protyle.toolbar.range); diff --git a/app/src/protyle/toolbar/Link.ts b/app/src/protyle/toolbar/Link.ts index 4de95126a..db3c3d1e6 100644 --- a/app/src/protyle/toolbar/Link.ts +++ b/app/src/protyle/toolbar/Link.ts @@ -58,12 +58,14 @@ export class Link extends ToolbarItem { } } -export const removeLink = (linkElement: HTMLElement, range: Range) => { +export const removeLink = (linkElement: HTMLElement, range?: Range) => { const types = linkElement.getAttribute("data-type").split(" "); if (types.length === 1) { const linkParentElement = linkElement.parentElement; linkElement.outerHTML = linkElement.innerHTML.replace(Constants.ZWSP, "") + ""; - focusByWbr(linkParentElement, range); + if (range) { + focusByWbr(linkParentElement, range); + } } else { types.find((itemType, index) => { if ("a" === itemType) { @@ -73,8 +75,10 @@ export const removeLink = (linkElement: HTMLElement, range: Range) => { }); linkElement.setAttribute("data-type", types.join(" ")); linkElement.removeAttribute("data-href"); - range.selectNodeContents(linkElement); - range.collapse(false); - focusByRange(range); + if (range) { + range.selectNodeContents(linkElement); + range.collapse(false); + focusByRange(range); + } } }; diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index b7b3c02ab..3ac01ae6e 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -807,9 +807,13 @@ export class Toolbar { refElement.innerHTML = "*"; } this.range.setStartAfter(refElement); - focusByRange(this.range); + if (getSelection().rangeCount === 0) { + focusByRange(this.range); + } } else { - focusByWbr(nodeElement, this.range); + if (getSelection().rangeCount === 0) { + focusByWbr(nodeElement, this.range); + } } nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss")); updateTransaction(protyle, id, nodeElement.outerHTML, html);