feat(附件管理): 完善附件管理功能

This commit is contained in:
xiaoqidun committed 2026-09-30 02:22:56 +08:00
1 parent 418a2fa0a7
commit 38004171cf
10 files changed
+254 -42

No files matched your search

+41 -2
View File
@@ -30,7 +30,6 @@ import (
"io"
"io/fs"
"math"
"path"
"reflect"
"slices"
"strconv"
@@ -212,6 +211,7 @@ func RunWASM() {
registerAsyncCallback("ofdgoImportImages", importImages)
registerCallback("ofdgoEditDocument", editDocument)
registerCallback("ofdgoUpdateInfo", updateInfo)
registerCallback("ofdgoChangeAttachment", changeAttachment)
registerCallback("ofdgoChangePage", changePage)
registerCallback("ofdgoBatchPages", batchPages)
registerCallback("ofdgoChangeOutline", changeOutline)
@@ -801,7 +801,7 @@ func renderPage(args []js.Value) (any, error) {
return nil, err
}
for _, attachment := range attachments {
attachmentNames[attachment.ID] = path.Base(currentSession.Reader.ResPath(attachment.FileLoc))
attachmentNames[attachment.ID] = attachment.FileName()
}
break
}
@@ -3535,6 +3535,45 @@ func updateInfo(args []js.Value) (any, error) {
})
}
// changeAttachment 编辑附件,逐个读取文件并复用库层事务和历史
// 入参: args 操作、附件标识、名称列表、文件读取回调
// 返回: any 编辑状态, error 错误信息
func changeAttachment(args []js.Value) (any, error) {
return changeObjects(func() error {
action, id := args[0].String(), args[1].String()
switch action {
case "rename":
return currentEditor.RenameAttachment(id, args[2].Index(0).String())
case "delete":
return currentEditor.DeleteAttachment(id)
case "add", "replace":
count := args[2].Length()
if count == 0 || action == "replace" && count != 1 {
return fmt.Errorf("请选择附件")
}
for i := range count {
result := args[3].Invoke(i)
if message := result.Get("error"); message.Type() == js.TypeString {
return fmt.Errorf("%s", message.String())
}
data, err := bytesFromJS(result.Get("bytes"))
if err != nil {
return err
}
if action == "replace" {
return currentEditor.ReplaceAttachment(id, data)
}
if _, err := currentEditor.AddAttachment(args[2].Index(i).String(), data); err != nil {
return err
}
}
return nil
default:
return fmt.Errorf("unsupported attachment action %q", action)
}
})
}
// editDocument 将已打开文档接入编辑器,沿用页面、资源及字体配置
// 入参: args 当前页索引,省略时使用首页
// 返回: any 编辑状态, error 错误信息
+5 -4
View File
@@ -18,7 +18,6 @@ import (
"bytes"
"fmt"
"io"
"path"
"reflect"
"slices"
"strings"
@@ -111,13 +110,14 @@ type DocumentDetails struct {
// OutlineInfo 目录节点信息
type OutlineInfo = ofdgo.OutlineInfo
// AttachmentInfo 可见附件信息,Size单位为KB
// AttachmentInfo 附件信息,Size单位为KB
type AttachmentInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Format string `json:"format,omitempty"`
Size *float64 `json:"size,omitempty"`
FileName string `json:"fileName"`
Visible bool `json:"visible"`
}
// PageInfo 页面信息
@@ -419,13 +419,14 @@ func (s *Session) Details() DocumentDetails {
info := DocumentDetails{}
if attachments, err := s.Reader.Attachments(); err == nil {
for _, attachment := range attachments {
if attachment.Visible {
if attachment.Visible || s.editing {
info.Attachments = append(info.Attachments, AttachmentInfo{
ID: attachment.ID,
Name: attachment.Name,
Format: attachment.Format,
Size: attachment.Size,
FileName: path.Base(s.Reader.ResPath(attachment.FileLoc)),
FileName: attachment.FileName(),
Visible: attachment.Visible,
})
}
}