diff --git a/sticky/README.md b/sticky/README.md index 7adf3e9..3e64497 100644 --- a/sticky/README.md +++ b/sticky/README.md @@ -1,4 +1,4 @@ -# sticky · 便利贴(v1.0.5) +# sticky · 便利贴(v1.0.6) 画布上的便签节点:一张纸片,写多行文字,能调字号、粗细、背景色,能拖大拖小。 **不连线、不进生图链路**——纯标注用途(说明节点、分组标题、随手记)。 @@ -32,6 +32,11 @@ > > v1.0.5 统一圆角:纸片内圈从写死的 10px 改成 `calc(var(--iwb-node-radius, 14px) - 1px)`, > 与宿主节点选中框(外圈 14px + 1px 边框)对齐,选中时两层边缘不再错开。 +> +> v1.0.6 修行高回缩:v1.0.3 为了「内容长高时 resize 起点不滞后」把 `node.h` 顶到内容高度, +> 但文本从两行删回一行时这个 h 没跟着降,外框就一直留着两行高。现在区分**自动跟随**与 +> **用户手动拖尺寸**:自动写出的 h 双向贴合内容(长高就涨、缩回就降);用户拖手柄改过 h +> 后退出自动跟随,手动尺寸照旧保留。 ## 长什么样 @@ -74,7 +79,8 @@ 让内容自然高度贴着文字(v1.0.2)。**外框 minHeight(v1.0.3)**:宿主 `NodeCard` 的 `style.minHeight = node.h` 才是拖不下去的真凶——插件在 `fitOuterBox()` 里用 `工具条 offsetHeight + 正文 style.height + 2px 边框` 算内容高度,首次把旧 `h` 校正到该值, - 并直接把宿主外框的 `minHeight` 覆盖掉;只认首次记录的 stale `h`,用户之后拖大的 `h` 原样保留, + 并直接把宿主外框的 `minHeight` 覆盖掉。插件自动写出的 h **双向跟随内容**(v1.0.6: + 两行退回一行会缩回去);一旦检测到 h 被外部拖手柄改写,就退出自动跟随、尊重手动尺寸, 所以拖大拖小仍然有效。 - **色板定位**:色板挂在工具条 `tools`(`position:relative`)内而非便签根节点,CSS `top:100%` 落在调色圆点正下方;挂在根节点时会贴便签底部右下角、盖住 resize 手柄(v1.0.2 的问题)。 @@ -97,7 +103,7 @@ ## 校验 ```bash -node sticky/tools/harness.mjs # 纯 node,51 项:注册/渲染/字号无上限/贴字自适应/rows=1/外框 minHeight 校正/圆角对齐/色板定位/双击编辑/换色/文本/幂等/插入 +node sticky/tools/harness.mjs # 纯 node,53 项:注册/渲染/字号无上限/贴字自适应/rows=1/外框 minHeight 双向回缩/圆角对齐/色板定位/双击编辑/换色/文本/幂等/插入 npm run validate # 全仓契约校验 ``` diff --git a/sticky/package.json b/sticky/package.json index 24b2f86..f2e243b 100644 --- a/sticky/package.json +++ b/sticky/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/sticky", "private": true, - "version": "1.0.5", + "version": "1.0.6", "description": "画布便利贴:多行文本 + 字号/粗细调节 + 8 色背景,纯标注节点不连线", "ibwPlugin": { "id": "sticky", diff --git a/sticky/src/index.js b/sticky/src/index.js index 0b1de23..fbba3fe 100644 --- a/sticky/src/index.js +++ b/sticky/src/index.js @@ -299,7 +299,10 @@ // 读出来还是旧高度,会自我锁死); // 2. 首次遇到「旧 h 明显大于内容」时把 node.h 校正到内容高度,顺手把外框 minHeight // 覆盖成同一个值——resize 起点 (onResizeStart 读 n0.h) 才和实际视觉一致,拖小立刻生效。 - // 用户之后手动拖大的 h 不会再次被校正(只针对首次记录的 stale h),所以仍然可以拖大拖小。 + // 区分两种 h: + // - 插件自动跟随内容写出的 h:内容长高就涨、缩回就降(v1.0.6 修的「两行退回一行不缩」); + // - 用户拖手柄改出的 h:h 相对上一帧变了 ⇒ 尊重手动尺寸,只在内容超过它时顶一下。 + // 首次遇到的旧大 h 校正一次(staleH),之后进入自动跟随。 function fitOuterBox(st, node) { if (!st || !st.root || !node) return var toolsH = st.tools.offsetHeight || 30 @@ -308,9 +311,24 @@ 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 + + if (st.lastBoxH != null && h !== st.lastBoxH) { + // 外部(用户拖手柄)改了 h:退出自动跟随,保留手动尺寸 + st.autoFollow = false + } else if (st.autoFollow == null) { + // 首帧:旧 h 若明显大于内容,先校正一次,然后进入自动跟随 + if (h > content + 2 && st.staleH === h) { node.h = content; h = content } + st.autoFollow = true + } + + if (st.autoFollow) { + // 自动跟随:文本/字号增减都双向贴合(这是两行退回一行能缩回去的关键) + if (Math.abs(h - content) > 1) { node.h = content; h = content } + } else if (h < content - 2) { + // 手动尺寸只在内容超出时才顶高,不覆盖用户主动拖大的高度 + node.h = content; h = content + } + st.lastBoxH = h 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 @@ -418,7 +436,7 @@ global.IWBPluginHost.register({ id: 'sticky', name: '便利贴', - version: '1.0.5', + version: '1.0.6', apiVersion: 1, capabilities: ['panels', 'nodeTypes', 'canvas'], }, function (ctx) { diff --git a/sticky/tools/harness.mjs b/sticky/tools/harness.mjs index 8218f80..452bbce 100644 --- a/sticky/tools/harness.mjs +++ b/sticky/tools/harness.mjs @@ -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.5', r.manifest.version === '1.0.5') +check('manifest.version = 1.0.6', r.manifest.version === '1.0.6') check('manifest.apiVersion = 1', r.manifest.apiVersion === 1) check('capabilities 含 panels/nodeTypes/canvas', ['panels', 'nodeTypes', 'canvas'].every((c) => r.manifest.capabilities.includes(c))) @@ -186,6 +186,25 @@ r.nodeApi.render(node2, { onUpdate() {} }, host2) check('用户拖大后的 h 保留(可拖大)', node2.h === 260 && card.style.minHeight === '260px', 'h=' + node2.h + ' minH=' + card.style.minHeight) +// v1.0.6:自动跟随内容的 h 必须双向伸缩——两行退回一行时外框要跟着缩回去。 +const card3 = makeNode('div') +const host3 = makeNode('div') +card3.appendChild(host3) +const node3 = { id: 'n3', type: 'plugin:sticky:sticky', + data: { text: '一行', fontSize: 40, fontWeight: 400, bgColor: '#FCE8B0' } } +const pass3 = { onUpdate() {} } +r.nodeApi.render(node3, pass3, host3) +const hOneLine = node3.h +node3.data.text = '第一行\n第二行' +r.nodeApi.render(node3, pass3, host3) +const hTwoLines = node3.h +check('文本一行变两行:外框自动长高', hTwoLines > hOneLine, hOneLine + ' → ' + hTwoLines) +node3.data.text = '一行' +r.nodeApi.render(node3, pass3, host3) +check('文本两行退回一行:外框自动缩回(v1.0.6)', + node3.h === hOneLine && card3.style.minHeight === hOneLine + 'px', + 'h=' + node3.h + ' minH=' + card3.style.minHeight) + function findByClass(root, cls) { const needle = cls.split('.').filter(Boolean).pop() const q = [root]