Skip to content

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

:bug: Keep bottom backlinks synchronized https://github.com/siyuan-note/siyuan/issues/18319

Daniel73f54bf541cf
diff --git a/app/src/dialog/processSystem.ts b/app/src/dialog/processSystem.ts
index 7ad194e1a3..6318cd588c 100644
--- a/app/src/dialog/processSystem.ts
+++ b/app/src/dialog/processSystem.ts
@@ -20,6 +20,7 @@ import {App} from "../index";
import {saveScroll} from "../protyle/scroll/saveScroll";
import {isInAndroid, isInHarmony, isInIOS, setStorageVal} from "../protyle/util/compatibility";
import {Plugin} from "../plugin";
+import {shouldForceBacklinkRefresh} from "../layout/dock/backlinkRefresh";
const BACKLINK_REFRESH_DELAY = 3600;
let backlinkRefreshTimeout: number;
@@ -66,8 +67,9 @@ export const setDefRefCount = (data: {
item.blockId !== data.rootID && item.blockId !== data.blockID && !data.defIDs?.includes(item.blockId))) {
return;
}
- item.markDirty();
- item.refreshIfVisible();
+ const ignoreFocus = shouldForceBacklinkRefresh(item.rootId, data.rootID);
+ item.markDirty(ignoreFocus);
+ item.refreshIfVisible(ignoreFocus);
});
/// #endif
getAllEditor().forEach(editor => {
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.ts
index ac90a54f3b..c2931136ce 100644
--- a/app/src/layout/dock/Backlink.ts
+++ b/app/src/layout/dock/Backlink.ts
@@ -41,6 +41,8 @@ export class Backlink extends Model {
} = {};
private dirty = false;
private destroyed = false;
+ private refreshQueued = false;
+ private ignoreFocusOnNextRefresh = false;
private ownerFocusoutListener?: (event: FocusEvent) => void;
constructor(options: {
@@ -621,6 +623,8 @@ export class Backlink extends Model {
if (this.type !== "bottom") {
return;
}
+ this.editors.forEach(item => item.destroy());
+ this.editors = [];
const loadingHTML = '<div class="backlinkList__loading"><img width="32px" height="32px" src="/stage/loading-pure.svg"></div>';
this.tree.element.innerHTML = loadingHTML;
this.mTree.element.innerHTML = loadingHTML;
@@ -629,9 +633,18 @@ export class Backlink extends Model {
public refresh() {
const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');
- if (!this.blockId || element.classList.contains("fn__rotate")) {
+ if (!this.blockId) {
return;
}
+ if (element.classList.contains("fn__rotate")) {
+ if (!this.refreshQueued && this.type === "bottom") {
+ this.saveStatus();
+ this.showBottomLoading();
+ }
+ this.refreshQueued = true;
+ return;
+ }
+ this.refreshQueued = false;
element.classList.add("fn__rotate");
this.dirty = false;
fetchPost("/api/ref/refreshBacklink", {
@@ -652,6 +665,7 @@ export class Backlink extends Model {
}
element.classList.add("fn__rotate");
this.dirty = false;
+ this.ignoreFocusOnNextRefresh = false;
// 解析当前反链面板所属 box:优先用已记录的 notebookId,首次为空时按 rootId 在已打开的编辑器里查找
let notebookId = this.notebookId;
if (!notebookId && this.rootId) {
@@ -677,6 +691,11 @@ export class Backlink extends Model {
if (this.destroyed || blockId !== this.blockId) {
return;
}
+ if (this.refreshQueued) {
+ element.classList.remove("fn__rotate");
+ this.refresh();
+ return;
+ }
if (!init) {
this.saveStatus();
}
@@ -865,15 +884,21 @@ export class Backlink extends Model {
element.querySelector("use").setAttribute("xlink:href", folded ? "#iconRight" : "#iconDown");
}
- public markDirty() {
+ public markDirty(ignoreFocus = false) {
this.dirty = true;
+ if (ignoreFocus) {
+ this.ignoreFocusOnNextRefresh = true;
+ }
}
public refreshIfVisible(ignoreFocus = false) {
if (this.type !== "bottom" || !this.dirty) {
return;
}
- if (!ignoreFocus && this.ownerProtyle.element.contains(document.activeElement)) {
+ if (ignoreFocus) {
+ this.ignoreFocusOnNextRefresh = true;
+ }
+ if (!this.ignoreFocusOnNextRefresh && this.ownerProtyle.element.contains(document.activeElement)) {
return;
}
if (this.element.classList.contains("fn__none") ||
diff --git a/app/src/layout/dock/backlinkRefresh.test.ts b/app/src/layout/dock/backlinkRefresh.test.ts
new file mode 100644
index 0000000000..445d8b1db6
--- /dev/null
+++ b/app/src/layout/dock/backlinkRefresh.test.ts
@@ -0,0 +1,14 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {shouldForceBacklinkRefresh} from "./backlinkRefresh";
+
+describe("shouldForceBacklinkRefresh", () => {
+ it("refreshes reference changes in the owner document while it is focused", () => {
+ assert.equal(shouldForceBacklinkRefresh("owner", "owner"), true);
+ });
+
+ it("keeps unrelated document changes deferred", () => {
+ assert.equal(shouldForceBacklinkRefresh("owner", "other"), false);
+ assert.equal(shouldForceBacklinkRefresh("", "other"), false);
+ });
+});
diff --git a/app/src/layout/dock/backlinkRefresh.ts b/app/src/layout/dock/backlinkRefresh.ts
new file mode 100644
index 0000000000..8110070b3c
--- /dev/null
+++ b/app/src/layout/dock/backlinkRefresh.ts
@@ -0,0 +1,3 @@
+export const shouldForceBacklinkRefresh = (ownerRootID: string, changedRootID: string) => {
+ return Boolean(ownerRootID && ownerRootID === changedRootID);
+};
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
new file mode 100644
index 0000000000..9eb42835ad
--- /dev/null
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -0,0 +1,27 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {isSameDragEditor, uniqueDragIds} from "./dragDocument";
+
+describe("isSameDragEditor", () => {
+ it("does not treat a nested Protyle as the target editor", () => {
+ const sourceElement = {} as Element;
+ const ownerProtyle = {contains: () => true} as unknown as Element;
+ const targetEditor = {contains: () => false} as unknown as Element;
+
+ assert.equal(ownerProtyle.contains(sourceElement), true);
+ assert.equal(isSameDragEditor(targetEditor, sourceElement), false);
+ });
+
+ it("recognizes a source block in the target editor", () => {
+ const sourceElement = {} as Element;
+ const targetEditor = {contains: (element: Element) => element === sourceElement} as unknown as Element;
+
+ assert.equal(isSameDragEditor(targetEditor, sourceElement), true);
+ });
+});
+
+describe("uniqueDragIds", () => {
+ it("removes empty and duplicate block IDs while preserving their order", () => {
+ assert.deepEqual(uniqueDragIds(["a", "", "b", "a", "b", "c"]), ["a", "b", "c"]);
+ });
+});
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
new file mode 100644
index 0000000000..e91c0f0820
--- /dev/null
+++ b/app/src/protyle/util/dragDocument.ts
@@ -0,0 +1,7 @@
+export const isSameDragEditor = (targetEditor: Pick<Element, "contains">, sourceElement: Element) => {
+ return targetEditor.contains(sourceElement);
+};
+
+export const uniqueDragIds = (ids: string[]) => {
+ return Array.from(new Set(ids.filter(Boolean)));
+};
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 4c0ad0da7f..c1bac58ce2 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -54,6 +54,7 @@ import {clearSelect} from "./clear";
import {dragoverTab} from "../render/av/view";
import {setFold} from "./blockFold";
import {isEncryptedBox} from "../../util/pathName";
+import {isSameDragEditor, uniqueDragIds} from "./dragDocument";
const convertListItemSubtype = (listItem: Element, subtype: string) => {
const actionElement = listItem.querySelector(".protyle-action");
@@ -91,7 +92,7 @@ const getTargetListItem = (targetElement: Element, isBottom: boolean) => {
// position: afterbegin 为拖拽成超级块; "afterend", "beforebegin" 一般拖拽
const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element,
- isSameDoc: boolean, position: InsertPosition, isCopy: boolean) => {
+ isSameEditor: boolean, position: InsertPosition, isCopy: boolean) => {
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
const copyFoldHeadingIds: { newId: string, oldId: string }[] = [];
@@ -192,7 +193,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
parentID: srcPos.parentID,
});
}
- if (!isSameDoc && !isCopy) {
+ if (!isSameEditor && !isCopy) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
if (sameElement) {
@@ -274,7 +275,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
});
const topSourceParentElement = topSourceElement.parentElement;
topSourceElement.remove();
- if (!isSameDoc) {
+ if (!isSameEditor) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${topSourceElement.getAttribute("data-node-id")}"]`);
if (sameElement) {
@@ -283,7 +284,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
if (topSourceParentElement.classList.contains("sb") && getSbChildBlockCount(topSourceParentElement) === 1) {
// 拖拽后,sb 只剩下一个元素
- if (isSameDoc) {
+ if (isSameEditor) {
const sbData = await cancelSB(protyle, topSourceParentElement);
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
undoOperations.push(sbData.undoOperations[1], sbData.undoOperations[0]);
@@ -305,7 +306,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
} else if (oldSourceParentElement.classList.contains("sb") && getSbChildBlockCount(oldSourceParentElement) === 1) {
// 拖拽后,sb 只剩下一个元素
- if (isSameDoc) {
+ if (isSameEditor) {
const sbData = await cancelSB(protyle, oldSourceParentElement);
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
undoOperations.push(sbData.undoOperations[1], sbData.undoOperations[0]);
@@ -447,7 +448,8 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean,
direct: "col" | "row", isCopy: boolean) => {
- const isSameDoc = protyle.element.contains(sourceElements[0]);
+ // 底部反链 Protyle 嵌套在正文 Protyle 中,仅 wysiwyg 包含关系能表示同一编辑器。
+ const isSameEditor = isSameDragEditor(protyle.wysiwyg.element, sourceElements[0]);
// 移动前记录源块所在的超级块,移动后刷新其手柄(移出后需重建)https://github.com/siyuan-note/siyuan/issues/9521
const originSbSet = new Set<Element>();
sourceElements.forEach(el => {
@@ -458,7 +460,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
});
// 把列表块中的唯一一个列表项块拖拽到列表块的左侧 https://github.com/siyuan-note/siyuan/issues/16315
- if (isSameDoc && sourceElements[0].classList.contains("li") && targetElement === sourceElements[0].parentElement &&
+ if (isSameEditor && sourceElements[0].classList.contains("li") && targetElement === sourceElements[0].parentElement &&
targetElement.childElementCount === sourceElements.length + 1) {
const outLiElement = sourceElements.find((element) => {
if (!targetElement.contains(element)) {
@@ -491,7 +493,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}];
// 临时插入,防止后面计算错误,最终再移动矫正
sbElement.lastElementChild.before(targetElement);
- const moveToResult = await moveTo(protyle, sourceElements, sbElement, isSameDoc, "afterbegin", isCopy);
+ const moveToResult = await moveTo(protyle, sourceElements, sbElement, isSameEditor, "afterbegin", isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
const newSourceParentElement = moveToResult.newSourceElements;
@@ -571,7 +573,7 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
};
const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean, isCopy: boolean) => {
- const isSameDoc = protyle.element.contains(sourceElements[0]);
+ const isSameEditor = isSameDragEditor(protyle.wysiwyg.element, sourceElements[0]);
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
// 移动前记录源块所在的超级块,移动后刷新其手柄(移出后需重建)
@@ -583,7 +585,7 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
}
});
- const moveToResult = await moveTo(protyle, sourceElements, targetElement, isSameDoc, isBottom ? "afterend" : "beforebegin", isCopy);
+ const moveToResult = await moveTo(protyle, sourceElements, targetElement, isSameEditor, isBottom ? "afterend" : "beforebegin", isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
const newSourceParentElement = moveToResult.newSourceElements;
@@ -947,7 +949,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// gutter 或反链面板拖拽
const sourceElements: Element[] = [];
const gutterTypes = gutterType.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);
- const selectedIds = gutterTypes[2].split(",");
+ const selectedIds = uniqueDragIds(gutterTypes[2].split(","));
if (event.altKey || event.shiftKey) {
if (event.y > protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {
insertEmptyBlock(protyle, "afterend", protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"));
@@ -976,11 +978,18 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
selectedIds.forEach(item => {
queryClass += `[data-node-id="${item}"],`;
});
+ const sourceElementIds = new Set<string>();
+ const appendSourceElement = (elementItem: Element) => {
+ const id = elementItem.getAttribute("data-node-id");
+ if (!id || sourceElementIds.has(id) || isInEmbedBlock(elementItem)) {
+ return;
+ }
+ sourceElementIds.add(id);
+ sourceElements.push(elementItem);
+ };
if (window.siyuan.dragElement) {
window.siyuan.dragElement.querySelectorAll(queryClass.substring(0, queryClass.length - 1)).forEach(elementItem => {
- if (!isInEmbedBlock(elementItem)) {
- sourceElements.push(elementItem);
- }
+ appendSourceElement(elementItem);
});
} else if (window.siyuan.config.system.workspaceDir.toLowerCase() === gutterTypes[3]) {
// 跨窗口拖拽
@@ -988,9 +997,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const targetProtyleElement = document.createElement("template");
targetProtyleElement.innerHTML = `<div>${event.dataTransfer.getData(gutterType)}</div>`;
targetProtyleElement.content.querySelectorAll(queryClass.substring(0, queryClass.length - 1)).forEach(elementItem => {
- if (!isInEmbedBlock(elementItem)) {
- sourceElements.push(elementItem);
- }
+ appendSourceElement(elementItem);
});
}