Skip to content

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

:bug: Preserve document edit mode in database row panels https://github.com/siyuan-note/siyuan/issues/20043

Daniel0d5cbe86cd61
diff --git a/app/src/editor/databaseRow.ts b/app/src/editor/databaseRow.ts
index 752fd18f62..9e991283c4 100644
--- a/app/src/editor/databaseRow.ts
+++ b/app/src/editor/databaseRow.ts
@@ -9,6 +9,7 @@ import {registerDatabaseRowRefresh} from "../protyle/render/av/databaseRowRefres
import {focusDatabasePrimary} from "../protyle/render/av/primaryFocus";
import {setPanelFocus} from "../layout/util";
import {preserveAVBindingRange} from "../protyle/render/av/binding";
+import {inheritDatabaseRowReadonly} from "../protyle/render/av/rowReadonly";
export const newDatabaseRowModel = (options: {
app: App,
@@ -29,6 +30,7 @@ export const newDatabaseRowModel = (options: {
}) => {
let customModel: Custom;
let contextProtyle: IProtyle;
+ let sourceProtyle: Partial<IProtyle>;
let ghostProtyle: Protyle;
let resizeObserver: ResizeObserver;
let unregisterRefresh: () => void;
@@ -63,6 +65,10 @@ export const newDatabaseRowModel = (options: {
if (!previousBodyElement || !contextProtyle) {
return;
}
+ if (sourceProtyle) {
+ inheritDatabaseRowReadonly(contextProtyle, sourceProtyle);
+ sourceProtyle = undefined;
+ }
const data = custom.data as typeof options.data;
const currentRenderVersion = ++renderVersion;
const bodyElement = document.createElement("div");
@@ -131,6 +137,9 @@ export const newDatabaseRowModel = (options: {
custom.data.title = title;
custom.tab.updateTitle(title);
});
+ custom.element.addEventListener("database-row-readonly", (event) => {
+ sourceProtyle = (event as CustomEvent<Partial<IProtyle>>).detail;
+ });
updateLayout(custom);
resizeObserver = new ResizeObserver(() => updateLayout(custom));
resizeObserver.observe(custom.element);
diff --git a/app/src/protyle/render/av/openDatabaseRow.ts b/app/src/protyle/render/av/openDatabaseRow.ts
index c7f4c7967b..b013aeb8be 100644
--- a/app/src/protyle/render/av/openDatabaseRow.ts
+++ b/app/src/protyle/render/av/openDatabaseRow.ts
@@ -15,6 +15,7 @@ import {searchMarkRender} from "../searchMarkRender";
import {registerDatabaseRowRefresh} from "./databaseRowRefresh";
import {focusDatabasePrimary} from "./primaryFocus";
import {preserveAVBindingRange} from "./binding";
+import {inheritDatabaseRowReadonly} from "./rowReadonly";
export interface IDatabaseRowOpenData {
avID: string;
@@ -120,6 +121,7 @@ const openMobileDatabaseRow = (protyle: Pick<IProtyle, "app">, data: IDatabaseRo
notebookId: data.notebookID,
after(editor) {
const contextProtyle = editor.protyle;
+ inheritDatabaseRowReadonly(contextProtyle, protyle);
rowElement.dataset.protyleId = contextProtyle.id;
unregisterRefresh = registerDatabaseRowRefresh(contextProtyle.id, {
getAVID: () => data.avID,
@@ -131,11 +133,12 @@ const openMobileDatabaseRow = (protyle: Pick<IProtyle, "app">, data: IDatabaseRo
});
};
/// #else
-const showDatabaseRowPreview = (model: Editor, data: IDatabaseRowOpenData) => {
+const showDatabaseRowPreview = (model: Editor, data: IDatabaseRowOpenData, source: Partial<IProtyle>) => {
if (!model?.editor?.protyle) {
return;
}
const editorProtyle = model.editor.protyle;
+ inheritDatabaseRowReadonly(editorProtyle, source);
editorProtyle.element.dataset.databaseRowId = data.boundBlockID || "";
editorProtyle.databaseAttributePanel?.expand(data.avID);
editorProtyle.contentElement.scrollTop = 0;
@@ -145,20 +148,20 @@ const showDatabaseRowPreview = (model: Editor, data: IDatabaseRowOpenData) => {
});
};
-const focusDatabaseRowPreview = (model: Editor, data: IDatabaseRowOpenData) => {
+const focusDatabaseRowPreview = (model: Editor, data: IDatabaseRowOpenData, source: Partial<IProtyle>) => {
const editorProtyle = model?.editor?.protyle;
if (!editorProtyle || !data.boundBlockID) {
return;
}
if (editorProtyle.block.showAll && editorProtyle.block.id === data.boundBlockID) {
- showDatabaseRowPreview(model, data);
+ showDatabaseRowPreview(model, data, source);
return;
}
zoomOut({
protyle: editorProtyle,
id: data.boundBlockID,
reload: true,
- callback: () => showDatabaseRowPreview(model, data),
+ callback: () => showDatabaseRowPreview(model, data, source),
});
};
@@ -201,6 +204,7 @@ export const openDatabaseRowByData = async (protyle: Pick<IProtyle, "app">, data
window.siyuan.menus.menu.remove();
openMobileFileById(protyle.app, data.boundBlockID, [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS],
undefined, undefined, (editorProtyle) => {
+ inheritDatabaseRowReadonly(editorProtyle, protyle);
editorProtyle.element.dataset.databaseRowId = data.boundBlockID;
editorProtyle.databaseAttributePanel?.expand(data.avID);
editorProtyle.contentElement.scrollTop = 0;
@@ -242,6 +246,7 @@ export const openDatabaseRowByData = async (protyle: Pick<IProtyle, "app">, data
afterOpen(model) {
if (model instanceof Custom) {
Object.assign(model.data, data, {blockID: data.databaseBlockID, notebookId: data.notebookID});
+ model.element.dispatchEvent(new CustomEvent("database-row-readonly", {detail: protyle}));
model.update();
}
},
@@ -259,7 +264,7 @@ export const openDatabaseRowByData = async (protyle: Pick<IProtyle, "app">, data
position: options.position,
zoomIn: true,
afterOpen(model: Editor) {
- focusDatabaseRowPreview(model, data);
+ focusDatabaseRowPreview(model, data, protyle);
},
});
return Boolean(opened);
@@ -283,7 +288,7 @@ export const openDatabaseRowByData = async (protyle: Pick<IProtyle, "app">, data
openedTab.parent.switchTab(openedTab.headElement);
openedTab.parent.showHeading();
if (openedModel instanceof Editor) {
- focusDatabaseRowPreview(openedModel as Editor, data);
+ focusDatabaseRowPreview(openedModel as Editor, data, protyle);
}
return true;
}
@@ -296,7 +301,7 @@ export const openDatabaseRowByData = async (protyle: Pick<IProtyle, "app">, data
keepAVPanel: options?.keepAVPanel,
zoomIn: true,
afterOpen(model: Editor) {
- showDatabaseRowPreview(model, data);
+ showDatabaseRowPreview(model, data, protyle);
},
});
return Boolean(opened);
diff --git a/app/src/protyle/render/av/rowReadonly.test.ts b/app/src/protyle/render/av/rowReadonly.test.ts
new file mode 100644
index 0000000000..bfb5aef916
--- /dev/null
+++ b/app/src/protyle/render/av/rowReadonly.test.ts
@@ -0,0 +1,142 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const {parse} = require("ifdef-loader/preprocessor");
+
+const createPanel = (mobile: boolean) => {
+ const siyuan = {config: {readonly: false, editor: {readOnly: true, databaseAttrShow: true}},
+ isPublish: false, languages: {}, dialogs: [] as unknown[]};
+ const source = {app: {}, disabled: false, block: {rootID: "document"}, options: {history: {}}} as IProtyle;
+ const target = {disabled: true, block: {rootID: "document"}, highlight: {}, hint: {}, id: "row"} as IProtyle;
+ let permanent = false;
+ let loaded: () => void;
+ const rendered: boolean[] = [];
+ const listeners = new Map<string, (event: {detail: Partial<IProtyle>}) => void>();
+ const body = {style: {}};
+ const element = {
+ dataset: {}, clientWidth: 800,
+ querySelector: () => body,
+ addEventListener: (name: string, listener: (event: {detail: Partial<IProtyle>}) => void) => listeners.set(name, listener),
+ dispatchEvent: (event: {type: string, detail: Partial<IProtyle>}) => listeners.get(event.type)?.(event),
+ append() {},
+ };
+ class Custom {
+ public data: Record<string, unknown>;
+ public element = element;
+ public update: () => void;
+
+ constructor(options: {data: Record<string, unknown>, update: () => void, init: (model: Custom) => void}) {
+ this.data = options.data;
+ this.update = options.update;
+ options.init(this);
+ }
+ }
+ class Protyle {
+ constructor(_app: unknown, _element: unknown, options: {after: (editor: {protyle: IProtyle}) => void}) {
+ loaded = () => options.after({protyle: target});
+ }
+ }
+ let model: Custom;
+ const dependencies: Record<string, unknown> = {
+ "../../util/onGet": {
+ disabledProtyle: (protyle: IProtyle) => { protyle.disabled = true; },
+ enableProtyle: (protyle: IProtyle) => { if (!permanent) { protyle.disabled = false; } },
+ },
+ "../layout/dock/Custom": {Custom},
+ "../../../layout/dock/Custom": {Custom},
+ "../protyle": {Protyle},
+ "../../index": {Protyle},
+ "../../../dialog": {Dialog: class { public element = {querySelector: () => element}; }},
+ "../protyle/ui/padding": {getEditorHorizontalPadding: () => ({left: 0, right: 0})},
+ };
+ const load = <T>(file: string) => {
+ const exports = {} as T;
+ runInNewContext(transpileModule(parse(readFileSync("src/" + file + ".ts", "utf8"),
+ {MOBILE: mobile, BROWSER: false}, false, true), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
+ }).outputText, {
+ exports, window: {siyuan}, document: {createElement: () => ({})},
+ ResizeObserver: class { observe() {} },
+ CustomEvent: class {
+ public detail: unknown;
+ constructor(public type: string, options: {detail: unknown}) { this.detail = options.detail; }
+ },
+ require: (name: string) => {
+ if (name.endsWith("/blockAttr")) {
+ return {renderAVAttribute: (_element: unknown, _id: string, protyle: IProtyle) => rendered.push(protyle.disabled)};
+ }
+ if (name.endsWith("/databaseRowRefresh")) {
+ return {registerDatabaseRowRefresh() {}};
+ }
+ return dependencies[name] || {};
+ },
+ });
+ return exports;
+ };
+ const readonly = load<typeof import("./rowReadonly")>("protyle/render/av/rowReadonly");
+ dependencies["./rowReadonly"] = readonly;
+ dependencies["../protyle/render/av/rowReadonly"] = readonly;
+ const desktop = load<typeof import("../../../editor/databaseRow")>("editor/databaseRow");
+ dependencies["../../../editor/util"] = {openFile: async (options: {
+ custom: {data: Record<string, unknown>}, afterOpen: (model: Custom) => void,
+ }) => {
+ if (!model) {
+ model = desktop.newDatabaseRowModel({app: source.app,
+ tab: {} as Parameters<typeof desktop.newDatabaseRowModel>[0]["tab"],
+ data: options.custom.data as Parameters<typeof desktop.newDatabaseRowModel>[0]["data"]}) as unknown as Custom;
+ }
+ options.afterOpen(model);
+ return {};
+ }};
+ const api = load<typeof import("./openDatabaseRow")>("protyle/render/av/openDatabaseRow");
+ const open = () => api.openDatabaseRowByData(source, {
+ avID: "database", databaseBlockID: "carrier", notebookID: "notebook", itemID: "row", valueID: "value",
+ title: "Entry", isDetached: true,
+ });
+ return {source, target, siyuan, rendered, open, load: () => loaded(), lockForever: () => { permanent = true; },
+ savedData: () => JSON.stringify(model.data)};
+};
+
+for (const mobile of [false, true]) {
+ test(`database row inherits temporary document unlock after loading (${mobile ? "mobile" : "desktop"})`, async () => {
+ const panel = createPanel(mobile);
+ assert.equal(await panel.open(), true);
+ assert.equal(panel.rendered.length, 0);
+ panel.load();
+ assert.equal(panel.rendered.at(-1), false);
+ panel.source.disabled = true;
+ await panel.open();
+ if (mobile) {
+ panel.load();
+ }
+ assert.equal(panel.rendered.at(-1), true);
+ panel.source.disabled = false;
+ await panel.open();
+ if (mobile) {
+ panel.load();
+ }
+ assert.equal(panel.rendered.at(-1), false);
+ if (!mobile) {
+ assert.doesNotMatch(panel.savedData(), /disabled|sourceProtyle|rootID/);
+ }
+ });
+
+ test(`database row retains readonly boundaries (${mobile ? "mobile" : "desktop"})`, async () => {
+ for (const mode of ["workspace", "publish", "history", "snapshot", "permanent", "other-document", "no-context"]) {
+ const panel = createPanel(mobile);
+ panel.siyuan.config.readonly = mode === "workspace";
+ panel.siyuan.isPublish = mode === "publish";
+ if (mode === "history") { panel.source.options.history.created = "archive"; }
+ if (mode === "snapshot") { panel.source.options.history.snapshot = "snapshot"; }
+ if (mode === "permanent") { panel.lockForever(); }
+ if (mode === "other-document") { panel.target.block.rootID = "another-document"; }
+ if (mode === "no-context") { delete panel.source.block; }
+ await panel.open();
+ panel.load();
+ assert.equal(panel.rendered.at(-1), true, mode);
+ }
+ });
+}
diff --git a/app/src/protyle/render/av/rowReadonly.ts b/app/src/protyle/render/av/rowReadonly.ts
new file mode 100644
index 0000000000..a329dc417d
--- /dev/null
+++ b/app/src/protyle/render/av/rowReadonly.ts
@@ -0,0 +1,15 @@
+import {disabledProtyle, enableProtyle} from "../../util/onGet";
+
+export const inheritDatabaseRowReadonly = (target: IProtyle, source: Partial<IProtyle>) => {
+ // 条目面板只继承所属文档的临时编辑状态,不解锁其他文档。
+ if (!source?.block?.rootID || source.block.rootID !== target.block.rootID ||
+ typeof source.disabled !== "boolean") {
+ return;
+ }
+ if (window.siyuan.config.readonly || window.siyuan.isPublish || source.disabled ||
+ source.options?.history?.created || source.options?.history?.snapshot) {
+ disabledProtyle(target);
+ } else {
+ enableProtyle(target);
+ }
+};