feat(编辑能力): 支持文档增删改及排序
build.yaml / build (push) Successful in 43s

This commit is contained in:
xiaoqidun committed 2026-10-03 14:39:50 +08:00
1 parent d37f41cd90
commit 0385e8c5fd
7 files changed
+625 -28

No files matched your search

+49 -1
View File
@@ -185,6 +185,7 @@ func (r *browserReaderAt) ReadAt(data []byte, offset int64) (int, error) {
func RunWASM() {
registerCallback("ofdgoOpen", openDocument)
registerCallback("ofdgoSelectDocument", selectDocument)
registerCallback("ofdgoChangeDocument", changePackageDocument)
registerAsyncCallback("ofdgoConvertPDF", convertPDFDocument)
registerAsyncCallback("ofdgoConvertFile", convertFile)
registerAsyncCallback("ofdgoPackFiles", packFiles)
@@ -1153,7 +1154,7 @@ func editorSummary() editorInfo {
if !currentSession.doc.Permissions.Edit {
info.EditWarnings = append(info.EditWarnings, "原文件声明不允许编辑")
}
if currentSession.doc.Signatures != "" {
if slices.ContainsFunc(currentSession.Reader.OFD.DocBody, func(body ofdgo.DocBody) bool { return body.Signatures != "" }) {
info.EditWarnings = append(info.EditWarnings, "修改文档可能使原签名失效")
}
info.PageCapabilities = make([]uint, currentEditor.PageCount())
@@ -3360,6 +3361,47 @@ func changeAttachment(args []js.Value) (any, error) {
})
}
// changePackageDocument 调用库层文档增删、改名或排序,不在浏览器内重写包结构
// 入参: args 操作及标题、页面尺寸、文档索引或移动位置
// 返回: any 文档信息, error 错误信息
func changePackageDocument(args []js.Value) (any, error) {
if currentEditor == nil || len(args) == 0 {
return nil, fmt.Errorf("no document is being edited")
}
return changeAtomicObjects(func() error {
var err error
switch args[0].String() {
case "add":
if len(args) != 5 {
return fmt.Errorf("invalid document arguments")
}
_, err = currentEditor.AddDocument(args[1].String(), args[2].Float(), args[3].Float(), args[4].Int())
case "delete":
if len(args) != 2 {
return fmt.Errorf("invalid document arguments")
}
err = currentEditor.DeleteDocument(args[1].Int())
case "move":
if len(args) != 3 {
return fmt.Errorf("invalid document arguments")
}
err = currentEditor.MoveDocument(args[1].Int(), args[2].Int())
case "rename":
if len(args) != 3 {
return fmt.Errorf("invalid document arguments")
}
err = currentEditor.RenameDocument(args[1].Int(), args[2].String())
default:
return fmt.Errorf("invalid document operation")
}
if err == nil {
copiedObjects, copiedStyle = nil, nil
clearImport()
}
return err
})
}
// selectDocument 切换包内文档,保留编辑快照并清除跨文档剪贴板
// 入参: args 文档索引
// 返回: any 文档信息, error 错误信息
@@ -3457,6 +3499,12 @@ func previewEditor(editor *ofdgo.Editor, annotations bool) (editorInfo, error) {
}
editor.SetFontFS()
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 {
copiedObjects, copiedStyle = nil, nil
clearImport()
}
ofdgo.WithRenderBackends(currentSession.Renderer.Backends())(session.Renderer)
if currentSession.fontFS != nil {
session.fontFS = currentSession.fontFS
+3 -1
View File
@@ -102,6 +102,8 @@ type DocumentInfo struct {
type DocumentEntry struct {
Index int `json:"index"`
Title string `json:"title"`
Root string `json:"root"`
ID string `json:"id"`
}
// DocumentDetails 不含页面数组的文档补充信息
@@ -433,7 +435,7 @@ func (s *Session) Summary() DocumentInfo {
DetailsPending: true,
}
for index, body := range s.Reader.OFD.DocBody {
info.Documents = append(info.Documents, DocumentEntry{Index: index, Title: body.DocInfo.Title})
info.Documents = append(info.Documents, DocumentEntry{Index: index, Title: body.DocInfo.Title, Root: body.DocRoot, ID: body.DocInfo.DocID})
}
if docInfo, err := s.Reader.DocInfo(); err == nil && docInfo != nil {
info.Title = docInfo.Title