feat(编辑能力): 接入版本管理并完善编辑隔离
build.yaml / build (push) Successful in 48s

This commit is contained in:
xiaoqidun committed 2026-10-10 21:29:51 +08:00
1 parent 5bc92dddd1
commit 89bbbba46e
4 files changed
+240 -4

No files matched your search

+59 -1
View File
@@ -186,6 +186,8 @@ func RunWASM() {
registerAsyncCallback("ofdgoDetectFormat", detectDocumentFormat)
registerCallback("ofdgoOpen", openDocument)
registerCallback("ofdgoSelectDocument", selectDocument)
registerCallback("ofdgoDocumentVersions", documentVersions)
registerCallback("ofdgoChangeVersion", changeDocumentVersion)
registerCallback("ofdgoChangeDocument", changePackageDocument)
registerCallback("ofdgoPackageDocuments", packageDocuments)
registerAsyncCallback("ofdgoConvertPDF", convertPDFDocument)
@@ -3443,6 +3445,60 @@ func packageDocuments(args []js.Value) (any, error) {
return entries, nil
}
// documentVersions 读取当前编辑文档的版本列表,不切换版本或解析页面
// 入参: args 保留参数
// 返回: any 版本列表, error 读取错误
func documentVersions(args []js.Value) (any, error) {
if currentEditor == nil {
return nil, fmt.Errorf("no document is being edited")
}
versions, err := currentEditor.DocumentVersions()
if err != nil {
return nil, err
}
entries := make([]VersionEntry, 1, len(versions)+1)
entries[0].Default = true
for _, version := range versions {
entries = append(entries, VersionEntry{ID: version.ID, Index: version.Index, Name: version.Name, Default: version.Current})
if version.Current {
entries[0].Default = false
}
}
return entries, nil
}
// changeDocumentVersion 调用库层版本操作,沿用编辑历史和预览更新流程
// 入参: args 操作、版本标识及可选名称
// 返回: any 文档信息, error 操作错误
func changeDocumentVersion(args []js.Value) (any, error) {
if len(args) != 3 {
return nil, fmt.Errorf("invalid version arguments")
}
return changeAtomicObjects(func() error {
id, name := args[1].String(), args[2].String()
switch args[0].String() {
case "add":
return currentEditor.Transaction(func(editor *ofdgo.Editor) error {
if err := editor.SelectVersion(id); err != nil {
return err
}
_, err := editor.AddVersion(name)
return err
})
case "rename":
return currentEditor.RenameVersion(id, name)
case "delete":
return currentEditor.DeleteVersion(id)
case "default":
return currentEditor.SetDefaultVersion(id)
case "select":
return currentEditor.SelectVersion(id)
default:
return fmt.Errorf("invalid version operation")
}
})
}
// changeAttachment 编辑附件,逐个读取文件并复用库层事务和历史
// 入参: args 操作、附件标识、名称列表、文件读取回调
// 返回: any 编辑状态, error 错误信息
@@ -3649,7 +3705,9 @@ func previewEditor(editor *ofdgo.Editor, annotations bool) (editorInfo, error) {
if currentSession != nil {
previous := currentSession.Reader
oldBody, newBody := previous.OFD.DocBody[previous.DocumentIndex()], reader.OFD.DocBody[reader.DocumentIndex()]
if oldBody.DocRoot != newBody.DocRoot || oldBody.DocInfo.DocID != newBody.DocInfo.DocID {
oldVersion, _ := previous.DocumentVersionID()
newVersion, _ := reader.DocumentVersionID()
if oldBody.DocRoot != newBody.DocRoot || oldBody.DocInfo.DocID != newBody.DocInfo.DocID || oldVersion != newVersion {
copiedObjects, copiedStyle = nil, nil
clearImport()
}
+10
View File
@@ -73,6 +73,7 @@ type pageBoxInfo struct {
// DocumentInfo 文档信息
type DocumentInfo struct {
DocumentIndex int `json:"documentIndex"`
DocumentVersion string `json:"documentVersion"`
Documents []DocumentEntry `json:"documents,omitempty"`
Encryption EncryptionInfo `json:"encryption"`
Version string `json:"version"`
@@ -98,6 +99,14 @@ type DocumentInfo struct {
DetailsPending bool `json:"detailsPending,omitempty"`
}
// VersionEntry 版本管理列表中的入口,空ID表示主文档
type VersionEntry struct {
ID string `json:"id"`
Index int32 `json:"index"`
Name string `json:"name"`
Default bool `json:"default"`
}
// DocumentEntry 包内文档入口,管理列表按需补充元数据和页数
type DocumentEntry struct {
Index int `json:"index"`
@@ -441,6 +450,7 @@ func (s *Session) Summary() DocumentInfo {
Outlines: s.doc.OutlineInfos(),
DetailsPending: true,
}
info.DocumentVersion, _ = s.Reader.DocumentVersionID()
for index, body := range s.Reader.OFD.DocBody {
info.Documents = append(info.Documents, DocumentEntry{Index: index, Title: body.DocInfo.Title, Root: body.DocRoot, ID: body.DocInfo.DocID})
}