Skip to content

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

:bug: Preserve select-all notification ownership and retries https://github.com/siyuan-note/siyuan/pull/20815

Daniel48f453c031bf
diff --git a/app/src/dialog/message.test.ts b/app/src/dialog/message.test.ts
index e9ac8bbd1a..859975714d 100644
--- a/app/src/dialog/message.test.ts
+++ b/app/src/dialog/message.test.ts
@@ -63,10 +63,14 @@ const cases = async (source: string) => {
api.showMessage(notification, -1, "info", "unrelated");
button().click();
check.equal(requests, 2, "only the update notification can invoke the download action");
+ api.showMessage("Select all", -1, "info", "select-all-tip");
+ const selectAllTip = message.querySelector('[data-id="select-all-tip"]');
check.equal(api.showMessage("Loading...", -1, "info", "select-all-tip"), "select-all-tip",
"replacing an existing notification returns its id so callers can hide it");
check.equal(message.querySelectorAll('[data-id="select-all-tip"]').length, 1,
"replacing an existing notification reuses its element");
+ check.equal(message.querySelector('[data-id="select-all-tip"]'), selectAllTip);
+ check.equal(selectAllTip.textContent, "Loading...");
return "update-message-ok";
};
diff --git a/app/src/protyle/scroll/index.ts b/app/src/protyle/scroll/index.ts
index e03ea73985..20ec8b0500 100644
--- a/app/src/protyle/scroll/index.ts
+++ b/app/src/protyle/scroll/index.ts
@@ -230,7 +230,7 @@ export class Scroll {
return true;
}
- // messageId 用于复用调用方已展示的通知,避免新增一条提示把通知列表挤得跳动
+ // 传入 messageId 时复用调用方的通知,由调用方负责关闭或恢复,避免覆盖后续提示。
public async loadAll(protyle: IProtyle, options?: { messageId?: string }) {
const rootID = protyle.block.rootID;
if (this.loadingAll || !rootID || protyle.block.showAll || !protyle.block.scroll) {
@@ -255,7 +255,7 @@ export class Scroll {
return true;
} finally {
this.loadingAll = false;
- if (messageID) {
+ if (messageID && !options?.messageId) {
hideMessage(messageID);
}
}
diff --git a/app/src/protyle/util/selectAllTip.ts b/app/src/protyle/util/selectAllTip.ts
index 412fa05b47..7a65868dd2 100644
--- a/app/src/protyle/util/selectAllTip.ts
+++ b/app/src/protyle/util/selectAllTip.ts
@@ -32,10 +32,16 @@ export const showSelectAllIncompleteTip = (protyle: IProtyle) => {
return;
}
button.disabled = true;
+ let messageContent: Element = button.closest(".b3-snackbar__content");
+ let selected = false;
try {
- if (hasUnloadedDocumentBlocks(editorElement, protyle.block.scroll) &&
- !await protyle.scroll.loadAll(protyle, {messageId: messageID})) {
- return;
+ if (hasUnloadedDocumentBlocks(editorElement, protyle.block.scroll)) {
+ const loading = protyle.scroll.loadAll(protyle, {messageId: messageID});
+ // 加载提示同步替换内容;保存节点以区分后来同 ID 的通知。
+ messageContent = document.querySelector(`#message [data-id="${messageID}"]`)?.firstElementChild;
+ if (!await loading) {
+ return;
+ }
}
if (!isCurrent()) {
return;
@@ -56,13 +62,19 @@ export const showSelectAllIncompleteTip = (protyle: IProtyle) => {
protyle.toolbar.showMultiSelectMode(protyle, elements[0] as HTMLElement);
}
}
- if (button.isConnected) {
- hideMessage(messageID);
- }
+ selected = true;
} catch (error) {
showMessage(escapeHtml(error.message), 7000, "error");
} finally {
button.disabled = false;
+ if (messageContent?.isConnected) {
+ if (!selected && isCurrent()) {
+ showSelectAllIncompleteTip(protyle);
+ }
+ if (messageContent.isConnected) {
+ hideMessage(messageID);
+ }
+ }
}
});
};
diff --git a/app/tests/selectAllTip.test.js b/app/tests/selectAllTip.test.js
index 2faaa11aca..aff387a3c3 100644
--- a/app/tests/selectAllTip.test.js
+++ b/app/tests/selectAllTip.test.js
@@ -31,6 +31,14 @@ const runCases = async sources => {
"protyle/wysiwyg/verticalNavigationState": {},
"menus/menuToggle": {},
};
+ // 仅替换滚动条构造及其他操作的依赖,通知流程使用真实的 loadAll。
+ for (const name of ["protyle/util/onGet", "protyle/util/compatibility", "protyle/util/hasClosest",
+ "protyle/wysiwyg/commonHotkey", "dialog/tooltip", "util/pathName", "protyle/scroll/visibility",
+ "protyle/scroll/loadAll", "protyle/scroll/dynamicLoadState", "protyle/scroll/saveScroll",
+ "protyle/scroll/slider", "util/touchDragBridge", "protyle/util/transactionQueue",
+ "util/keymapBindings", "protyle/scroll/page", "protyle/scroll/pageButtons"]) {
+ modules[name] = {};
+ }
const load = name => {
if (modules[name]) {
return modules[name];
@@ -53,46 +61,58 @@ const runCases = async sources => {
};
const {showSelectAllIncompleteTip} = load("protyle/util/selectAllTip");
const {initMessage} = load("dialog/message");
+ const {Scroll} = load("protyle/scroll/index");
const {renderMultiSelectToolbar} = load("mobile/util/multiSelectToolbar");
const settle = () => new Promise(resolve => setTimeout(resolve, 10));
- const fixture = () => {
- document.body.innerHTML = '<div id="message"></div><div id="editor"><div id="wysiwyg"></div></div><div id="toolbar"></div>';
- window.siyuan = {zIndex: 0, config: {appearance: {notifications: {}}}, languages: {
- selectAllIncompleteTip: "Unloaded blocks remain unselected",
- loadAllAndSelectAll: "Load full document and select all",
- }};
- initMessage();
+ const fixture = (reset = true) => {
+ if (reset) {
+ document.body.innerHTML = '<div id="message"></div>';
+ window.siyuan = {zIndex: 0, config: {appearance: {notifications: {}}, editor: {dynamicLoadBlocks: 64}}, languages: {
+ selectAllIncompleteTip: "Unloaded blocks remain unselected",
+ loadAllAndSelectAll: "Load full document and select all",
+ loading: "Loading...",
+ }};
+ initMessage();
+ }
counted = undefined;
focused = undefined;
toolbarOpened = 0;
- const element = document.getElementById("wysiwyg");
+ const editor = document.createElement("div");
+ const element = document.createElement("div");
+ editor.append(element);
+ document.body.append(editor);
element.innerHTML = '<div data-node-id="middle" class="protyle-wysiwyg--select"><div contenteditable="true">Middle</div>' +
'<div data-node-id="nested" class="protyle-wysiwyg--select protyle-wysiwyg--select-mode" select-start="0" select-end="1">Nested</div></div>';
- const subElement = document.getElementById("toolbar");
+ const subElement = document.createElement("div");
+ document.body.append(subElement);
let complete;
let requests = 0;
- let loadAllOptions;
const protyle = {
- element: document.getElementById("editor"),
+ element: editor,
block: {rootID: "document", parentID: "document", scroll: true},
options: {}, wysiwyg: {element},
- scroll: {loadAll: (currentProtyle, options) => {
- requests++;
- loadAllOptions = options;
- return new Promise((resolve, reject) => complete = (success, error) => {
- if (error) {
- reject(new Error("Loading failed"));
- } else {
- if (success) {
- element.insertAdjacentHTML("afterbegin", '<div data-node-id="first" data-node-index="0"><div contenteditable="true">First</div></div>');
- element.insertAdjacentHTML("beforeend", '<div data-node-id="last"><div contenteditable="true">Last</div></div><div class="placeholder"></div>');
- element.setAttribute("data-bottom-eof", "");
- protyle.block.scroll = false;
- }
- resolve(success);
+ scroll: Object.assign(Object.create(Scroll.prototype), {
+ invalidateDynamicLoad: () => {},
+ update: () => {},
+ loadUntilBoundary: (currentProtyle, rootID, mode) => {
+ if (mode === 2) {
+ return Promise.resolve(true);
}
- });
- }},
+ requests++;
+ return new Promise((resolve, reject) => complete = (success, error) => {
+ if (error) {
+ reject(new Error("Loading failed"));
+ } else {
+ if (success) {
+ element.insertAdjacentHTML("afterbegin", '<div data-node-id="first" data-node-index="0"><div contenteditable="true">First</div></div>');
+ element.insertAdjacentHTML("beforeend", '<div data-node-id="last"><div contenteditable="true">Last</div></div><div class="placeholder"></div>');
+ element.setAttribute("data-bottom-eof", "");
+ }
+ resolve(success);
+ }
+ });
+ },
+ }),
toolbar: {
subElement,
isMultiSelectMode: () => !!subElement.querySelector(".multiSelectCount"),
@@ -104,9 +124,10 @@ const runCases = async sources => {
};
const show = () => showSelectAllIncompleteTip(protyle);
const button = () => document.querySelector('#message [data-id="selectAllIncompleteTip"] button');
+ const notification = () => document.querySelector('#message [data-id="selectAllIncompleteTip"]');
const selected = () => Array.from(element.querySelectorAll(".protyle-wysiwyg--select"), item => item.dataset.nodeId);
- return {protyle, element, show, button, selected, complete: (...args) => complete(...args),
- requests: () => requests, loadAllOptions: () => loadAllOptions};
+ return {protyle, element, show, button, notification, selected, complete: (...args) => complete(...args),
+ requests: () => requests};
};
// 重复提示会替换按钮,必须绑定最新按钮,并等待全文成功加载后再选择顶层块。
@@ -119,12 +140,17 @@ const runCases = async sources => {
const previous = f.button();
f.show();
check.notEqual(previous, f.button());
- f.button().click();
- f.button().click();
+ const button = f.button();
+ const notification = f.notification();
+ button.click();
+ button.click();
check.equal(f.requests(), 1);
// 加载提示复用本条通知的 id,避免新增通知导致列表位置跳动
- check.deepEqual(f.loadAllOptions(), {messageId: "selectAllIncompleteTip"});
- check.equal(f.button().disabled, true);
+ check.equal(f.notification(), notification);
+ check.equal(document.querySelectorAll("#message .b3-snackbar").length, 1);
+ check.equal(notification.textContent, "Loading...");
+ check.equal(f.button(), null);
+ check.equal(button.disabled, true);
check.deepEqual(f.selected(), ["middle", "nested"]);
f.complete(true);
await settle();
@@ -147,31 +173,91 @@ const runCases = async sources => {
const reopened = fixture();
reopened.show();
reopened.button().click();
- reopened.button().closest(".b3-snackbar").remove();
+ reopened.notification().remove();
reopened.complete(true);
await settle();
check.equal(toolbarOpened, 1);
check.deepEqual(reopened.selected(), ["first", "middle", "last"]);
- mobile = false;
- for (const error of [false, true]) {
+ for (mobile of [false, true]) {
+ for (const error of [false, true]) {
+ const f = fixture();
+ f.show();
+ const notification = f.notification();
+ f.button().click();
+ f.complete(false, error);
+ await settle();
+ check.deepEqual(f.selected(), ["middle", "nested"]);
+ check.equal(counted, undefined);
+ check.equal(f.button().disabled, false);
+ check.equal(f.notification(), notification, "retry reuses the same notification");
+ if (error) {
+ check.match(document.getElementById("message").textContent, /Loading failed/);
+ }
+ f.button().click();
+ f.complete(true);
+ await settle();
+ check.deepEqual(f.selected(), ["first", "middle", "last"]);
+ }
+ }
+
+ // 用户关闭提示或禁用通知后,失败的加载不重新显示提示,也不遗留加载状态。
+ for (const dismiss of [f => f.notification().remove(),
+ () => window.siyuan.config.appearance.notifications.selectAllIncompleteTip = false]) {
const f = fixture();
f.show();
f.button().click();
- f.complete(false, error);
+ dismiss(f);
+ f.complete(false);
await settle();
- check.deepEqual(f.selected(), ["middle", "nested"]);
- check.equal(counted, undefined);
- check.equal(f.button().disabled, false);
- if (error) {
- check.match(document.getElementById("message").textContent, /Loading failed/);
+ check.equal(f.notification(), null);
+ }
+
+ // 旧加载结束后不能关闭或恢复已被其他文档替换的提示,包括新的加载状态。
+ for (mobile of [false, true]) {
+ for (const outcome of ["success", "failure", "error"]) {
+ for (const startNewLoad of [false, true]) {
+ const first = fixture();
+ first.show();
+ first.button().click();
+ const second = fixture(false);
+ second.protyle.block.rootID = "other-document";
+ second.show();
+ const secondButton = second.button();
+ if (startNewLoad) {
+ secondButton.click();
+ }
+ const content = second.notification().firstElementChild;
+ first.complete(outcome === "success", outcome === "error");
+ await settle();
+ check.equal(content.isConnected, true);
+ check.equal(second.notification().firstElementChild, content);
+ if (startNewLoad) {
+ check.equal(second.notification().textContent, "Loading...");
+ second.complete(true);
+ await settle();
+ check.deepEqual(second.selected(), ["first", "middle", "last"]);
+ check.equal(second.notification(), null);
+ } else {
+ check.equal(second.button(), secondButton);
+ check.equal(secondButton.disabled, false);
+ }
+ }
}
- f.button().click();
- f.complete(true);
+ }
+
+ // 非通知按钮调用 loadAll 时,仍自行清理独立的加载提示。
+ for (const success of [false, true]) {
+ const f = fixture();
+ const loading = f.protyle.scroll.loadAll(f.protyle);
+ check.match(document.getElementById("message").textContent, /Loading/);
+ f.complete(success);
+ check.equal(await loading, success);
await settle();
- check.deepEqual(f.selected(), ["first", "middle", "last"]);
+ check.equal(document.querySelector("#message .b3-snackbar"), null);
}
+ mobile = false;
for (const change of [f => f.protyle.block.rootID = "other", f => f.protyle.block.parentID = "zoomed",
f => f.protyle.element.remove(), f => f.protyle.wysiwyg.element = document.createElement("div")]) {
for (const duringLoad of [false, true]) {
@@ -222,7 +308,7 @@ if (process.versions.electron && process.type === "browser") {
let code = 0;
try {
const sources = Object.fromEntries(["protyle/util/selectAllTip", "protyle/util/documentRange",
- "protyle/wysiwyg/blockSelection", "dialog/message", "mobile/util/multiSelectToolbar", "util/escape"].map(name => [name,
+ "protyle/wysiwyg/blockSelection", "protyle/scroll/index", "dialog/message", "mobile/util/multiSelectToolbar", "util/escape"].map(name => [name,
ts.transpileModule(readFileSync(path.join(__dirname, `../src/${name}.ts`), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
}).outputText]));