Skip to content

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

:sparkles: Support document custom attributes on Protyle containers https://github.com/siyuan-note/siyuan/issues/12137

Danielf37b346c1aea
diff --git a/app/src/protyle/util/syncRootAttributes.test.ts b/app/src/protyle/util/syncRootAttributes.test.ts
new file mode 100644
index 0000000000..6a16601c11
--- /dev/null
+++ b/app/src/protyle/util/syncRootAttributes.test.ts
@@ -0,0 +1,70 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {syncRootAttributes} from "./syncRootAttributes";
+
+class TestElement {
+ attributes = new Map<string, string>();
+
+ removeAttribute(name: string) {
+ this.attributes.delete(name);
+ }
+
+ setAttribute(name: string, value: string) {
+ this.attributes.set(name, value);
+ }
+}
+
+describe("syncRootAttributes", () => {
+ it("syncs the document ID and custom attributes only", () => {
+ const element = new TestElement();
+ const managedCustomAttributes = new Set<string>();
+
+ syncRootAttributes(element, managedCustomAttributes, {
+ id: "20260826120000-abcdefg",
+ title: "Document",
+ style: "color: red",
+ "custom-layout": "book",
+ "custom-content": "<strong>value</strong>",
+ });
+
+ assert.deepEqual(Object.fromEntries(element.attributes), {
+ "custom-layout": "book",
+ "custom-content": "<strong>value</strong>",
+ "data-node-id": "20260826120000-abcdefg",
+ });
+ assert.deepEqual([...managedCustomAttributes], ["custom-layout", "custom-content"]);
+ });
+
+ it("clears stale document attributes without removing runtime attributes", () => {
+ const element = new TestElement();
+ element.attributes.set("data-notebook-id", "notebook");
+ element.attributes.set("custom-runtime-state", "keep");
+ const managedCustomAttributes = new Set<string>();
+
+ syncRootAttributes(element, managedCustomAttributes, {
+ id: "20260826120000-aaaaaaa",
+ "custom-layout": "person",
+ });
+ syncRootAttributes(element, managedCustomAttributes, {
+ id: "20260826120001-bbbbbbb",
+ "custom-theme": "book",
+ "custom-empty": "",
+ });
+
+ assert.deepEqual(Object.fromEntries(element.attributes), {
+ "data-notebook-id": "notebook",
+ "custom-runtime-state": "keep",
+ "data-node-id": "20260826120001-bbbbbbb",
+ "custom-theme": "book",
+ });
+ assert.deepEqual([...managedCustomAttributes], ["custom-theme"]);
+ });
+
+ it("uses the current Protyle root ID when the IAL has no ID", () => {
+ const element = new TestElement();
+
+ syncRootAttributes(element, new Set<string>(), {}, "20260826120000-abcdefg");
+
+ assert.equal(element.attributes.get("data-node-id"), "20260826120000-abcdefg");
+ });
+});
diff --git a/app/src/protyle/util/syncRootAttributes.ts b/app/src/protyle/util/syncRootAttributes.ts
new file mode 100644
index 0000000000..f0ad2c2891
--- /dev/null
+++ b/app/src/protyle/util/syncRootAttributes.ts
@@ -0,0 +1,28 @@
+interface IAttributeTarget {
+ removeAttribute: (name: string) => void;
+ setAttribute: (name: string, value: string) => void;
+}
+
+export const syncRootAttributes = (
+ element: IAttributeTarget,
+ managedCustomAttributes: Set<string>,
+ ial: Record<string, string>,
+ rootID = ""
+) => {
+ managedCustomAttributes.forEach(attribute => element.removeAttribute(attribute));
+ managedCustomAttributes.clear();
+
+ Object.keys(ial).forEach(attribute => {
+ if (attribute.startsWith("custom-") && ial[attribute]) {
+ element.setAttribute(attribute, ial[attribute]);
+ managedCustomAttributes.add(attribute);
+ }
+ });
+
+ const nodeID = ial.id || rootID;
+ if (nodeID) {
+ element.setAttribute("data-node-id", nodeID);
+ } else {
+ element.removeAttribute("data-node-id");
+ }
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 7dc7b1b6cc..d33a9dc26d 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -200,6 +200,7 @@ import {shouldOpenListItemAttr} from "./listContext";
import {getBlockEdgeCaretRange, isCaretRangeInsideElement} from "./blockEdgeCaret";
import {LargeListVirtualizer} from "./listVirtualization";
import {forEachPluginSubscriber} from "../../plugin/EventBusCore";
+import {syncRootAttributes} from "../util/syncRootAttributes";
interface IShiftClickBlockPoint {
blockElement: HTMLElement;
@@ -345,6 +346,8 @@ export class WYSIWYG {
public element: HTMLDivElement;
public preventKeyup: boolean;
+ private protyle: IProtyle;
+ private rootCustomAttributes = new Set<string>();
private preventClick: boolean;
private preventInput: boolean;
private copyAsRichText = false;
@@ -454,6 +457,7 @@ export class WYSIWYG {
}
constructor(protyle: IProtyle) {
+ this.protyle = protyle;
this.element = document.createElement("div");
this.element.className = "protyle-wysiwyg";
this.element.setAttribute("spellcheck", "false");
@@ -486,6 +490,7 @@ export class WYSIWYG {
}
public renderCustom(ial: Record<string, string>) {
+ syncRootAttributes(this.protyle.element, this.rootCustomAttributes, ial, this.protyle.block.rootID);
let isFullWidth = ial[Constants.CUSTOM_SY_FULLWIDTH];
if (!isFullWidth) {
isFullWidth = window.siyuan.config.editor.fullWidth ? "true" : "false";