Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: fix https://github.com/siyuan-note/siyuan/issues/8315

Vanessaa1f8339d4bcb
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 6d66ee6a7d..9d37df202d 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,否则 < 会变为 &lt;
@@ -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 4de95126ab..db3c3d1e64 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, "") + "<wbr>";
- 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 b7b3c02ab2..3ac01ae6e0 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);