fix(sticky): v1.0.2 便签贴字(textarea 内容自适应)+ 色板跟随调色按钮
validate / validate (push) Canceled after 0s

This commit is contained in:
qiqi committed 2026-11-24 11:20:19 +08:00
1 parent 9ed18827a8
commit 8c6ed43078
4 files changed
+67 -12

No files matched your search

+17 -7
View File
@@ -1,4 +1,4 @@
# sticky · 便利贴(v1.0.1)
# sticky · 便利贴(v1.0.2)
画布上的便签节点:一张纸片,写多行文字,能调字号、粗细、背景色,能拖大拖小。
**不连线、不进生图链路**——纯标注用途(说明节点、分组标题、随手记)。
@@ -13,6 +13,12 @@
> v1.0.1 修复三个真机问题:① 节点拖动变大后下半漏出宿主底色(根容器改 `flex:1` 撑满);
> ② 字号被闭集钳在 24(改为无上限、±2 步进);③ 正文单击即编辑(改为照宿主文本节点那样
> **单击选中/拖动、双击才进编辑**)。
>
> v1.0.2 修复两个体验问题:① 便签高度不能贴近文字(下方一大块空白)——textarea 改为
> **高度随内容自适应**(`flex:0 0 auto` + JS 按 `scrollHeight` 算高),节点外框 `height:auto`
> 会跟着收缩,resize 手柄就能拖到贴字;② 色板弹在便签右下角、盖住 resize 手柄——
> 色板 DOM 从根节点挪进**工具条**(`top:100%` 落在调色按钮正下方)。
> 选中气泡上的「节点颜色」按钮是宿主画的、对本插件无效,需改宿主才能去掉——本版不动。
## 长什么样
@@ -22,7 +28,7 @@
|---|---|
| `−` / `+` | 字号缩放,每步 ±2,下限 6、**无上限** |
| `B` | 正文粗细在 400 / 600 间切换(按钮高亮表示已加粗) |
| 圆点 | 展开色板:8 个预设色 + 底部彩虹色轮 |
| 圆点 | 展开色板:8 个预设色 + 底部彩虹色轮;**色板落在圆点正下方**,不挡 resize 手柄 |
| 色轮 | 调起系统颜色选择器,可选任意色 |
| 正文区 | 多行 textarea,透明底,随字号自动换行;**单击=选中/拖动节点,双击=进入编辑,失焦/ESC 退出** |
@@ -46,10 +52,14 @@
- **注册**:`ctx.nodes.register({ id:'sticky', title:'便利贴', ports:{in:false,out:false}, height:180, render })`
—— `ports` 全关,纯标注节点;宿主生成的类型是 `plugin:sticky:sticky`。
- **不设宽高**:节点缩放用宿主自带手柄(每个节点右下角 `.iwb-resize`,对插件节点天然生效)。
插件没有写 `w/h` 的 API,也不该自己排尺寸。但**节点体必须 `flex:1 1 auto; min-height:0` 撑满**
宿主容器 `.iwb-plugin-node-body`(它是 flex 列且 `padding:0`)——只写 `height:100%` 在
WKWebView 下对 flex 项算不准,拖大后下半会露出宿主底色(v1.0.0 的真机问题)。
- **节点尺寸**:节点缩放用宿主自带手柄(右下角 `.iwb-resize`,对插件节点天然生效),插件不写
`w/h`。两层 flex 各管一件事:**根容器 `.iwb-sticky-node` 用 `flex:1 1 auto; min-height:0` 撑满**
宿主容器 `.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)。
- **色板定位**:色板挂在工具条 `tools`(`position:relative`)内而非便签根节点,CSS `top:100%`
落在调色圆点正下方;挂在根节点时会贴便签底部右下角、盖住 resize 手柄(v1.0.2 的问题)。
- **样式时机**:CSS 在脚本执行时就注入唯一 `<style id="iwb-sticky-style">`,不等 setup / mount
—— 从存档直接进画布(面板从不 mount)时节点也有样式。
- **幂等同步**:`render(node, props, host)` 每次渲染都被宿主无依赖地调一次;插件缓存 host 上的
@@ -68,7 +78,7 @@
## 校验
```bash
node sticky/tools/harness.mjs # 纯 node,40 项:注册/渲染/字号无上限/双击编辑/换色/文本/幂等/插入
node sticky/tools/harness.mjs # 纯 node,43 项:注册/渲染/字号无上限/贴字自适应/色板定位/双击编辑/换色/文本/幂等/插入
npm run validate # 全仓契约校验
```
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@iwb-plugin/sticky",
"private": true,
"version": "1.0.1",
"version": "1.0.2",
"description": "画布便利贴:多行文本 + 字号/粗细调节 + 8 色背景,纯标注节点不连线",
"ibwPlugin": {
"id": "sticky",
+24 -3
View File
@@ -46,7 +46,10 @@
'.iwb-sticky-pal-sw{width:18px;height:18px;border-radius:50%;border:1px solid rgba(0,0,0,.2);cursor:pointer;padding:0}' +
'.iwb-sticky-wheel{grid-column:1/-1;display:flex;justify-content:center;padding-top:8px;border-top:1px solid var(--iwb-border,#d5d9e0)}' +
'.iwb-sticky-wheel-btn{cursor:pointer;width:22px;height:22px;border-radius:50%;border:0;padding:0;background:conic-gradient(#e24b4a,#ef9f27,#fac775,#639922,#1d9e75,#378add,#7f77dd,#d4537e,#e24b4a)}' +
'.iwb-sticky-body{flex:1 1 auto;min-height:0;width:100%;box-sizing:border-box;resize:none;border:0;outline:0;background:transparent;color:inherit;font-family:inherit;line-height:1.7;padding:12px;overflow:auto;overflow-wrap:anywhere;cursor:move}' +
// 高度随内容自适应(贴字):不再 flex:1 撑满剩余空间,否则字号大/文字少时
// 下方留出大片空白;高度由 syncSkeleton 按行数×字号算,宿主外框 height:auto
// 会跟着内容收缩,resize 手柄就能拖到贴字。overflow:hidden 避免自适应高度出滚动条。
'.iwb-sticky-body{flex:0 0 auto;min-height:1.7em;width:100%;box-sizing:border-box;resize:none;border:0;outline:0;background:transparent;color:inherit;font-family:inherit;line-height:1.7;padding:12px;overflow:hidden;overflow-wrap:anywhere;cursor:move}' +
// 与宿主文本节点一致:只读态单击穿透给节点拖动/选中;双击进入编辑才变 text 光标。
// 宿主 style.css 有 `.iwb-node textarea[readonly]{pointer-events:none}`,这里用自身 class 兜底。
'.iwb-sticky-body.iwb-sticky-editing{cursor:text}' +
@@ -154,7 +157,10 @@
body.setAttribute('readonly', '')
tools.appendChild(minus); tools.appendChild(plus); tools.appendChild(bold); tools.appendChild(dot)
root.appendChild(tools); root.appendChild(pal); root.appendChild(colorInput); root.appendChild(body)
// 色板挂在工具条内(tools 已 position:relative):CSS top:100% 落在调色按钮正下方,
// 而不是贴整个便签底部右下角(那样会盖住宿主 resize 手柄、且离按钮很远)。
tools.appendChild(pal)
root.appendChild(tools); root.appendChild(colorInput); root.appendChild(body)
host.appendChild(root)
var st = { root: root, tools: tools, minus: minus, plus: plus, bold: bold, dot: dot,
@@ -266,6 +272,20 @@
return st
}
// textarea 高度贴字:数据(文本/字号)变了才重算,避免每次 render 都 reflow。
// 设 auto → 读 scrollHeight → 写回,是 textarea 自适应的标准做法;浏览器里行数/自动换行都算准。
function fitBody(st, text, size) {
if (st.fitText === text && st.fitSize === size) return
st.fitText = text; st.fitSize = size
var b = st.body
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'
// 高度没变就不留空字符串(兼容 stub:style.height 是普通对象)
if (!b.style.height && prev) b.style.height = prev
}
function syncSkeleton(st, data) {
var bg = data.bgColor || DEFAULT_BG
var size = normSize(data.fontSize == null ? DEFAULT_SIZE : data.fontSize)
@@ -279,6 +299,7 @@
if (!st.editing && document.activeElement !== st.body && st.body.value !== text) st.body.value = text
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)
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' : '')
}
@@ -365,7 +386,7 @@
global.IWBPluginHost.register({
id: 'sticky',
name: '便利贴',
version: '1.0.1',
version: '1.0.2',
apiVersion: 1,
capabilities: ['panels', 'nodeTypes', 'canvas'],
}, function (ctx) {
+25 -1
View File
@@ -26,6 +26,13 @@ function makeNode(tag) {
tagName: String(tag).toUpperCase(),
children: [], parentNode: null, _text: '', attrs: {}, listeners: {},
style: {}, className: '', hidden: false, value: '',
// textarea 自适应高度需要 scrollHeight:按「行数 × 行高 + 上下 padding」模拟。
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
},
get textContent() { return this._text },
set textContent(v) { this._text = String(v); this.children = [] },
get firstChild() { return this.children[0] || null },
@@ -120,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.1', r.manifest.version === '1.0.1')
check('manifest.version = 1.0.2', r.manifest.version === '1.0.2')
check('manifest.apiVersion = 1', r.manifest.apiVersion === 1)
check('capabilities 含 panels/nodeTypes/canvas',
['panels', 'nodeTypes', 'canvas'].every((c) => r.manifest.capabilities.includes(c)))
@@ -147,6 +154,17 @@ check('节点体有工具条 A+/A−/B + 色板按钮', !!findByClass(nh.h, 'iwb
check('节点体有 textarea', findByClass(nh.h, 'iwb-sticky-body') && findByClass(nh.h, 'iwb-sticky-body').tagName === 'TEXTAREA')
check('默认文本同步到 textarea', findByClass(nh.h, 'iwb-sticky-body').value === '标题')
// v1.0.2:色板挂在工具条内(top:100% 落调色按钮下方),不再贴便签底部右下角盖住 resize 手柄
const toolsBox = findByClass(nh.h, 'iwb-sticky-tools')
const palBox = findByClass(nh.h, 'iwb-sticky-pal')
check('色板挂在工具条内(跟随调色按钮,不盖 resize 手柄)', palBox && palBox.parentNode === toolsBox)
check('色板 CSS top:100%(落按钮下方)', /\.iwb-sticky-pal\{position:absolute;top:100%/.test(injectedCssOf(r)))
// v1.0.2:textarea 高度随内容自适应(flex:0 0 auto + JS 写 height)
check('textarea flex:0 0 auto(不 flex 撑满留白)', /\.iwb-sticky-body\{flex:0 0 auto/.test(injectedCssOf(r)))
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)
function findByClass(root, cls) {
const needle = cls.split('.').filter(Boolean).pop()
const q = [root]
@@ -188,6 +206,12 @@ nh.node.data.fontSize = 100
nh.h.__updates.length = 0
findByAct(nh.h, 'plus').fire('click', { target: findByAct(nh.h, 'plus') })
check('字号无上限(100 → 102)', nh.h.__updates.some((u) => u.patch.fontSize === 102))
// 字号变大 → textarea 高度跟着涨(贴字自适应)
const taSize = findByClass(nh.h, 'iwb-sticky-body')
const hBefore = parseFloat(taSize.style.height)
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
findByAct(nh.h, 'bold').fire('click', { target: findByAct(nh.h, 'bold') })
check('B 切 fontWeight=600', nh.h.__updates.some((u) => u.patch.fontWeight === 600))