Skip to content

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

:art: Improve data snapshot history preview https://github.com/siyuan-note/siyuan/issues/17724

Daniel537e13ffbc59
diff --git a/app/src/history/doc.ts b/app/src/history/doc.ts
index 72eff38fb2..4108f1ca11 100644
--- a/app/src/history/doc.ts
+++ b/app/src/history/doc.ts
@@ -9,9 +9,10 @@ import {isMobile} from "../util/functions";
import type {App} from "../index";
import {resizeSide} from "./resizeSide";
import {escapeHtml} from "../util/escape";
-import {openRepoFile, renderRepoFileList, rollbackRepoFile, saveRepoFile} from "./repoFile";
+import {renderRepoFile, renderRepoFileList, rollbackRepoFile, saveRepoFile} from "./repoFile";
let historyEditor: Protyle;
+const repoHistoryEditors = new WeakMap<HTMLElement, Protyle>();
let isLoading = false;
const renderDoc = (element: HTMLElement, currentPage: number, id: string) => {
@@ -76,6 +77,13 @@ const renderRepo = async (element: HTMLElement, currentPage: number, id: string)
const pageNumElement = element.querySelector('[data-type="jumpSnapshotPage"]');
const pageInfoElement = nextElement.nextElementSibling.nextElementSibling;
const listElement = element.querySelector(".b3-list--background");
+ const previewElement = element.querySelector('[data-type="repoPanel"]');
+ repoHistoryEditors.get(element)?.destroy();
+ repoHistoryEditors.delete(element);
+ element.querySelector(".protyle-title__input").classList.add("fn__none");
+ previewElement.classList.add("fn__none");
+ previewElement.removeAttribute("data-request-id");
+ previewElement.innerHTML = "";
element.setAttribute("data-loading", "true");
element.setAttribute("data-page", currentPage.toString());
pageNumElement.textContent = currentPage.toString();
@@ -174,9 +182,16 @@ export const openDocHistory = (options: {
<div class="fn__flex-1"></div>
</div>
</div>
- <ul class="b3-list b3-list--background fn__flex-1" style="padding: 8px 0">
- <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
- </ul>
+ <div class="fn__flex fn__flex-1 history__panel">
+ <ul class="b3-list b3-list--background history__side" ${isMobile() ? "" : `style="width: ${window.siyuan.storage[Constants.LOCAL_HISTORY].sideDocWidth}"`}>
+ <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
+ </ul>
+ <div class="history__resize"></div>
+ <div class="fn__flex-1 fn__flex-column">
+ <div class="protyle-title__input fn__none ft__center ft__breakword"></div>
+ <div class="fn__flex-1 history__text fn__none" style="padding: 0" data-type="repoPanel"></div>
+ </div>
+ </div>
</div>
</div>
</div>`;
@@ -188,6 +203,8 @@ export const openDocHistory = (options: {
containerClassName: "b3-dialog__container--theme",
destroyCallback() {
historyEditor = undefined;
+ repoHistoryEditors.get(repoElement)?.destroy();
+ repoHistoryEditors.delete(repoElement);
}
});
dialog.element.setAttribute("data-key", Constants.DIALOG_HISTORYDOC);
@@ -200,6 +217,8 @@ export const openDocHistory = (options: {
});
const docElement = fileElement.querySelector('.history__text[data-type="docPanel"]') as HTMLElement;
const mdElement = fileElement.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement;
+ const repoPreviewElement = repoElement.querySelector('[data-type="repoPanel"]') as HTMLElement;
+ const repoTitleElement = repoElement.querySelector(".protyle-title__input") as HTMLElement;
renderDoc(fileElement, 1, options.id);
historyEditor = new Protyle(options.app, docElement, {
blockId: "",
@@ -218,6 +237,18 @@ export const openDocHistory = (options: {
disabledProtyle(historyEditor.protyle);
const pageNumElement = fileElement.querySelector('[data-type="jumpRepoPage"]');
const titleElement = fileElement.querySelector(".protyle-title__input");
+ const previewRepoFile = (element: Element) => {
+ repoHistoryEditors.get(repoElement)?.destroy();
+ repoHistoryEditors.delete(repoElement);
+ repoTitleElement.textContent = element.querySelector(".b3-list-item__text").textContent.trim();
+ repoTitleElement.classList.remove("fn__none");
+ repoPreviewElement.classList.remove("fn__none");
+ renderRepoFile(options.app, element, repoPreviewElement, (editor) => {
+ repoHistoryEditors.set(repoElement, editor);
+ });
+ element.parentElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus");
+ element.classList.add("b3-list-item--focus");
+ };
dialog.element.addEventListener("click", (event) => {
let target = event.target as HTMLElement;
while (target && !target.isEqualNode(dialog.element)) {
@@ -253,7 +284,7 @@ export const openDocHistory = (options: {
event.preventDefault();
break;
} else if (type === "view" && repoFileElement) {
- openRepoFile(options.app, repoFileElement);
+ previewRepoFile(repoFileElement);
event.stopPropagation();
event.preventDefault();
break;
@@ -272,6 +303,12 @@ export const openDocHistory = (options: {
event.stopPropagation();
event.preventDefault();
break;
+ } else if (target.classList.contains("b3-list-item") &&
+ target.getAttribute("data-type") === "searchFileItem") {
+ previewRepoFile(target);
+ event.stopPropagation();
+ event.preventDefault();
+ break;
} else if (target.classList.contains("b3-list-item") &&
target.getAttribute("data-type") !== "searchFileItem" && !isLoading) {
getHistoryPath(target, opElement.value, options.id, (item) => {
@@ -345,7 +382,8 @@ export const openDocHistory = (options: {
target = target.parentElement;
}
});
- resizeSide(dialog.element.querySelector(".history__resize"), dialog.element.querySelector(".history__side"), "sideDocWidth");
+ resizeSide(fileElement.querySelector(".history__resize"), fileElement.querySelector(".history__side"), "sideDocWidth");
+ resizeSide(repoElement.querySelector(".history__resize"), repoElement.querySelector(".history__side"), "sideDocWidth");
};
const getHistoryPath = (target: Element, op: string, id: string, cb: (item: any) => void) => {
diff --git a/app/src/history/history.ts b/app/src/history/history.ts
index 57b2edad1a..7418bb1e24 100644
--- a/app/src/history/history.ts
+++ b/app/src/history/history.ts
@@ -16,9 +16,10 @@ import {closeModel} from "../mobile/util/closePanel";
import type {App} from "../index";
import {resizeSide} from "./resizeSide";
import {isSupportCSSHL, searchMarkRender} from "../protyle/render/searchMarkRender";
-import {openRepoFile, renderRepoFileList, rollbackRepoFile, saveRepoFile} from "./repoFile";
+import {renderRepoFile, renderRepoFileList, rollbackRepoFile, saveRepoFile} from "./repoFile";
let historyEditor: Protyle;
+const repoHistoryEditors = new WeakMap<Element, Protyle>();
const renderDoc = (element: HTMLElement, currentPage: number) => {
const previousElement = element.querySelector('[data-type="docprevious"]');
@@ -265,19 +266,54 @@ ${actionHTML}
</li>`;
/// #endif
});
- element.lastElementChild.innerHTML = `${repoHTML}`;
+ element.querySelector('[data-type="repoList"]').innerHTML = `${repoHTML}`;
+};
+
+const clearRepoPreview = (element: Element) => {
+ const previewElement = element.querySelector('[data-type="repoPreviewPanel"]');
+ repoHistoryEditors.get(element)?.destroy();
+ repoHistoryEditors.delete(element);
+ element.querySelector('[data-type="repoPreview"] .protyle-title__input').classList.add("fn__none");
+ previewElement.classList.add("fn__none");
+ previewElement.removeAttribute("data-request-id");
+ previewElement.innerHTML = "";
+};
+
+const setRepoSearchLayout = (element: Element, enabled: boolean) => {
+ const listElement = element.querySelector('[data-type="repoList"]') as HTMLElement;
+ const resizeElement = element.querySelector(".history__resize");
+ const previewElement = element.querySelector('[data-type="repoPreview"]');
+ clearRepoPreview(element);
+ if (enabled) {
+ listElement.classList.remove("fn__flex-1");
+ listElement.classList.add("history__side");
+ if (!isMobile()) {
+ listElement.style.width = window.siyuan.storage[Constants.LOCAL_HISTORY].sideWidth;
+ }
+ resizeElement.classList.remove("fn__none");
+ previewElement.classList.remove("fn__none");
+ } else {
+ listElement.classList.add("fn__flex-1");
+ listElement.classList.remove("history__side");
+ listElement.style.width = "";
+ resizeElement.classList.add("fn__none");
+ previewElement.classList.add("fn__none");
+ }
};
const renderRepoSearchResult = (response: IWebSocketData, element: Element) => {
- renderRepoFileList(response.data.files, element.lastElementChild, true);
+ renderRepoFileList(response.data.files, element.querySelector('[data-type="repoList"]'), true);
};
const renderRepo = (element: Element, currentPage: number) => {
const selectElement = element.querySelector(".b3-select") as HTMLSelectElement;
const selectValue = selectElement.value;
+ const searchInputElement = element.querySelector("input") as HTMLInputElement;
+ const keyword = searchInputElement.value.trim();
selectElement.disabled = true;
- element.lastElementChild.innerHTML = '<li style="position: relative;height: 100%;"><div class="fn__loading"><img width="64px" src="/stage/loading-pure.svg"></div></li>';
+ setRepoSearchLayout(element, Boolean(keyword && selectValue === "getRepoSnapshots"));
+ element.querySelector('[data-type="repoList"]').innerHTML = '<li style="position: relative;height: 100%;"><div class="fn__loading"><img width="64px" src="/stage/loading-pure.svg"></div></li>';
const pageBtn = element.querySelector('button[data-type="jumpRepoPage"]');
pageBtn.textContent = `${currentPage}`;
@@ -286,13 +322,11 @@ const renderRepo = (element: Element, currentPage: number) => {
const pageElement = nextElement.nextElementSibling.nextElementSibling;
element.setAttribute("data-init", "true");
- const searchInputElement = element.querySelector("input") as HTMLInputElement;
if (selectValue === "getRepoSnapshots") {
searchInputElement.parentElement.classList.remove("fn__none");
} else {
searchInputElement.parentElement.classList.add("fn__none");
}
- const keyword = searchInputElement.value.trim();
if (keyword && selectValue === "getRepoSnapshots") {
const searchBtnElement = searchInputElement.nextElementSibling as HTMLButtonElement;
searchBtnElement.disabled = true;
@@ -503,9 +537,16 @@ export const openHistory = (app: App, tab: "doc" | "notebook" | "repo" = "doc")
</button>
</div>
</div>
- <ul class="b3-list b3-list--background fn__flex-1" style="padding: 8px 0">
- <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
- </ul>
+ <div class="fn__flex fn__flex-1 history__panel">
+ <ul data-type="repoList" class="b3-list b3-list--background fn__flex-1" style="padding: 8px 0">
+ <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
+ </ul>
+ <div class="history__resize fn__none"></div>
+ <div data-type="repoPreview" class="fn__flex-1 fn__flex-column fn__none">
+ <div class="protyle-title__input fn__none ft__center ft__breakword"></div>
+ <div class="fn__flex-1 history__text fn__none" style="padding: 0" data-type="repoPreviewPanel"></div>
+ </div>
+ </div>
</div>
</div>
</div>`;
@@ -531,6 +572,9 @@ export const openHistory = (app: App, tab: "doc" | "notebook" | "repo" = "doc")
containerClassName: "b3-dialog__container--theme",
destroyCallback() {
historyEditor = undefined;
+ const repoElement = dialog.element.querySelector('#historyContainer [data-type="repo"]');
+ repoHistoryEditors.get(repoElement)?.destroy();
+ repoHistoryEditors.delete(repoElement);
}
});
dialog.element.setAttribute("data-key", Constants.DIALOG_HISTORY);
@@ -540,7 +584,10 @@ export const openHistory = (app: App, tab: "doc" | "notebook" | "repo" = "doc")
} else {
dialog.element.querySelector("input").focus();
}
- resizeSide(dialog.element.querySelector(".history__resize"), dialog.element.querySelector(".history__side"), "sideWidth");
+ const docPanelElement = dialog.element.querySelector('#historyContainer [data-type="doc"]');
+ const repoPanelElement = dialog.element.querySelector('#historyContainer [data-type="repo"]');
+ resizeSide(docPanelElement.querySelector(".history__resize"), docPanelElement.querySelector(".history__side"), "sideWidth");
+ resizeSide(repoPanelElement.querySelector(".history__resize"), repoPanelElement.querySelector('[data-type="repoList"]'), "sideWidth");
}
};
@@ -581,6 +628,20 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
disabledProtyle(historyEditor.protyle);
const repoElement = element.querySelector('#historyContainer [data-type="repo"]');
const historyElement = element.querySelector('#historyContainer [data-type="doc"]');
+ const previewRepoFile = (itemElement: Element) => {
+ const previewElement = repoElement.querySelector('[data-type="repoPreviewPanel"]');
+ const previewTitleElement = repoElement.querySelector('[data-type="repoPreview"] .protyle-title__input');
+ repoHistoryEditors.get(repoElement)?.destroy();
+ repoHistoryEditors.delete(repoElement);
+ previewTitleElement.textContent = itemElement.querySelector(".b3-list-item__text").textContent.trim();
+ previewTitleElement.classList.remove("fn__none");
+ previewElement.classList.remove("fn__none");
+ renderRepoFile(app, itemElement, previewElement, (editor) => {
+ repoHistoryEditors.set(repoElement, editor);
+ });
+ itemElement.parentElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus");
+ itemElement.classList.add("b3-list-item--focus");
+ };
const repoSelectElement = repoElement.querySelector(".b3-select") as HTMLSelectElement;
const searchFileElement = repoElement.querySelector(".b3-text-field") as HTMLInputElement;
repoSelectElement.addEventListener("change", () => {
@@ -679,8 +740,9 @@ const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
event.stopPropagation();
event.preventDefault();
break;
- } else if (type === "view") {
- openRepoFile(app, target.closest(".b3-list-item"));
+ } else if (target.classList.contains("b3-list-item") &&
+ target.getAttribute("data-type") === "searchFileItem") {
+ previewRepoFile(target);
event.stopPropagation();
event.preventDefault();
break;
diff --git a/app/src/history/repoFile.ts b/app/src/history/repoFile.ts
index aa2c8e0168..68e09bc9d9 100644
--- a/app/src/history/repoFile.ts
+++ b/app/src/history/repoFile.ts
@@ -1,6 +1,5 @@
import {renderAssetsPreview} from "../asset/renderAssets";
import {Constants} from "../constants";
-import {Dialog} from "../dialog";
import {confirmDialog} from "../dialog/confirmDialog";
import type {App} from "../index";
import {Protyle} from "../protyle";
@@ -8,7 +7,6 @@ import {saveExportFile} from "../protyle/util/compatibility";
import {disabledProtyle, onGet} from "../protyle/util/onGet";
import {escapeHtml} from "../util/escape";
import {fetchPost} from "../util/fetch";
-import {isMobile} from "../util/functions";
import {pathPosix} from "../util/pathName";
import * as dayjs from "dayjs";
@@ -21,6 +19,8 @@ interface IRepoFile {
updated: number;
}
+let repoFileRequestId = 0;
+
export const renderRepoFileList = (files: IRepoFile[], element: Element, showPath: boolean) => {
if (files.length === 0) {
element.innerHTML = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
@@ -41,11 +41,6 @@ export const renderRepoFileList = (files: IRepoFile[], element: Element, showPat
</div>
<div class="fn__flex" style="height: 26px">
<span class="fn__flex-1"></span>
- <span class="b3-list-item__action" data-type="view">
- <svg><use xlink:href="#iconEye"></use></svg>
- <span class="fn__space"></span>${window.siyuan.languages.cardPreview}
- </span>
- <span class="fn__space"></span>
<span class="b3-list-item__action" data-type="saveAs">
<svg><use xlink:href="#iconDownload"></use></svg>
<span class="fn__space"></span>${window.siyuan.languages.saveAs}
@@ -68,9 +63,6 @@ export const renderRepoFileList = (files: IRepoFile[], element: Element, showPat
${dayjs(item.updated).format("YYYY-MM-DD HH:mm:ss")}
</div>
</div>
- <span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="view" aria-label="${window.siyuan.languages.cardPreview}">
- <svg><use xlink:href="#iconEye"></use></svg>
- </span>
<span class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="saveAs" aria-label="${window.siyuan.languages.saveAs}">
<svg><use xlink:href="#iconDownload"></use></svg>
</span>
@@ -107,19 +99,20 @@ export const saveRepoFile = (element: Element) => {
});
};
-export const openRepoFile = (app: App, element: Element) => {
+export const renderRepoFile = (app: App, element: Element, contentElement: Element,
+ onEditor?: (editor: Protyle) => void) => {
+ const fileId = element.getAttribute("data-id");
const snapshotId = element.getAttribute("data-snapshot") || "";
- const dialog = new Dialog({
- title: element.querySelector(".b3-list-item__text").textContent.trim(),
- content: '<div class="b3-dialog__content"><div style="border-radius: var(--b3-border-radius-b);"></div></div>',
- width: isMobile() ? "100vw" : "80vw",
- height: isMobile() ? "100dvh" : "70vh",
- disableAnimation: true,
- });
- const contentElement = dialog.element.querySelector(".b3-dialog__content");
+ const requestId = (++repoFileRequestId).toString();
+ contentElement.setAttribute("data-id", fileId);
+ contentElement.setAttribute("data-request-id", requestId);
+ contentElement.innerHTML = '<div style="border-radius: var(--b3-border-radius-b);"></div>';
fetchPost("/api/repo/openRepoSnapshotFile", {
- id: element.getAttribute("data-id")
+ id: fileId
}, (response) => {
+ if (!contentElement.isConnected || contentElement.getAttribute("data-request-id") !== requestId) {
+ return;
+ }
const type = pathPosix().extname(response.data.content).toLowerCase();
if (Constants.SIYUAN_ASSETS_IMAGE.concat(Constants.SIYUAN_ASSETS_AUDIO).concat(Constants.SIYUAN_ASSETS_VIDEO).includes(type)) {
contentElement.firstElementChild.innerHTML = renderAssetsPreview(response.data.content);
@@ -142,6 +135,7 @@ export const openRepoFile = (app: App, element: Element) => {
typewriterMode: false
});
disabledProtyle(viewEditor.protyle);
+ onEditor?.(viewEditor);
onGet({
data: response,
protyle: viewEditor.protyle,
diff --git a/app/src/protyle/util/destroy.ts b/app/src/protyle/util/destroy.ts
index b3ab4e9303..b45785dfdd 100644
--- a/app/src/protyle/util/destroy.ts
+++ b/app/src/protyle/util/destroy.ts
@@ -24,12 +24,14 @@ export const destroy = (protyle: IProtyle) => {
if (protyle.undo) {
protyle.undo.clear();
}
- try {
- protyle.ws.send("closews", {});
- } catch (e) {
- setTimeout(() => {
+ if (protyle.ws) {
+ try {
protyle.ws.send("closews", {});
- }, 10240);
+ } catch (e) {
+ setTimeout(() => {
+ protyle.ws?.send("closews", {});
+ }, 10240);
+ }
}
protyle.app.plugins.forEach(item => {
item.eventBus.emit("destroy-protyle", {
diff --git a/kernel/go.sum b/kernel/go.sum
index f4a5884973..57b992cb18 100644
--- a/kernel/go.sum
+++ b/kernel/go.sum
@@ -397,7 +397,7 @@ github.com/shurcooL/gofontwoff v0.0.0-20181114050219-180f79e6909d/go.mod h1:05Ut
github.com/simplereach/timeutils v1.2.0/go.mod h1:VVbQDfN/FHRZa1LSqcwo4kNZ62OOyqLLGQKYB3pB0Q8=
github.com/siyuan-note/dataparser v0.0.0-20260115084335-b57cb8bc7c17 h1:cZbH1QcW/LWYtaKBv2i0O96qVm6HXKeEE3Z74VYdUHM=
github.com/siyuan-note/dataparser v0.0.0-20260115084335-b57cb8bc7c17/go.mod h1:zE2SJ4t+2lFAeLgmhi27YtfzdyOWh3l9DXXUr9m/xU0=
-github.com/siyuan-note/dejavu v0.0.0-20260729040332-c78b6cd83f01 h1:3LJ4lKSJ94mPSBNNKpTiupWfMR8IKM/6nEft5pXh1PE=
+github.com/siyuan-note/dejavu v0.0.0-20260729040332-c78b6cd83f01 h1:zzWRvns0QEWpyJlG5DPPLrm+NhDOCaDEVdYdbW3RhCc=
github.com/siyuan-note/dejavu v0.0.0-20260729040332-c78b6cd83f01/go.mod h1:7yPSZX7EvgUx0Zh6FXlsXtFt3IFUq0WVMxyKRNd9STI=
github.com/siyuan-note/encryption v0.0.0-20260715062728-9cb8e9548044 h1:b1/Q4DSREDvGh1/NbzR9873EKrjakdoUtsNlghWxUOI=
github.com/siyuan-note/encryption v0.0.0-20260715062728-9cb8e9548044/go.mod h1:t6jXpJdrXzRsdQF+XGaw35f16JVZcDyIkk5/6//ywnY=