fix(plugin): sticky 1.0.8 便利贴宽度变化后不算高度(文字下面多一行空白)
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)。
This commit is contained in:
1 parent
18eaeb96f2
commit
f3c16803a3
2 files changed
+45
-9
No files matched your search
+3
-3
@@ -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",
|
||||
|
||||
+42
-6
@@ -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) {
|
||||
Reference in new issue
Block a user