From f3c16803a3905dcb493ca230119932339ec4ee28 Mon Sep 17 00:00:00 2001 From: qiqi Date: Thu, 26 Nov 2026 12:46:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugin):=20sticky=201.0.8=20=E4=BE=BF?= =?UTF-8?q?=E5=88=A9=E8=B4=B4=E5=AE=BD=E5=BA=A6=E5=8F=98=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E4=B8=8D=E7=AE=97=E9=AB=98=E5=BA=A6=EF=BC=88=E6=96=87=E5=AD=97?= =?UTF-8?q?=E4=B8=8B=E9=9D=A2=E5=A4=9A=E4=B8=80=E8=A1=8C=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fitBody 的高度缓存只比 text/fontSize:宿主拖宽节点、侧栏开合、首帧 clientWidth=0 时都会沿用旧行数的高度,文字少一行时正好空出一整行;scrollHeight 的取整误差还会叠半行。 现在把 clientWidth 纳入缓存键、高度按「行高 × 整数行 + padding」对齐,并加 ResizeObserver。 源仓 iwb-plugins-desktop sticky/src/index.js + 契约测试 harness 两条新断言; 本仓 artifacts 由 scripts/sync-plugins-from-repo.js 生成(内容寻址 + SRI)。 --- plugins/index.json | 6 +-- ...9f0c7.js => plugin-sticky.f5f77ac81467.js} | 48 ++++++++++++++++--- 2 files changed, 45 insertions(+), 9 deletions(-) rename plugins/runtime/{plugin-sticky.af8e7249f0c7.js => plugin-sticky.f5f77ac81467.js} (90%) diff --git a/plugins/index.json b/plugins/index.json index 10bf137..3447b26 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -75,9 +75,9 @@ }, { "id": "sticky", - "file": "plugin-sticky.af8e7249f0c7.js", - "version": "1.0.7", - "integrity": "sha256-r45ySfDHwBDMFUTuWpOpq21cJcqlYtq5DnOKUtndSLI=" + "file": "plugin-sticky.f5f77ac81467.js", + "version": "1.0.8", + "integrity": "sha256-9fd6yBRnWxcDNDN1G+KywL9vp5JN9CaTMja4s5pK1As=" }, { "id": "style-source", diff --git a/plugins/runtime/plugin-sticky.af8e7249f0c7.js b/plugins/runtime/plugin-sticky.f5f77ac81467.js similarity index 90% rename from plugins/runtime/plugin-sticky.af8e7249f0c7.js rename to plugins/runtime/plugin-sticky.f5f77ac81467.js index 65b1a30..350e90f 100644 --- a/plugins/runtime/plugin-sticky.af8e7249f0c7.js +++ b/plugins/runtime/plugin-sticky.f5f77ac81467.js @@ -280,19 +280,55 @@ body.addEventListener('input', onInput) st._listeners = { tools: onTool, pal: onPal, color: onColor, input: onInput, dblclick: onDblClick, setEditing: setEditing } + + // 宽度变化 → 重新算行数(2026-11-26)。宿主改卡片宽度的时机都不经过 render: + // 拖右下角改宽、左右侧栏开合、首帧布局落后于脚本。没有这条监听时, + // 「先窄后宽」会让 textarea 一直按窄的时候的行数留高度 —— 文字下面就多出一行空白。 + if (typeof global.ResizeObserver === 'function') { + try { + st.ro = new global.ResizeObserver(function () { + // 宿主销毁节点后 observer 会继续活着:发现骨架已摘除就自我断开 + if (st.root && st.root.isConnected === false) { try { st.ro.disconnect() } catch (e3) { /* noop */ } st.ro = null; return } + var d = (st.node && st.node.data) || {} + fitBody(st, d.text == null ? '' : String(d.text), normSize(d.fontSize == null ? DEFAULT_SIZE : d.fontSize)) + fitOuterBox(st, st.node) + }) + st.ro.observe(body) + } catch (e3) { st.ro = null } + } return st } - // textarea 高度贴字:数据(文本/字号)变了才重算,避免每次 render 都 reflow。 + // textarea 高度贴字:文本 / 字号 / **宽度** 变了才重算,避免每次 render 都 reflow。 // 设 auto → 读 scrollHeight → 写回,是 textarea 自适应的标准做法;浏览器里行数/自动换行都算准。 + // + // 2026-11-26(用户反馈「文字下面又多出一行空白行」,v1.0.4 修过又回来了): + // ① 旧实现只比 text/size ⇒ **宽度一变就沿用旧行数的高度**。宿主会在这些时候改卡片宽度: + // 拖动改宽、左右侧栏开合、首帧还没布局完(clientWidth=0 时会算出很多行)。宽了以后 + // 文本少一行,高度却按多一行留着 —— 正好在文字下面空一整行。 + // ② scrollHeight 是整数像素,line-height 1.7×14=23.8 时每行都有取整误差,累加后同样表现为 + // 「多出半行到一行」。改成先按行数取整、再乘回去。 function fitBody(st, text, size) { - if (st.fitText === text && st.fitSize === size) return - st.fitText = text; st.fitSize = size var b = st.body + var w = b.clientWidth || 0 + if (st.fitText === text && st.fitSize === size && st.fitW === w) return + st.fitText = text; st.fitSize = size; st.fitW = w + var padY = 24 + var lineH = Math.round(size * 1.7) || 24 + try { + var cs = global.getComputedStyle ? global.getComputedStyle(b) : null + if (cs) { + var pt = parseFloat(cs.paddingTop), pb = parseFloat(cs.paddingBottom) + if (isFinite(pt) && isFinite(pb)) padY = pt + pb + var lh = parseFloat(cs.lineHeight) + if (isFinite(lh) && lh > 0) lineH = lh + } + } catch (e) { /* noop */ } var prev = b.style.height b.style.height = 'auto' - var h = b.scrollHeight - b.style.height = (h && h > 0 ? h : Math.max(1, text.split('\n').length) * Math.round(size * 1.7) + 24) + 'px' + var raw = b.scrollHeight + var lines = (raw > 0 && lineH > 0) ? Math.max(1, Math.round((raw - padY) / lineH)) : Math.max(1, String(text).split('\n').length) + b.style.height = Math.round(lines * lineH + padY) + 'px' // 高度没变就不留空字符串(兼容 stub:style.height 是普通对象) if (!b.style.height && prev) b.style.height = prev } @@ -441,7 +477,7 @@ global.IWBPluginHost.register({ id: 'sticky', name: '便利贴', - version: '1.0.7', + version: '1.0.8', apiVersion: 1, capabilities: ['panels', 'nodeTypes', 'canvas'], }, function (ctx) {