mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 08:46:55 +08:00
1 parent
fb4f03465b
commit
413d41c31d
7 files changed
+261
-73
No files matched your search
+45
-10
@@ -1591,14 +1591,31 @@ func (e *businessEngine) loadPDF(args []platformValue) (any, error) {
|
||||
return map[string]any{"done": true, "doc": e.currentSession.Summary(), "warnings": string(warnings)}, nil
|
||||
}
|
||||
|
||||
// preparePDF 按命令准备页面快照或完整文档,不将未完成内容交给编辑和输出
|
||||
// checkDocument 检查命令所需内容,不隐式转换或产生输出
|
||||
// 入参: name 命令名称
|
||||
// 返回: error 文档尚未完整时返回ErrIncompleteDocument
|
||||
func (e *businessEngine) checkDocument(name string) error {
|
||||
if e.pdf == nil {
|
||||
return nil
|
||||
}
|
||||
switch name {
|
||||
case "ofdgoCheckDocument", "ofdgoPDFContext", "ofdgoOpenPDF", "ofdgoOpen", "ofdgoLoadPDF", "ofdgoConfigure", "ofdgoDocumentInfo", "ofdgoSVGFontData", "ofdgoRenderBackends", "ofdgoOutputFormats", "ofdgoExportFormats", "ofdgoParsePageRange", "ofdgoMatchFontFiles", "ofdgoOutlineLinks", "ofdgoDocumentNavigation", "ofdgoInvoices", "ofdgoRenderPage", "ofdgoSearchPage", "ofdgoPageTextString", "ofdgoOpenLinks", "ofdgoDiscardPrint", "ofdgoDiscardSave", "ofdgoDetectFormat", "ofdgoConvertFile", "ofdgoPackFiles", "ofdgoLoadImport", "ofdgoCreateDocument", "ofdgoLayoutPrint", "ofdgoPrintSide", "ofdgoPrintPDF":
|
||||
return nil
|
||||
default:
|
||||
return ofdgo.ErrIncompleteDocument
|
||||
}
|
||||
}
|
||||
|
||||
// preparePDF 只为页面读取和后台加载绑定转换上下文,不补全其他命令
|
||||
// 入参: name 命令名称, args 命令参数
|
||||
// 返回: error 转换或取消错误
|
||||
func (e *businessEngine) preparePDF(name string, args []platformValue) error {
|
||||
if e.pdf == nil {
|
||||
return nil
|
||||
}
|
||||
if name == "ofdgoPDFContext" || name == "ofdgoOpenPDF" || name == "ofdgoOpen" {
|
||||
switch name {
|
||||
case "ofdgoLoadPDF", "ofdgoRenderPage", "ofdgoSearchPage", "ofdgoPageTextString", "ofdgoOpenLinks":
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
e.pdf.operation, e.pdf.report = e.newEditorOperationContext(e.pdf.progress)
|
||||
@@ -1619,7 +1636,7 @@ func (e *businessEngine) preparePDF(name string, args []platformValue) error {
|
||||
}
|
||||
}
|
||||
switch name {
|
||||
case "ofdgoLoadPDF", "ofdgoConfigure", "ofdgoDocumentInfo", "ofdgoSVGFontData", "ofdgoRenderBackends", "ofdgoOutputFormats", "ofdgoExportFormats", "ofdgoParsePageRange", "ofdgoMatchFontFiles", "ofdgoOutlineLinks", "ofdgoDocumentNavigation", "ofdgoInvoices":
|
||||
case "ofdgoLoadPDF":
|
||||
return nil
|
||||
case "ofdgoOpenLinks":
|
||||
if len(args) == 0 {
|
||||
@@ -1644,8 +1661,7 @@ func (e *businessEngine) preparePDF(name string, args []platformValue) error {
|
||||
e.pdf.page = index
|
||||
return nil
|
||||
default:
|
||||
_, err := e.finishPDF()
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5950,7 +5966,11 @@ func (e *businessEngine) createDocument(args []platformValue) (any, error) {
|
||||
return nil, err
|
||||
}
|
||||
editor.SetHistoryLimit(100)
|
||||
return e.previewEditor(editor, args[3].Bool())
|
||||
info, err := e.previewEditor(editor, args[3].Bool())
|
||||
if err == nil {
|
||||
e.clearPDF()
|
||||
}
|
||||
return info, err
|
||||
}
|
||||
|
||||
// updateInfo 修改完整文档描述,按字段来源保留自定义内容的扩展属性
|
||||
@@ -6044,7 +6064,7 @@ func (e *businessEngine) documentVersions(args []platformValue) (any, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
versions, err := reader.DocumentVersions()
|
||||
versions, err := reader.DocumentVersionsContext(e.operationContext())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -6635,7 +6655,8 @@ func (e *businessEngine) editDocument(args []platformValue) (any, error) {
|
||||
e.currentSession.editing = true
|
||||
return e.editorSummary(), nil
|
||||
}
|
||||
editor, err := e.currentSession.Reader.Editor()
|
||||
ctx := e.operationContext()
|
||||
editor, err := e.currentSession.Reader.EditorContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -6648,13 +6669,19 @@ func (e *businessEngine) editDocument(args []platformValue) (any, error) {
|
||||
if len(args) != 0 {
|
||||
page = args[0].Int()
|
||||
}
|
||||
content, err := editor.Page(page)
|
||||
content, err := editor.PageContext(ctx, page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := e.syncEditorPageIDs(page, content); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.currentEditor = editor
|
||||
e.currentSession.editor = editor
|
||||
e.currentSession.editing = true
|
||||
@@ -7557,7 +7584,7 @@ func (e *businessEngine) saveDocument(args []platformValue) (any, error) {
|
||||
if e.currentSession == nil {
|
||||
return nil, fmt.Errorf("ofd document is not opened")
|
||||
}
|
||||
editor, err := e.currentSession.Reader.Editor()
|
||||
editor, err := e.currentSession.Reader.EditorContext(e.operationContext())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -7662,6 +7689,12 @@ func (c *outputContext) Err() error {
|
||||
// businessCommands 共用业务命令表
|
||||
func (e *businessEngine) businessCommands() map[string]func([]platformValue) (any, error) {
|
||||
return map[string]func([]platformValue) (any, error){
|
||||
"ofdgoCheckDocument": func(args []platformValue) (any, error) {
|
||||
if len(args) != 1 {
|
||||
return nil, fmt.Errorf("missing operation name")
|
||||
}
|
||||
return nil, e.checkDocument(args[0].String())
|
||||
},
|
||||
"ofdgoPreparePrint": e.preparePrint,
|
||||
"ofdgoLayoutPrint": e.layoutPrint,
|
||||
"ofdgoPrintSide": e.printSide,
|
||||
@@ -7818,6 +7851,8 @@ func (e *businessEngine) operationContext() context.Context {
|
||||
func failureResult(err error) apiResult {
|
||||
result := apiResult{Error: err.Error()}
|
||||
switch {
|
||||
case errors.Is(err, ofdgo.ErrIncompleteDocument):
|
||||
result.Code, result.Error = "documentPending", "文档尚未准备完成"
|
||||
case errors.Is(err, ofdgo.ErrPrintDenied):
|
||||
result.Code, result.Error = "printDenied", "文档禁止打印"
|
||||
case errors.Is(err, ofdgo.ErrPDFPassword):
|
||||
|
||||
@@ -311,6 +311,9 @@ func (e *NativeEngine) Call(ctx context.Context, name string, args []any, emit f
|
||||
if fn == nil {
|
||||
return nativeFailure(fmt.Errorf("unknown operation: %s", name))
|
||||
}
|
||||
if err := e.checkDocument(name); err != nil {
|
||||
return nativeFailure(err)
|
||||
}
|
||||
call := &nativeCall{engine: e, ctx: ctx, emit: emit}
|
||||
if e.pdf != nil {
|
||||
e.pdf.progress = platformValueOf(call.progress("conversion", "", true))
|
||||
|
||||
@@ -68,32 +68,48 @@ func (w *wasmEngines) open(args []js.Value) (any, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// call 按会话标识分派业务命令,不提供默认会话
|
||||
// 入参: args 会话标识、命令及业务参数
|
||||
// call 按会话标识分派业务命令,统一绑定本次调用的取消上下文
|
||||
// 入参: args 会话标识、命令、取消检查点及业务参数
|
||||
// 返回: any 业务结果, error 调用错误
|
||||
func (w *wasmEngines) call(args []js.Value) (any, error) {
|
||||
if len(args) < 2 || args[0].Type() != js.TypeString || args[1].Type() != js.TypeString {
|
||||
if len(args) < 3 || args[0].Type() != js.TypeString || args[1].Type() != js.TypeString {
|
||||
return nil, fmt.Errorf("missing engine ID or operation")
|
||||
}
|
||||
e := w.engines[args[0].String()]
|
||||
if e == nil {
|
||||
return nil, fmt.Errorf("unknown engine: %s", args[0].String())
|
||||
}
|
||||
if e.pdf != nil {
|
||||
defer func() {
|
||||
if e.currentSession != nil {
|
||||
e.currentSession.Renderer.OutputContext = context.Background()
|
||||
}
|
||||
}()
|
||||
}
|
||||
fn := e.businessCommands()[args[1].String()]
|
||||
name := args[1].String()
|
||||
fn := e.businessCommands()[name]
|
||||
if fn == nil {
|
||||
return nil, fmt.Errorf("unknown operation: %s", args[1].String())
|
||||
return nil, fmt.Errorf("unknown operation: %s", name)
|
||||
}
|
||||
if err := e.preparePDF(args[1].String(), args[2:]); err != nil {
|
||||
if err := e.checkDocument(name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fn(args[2:])
|
||||
e.ctx = context.Background()
|
||||
if args[2].Type() == js.TypeFunction {
|
||||
e.ctx = e.newOutputContext(args[2])
|
||||
}
|
||||
ctx := e.ctx
|
||||
if name != "ofdgoSaveSnapshot" && e.currentSession != nil {
|
||||
e.currentSession.Renderer.OutputContext = ctx
|
||||
}
|
||||
defer func() {
|
||||
if e.ctx == ctx {
|
||||
e.ctx = nil
|
||||
}
|
||||
if name != "ofdgoSaveSnapshot" && e.currentSession != nil {
|
||||
e.currentSession.Renderer.OutputContext = context.Background()
|
||||
}
|
||||
}()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := e.preparePDF(name, args[3:]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fn(args[3:])
|
||||
}
|
||||
|
||||
// close 释放会话的文档、编辑器、导入及剪贴板,不关闭共享资源
|
||||
|
||||
Reference in new issue
Block a user