Skip to content

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

:sparkles: Defer document icons until an explicit selection https://github.com/siyuan-note/siyuan/issues/20897

Daniel4de46739b15f
diff --git a/app/src/protyle/header/Background.test.ts b/app/src/protyle/header/Background.test.ts
new file mode 100644
index 0000000000..1c33d079fc
--- /dev/null
+++ b/app/src/protyle/header/Background.test.ts
@@ -0,0 +1,58 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {createSourceFile, forEachChild, isCallExpression, ScriptTarget, transpileModule} from "typescript";
+
+test("adding a document icon opens selection without changing the document or its header", () => {
+ const source = createSourceFile("Background.ts", readFileSync(__dirname + "/Background.ts", "utf8"), ScriptTarget.Latest, true);
+ let listener: string;
+ const collect = (node: import("typescript").Node) => {
+ if (isCallExpression(node) && node.expression.getText(source) === "this.element.addEventListener" &&
+ node.arguments[0].getText(source) === '"click"') {
+ listener = node.getText(source);
+ }
+ forEachChild(node, collect);
+ };
+ collect(source);
+ assert.ok(listener);
+ const element = new EventTarget();
+ Object.defineProperty(element, "isEqualNode", {value: (other: EventTarget) => other === element});
+ const panels: unknown[][] = [];
+ const writes: unknown[] = [];
+ const changes: string[] = [];
+ const iconRect = {left: 100, bottom: 200, height: 60, width: 60};
+ const buttonRect = {left: 120, bottom: 240, height: 28, width: 110};
+ const target = (type: string) => ({
+ getAttribute: () => type, getBoundingClientRect: () => type === "icon" ? buttonRect : iconRect,
+ isEqualNode: (): boolean => false, parentElement: element, querySelector: (): null => null,
+ });
+ const background = {element, iconElement: {
+ getBoundingClientRect: () => iconRect,
+ classList: {remove: (name: string) => changes.push(name)},
+ }};
+ const protyle = {element: {}, block: {rootID: "document"}, disabled: false,
+ model: {parent: {setDocIcon: (value: string) => changes.push(value)}}};
+ new Function("protyle", "hideElements", "openEmojiPanel", "fetchPost", transpileModule(listener, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText).call(background, protyle, () => {}, (...args: unknown[]) => panels.push(args),
+ (...args: unknown[]) => writes.push(args));
+ const click = (type: string) => {
+ const event = new Event("click", {cancelable: true});
+ Object.defineProperty(event, "target", {value: target(type)});
+ element.dispatchEvent(event);
+ return event;
+ };
+ assert.equal(click("icon").defaultPrevented, true);
+ assert.deepEqual(panels[0], ["document", "doc", {
+ x: buttonRect.left, y: buttonRect.bottom, h: buttonRect.height, w: buttonRect.width,
+ }, undefined, undefined, {ownerElement: protyle.element}]);
+ assert.deepEqual(writes, [], "opening the picker does not persist a provisional icon");
+ assert.deepEqual(changes, [], "opening the picker does not reveal or replace the document icon");
+ click("open-emoji");
+ assert.deepEqual(panels[1], ["document", "doc", {
+ x: iconRect.left, y: iconRect.bottom, h: iconRect.height, w: iconRect.width,
+ }, undefined, null, {ownerElement: protyle.element}], "existing icons retain their own picker anchor");
+ protyle.disabled = true;
+ click("icon");
+ assert.equal(panels.length, 2, "read-only headers cannot open icon selection");
+});
diff --git a/app/src/protyle/header/Background.ts b/app/src/protyle/header/Background.ts
index 7b7e0eca3f..1b29adcf2d 100644
--- a/app/src/protyle/header/Background.ts
+++ b/app/src/protyle/header/Background.ts
@@ -4,7 +4,7 @@ import {getRandom} from "../../util/functions";
import {hideElements} from "../ui/hideElements";
import {uploadFiles} from "../upload";
import {fetchPost} from "../../util/fetch";
-import {getRandomEmoji, openEmojiPanel, unicode2Emoji, updateFileTreeEmoji, updateOutlineEmoji} from "../../emoji";
+import {openEmojiPanel, unicode2Emoji} from "../../emoji";
import {upDownHint} from "../../util/upDownHint";
/// #if !MOBILE
import {openGlobalSearch} from "../../search/util";
@@ -380,26 +380,13 @@ export class Background {
event.stopPropagation();
break;
} else if (type === "icon" && !protyle.disabled) {
- const emoji = getRandomEmoji();
- if (emoji) {
- updateFileTreeEmoji(emoji, protyle.block.rootID);
- updateOutlineEmoji(emoji, protyle.block.rootID);
- fetchPost("/api/attr/setBlockAttrs", {
- id: protyle.block.rootID,
- attrs: {"icon": emoji}
- });
- if (protyle.model) {
- protyle.model.parent.setDocIcon(emoji);
- }
- this.iconElement.classList.remove("fn__none");
- const rect = this.iconElement.getBoundingClientRect();
- openEmojiPanel(protyle.block.rootID, "doc", {
- x: rect.left,
- y: rect.bottom,
- h: rect.height,
- w: rect.width
- }, undefined, undefined, {ownerElement: protyle.element});
- }
+ const rect = target.getBoundingClientRect();
+ openEmojiPanel(protyle.block.rootID, "doc", {
+ x: rect.left,
+ y: rect.bottom,
+ h: rect.height,
+ w: rect.width
+ }, undefined, undefined, {ownerElement: protyle.element});
event.preventDefault();
event.stopPropagation();
break;
diff --git a/app/tests/emojiPanel.test.js b/app/tests/emojiPanel.test.js
index 305cc70e72..de8504ae4a 100644
--- a/app/tests/emojiPanel.test.js
+++ b/app/tests/emojiPanel.test.js
@@ -24,6 +24,7 @@ const cases = (source, helpers, languages) => {
const messages = [];
let sheets = 0;
let disposed = 0;
+ const treeUpdates = [];
class Dialog {
constructor(options) {
this.options = options;
@@ -52,6 +53,8 @@ const cases = (source, helpers, languages) => {
setPosition() {}, updateHotkeyTip: value => value, setStorageVal() {}, activeBlur() {},
bindBottomSheetDialog: () => {sheets++; return () => disposed++;},
reloadEmoji() {}, addEmoji: value => recent.push(value), showMessage: message => messages.push(message),
+ getRandomEmoji: () => "1f600",
+ updateFileTreeEmoji: (...args) => treeUpdates.push(args), updateOutlineEmoji() {},
fetchPost: (url, data, callback) => {
requests.push({url, data});
callback(failure ? {code: 1} : {code: 0, data: {path: "custom-icon.png"}});
@@ -101,8 +104,33 @@ const cases = (source, helpers, languages) => {
requests.length = 0;
selected.length = 0;
}
- check.equal(sheets, 2, "both mobile openings use the shared sheet");
- check.equal(disposed, sheets, "mobile sheet handlers are disposed on cancel and save");
+ for (mobile of [false, true]) {
+ window.siyuan.storage.emojis.currentTab = "emoji";
+ openEmojiPanel("document", "doc", {x: 100, y: 100, h: 28, w: 100});
+ dialog = dialogs.at(-1);
+ check.equal(requests.length, 0, "opening document selection does not write an icon");
+ dialog.destroy();
+ check.equal(requests.length, 0, "canceling document selection leaves its icon unset");
+ openEmojiPanel("document", "doc", {x: 100, y: 100, h: 28, w: 100});
+ dialog = dialogs.at(-1);
+ dialog.element.querySelector('[data-action="random"]').click();
+ check.equal(requests.length, 1, "an explicit random choice writes once");
+ check.equal(requests[0].url, "/api/attr/setBlockAttrs");
+ check.deepEqual(requests[0].data, {id: "document", attrs: {icon: "1f600"}});
+ check.deepEqual(treeUpdates.at(-1), ["1f600", "document"]);
+ dialog.destroy();
+ requests.length = 0;
+ openEmojiPanel("document", "doc", {x: 100, y: 100, h: 28, w: 100});
+ dialog = dialogs.at(-1);
+ dialog.element.querySelector(".emojis__panel").innerHTML = '<button class="emojis__item" data-unicode="1f680">Icon</button>';
+ dialog.element.querySelector(".emojis__item").click();
+ check.equal(requests.length, 1, "a selected emoji writes once");
+ check.deepEqual(requests[0].data, {id: "document", attrs: {icon: "1f680"}});
+ check.equal(dialog.element.isConnected, false, "selecting an emoji closes the picker");
+ requests.length = 0;
+ }
+ check.equal(sheets, 5, "mobile document and database openings use the shared sheet");
+ check.equal(disposed, sheets, "mobile sheet handlers are disposed with each closed panel");
return "Emoji panel uploads passed";
};
@@ -131,7 +159,7 @@ if (process.versions.electron && process.type === "browser") {
app.exit(0);
})().catch(error => { console.error(error); require("electron").app.exit(1); });
} else {
- require("node:test").test("database icons offer uploads and apply them only after successful selection on desktop and mobile", {
+ require("node:test").test("icon selection defers document writes and supports database uploads on desktop and mobile", {
skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
timeout: 30000,
}, async () => {