From 830378c6e436f4631d56d190b67421dd9eab896e Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Sat, 12 Sep 2026 13:47:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E5=80=BC=E8=A7=A3=E6=9E=90=E4=B8=8E?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=BC=93=E5=AD=98=EF=BC=8C=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D=E5=92=8C=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/webui/ofdgo.js | 15 +++------------ ofdgo_geom.go | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/assets/webui/ofdgo.js b/assets/webui/ofdgo.js index a484de8..913845e 100644 --- a/assets/webui/ofdgo.js +++ b/assets/webui/ofdgo.js @@ -53,7 +53,6 @@ const state = { pageObserver: null, visiblePages: new Set(), scrollFrame: 0, - thumbnailCache: new Map(), thumbnailInFlight: new Set(), thumbnailObserver: null, visibleThumbnails: new Set(), @@ -1549,7 +1548,6 @@ async function processPageRenderQueue() { const page = await callWASM("ofdgoRenderPage", task.index); if (task.openSeq === state.openSeq) { state.pageCache.set(task.index, page); - cacheThumbnail(task.index, page.svg); } task.resolve(page); } catch (err) { @@ -1809,7 +1807,7 @@ function renderPageList() { const thumb = document.createElement("span"); thumb.className = "thumb-paper"; thumb.setAttribute("aria-hidden", "true"); - setThumbnailContent(thumb, state.thumbnailCache.get(page.index), page.index, openSeq); + setThumbnailContent(thumb, state.pageCache.get(page.index)?.svg, page.index, openSeq); const label = document.createElement("span"); label.className = "thumb-label"; @@ -1831,7 +1829,6 @@ function renderPageList() { } function resetThumbnails() { - state.thumbnailCache.clear(); state.visibleThumbnails.clear(); state.thumbnailInFlight.clear(); if (state.thumbnailObserver) { @@ -1840,12 +1837,6 @@ function resetThumbnails() { } } -function cacheThumbnail(index, svgText) { - if (typeof svgText === "string" && svgText) { - state.thumbnailCache.set(index, svgText); - } -} - function setThumbnailContent(container, svgText, index, openSeq = state.openSeq) { const renderKey = `${openSeq}:${index}`; if (svgText && container.dataset.renderKey === renderKey) { @@ -1923,7 +1914,7 @@ async function renderThumbnail(index, openSeq = state.openSeq) { if (openSeq !== state.openSeq || !state.doc) { return; } - if (state.thumbnailCache.has(index)) { + if (state.pageCache.has(index)) { updateThumbnail(index, openSeq); return; } @@ -1956,7 +1947,7 @@ function updateThumbnail(index, openSeq = state.openSeq) { } const thumb = el.pageList.querySelector(`[data-page-index="${index}"] .thumb-paper`); if (thumb) { - setThumbnailContent(thumb, state.thumbnailCache.get(index), index, openSeq); + setThumbnailContent(thumb, state.pageCache.get(index)?.svg, index, openSeq); } } diff --git a/ofdgo_geom.go b/ofdgo_geom.go index d61bb7f..1af8626 100644 --- a/ofdgo_geom.go +++ b/ofdgo_geom.go @@ -17,6 +17,7 @@ package ofdgo import ( "fmt" "math" + "slices" "strconv" "strings" ) @@ -30,17 +31,21 @@ type Box struct { // 入参: s 字符串 // 返回: Box 矩形对象, error 错误信息 func ParseBox(s string) (Box, error) { - parts := strings.Fields(s) - if len(parts) != 4 { - return Box{}, fmt.Errorf("invalid box: %s", s) - } var values [4]float64 - for i, part := range parts { + i := 0 + for part := range strings.FieldsSeq(s) { + if i == len(values) { + return Box{}, fmt.Errorf("invalid box: %s", s) + } value, err := strconv.ParseFloat(part, 64) if err != nil { return Box{}, fmt.Errorf("invalid box: %s", s) } values[i] = value + i++ + } + if i != len(values) { + return Box{}, fmt.Errorf("invalid box: %s", s) } return Box{X: values[0], Y: values[1], W: values[2], H: values[3]}, nil } @@ -146,11 +151,10 @@ func parseFloats(s string) []float64 { // 入参: s 字符串 // 返回: []float64 浮点数数组 func parseFloatsWithG(s string) []float64 { - parts := strings.Fields(s) var result []float64 gFlag := false gCount := 0 - for _, p := range parts { + for p := range strings.FieldsSeq(s) { if p == "g" { gFlag = true continue @@ -166,6 +170,7 @@ func parseFloatsWithG(s string) []float64 { } if gCount > 0 { v, _ := strconv.ParseFloat(p, 64) + result = slices.Grow(result, gCount) for j := 0; j < gCount; j++ { result = append(result, v) }