fix(sticky): v1.0.3 外框高度贴字(覆盖宿主 minHeight)+ 调色点右上
validate / validate (push) Canceled after 0s

This commit is contained in:
qiqi committed 2026-11-24 11:37:24 +08:00
1 parent 8c6ed43078
commit 5ff3318fb9
4 files changed
+67 -9

No files matched your search

+14 -3
View File
@@ -1,4 +1,4 @@
# sticky · 便利贴(v1.0.2)
# sticky · 便利贴(v1.0.3)
画布上的便签节点:一张纸片,写多行文字,能调字号、粗细、背景色,能拖大拖小。
**不连线、不进生图链路**——纯标注用途(说明节点、分组标题、随手记)。
@@ -19,6 +19,12 @@
> 会跟着收缩,resize 手柄就能拖到贴字;② 色板弹在便签右下角、盖住 resize 手柄——
> 色板 DOM 从根节点挪进**工具条**(`top:100%` 落在调色按钮正下方)。
> 选中气泡上的「节点颜色」按钮是宿主画的、对本插件无效,需改宿主才能去掉——本版不动。
>
> v1.0.3 修掉「v1.0.2 看着修了其实没修透」的高度问题:宿主 `NodeCard` 把 `node.h` 写成
> 外框 `minHeight`(`height:auto`),旧的 `h` 仍在撑着外框,所以正文贴字了、下面还是一截空白,
> resize 手柄也拖不下去。现在插件在渲染时按「工具条 + 正文」算内容高度,首次遇到旧 `h`
> 明显大于内容时把它校正到内容高度,并同步覆盖外框 `minHeight`;用户后续拖大的 `h` 会保留,
> 仍能拖大拖小。调色圆点移到工具条**最右上**,色板 `right:8px` 正对圆点右缘、落其下方。
## 长什么样
@@ -57,9 +63,14 @@
宿主容器 `.iwb-plugin-node-body`(flex 列、`padding:0`)——只写 `height:100%` 在 WKWebView 下
对 flex 项算不准,拖大后下半会露出宿主底色(v1.0.0 的问题);**textarea 反过来用 `flex:0 0 auto`
+ JS 按 `scrollHeight` 算高**(文本/字号变了才重算,缓存 `fitText/fitSize` 避免每次 render reflow),
让内容自然高度贴着文字——宿主外框 `height:auto` 于是能跟着收缩,resize 手柄可拖到贴字(v1.0.2)。
让内容自然高度贴着文字(v1.0.2)。**外框 minHeight(v1.0.3)**:宿主 `NodeCard` 的
`style.minHeight = node.h` 才是拖不下去的真凶——插件在 `fitOuterBox()` 里用
`工具条 offsetHeight + 正文 style.height + 2px 边框` 算内容高度,首次把旧 `h` 校正到该值,
并直接把宿主外框的 `minHeight` 覆盖掉;只认首次记录的 stale `h`,用户之后拖大的 `h` 原样保留,
所以拖大拖小仍然有效。
- **色板定位**:色板挂在工具条 `tools`(`position:relative`)内而非便签根节点,CSS `top:100%`
落在调色圆点正下方;挂在根节点时会贴便签底部右下角、盖住 resize 手柄(v1.0.2 的问题)。
圆点用 `margin-left:auto` 推到工具条最右上(v1.0.3),色板 `right:8px` 与圆点右缘对齐。
- **样式时机**:CSS 在脚本执行时就注入唯一 `<style id="iwb-sticky-style">`,不等 setup / mount
—— 从存档直接进画布(面板从不 mount)时节点也有样式。
- **幂等同步**:`render(node, props, host)` 每次渲染都被宿主无依赖地调一次;插件缓存 host 上的
@@ -78,7 +89,7 @@
## 校验
```bash
node sticky/tools/harness.mjs # 纯 node,43 项:注册/渲染/字号无上限/贴字自适应/色板定位/双击编辑/换色/文本/幂等/插入
node sticky/tools/harness.mjs # 纯 node,49 项:注册/渲染/字号无上限/贴字自适应/外框 minHeight 校正/色板定位/双击编辑/换色/文本/幂等/插入
npm run validate # 全仓契约校验
```
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@iwb-plugin/sticky",
"private": true,
"version": "1.0.2",
"version": "1.0.3",
"description": "画布便利贴:多行文本 + 字号/粗细调节 + 8 色背景,纯标注节点不连线",
"ibwPlugin": {
"id": "sticky",
+31 -4
View File
@@ -40,7 +40,8 @@
'.iwb-sticky-btn{cursor:pointer;border:1px solid rgba(0,0,0,.2);border-radius:6px;background:transparent;color:inherit;font-size:11px;padding:2px 6px;font-family:inherit;line-height:1.4;user-select:none}' +
'.iwb-sticky-btn:hover{background:rgba(0,0,0,.07)}' +
'.iwb-sticky-btn-on{font-weight:600;background:rgba(0,0,0,.1)}' +
'.iwb-sticky-dot{display:inline-block;width:14px;height:14px;border-radius:50%;border:1px solid rgba(0,0,0,.25);cursor:pointer;vertical-align:middle;padding:0}' +
// 调色圆点推到工具条最右端(右上),色板 right:8px 与圆点右缘对齐,落在其正下方。
'.iwb-sticky-dot{display:inline-block;width:14px;height:14px;border-radius:50%;border:1px solid rgba(0,0,0,.25);cursor:pointer;vertical-align:middle;padding:0;margin-left:auto}' +
'.iwb-sticky-dot-on{box-shadow:0 0 0 2px rgba(0,0,0,.55)}' +
'.iwb-sticky-pal{position:absolute;top:100%;right:8px;z-index:20;display:grid;grid-template-columns:repeat(4,18px);gap:8px 6px;padding:10px;border-radius:10px;border:1px solid var(--iwb-border,#d5d9e0);background:var(--iwb-layer2,#fff);box-shadow:0 4px 16px rgba(0,0,0,.18)}' +
'.iwb-sticky-pal-sw{width:18px;height:18px;border-radius:50%;border:1px solid rgba(0,0,0,.2);cursor:pointer;padding:0}' +
@@ -137,7 +138,7 @@
}
st.node = node
st.props = props
syncSkeleton(st, data)
syncSkeleton(st, data, node)
}
function buildSkeleton(host, props) {
@@ -286,7 +287,32 @@
if (!b.style.height && prev) b.style.height = prev
}
function syncSkeleton(st, data) {
// 宿主 NodeCard 把 node.h 当作 minHeight 写在外框上(style.minHeight = node.h, height:auto)。
// 这就是 v1.0.2「内容贴字了但外框还有一截空白、手柄拖不下去」的真正原因:插件只改了
// textarea 高度,外框仍被 node.h 撑住。这里做两件事:
// 1. 内容高度 = 工具条 + 正文(不用 root.offsetHeight:root 会被旧的 minHeight 拉高,
// 读出来还是旧高度,会自我锁死);
// 2. 首次遇到「旧 h 明显大于内容」时把 node.h 校正到内容高度,顺手把外框 minHeight
// 覆盖成同一个值——resize 起点 (onResizeStart 读 n0.h) 才和实际视觉一致,拖小立刻生效。
// 用户之后手动拖大的 h 不会再次被校正(只针对首次记录的 stale h),所以仍然可以拖大拖小。
function fitOuterBox(st, node) {
if (!st || !st.root || !node) return
var toolsH = st.tools.offsetHeight || 30
var bodyH = parseFloat(st.body.style.height) || 0
if (!bodyH) return
var content = Math.round(toolsH + bodyH + 2) // +2 = 宿主节点上下各 1px 边框
var h = Number(node.h) || 0
if (st.staleH == null) st.staleH = h
// 文字变大后内容超过旧 h:同步把 h 顶上去,resize 起点才不会滞后一大截。
// 首次遇到的旧大 h 也校正到内容高度;用户之后拖出来的其它更大 h 原样保留。
if (h < content - 2 || (h > content + 2 && st.staleH === h)) node.h = content
var floor = Math.max(content, Number(node.h) || 0)
// root → host(.iwb-plugin-node-body) → card(.iwb-node)
var card = st.root.parentNode && st.root.parentNode.parentNode
if (card && card.style && card.style.minHeight !== floor + 'px') card.style.minHeight = floor + 'px'
}
function syncSkeleton(st, data, node) {
var bg = data.bgColor || DEFAULT_BG
var size = normSize(data.fontSize == null ? DEFAULT_SIZE : data.fontSize)
var weight = data.fontWeight === 600 ? 600 : 400
@@ -300,6 +326,7 @@
if (st.body.style.fontSize !== size + 'px') st.body.style.fontSize = size + 'px'
if (st.body.style.fontWeight !== String(weight)) st.body.style.fontWeight = String(weight)
fitBody(st, text, size)
fitOuterBox(st, node)
if (st.bold.className !== 'iwb-sticky-btn' + (weight === 600 ? ' iwb-sticky-btn-on' : '')) {
st.bold.className = 'iwb-sticky-btn' + (weight === 600 ? ' iwb-sticky-btn-on' : '')
}
@@ -386,7 +413,7 @@
global.IWBPluginHost.register({
id: 'sticky',
name: '便利贴',
version: '1.0.2',
version: '1.0.3',
apiVersion: 1,
capabilities: ['panels', 'nodeTypes', 'canvas'],
}, function (ctx) {
+21 -1
View File
@@ -127,7 +127,7 @@ const r = runPlugin()
check('manifest.id = sticky', r.manifest.id === 'sticky')
check('manifest.name = 便利贴', r.manifest.name === '便利贴')
check('manifest.version = 1.0.2', r.manifest.version === '1.0.2')
check('manifest.version = 1.0.3', r.manifest.version === '1.0.3')
check('manifest.apiVersion = 1', r.manifest.apiVersion === 1)
check('capabilities 含 panels/nodeTypes/canvas',
['panels', 'nodeTypes', 'canvas'].every((c) => r.manifest.capabilities.includes(c)))
@@ -164,6 +164,26 @@ check('textarea flex:0 0 auto(不 flex 撑满留白)', /\.iwb-sticky-body\{f
function injectedCssOf(rr) { return (findById(rr.head, 'iwb-sticky-style') || {}).textContent || '' }
const taInit = findByClass(nh.h, 'iwb-sticky-body')
check('textarea 初始高度按内容算出(贴字)', parseFloat(taInit.style.height) > 0)
check('调色圆点推到工具条最右(右上)', /\.iwb-sticky-dot\{[^}]*margin-left:auto/.test(injectedCssOf(r)))
// v1.0.3:宿主外框 minHeight 来自 node.h —— 旧 h 会把便签撑出一截空白且手柄拖不下去。
// 用带父节点卡片的 host 复现:渲染后必须把外框 minHeight 校正成内容高度,并同步改正 node.h。
const card = makeNode('div')
card.style.minHeight = '360px'
const host2 = makeNode('div')
card.appendChild(host2)
const node2 = { id: 'n2', type: 'plugin:sticky:sticky', h: 360,
data: { text: '范例', fontSize: 40, fontWeight: 600, bgColor: '#FCE8B0' } }
r.nodeApi.render(node2, { onUpdate() {} }, host2)
const expectedH = Math.round(30 + parseFloat(findByClass(host2, 'iwb-sticky-body').style.height) + 2)
check('外框 minHeight 覆盖为内容高度(不再被旧 h 撑住)', card.style.minHeight === expectedH + 'px',
'card=' + card.style.minHeight + ' expected=' + expectedH + 'px')
check('旧 node.h 校正为内容高度(resize 起点贴字)', node2.h === expectedH, 'h=' + node2.h)
// 用户之后手动拖大的 h 不能被再次压回(仍然可拖大拖小)
node2.h = 260
r.nodeApi.render(node2, { onUpdate() {} }, host2)
check('用户拖大后的 h 保留(可拖大)', node2.h === 260 && card.style.minHeight === '260px',
'h=' + node2.h + ' minH=' + card.style.minHeight)
function findByClass(root, cls) {
const needle = cls.split('.').filter(Boolean).pop()