Skip to content

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

:bug: Preserve database row tabs https://github.com/siyuan-note/siyuan/issues/11371

Danielaf3c14732736
diff --git a/app/src/editor/databaseRow.ts b/app/src/editor/databaseRow.ts
index ac2d02f1b3..1cbbf3c86d 100644
--- a/app/src/editor/databaseRow.ts
+++ b/app/src/editor/databaseRow.ts
@@ -2,35 +2,34 @@ import {Custom} from "../layout/dock/Custom";
import {Tab} from "../layout/Tab";
import {App} from "../index";
import {renderAVAttribute} from "../protyle/render/av/blockAttr";
-import {getAllModels} from "../layout/getAll";
+import {Protyle} from "../protyle";
export const newDatabaseRowModel = (options: {
app: App,
tab: Tab,
data: {
avID: string,
+ blockID: string,
+ notebookId: string,
itemID: string,
valueID: string,
title: string,
},
- protyle?: IProtyle,
}) => {
- const render = (custom: Custom, retry = true) => {
+ let customModel: Custom;
+ let contextProtyle: IProtyle;
+ let ghostProtyle: Protyle;
+ let destroyed = false;
+ const render = (custom: Custom) => {
const previousBodyElement = custom.element.querySelector<HTMLElement>(".protyle-db-row__body");
- if (!previousBodyElement) {
+ if (!previousBodyElement || !contextProtyle) {
return;
}
const bodyElement = document.createElement("div");
bodyElement.className = "custom-attr protyle-db-row__body";
previousBodyElement.replaceWith(bodyElement);
- const sourceProtyle = options.protyle?.ws?.ws?.readyState === WebSocket.OPEN ?
- options.protyle : getAllModels().editor[0]?.editor.protyle;
- if (sourceProtyle) {
- renderAVAttribute(bodyElement, options.data.itemID, sourceProtyle, undefined,
- {avID: options.data.avID, itemID: options.data.itemID, valueID: options.data.valueID});
- } else if (retry) {
- setTimeout(() => render(custom, false), 0);
- }
+ renderAVAttribute(bodyElement, options.data.itemID, contextProtyle, undefined,
+ {avID: options.data.avID, itemID: options.data.itemID, valueID: options.data.valueID});
};
const model = new Custom({
app: options.app,
@@ -38,6 +37,7 @@ export const newDatabaseRowModel = (options: {
type: "siyuan-database-row",
data: options.data,
init(custom) {
+ customModel = custom;
custom.element.innerHTML = `<div class="protyle-db-row fn__flex-1 fn__flex-column">
<div class="protyle-content">
<div class="protyle-top">
@@ -47,10 +47,24 @@ export const newDatabaseRowModel = (options: {
</div>
</div>`;
custom.element.querySelector(".protyle-db-row__title span").textContent = options.data.title || window.siyuan.languages.untitled;
- render(custom);
+ ghostProtyle = new Protyle(options.app, document.createElement("div"), {
+ blockId: options.data.blockID,
+ notebookId: options.data.notebookId,
+ after(editor) {
+ if (destroyed) {
+ return;
+ }
+ contextProtyle = editor.protyle;
+ render(custom);
+ },
+ });
+ },
+ destroy() {
+ destroyed = true;
+ ghostProtyle?.destroy();
},
update() {
- render(options.tab.model as Custom);
+ render(customModel);
},
});
return model;
diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts
index 0b5b44a1f0..918f5b8537 100644
--- a/app/src/editor/util.ts
+++ b/app/src/editor/util.ts
@@ -1,7 +1,7 @@
import {Tab} from "../layout/Tab";
import {Editor} from "./index";
import {Wnd} from "../layout/Wnd";
-import {getInstanceById, getWndByLayout, pdfIsLoading, setPanelFocus} from "../layout/util";
+import {getInstanceById, getWndByLayout, newModelByInitData, pdfIsLoading, setPanelFocus} from "../layout/util";
import {getDockByType} from "../layout/tabUtil";
import {getAllModels, getAllTabs} from "../layout/getAll";
import {highlightById, scrollCenter} from "../util/highlightById";
@@ -30,12 +30,10 @@ import {objEquals} from "../util/functions";
import {resize} from "../protyle/util/resize";
import {Search} from "../search";
import {App} from "../index";
-import {newCardModel} from "../card/newCardTab";
import {preventScroll} from "../protyle/scroll/preventScroll";
import {clearOBG} from "../layout/dock/util";
import {Model} from "../layout/Model";
import {hideElements} from "../protyle/ui/hideElements";
-import {newDatabaseRowModel} from "./databaseRow";
const isSameCustomTab = (type: string, data: any, options: IOpenFileOptions) => {
if (!options.custom || (options.custom.id && options.custom.id !== type)) {
@@ -491,29 +489,13 @@ const newTab = (options: IOpenFileOptions) => {
title: options.custom.title,
callback(tab) {
if (options.custom.id) {
- if (options.custom.id === "siyuan-card") {
- tab.addModel(newCardModel({
- app: options.app,
- tab,
- data: options.custom.data
- }));
- } else if (options.custom.id === "siyuan-database-row") {
- tab.addModel(newDatabaseRowModel({
- app: options.app,
- tab,
- data: options.custom.data,
- protyle: options.custom.protyle,
- }));
- } else {
- options.app.plugins.find(p => {
- if (p.models[options.custom.id]) {
- tab.addModel(p.models[options.custom.id]({
- tab,
- data: options.custom.data
- }));
- return true;
- }
- });
+ const model = newModelByInitData(options.app, tab, {
+ instance: "Custom",
+ customModelType: options.custom.id,
+ customModelData: options.custom.data,
+ });
+ if (model) {
+ tab.addModel(model);
}
} else {
// plugin 0.8.3 历史兼容
diff --git a/app/src/layout/tabUtil.ts b/app/src/layout/tabUtil.ts
index 11ea437b24..5dd8b76fad 100644
--- a/app/src/layout/tabUtil.ts
+++ b/app/src/layout/tabUtil.ts
@@ -15,7 +15,6 @@ import {Bookmark} from "./dock/Bookmark";
import {Tag} from "./dock/Tag";
import {Search} from "../search";
import {Custom} from "./dock/Custom";
-import {newCardModel} from "../card/newCardTab";
import {updateHotkeyTip} from "../protyle/util/compatibility";
import {openSearch} from "../search/spread";
import {openRecentDocs} from "../business/openRecentDocs";
@@ -415,23 +414,11 @@ export const copyTab = (app: App, tab: Tab) => {
});
} else if (tab.model instanceof Custom) {
const custom = tab.model as Custom;
- if (custom.type === "siyuan-card") {
- model = newCardModel({
- app,
- tab: newTab,
- data: custom.data
- });
- } else {
- app.plugins.find(item => {
- if (item.models[custom.type]) {
- model = item.models[custom.type]({
- tab: newTab,
- data: custom.data
- });
- return true;
- }
- });
- }
+ model = newModelByInitData(app, newTab, {
+ instance: "Custom",
+ customModelType: custom.type,
+ customModelData: custom.data,
+ });
} else if (!tab.model && tab.headElement) {
const initData = JSON.parse(tab.headElement.getAttribute("data-initdata") || "{}");
if (initData) {
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index 6bcbcf093e..494bec8fb8 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -33,6 +33,10 @@ import {setStorageVal} from "../protyle/util/compatibility";
import {adjustDockPadding} from "./dock/util";
import {setTitle} from "../util/processTitle";
+const isBuiltInCustomModel = (type: string) => {
+ return type === "siyuan-card" || type === "siyuan-database-row";
+};
+
export const setPanelFocus = (element: Element, isSaveLayout = true) => {
if (element.getAttribute("data-type") === "wnd") {
const title = element.querySelector('.layout-tab-bar .item--focus[data-type="tab-header"] .item__text')?.textContent || "";
@@ -435,6 +439,15 @@ export const JSONToCenter = (
config: json.config
}));
} else if (json.instance === "Custom") {
+ if (json.customModelType === "siyuan-database-row") {
+ const notebookId = json.customModelData?.notebookId;
+ const notebook = window.siyuan.notebooks.find((item) => item.id === notebookId);
+ if (notebook?.closed && isEncryptedBox(notebookId)) {
+ (layout as Tab).headElement.removeAttribute("data-init-active");
+ removedTabs.push(layout as Tab);
+ return;
+ }
+ }
if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
(layout as Tab).headElement.classList.add("item--unupdate");
}
@@ -482,7 +495,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
const initData = item.getAttribute("data-initdata");
if (initData) {
const initDataObj = JSON.parse(initData);
- if (initDataObj.instance === "Custom" && initDataObj.customModelType !== "siyuan-card") {
+ if (initDataObj.instance === "Custom" && !isBuiltInCustomModel(initDataObj.customModelType)) {
let hasPlugin = false;
app.plugins.find(plugin => {
if (Object.keys(plugin.models).includes(initDataObj.customModelType)) {
@@ -611,6 +624,7 @@ export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any, brea
json.rootId = layout.editor.protyle.block.rootID;
json.mode = layout.editor.protyle.preview.element.classList.contains("fn__none") ? "wysiwyg" : "preview";
json.action = (layout.editor.protyle.block.showAll && layout.editor.protyle.block.id !== layout.editor.protyle.block.rootID) ? Constants.CB_GET_ALL : Constants.CB_GET_SCROLL;
+ json.databaseRowId = layout.editor.protyle.element.dataset.databaseRowId;
json.instance = "Editor";
} else if (layout instanceof Asset) {
json.path = layout.path;
@@ -799,7 +813,7 @@ export const newModelByInitData = (app: App, tab: Tab, json: any) => {
json.action = json.action.filter((item: string) => item !== Constants.CB_GET_ALL);
}
}
- model = new Editor({
+ const editorModel = new Editor({
app,
tab,
rootId: json.rootId,
@@ -809,7 +823,17 @@ export const newModelByInitData = (app: App, tab: Tab, json: any) => {
scrollPosition: json.scrollPosition,
action: Array.isArray(json.action) ? json.action.concat(Constants.CB_GET_FOCUS) :
(json.action ? [json.action, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS]),
+ afterInitProtyle(editor) {
+ if (json.databaseRowId) {
+ editor.protyle.databaseAttributePanel?.expand();
+ editor.protyle.contentElement.scrollTop = 0;
+ }
+ },
});
+ if (json.databaseRowId) {
+ editorModel.editor.protyle.element.dataset.databaseRowId = json.databaseRowId;
+ }
+ model = editorModel;
}
return model;
};
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index 1afa7e74a8..97e449d2b2 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -43,12 +43,40 @@ import {callMobileAppShowKeyboard} from "../../../mobile/util/mobileAppUtil";
/// #if !MOBILE
import {openFile, openFileById} from "../../../editor/util";
import {Editor} from "../../../editor";
-import {getAllModels} from "../../../layout/getAll";
+import {getAllTabs} from "../../../layout/getAll";
const isDetachedDatabaseCell = (cellElement: HTMLElement) => {
return cellElement.dataset.detached === "true" || !cellElement.querySelector(".av__celltext--ref");
};
+const showDatabaseRowPreview = (model: Editor, blockID: string) => {
+ if (!model?.editor?.protyle) {
+ return;
+ }
+ model.editor.protyle.element.dataset.databaseRowId = blockID;
+ model.editor.protyle.databaseAttributePanel?.expand();
+ model.editor.protyle.contentElement.scrollTop = 0;
+};
+
+const getDatabaseRowPreviewTab = (blockID: string) => {
+ return getAllTabs().find((tab) => {
+ if (tab.model instanceof Editor) {
+ return tab.model.editor.protyle.element.dataset.databaseRowId === blockID;
+ }
+ const initData = tab.headElement?.getAttribute("data-initdata");
+ if (!initData) {
+ return false;
+ }
+ try {
+ const initObj = JSON.parse(initData) as ILayoutJSON;
+ return initObj.instance === "Editor" && initObj.databaseRowId === blockID;
+ } catch (e) {
+ console.warn("Failed to parse database row tab init data:", e);
+ return false;
+ }
+ });
+};
+
const openDatabaseRow = (protyle: IProtyle, target: HTMLElement, blockElement: HTMLElement) => {
const cellElement = hasClosestByClassName(target, "av__cell") as HTMLElement;
const rowElement = hasClosestByClassName(target, "av__row") || hasClosestByClassName(target, "av__gallery-item");
@@ -67,25 +95,25 @@ const openDatabaseRow = (protyle: IProtyle, target: HTMLElement, blockElement: H
title,
data: {
avID: blockElement.dataset.avId,
+ blockID: blockElement.dataset.nodeId,
+ notebookId: protyle.notebookId,
itemID: rowElement.getAttribute("data-id"),
valueID: cellElement.dataset.id,
title,
},
- protyle,
},
});
return;
}
const blockID = cellElement.querySelector<HTMLElement>(".av__celltext--ref")?.dataset.id;
if (blockID) {
- const openedEditor = getAllModels().editor.find((item) => {
- return item.editor.protyle.element.dataset.databaseRowId === blockID;
- });
- if (openedEditor) {
- openedEditor.parent.parent.switchTab(openedEditor.parent.headElement);
- openedEditor.parent.parent.showHeading();
- openedEditor.editor.protyle.databaseAttributePanel?.expand();
- openedEditor.editor.protyle.contentElement.scrollTop = 0;
+ const openedTab = getDatabaseRowPreviewTab(blockID);
+ if (openedTab) {
+ openedTab.parent.switchTab(openedTab.headElement);
+ openedTab.parent.showHeading();
+ if (openedTab.model instanceof Editor) {
+ showDatabaseRowPreview(openedTab.model, blockID);
+ }
return;
}
openFileById({
@@ -96,13 +124,7 @@ const openDatabaseRow = (protyle: IProtyle, target: HTMLElement, blockElement: H
removeCurrentTab: false,
zoomIn: true,
afterOpen(model: Editor) {
- if (!model?.editor?.protyle) {
- return;
- }
- model.editor.protyle.element.dataset.databaseRowPreview = "true";
- model.editor.protyle.element.dataset.databaseRowId = blockID;
- model.editor.protyle.databaseAttributePanel?.expand();
- model.editor.protyle.contentElement.scrollTop = 0;
+ showDatabaseRowPreview(model, blockID);
},
});
}
diff --git a/app/src/protyle/scroll/saveScroll.ts b/app/src/protyle/scroll/saveScroll.ts
index 76666bd6b6..8d9bc11386 100644
--- a/app/src/protyle/scroll/saveScroll.ts
+++ b/app/src/protyle/scroll/saveScroll.ts
@@ -9,7 +9,7 @@ import {isEncryptedBox} from "../../util/pathName";
export const saveScroll = (protyle: IProtyle, getObject = false) => {
if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly ||
- (protyle.element.dataset.databaseRowPreview === "true" && !getObject)) {
+ (protyle.element.dataset.databaseRowId && !getObject)) {
// 报错或者空白页面
return undefined;
}
diff --git a/app/src/types/config.d.ts b/app/src/types/config.d.ts
index 91d21ed567..03d5913d3f 100644
--- a/app/src/types/config.d.ts
+++ b/app/src/types/config.d.ts
@@ -2213,6 +2213,10 @@ declare namespace Config {
* (Editor) Block ID
*/
blockId: string;
+ /**
+ * 数据库行预览块 ID
+ */
+ databaseRowId?: string;
/**
* Object name
*/
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index c04361e1cb..0039829266 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -700,6 +700,7 @@ interface ILayoutJSON extends ILayoutOptions {
action?: TProtyleAction
icon?: string
rootId?: string
+ databaseRowId?: string
active?: boolean
pin?: boolean
isPreview?: boolean
@@ -752,7 +753,6 @@ interface IOpenFileOptions {
title: string,
icon: string,
data?: any
- protyle?: IProtyle,
id: string,
fn?: (options: {
tab: import("../layout/Tab").Tab,