fix(sticky): 1.0.8 宽度变化重算高度 + 整数行对齐(文字下方多一行空白)
fitBody 只比 text/fontSize ⇒ 宿主改宽度(拖宽、侧栏开合、首帧未布局)时沿用旧行数高度。 高度缓存加入 clientWidth,高度按行高整数行对齐,并加 ResizeObserver(骨架摘除时自断)。 harness 增加宽度 160→420 变矮、整数行残差两条断言;版本断言改为对齐 package.json。
This commit is contained in:
1 parent
27a0ad9952
commit
6e6bc3389d
3 files changed
+77
-10
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/sticky",
|
||||
"private": true,
|
||||
"version": "1.0.7",
|
||||
"version": "1.0.8",
|
||||
"description": "画布便利贴:多行文本 + 字号/粗细调节 + 8 色背景,纯标注节点不连线",
|
||||
"ibwPlugin": {
|
||||
"id": "sticky",
|
||||
|
||||
+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) {
|
||||
|
||||
@@ -27,11 +27,20 @@ function makeNode(tag) {
|
||||
children: [], parentNode: null, _text: '', attrs: {}, listeners: {},
|
||||
style: {}, className: '', hidden: false, value: '',
|
||||
// textarea 自适应高度需要 scrollHeight:按「行数 × 行高 + 上下 padding」模拟。
|
||||
// 给了 clientWidth 就按宽度粗估换行(越宽行数越少)—— v1.0.8「宽度变了高度要跟着重算」靠它验证。
|
||||
get scrollHeight() {
|
||||
if (String(tag).toUpperCase() !== 'TEXTAREA') return 0
|
||||
const lines = Math.max(1, String(this.value || '').split('\n').length)
|
||||
const size = parseFloat(this.style.fontSize) || 14
|
||||
return Math.ceil(lines * size * 1.7) + 24
|
||||
const w = Number(this.clientWidth) || 0
|
||||
const text = String(this.value || '')
|
||||
let lines = 0
|
||||
if (w > 40) {
|
||||
const per = Math.max(1, Math.floor((w - 24) / (size * 0.55)))
|
||||
text.split('\n').forEach((p) => { lines += Math.max(1, Math.ceil(p.length / per)) })
|
||||
} else {
|
||||
lines = Math.max(1, text.split('\n').length)
|
||||
}
|
||||
return Math.ceil(Math.max(1, lines) * size * 1.7) + 24
|
||||
},
|
||||
get textContent() { return this._text },
|
||||
set textContent(v) { this._text = String(v); this.children = [] },
|
||||
@@ -127,7 +136,8 @@ const r = runPlugin()
|
||||
|
||||
check('manifest.id = sticky', r.manifest.id === 'sticky')
|
||||
check('manifest.name = 便利贴', r.manifest.name === '便利贴')
|
||||
check('manifest.version = 1.0.7', r.manifest.version === '1.0.7')
|
||||
const PKG_VERSION = JSON.parse(readFileSync(path.resolve(HERE, '../package.json'), 'utf8')).version
|
||||
check('manifest.version = package.json', r.manifest.version === PKG_VERSION, r.manifest.version + ' / ' + PKG_VERSION)
|
||||
check('manifest.apiVersion = 1', r.manifest.apiVersion === 1)
|
||||
check('capabilities 含 panels/nodeTypes/canvas',
|
||||
['panels', 'nodeTypes', 'canvas'].every((c) => r.manifest.capabilities.includes(c)))
|
||||
@@ -253,6 +263,27 @@ nh.node.data.fontSize = 40
|
||||
r.nodeApi.render(nh.node, nh.props, nh.h)
|
||||
check('字号 14→40 时 textarea 高度重算变大', parseFloat(taSize.style.height) > hBefore)
|
||||
nh.node.data.fontSize = 14
|
||||
// v1.0.8(用户反馈「文字下面又多出一行空白行」):宽度变化必须重算高度。
|
||||
// 旧实现只比 text/size ⇒ 先窄后宽时行数少了、高度仍按多的那行留着,正好空出一整行。
|
||||
{
|
||||
const hostW = makeNode('div')
|
||||
const nodeW = { id: 'w1', type: 'plugin:sticky:sticky', data: { text: 'x'.repeat(120), fontSize: 14, fontWeight: 400, bgColor: '#FCE8B0' } }
|
||||
const propsW = { onUpdate() {} }
|
||||
const taW = () => findByClass(hostW, 'iwb-sticky-body')
|
||||
r.nodeApi.render(nodeW, propsW, hostW)
|
||||
taW().clientWidth = 160
|
||||
r.nodeApi.render(nodeW, propsW, hostW)
|
||||
const tall = parseFloat(taW().style.height)
|
||||
taW().clientWidth = 420
|
||||
r.nodeApi.render(nodeW, propsW, hostW)
|
||||
const wide = parseFloat(taW().style.height)
|
||||
check('宽度 160→420(同一段文字)textarea 高度跟着变矮', wide < tall, tall + ' → ' + wide)
|
||||
// 行高对齐:高度必须是「行高 × 整数行 + padding」,不能带半行误差
|
||||
const padY = 24
|
||||
const lineH = 14 * 1.7
|
||||
const rem = ((wide - padY) % lineH + lineH) % lineH
|
||||
check('高度按整数行对齐(无半行残差)', rem < 1.5 || rem > lineH - 1.5, 'remainder=' + rem.toFixed(2))
|
||||
}
|
||||
findByAct(nh.h, 'bold').fire('click', { target: findByAct(nh.h, 'bold') })
|
||||
check('B 切 fontWeight=600', nh.h.__updates.some((u) => u.patch.fontWeight === 600))
|
||||
|
||||
|
||||
Reference in new issue
Block a user