feat(功能强化): 新增全文搜索与结果定位,优化搜索框及翻页按钮显示
build.yaml / build (push) Successful in 43s

This commit is contained in:
xiaoqidun committed 2026-09-12 22:18:10 +08:00
1 parent 66f0d8a928
commit 2ea937b7be
11 files changed
+641 -19

No files matched your search

+15 -1
View File
@@ -39,6 +39,7 @@ func RunWASM() {
registerCallback("ofdgoDocumentInfo", documentInfo)
registerCallback("ofdgoAttachmentData", attachmentData)
registerCallback("ofdgoRenderPage", renderPage)
registerCallback("ofdgoSearchPage", searchPage)
registerCallback("ofdgoExportFormats", exportFormats)
registerCallback("ofdgoExportPage", exportPage)
registerCallback("ofdgoExportPDF", exportPDF)
@@ -118,7 +119,10 @@ func configureDocument(args []js.Value) (any, error) {
return nil, err
}
}
currentSession.Renderer.RenderAnnotations = args[1].Bool()
if currentSession.Renderer.RenderAnnotations != args[1].Bool() {
currentSession.Renderer.RenderAnnotations = args[1].Bool()
clear(currentSession.textCache)
}
return currentSession.Summary(), nil
}
@@ -181,6 +185,16 @@ func renderPage(args []js.Value) (any, error) {
}), nil
}
// searchPage 搜索OFD页面文字
// 入参: args 浏览器参数
// 返回: any 搜索结果, error 错误信息
func searchPage(args []js.Value) (any, error) {
if currentSession == nil {
return nil, fmt.Errorf("ofd document is not opened")
}
return currentSession.SearchPage(args[0].Int(), args[1].String())
}
// exportFormats 获取导出格式
// 入参: args 浏览器参数
// 返回: any 导出格式, error 错误信息
+22
View File
@@ -107,6 +107,7 @@ type Session struct {
doc *ofdgo.Document
pageCache map[int]*ofdgo.PageContent
boxCache map[int]ofdgo.Box
textCache map[int]*ofdgo.PageText
signatures []SignatureInfo
signatureError error
signaturesRead bool
@@ -239,6 +240,7 @@ func Open(data []byte, opts OpenOptions) (*Session, error) {
doc: doc,
pageCache: make(map[int]*ofdgo.PageContent),
boxCache: make(map[int]ofdgo.Box),
textCache: make(map[int]*ofdgo.PageText),
}, nil
}
@@ -264,9 +266,29 @@ func (s *Session) SetFonts(fonts []FontFile) error {
} else {
s.Renderer.SetFontFS()
}
clear(s.textCache)
return nil
}
// SearchPage 搜索指定页面,复用当前会话的文字索引
// 入参: index 页面索引, query 搜索文字
// 返回: []ofdgo.TextMatch 匹配结果, error 错误信息
func (s *Session) SearchPage(index int, query string) ([]ofdgo.TextMatch, error) {
text := s.textCache[index]
if text == nil {
_, page, err := s.pageContent(index)
if err != nil {
return nil, err
}
text, err = s.Renderer.PageText(page)
if err != nil {
return nil, err
}
s.textCache[index] = text
}
return text.Search(query), nil
}
// Summary 获取首屏所需信息,不解析页面图元或执行验签
// 返回: DocumentInfo 文档信息
func (s *Session) Summary() DocumentInfo {