mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
1 parent
6a4b322e2c
commit
151ddb0a20
21 files changed
+819
-195
No files matched your search
@@ -419,6 +419,7 @@ type outputContext struct {
|
||||
type businessEngine struct {
|
||||
currentSession *Session
|
||||
currentEditor *ofdgo.Editor
|
||||
saveSnapshot *ofdgo.Editor
|
||||
pendingImport *ofdgo.Reader
|
||||
copiedObjects *editorClipboard
|
||||
copiedStyle *ofdgo.GraphicObject
|
||||
@@ -7162,6 +7163,54 @@ func (e *businessEngine) saveSigned(args []platformValue) (any, error) {
|
||||
return successResult(map[string]any{"mime": "application/ofd", "label": "OFD"}), nil
|
||||
}
|
||||
|
||||
// prepareSave 创建独立保存快照,后续写出不占用当前编辑文档
|
||||
// 入参: args 取消检查回调
|
||||
// 返回: any 快照修订, error 准备错误
|
||||
func (e *businessEngine) prepareSave(args []platformValue) (any, error) {
|
||||
e.saveSnapshot = nil
|
||||
editor := e.currentEditor
|
||||
if editor == nil {
|
||||
if e.currentSession == nil {
|
||||
return nil, fmt.Errorf("ofd document is not opened")
|
||||
}
|
||||
var err error
|
||||
editor, err = e.currentSession.Reader.Editor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ctx := e.operationContext()
|
||||
if len(args) > 0 {
|
||||
ctx = e.newOutputContext(args[0])
|
||||
}
|
||||
snapshot, err := editor.Snapshot(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.saveSnapshot = snapshot
|
||||
return successResult(map[string]any{"revision": editor.Revision()}), nil
|
||||
}
|
||||
|
||||
// savePrepared 写出并释放已准备的快照,不修改当前编辑状态
|
||||
// 入参: args 写出回调、进度回调及输出配置
|
||||
// 返回: any 保存结果, error 保存错误
|
||||
func (e *businessEngine) savePrepared(args []platformValue) (any, error) {
|
||||
snapshot := e.saveSnapshot
|
||||
e.saveSnapshot = nil
|
||||
if snapshot == nil {
|
||||
return nil, fmt.Errorf("save snapshot is not prepared")
|
||||
}
|
||||
return e.saveEditor(snapshot, args)
|
||||
}
|
||||
|
||||
// discardSave 释放尚未写出的保存快照
|
||||
// 入参: args 空参数
|
||||
// 返回: any 操作结果, error 错误信息
|
||||
func (e *businessEngine) discardSave(args []platformValue) (any, error) {
|
||||
e.saveSnapshot = nil
|
||||
return successResult(nil), nil
|
||||
}
|
||||
|
||||
// saveDocument 分块写出当前编辑文档或指定页面
|
||||
// 入参: args 数据写出回调、可选准备进度回调和可选页面索引
|
||||
// 返回: any 保存结果, error 错误信息
|
||||
@@ -7403,6 +7452,9 @@ func (e *businessEngine) businessCommands() map[string]func([]platformValue) (an
|
||||
"ofdgoUndo": func([]platformValue) (any, error) { return e.restoreEditor(false) },
|
||||
"ofdgoRedo": func([]platformValue) (any, error) { return e.restoreEditor(true) },
|
||||
"ofdgoSaveDocument": e.saveDocument,
|
||||
"ofdgoPrepareSave": e.prepareSave,
|
||||
"ofdgoSaveSnapshot": e.savePrepared,
|
||||
"ofdgoDiscardSave": e.discardSave,
|
||||
"ofdgoSetEncryption": e.setEncryption,
|
||||
"ofdgoSaveSigned": e.saveSigned,
|
||||
"ofdgoPreviewSignatureSeal": e.previewSignatureSeal,
|
||||
|
||||
@@ -82,6 +82,7 @@ type NativeEngine struct {
|
||||
CleanupOutput func(context.Context, NativeOutputTarget) error
|
||||
gate chan struct{}
|
||||
once sync.Once
|
||||
saves sync.WaitGroup
|
||||
closed bool
|
||||
}
|
||||
|
||||
@@ -149,13 +150,33 @@ func (e *NativeEngine) Call(ctx context.Context, name string, args []any, emit f
|
||||
case <-ctx.Done():
|
||||
return nativeFailure(ctx.Err())
|
||||
}
|
||||
defer func() { <-e.gate }()
|
||||
locked := true
|
||||
defer func() {
|
||||
if locked {
|
||||
<-e.gate
|
||||
}
|
||||
}()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nativeFailure(err)
|
||||
}
|
||||
if e.closed {
|
||||
return nativeFailure(fmt.Errorf("native engine is closed"))
|
||||
}
|
||||
if name == "ofdgoSaveSnapshot" {
|
||||
snapshot := e.saveSnapshot
|
||||
e.saveSnapshot = nil
|
||||
if snapshot == nil {
|
||||
return nativeFailure(fmt.Errorf("save snapshot is not prepared"))
|
||||
}
|
||||
output := &NativeEngine{OpenWriter: e.OpenWriter}
|
||||
output.currentEditor = snapshot
|
||||
e.saves.Add(1)
|
||||
defer e.saves.Done()
|
||||
<-e.gate
|
||||
locked = false
|
||||
defer output.Close()
|
||||
return output.Call(ctx, "ofdgoSaveDocument", args, emit)
|
||||
}
|
||||
e.ctx = ctx
|
||||
if e.currentSession != nil {
|
||||
e.currentSession.Renderer.OutputContext = ctx
|
||||
@@ -219,6 +240,8 @@ func (e *NativeEngine) Close() error {
|
||||
e.gate <- struct{}{}
|
||||
defer func() { <-e.gate }()
|
||||
e.closed = true
|
||||
e.saveSnapshot = nil
|
||||
e.saves.Wait()
|
||||
var err error
|
||||
if e.currentSession != nil {
|
||||
err = e.currentSession.Close()
|
||||
@@ -728,7 +751,7 @@ func (c *nativeCall) prepare(name string, args []any) ([]any, error) {
|
||||
args = append(args, c.fileCallback(files))
|
||||
}
|
||||
switch name {
|
||||
case "ofdgoResources":
|
||||
case "ofdgoResources", "ofdgoPrepareSave":
|
||||
return append(args, checkpoint), nil
|
||||
case "ofdgoImportPages", "ofdgoImportDocuments", "ofdgoImportImages":
|
||||
return append(args, c.progress("import", "", true)), nil
|
||||
|
||||
@@ -56,6 +56,8 @@ func RunWASM() {
|
||||
"ofdgoImportPages": true,
|
||||
"ofdgoImportDocuments": true,
|
||||
"ofdgoSaveDocument": true,
|
||||
"ofdgoPrepareSave": true,
|
||||
"ofdgoSaveSnapshot": true,
|
||||
"ofdgoSetEncryption": true,
|
||||
"ofdgoSaveSigned": true,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user