feat(版本管理): 支持阅读切换文档版本
build.yaml / build (push) Successful in 46s

This commit is contained in:
xiaoqidun committed 2026-10-10 22:01:22 +08:00
1 parent b20f411f0d
commit e6a8786f0c
6 files changed
+151 -17

No files matched your search

+2 -2
View File
@@ -353,8 +353,8 @@
<h2 id="versionTitle" class="form-title">版本管理</h2>
<div id="versionNotice" class="font-permission-hint" role="status" hidden></div>
<div id="versionList" class="document-list" role="listbox" aria-label="版本列表"></div>
<label class="form-row"><span>名称</span><input id="versionName" type="text" autocomplete="off" aria-label="版本名称"></label>
<div class="document-actions" role="group" aria-label="版本操作">
<label id="versionNameRow" class="form-row"><span>名称</span><input id="versionName" type="text" autocomplete="off" aria-label="版本名称"></label>
<div id="versionActions" class="document-actions" role="group" aria-label="版本操作">
<button id="versionAdd" class="button" type="button" title="新增版本" aria-label="新增版本">新增</button>
<button id="versionRename" class="button" type="button" title="修改名称" aria-label="修改名称">改名</button>
<button id="versionDefault" class="button" type="button" title="默认版本" aria-label="默认版本">默认</button>
+5
View File
@@ -1562,6 +1562,10 @@ body[data-hide-meta] .meta-panel {
padding-inline: 6px;
}
.document-actions.version-readonly {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.document-list {
max-height: 45dvh;
min-height: 80px;
@@ -2015,6 +2019,7 @@ button.outline-link:hover {
.form-row[hidden],
.form-pair[hidden],
.form-actions > [hidden],
.document-actions > [hidden],
.annotation-detail[hidden],
.tool-group[hidden],
.page-list[hidden],
+71 -11
View File
@@ -259,6 +259,9 @@ const el = {
documentUpButton: document.querySelector("#documentUpButton"),
documentDownButton: document.querySelector("#documentDownButton"),
versionManageButton: document.querySelector("#versionManageButton"),
versionTitle: document.querySelector("#versionTitle"),
versionNameRow: document.querySelector("#versionNameRow"),
versionActions: document.querySelector("#versionActions"),
versionPanel: document.querySelector("#versionPanel"),
versionForm: document.querySelector("#versionForm"),
versionNotice: document.querySelector("#versionNotice"),
@@ -2078,7 +2081,7 @@ function setEditorInfo(doc) {
}
async function openVersionManagement() {
if (!state.editing || !state.ready || state.exporting || document.body.hasAttribute("aria-busy")) return;
if (!state.doc || !state.ready || state.exporting || document.body.hasAttribute("aria-busy")) return;
state.versionManagement = { id: state.doc.documentVersion || "", entries: null, loading: false, failed: false };
el.versionList.replaceChildren();
el.versionName.value = "";
@@ -2148,9 +2151,16 @@ function selectManagedVersion(id, focus = false) {
async function changeVersion(action) {
const manager = state.versionManagement;
if (!state.editing || !manager?.entries || manager.loading || manager.failed || document.body.hasAttribute("aria-busy")) return;
if (!state.ready || state.exporting || !state.editing && action !== "select" || !manager?.entries || manager.loading || manager.failed || document.body.hasAttribute("aria-busy")) return;
const entry = manager.entries.find(entry => entry.id === manager.id);
if (!entry) return;
if (!state.editing) {
if (await switchReadingVersion(entry.id) && state.versionManagement === manager && el.versionPanel.open) {
manager.id = state.doc.documentVersion || "";
await refreshVersionManagement(true);
}
return;
}
if (action === "delete" && (!entry.id || !window.confirm(`删除版本“${entry.name || entry.index}”?`))) return;
el.versionStatus.textContent = "正在处理版本";
if (await changeDocument("ofdgoChangeVersion", null, action, entry.id, el.versionName.value.trim())) {
@@ -2160,20 +2170,63 @@ async function changeVersion(action) {
}
}
async function switchReadingVersion(id) {
if (!state.doc || state.editing || !state.ready || state.exporting || document.body.hasAttribute("aria-busy")) return;
if (id === (state.doc.documentVersion || "")) return true;
const dirty = state.dirty;
const before = state.editorInfo ? editorLocation() : null, revision = state.editorInfo?.revision;
const openSeq = ++state.openSeq;
setBusy(true, "正在切换版本");
try {
const doc = await callWASM("ofdgoSelectVersion", id);
if (openSeq !== state.openSeq) return;
state.objectClipboard = state.styleClipboard = null;
resetCompositeScope();
canvasEditor.clear();
canvasEditor.setTool("");
delete state.textDefaults.fontChoice;
updateTextFonts(null, true);
if (state.editorInfo) {
if (!dirty) state.savedRevision = doc.revision;
setEditorInfo(doc);
}
await openDocument({ doc, openSeq, skipAutoFonts: true, resetScroll: true, clearSelection: true,
fitMode: state.fitMode, scale: state.scale, activateActions: true });
if (openSeq !== state.openSeq) return;
if (before) {
rememberEditorView(before, revision);
const view = state.editorViews.get(doc.revision);
if (view) view.reading = true;
}
return true;
} catch (error) {
if (openSeq === state.openSeq) el.versionStatus.textContent = error.message;
} finally {
if (openSeq === state.openSeq) setBusy(false);
}
}
function updateVersionControls() {
const manager = state.versionManagement, busy = document.body.hasAttribute("aria-busy");
const disabled = !state.editing || !state.ready || state.exporting || busy;
el.versionManageButton.hidden = !state.editing;
const disabled = !state.doc || !state.ready || state.exporting || busy;
const title = state.editing ? "版本管理" : "版本浏览";
el.versionManageButton.hidden = !state.editing && !(state.doc?.versionCount > 0);
el.versionManageButton.title = title;
el.versionManageButton.setAttribute("aria-label", title);
el.versionTitle.textContent = title;
el.versionManageButton.disabled = disabled;
el.versionNotice.textContent = state.editorInfo?.editWarnings?.join(";") || "";
el.versionNotice.textContent = state.editing ? state.editorInfo?.editWarnings?.join(";") || "" : "";
el.versionNotice.hidden = !el.versionNotice.textContent;
el.versionNameRow.hidden = !state.editing;
el.versionActions.classList.toggle("version-readonly", !state.editing);
for (const button of [el.versionAdd, el.versionRename, el.versionDelete, el.versionDefault]) button.hidden = !state.editing;
const entry = manager?.entries?.find(entry => entry.id === manager.id);
const unavailable = disabled || !entry || Boolean(manager?.loading || manager?.failed);
el.versionList.inert = busy || Boolean(manager?.loading);
el.versionName.disabled = el.versionAdd.disabled = unavailable;
el.versionRename.disabled = unavailable || !entry?.id || el.versionName.value.trim() === entry.name;
el.versionDelete.disabled = unavailable || !entry?.id;
el.versionDefault.disabled = unavailable || Boolean(entry?.default);
el.versionName.disabled = el.versionAdd.disabled = unavailable || !state.editing;
el.versionRename.disabled = unavailable || !state.editing || !entry?.id || el.versionName.value.trim() === entry.name;
el.versionDelete.disabled = unavailable || !state.editing || !entry?.id;
el.versionDefault.disabled = unavailable || !state.editing || Boolean(entry?.default);
el.versionSelect.disabled = unavailable || entry?.id === (state.doc?.documentVersion || "");
}
@@ -3235,6 +3288,8 @@ async function changeDocument(name, item, ...args) {
if (doc.revision === state.editorInfo?.revision) {
return true;
}
const restoredView = name === "ofdgoUndo" ? state.editorViews.get(revision) : name === "ofdgoRedo" ? state.editorViews.get(doc.revision) : null;
if (restoredView?.reading && state.savedRevision === revision) state.savedRevision = doc.revision;
refreshManagement = el.documentPanel.open && (name === "ofdgoChangeDocument" || name === "ofdgoUpdateInfo" || restoring);
const previousDocument = state.doc.documents?.[state.doc.documentIndex || 0];
const nextDocument = doc.documents?.[doc.documentIndex || 0];
@@ -3272,7 +3327,7 @@ async function changeDocument(name, item, ...args) {
}
return;
}
const view = name === "ofdgoUndo" ? state.editorViews.get(revision) : name === "ofdgoRedo" ? state.editorViews.get(doc.revision) : null;
const view = restoredView;
const reindex = !!scope && (scoped && ["ofdgoDeleteObject", "ofdgoDeleteObjects", "ofdgoCopyObjects", "ofdgoOrderObjects", "ofdgoEraseObjects", "ofdgoEraseObjectsPath", "ofdgoGroupObjects"].includes(name)
|| inserting || name === "ofdgoPasteObjects" && !!args[4]);
const reindexed = restoring ? view?.reindex && view.before : reindex && before;
@@ -3953,6 +4008,7 @@ async function recoverWASM() {
try {
await openDocument({
pageIndex: state.pageIndex,
versionID: state.doc?.documentVersion,
fitMode: state.fitMode || "width",
scale: state.scale,
skipAutoFonts: true,
@@ -4676,12 +4732,16 @@ async function openDocument(options = {}) {
if (openSeq !== state.openSeq) {
return;
}
const doc = options.doc || (options.reuseSession
let doc = options.doc || (options.reuseSession
? await callWASM("ofdgoConfigure", fonts, state.renderAnnotations)
: await openWithCredentials(fonts, openSeq));
if (openSeq !== state.openSeq) {
return;
}
if (options.versionID !== undefined && options.versionID !== (doc.documentVersion || "")) {
doc = await callWASM("ofdgoSelectVersion", options.versionID);
if (openSeq !== state.openSeq) return;
}
const pageCount = doc.pageCount || 0;
const pageIndex = Math.min(Math.max(options.pageIndex || 0, 0), Math.max(pageCount - 1, 0));
state.doc = doc;