Skip to content

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

:bug: Restore unreferenced database previews https://github.com/siyuan-note/siyuan/issues/19896

Daniel5a93c145c412
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index 9b169388c2..e8bc165af7 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -267,11 +267,12 @@ export const renderGallery = async (options: {
let data: IAV = options.data;
if (!data) {
+ const standalone = options.protyle.block.action?.includes(Constants.CB_GET_AV_NO_CREATE);
const avPageSize = getPageSize(options.blockElement);
const locateParams = getAVLocateParams(options.blockElement, !created && !snapshot);
const common = {
id: options.blockElement.getAttribute("data-av-id"),
- blockID: options.blockElement.getAttribute("data-node-id"),
+ blockID: standalone ? "" : options.blockElement.getAttribute("data-node-id"),
viewID: locateParams?.viewID || (window.siyuan.isPublish ? getPublishAVView(options.blockElement) : ""),
};
const paging = {
@@ -287,7 +288,7 @@ export const renderGallery = async (options: {
}, undefined, false) : fetchSyncPost("/api/av/renderAttributeView", {
...common, ...paging,
initialLayout: options.blockElement.getAttribute("data-av-type"),
- createIfNotExist: !window.siyuan.isPublish,
+ createIfNotExist: !window.siyuan.isPublish && !standalone,
targetItemID: locateParams?.targetItemID || "",
targetGroupID: locateParams?.targetGroupID || "",
}, undefined, false));
diff --git a/app/src/protyle/render/av/kanban/render.ts b/app/src/protyle/render/av/kanban/render.ts
index cd428fe1eb..bc519e7e29 100644
--- a/app/src/protyle/render/av/kanban/render.ts
+++ b/app/src/protyle/render/av/kanban/render.ts
@@ -151,11 +151,12 @@ export const renderKanban = async (options: {
let data: IAV = options.data;
if (!data) {
+ const standalone = options.protyle.block.action?.includes(Constants.CB_GET_AV_NO_CREATE);
const avPageSize = getPageSize(options.blockElement);
const locateParams = getAVLocateParams(options.blockElement, !created && !snapshot);
const common = {
id: options.blockElement.getAttribute("data-av-id"),
- blockID: options.blockElement.getAttribute("data-node-id"),
+ blockID: standalone ? "" : options.blockElement.getAttribute("data-node-id"),
viewID: locateParams?.viewID || (window.siyuan.isPublish ? getPublishAVView(options.blockElement) : ""),
};
const paging = {
@@ -171,7 +172,7 @@ export const renderKanban = async (options: {
}, undefined, false) : fetchSyncPost("/api/av/renderAttributeView", {
...common, ...paging,
initialLayout: options.blockElement.getAttribute("data-av-type"),
- createIfNotExist: !window.siyuan.isPublish,
+ createIfNotExist: !window.siyuan.isPublish && !standalone,
targetItemID: locateParams?.targetItemID || "",
targetGroupID: locateParams?.targetGroupID || "",
}, undefined, false));
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index 41cdeceaae..4e7f1b8700 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -679,10 +679,12 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:
const locateParams = getAVLocateParams(e, !created && !snapshot);
let data: IAV;
if (!avData) {
+ // 未引用数据库预览没有实际载体,不向内核传递临时块 ID。
+ const standalone = protyle.block.action?.includes(Constants.CB_GET_AV_NO_CREATE);
const common = {
calendarRange: getCalendarRequestRange(e, locateParams?.viewID || undefined),
id: e.getAttribute("data-av-id"),
- blockID: e.getAttribute("data-node-id"),
+ blockID: standalone ? "" : e.getAttribute("data-node-id"),
viewID: locateParams?.viewID || (window.siyuan.isPublish ? getPublishAVView(e) : ""),
};
const paging = {
@@ -698,7 +700,7 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:
}, undefined, false) : fetchSyncPost("/api/av/renderAttributeView", {
...common, ...paging,
initialLayout: e.getAttribute("data-av-type"),
- createIfNotExist: !window.siyuan.isPublish && !protyle.block.action?.includes(Constants.CB_GET_AV_NO_CREATE),
+ createIfNotExist: !window.siyuan.isPublish && !standalone,
targetItemID: locateParams?.targetItemID || "",
targetGroupID: locateParams?.targetGroupID || "",
}, undefined, false));
diff --git a/app/src/protyle/render/av/standalonePreview.test.ts b/app/src/protyle/render/av/standalonePreview.test.ts
new file mode 100644
index 0000000000..002889bda1
--- /dev/null
+++ b/app/src/protyle/render/av/standalonePreview.test.ts
@@ -0,0 +1,52 @@
+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";
+
+for (const layout of ["table", "gallery", "kanban"]) {
+ test(`${layout} previews omit temporary carriers and never create missing databases`, async () => {
+ const requests: Array<{blockID: string, createIfNotExist: boolean}> = [];
+ const methods: Record<string, (...args: unknown[]) => Promise<void>> = {};
+ const imports: Record<string, unknown> = {
+ Constants: {CB_GET_AV_NO_CREATE: "standalone"},
+ getAVSelectedItemPoints: (): [] => [],
+ getPageSize: () => ({unGroupPageSize: 50, groupPageSize: {}}),
+ isCurrentAVRender: () => false,
+ fetchSyncPost: async (url: string, request: typeof requests[number]) => {
+ assert.equal(url, "/api/av/renderAttributeView");
+ requests.push(request);
+ return {code: 0};
+ },
+ };
+ runInNewContext(transpileModule(readFileSync(
+ `src/protyle/render/av/${layout === "table" ? "" : layout + "/"}render.ts`, "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2022},
+ }).outputText, {
+ exports: methods,
+ window: {siyuan: {}},
+ require: () => new Proxy(imports, {get: (target, key: string) => target[key] || ((): undefined => undefined)}),
+ });
+ const block = {
+ style: {}, firstElementChild: {innerHTML: "loading"},
+ getAttribute: (name: string) => ({
+ "data-type": "NodeAttributeView", "data-node-id": "carrier", "data-av-id": "database",
+ "data-av-type": layout,
+ })[name] || null,
+ closest: (): null => null, removeAttribute() {}, querySelector: (): null => null, querySelectorAll: (): [] => [],
+ };
+ for (const standalone of [true, false]) {
+ const protyle = {options: {}, block: {action: standalone ? ["standalone"] : []}, contentElement: {scrollTop: 0}};
+ if (layout === "table") {
+ await methods.avRender(block, protyle);
+ } else {
+ await methods[layout === "gallery" ? "renderGallery" : "renderKanban"]({blockElement: block, protyle});
+ }
+ }
+ assert.equal(requests.length, 2);
+ assert.equal(requests[0].blockID, "");
+ assert.equal(requests[0].createIfNotExist, false);
+ assert.equal(requests[1].blockID, "carrier");
+ assert.equal(requests[1].createIfNotExist, true);
+ });
+}