// iwb-plugin: 画布便利贴 —— 多行文本 + 字号/粗细调节 + 8 色背景,纯标注节点不连线。 // // 设计口径(用户 2026-11-24 确认): // - 不要"标题"概念:便签上方那条"便利贴"标签是宿主强制的标题带(hasTitle 写死), // 节点名注册成"便利贴"即可,用户不用再填标题字段。 // - 想当说明标题就把字号调大(无上限,A+ 每点一次 +2),想放多行正文就用小字(默认 14)。 // - 不连线:ports:{in:false,out:false}。 // - 节点缩放用宿主自带手柄(右下角),插件不设宽高。 // // 状态全部存 node.data(props.onUpdate 写回,随画布落盘): // text / fontSize / fontWeight / bgColor // 字号:A−/A+ 每步 ±2,下限 6、无上限(用户明确要求去掉最大字号限制); // 8 个预设色是运行时数据、色轮走系统颜色选择器,源码里不写死中性色(守门只卡字面量)。 ;(function (global) { 'use strict' if (!global.IWBPluginHost) return // 必须在脚本执行时就注入:面板可能瞬间挂载、节点可能从存档直接出现(面板从不 mount)。 function ensureStyle() { if (typeof document === 'undefined' || !document.createElement) return if (document.getElementById('iwb-sticky-style')) return var css = '' + '#iwb-sticky-style{}' + '[hidden]{display:none!important}' + '.iwb-sticky-panel{display:flex;flex-direction:column;gap:12px;font-size:13px;color:var(--iwb-text,#1c2333)}' + '.iwb-sticky-panel-tip{font-size:12px;line-height:1.6;color:var(--iwb-text2,#4d5668);opacity:.9}' + '.iwb-sticky-add{cursor:pointer;border:1px solid var(--iwb-brand,#6b7280);border-radius:8px;background:var(--iwb-layer2,#fff);color:var(--iwb-text,#1c2333);font-size:13px;padding:10px 14px;font-family:inherit}' + '.iwb-sticky-add:hover{background:var(--iwb-hover,#f0f1f4)}' + '.iwb-sticky-list{display:flex;flex-direction:column;gap:8px;margin-top:4px}' + '.iwb-sticky-row{display:flex;align-items:center;justify-content:space-between;gap:10px;padding:8px 10px;border:1px solid var(--iwb-border,#d5d9e0);border-radius:8px;background:var(--iwb-layer,#f5f6f8)}' + '.iwb-sticky-row-txt{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;flex:1;min-width:0}' + '.iwb-sticky-go{cursor:pointer;border:1px solid var(--iwb-border,#d5d9e0);border-radius:6px;background:var(--iwb-layer2,#fff);color:var(--iwb-text,#1c2333);font-size:12px;padding:4px 10px;white-space:nowrap;font-family:inherit}' + '.iwb-sticky-go:hover{border-color:var(--iwb-brand,#6b7280)}' + // ---- 节点体 ---- // 宿主容器 .iwb-plugin-node-body 是 flex 列且 padding:0;这里必须 flex:1 撑满, // 不能只写 height:100%(WKWebView 下百分比对 flex 项算不准,拖大后下半露出宿主底色)。 // 圆角对齐宿主节点:外框 .iwb-node 用 var(--iwb-node-radius)=14px 且有 1px 边框, // 纸片在框内 ⇒ 内圈半径 = 外圈 - 1px,选中时两层边缘才不会错开。 '.iwb-sticky-node{position:relative;display:flex;flex-direction:column;flex:1 1 auto;min-height:0;width:100%;box-sizing:border-box;border-radius:calc(var(--iwb-node-radius, 14px) - 1px);overflow:visible}' + '.iwb-sticky-tools{display:flex;align-items:center;gap:6px;padding:6px 8px;border-bottom:1px solid rgba(0,0,0,.08);flex:0 0 auto;position:relative;z-index:2}' + '.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)}' + // 调色圆点推到工具条最右端(右上),色板 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}' + '.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)}' + // 高度随内容自适应(贴字):不再 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}' + '.iwb-sticky-body::placeholder{opacity:.45}' + '.iwb-sticky-color-input{position:absolute;width:0;height:0;opacity:0;pointer-events:none}' try { var styleEl = el('style', { text: css }) styleEl.setAttribute('id', 'iwb-sticky-style') ;(document.head || document.documentElement).appendChild(styleEl) } catch (e) { /* 样式注入失败不影响功能 */ } } // 插件面板/节点体是真实 DOM(不是 React vDOM):createElement + addEventListener。 function el(tag, attrs, kids) { var node = document.createElement(tag) if (attrs) { for (var key in attrs) { var value = attrs[key] if (value === null || value === undefined) continue if (key === 'class' || key === 'className') node.className = value else if (key === 'text') node.textContent = String(value) else if (key === 'hidden') node.hidden = Boolean(value) else if (key.slice(0, 2) === 'on') node.addEventListener(key.slice(2).toLowerCase(), value) else node.setAttribute(key, String(value)) } } if (kids !== null && kids !== undefined) { var list = Array.isArray(kids) ? kids : [kids] for (var i = 0; i < list.length; i++) { if (list[i] === null || list[i] === undefined || list[i] === false) continue node.appendChild(typeof list[i] === 'string' ? document.createTextNode(list[i]) : list[i]) } } return node } var MIN_SIZE = 6 // 下限(用户只要求取消上限,下限保留防误操作缩到看不见) var SIZE_STEP = 2 // A−/A+ 每步 ±2 var DEFAULT_SIZE = 14 var PALETTE = [ { name: '黄', value: '#FCE8B0' }, { name: '粉', value: '#F4C0D1' }, { name: '橙', value: '#EF9F27' }, { name: '绿', value: '#9FE1CB' }, { name: '蓝', value: '#B5D4F4' }, { name: '浅紫', value: '#CECBF6' }, { name: '玫红', value: '#ED93B1' }, { name: '灰', value: '#D3D1C7' }, ] var DEFAULT_BG = '#FCE8B0' // 只保证是 >= 下限的有限数,不再钳到闭集/上限;A+ 可无限放大。 function normSize(v) { var n = Number(v) if (!isFinite(n) || n < MIN_SIZE) return DEFAULT_SIZE return Math.round(n) } // 依据背景亮度取黑/白文字(运行时计算,源码里不写死中性色)。 function textColorOf(hex) { var m = /^#?([0-9a-f]{6})$/i.exec(String(hex || '')) if (!m) return '#1c2333' var n = parseInt(m[1], 16) var r = (n >> 16) & 255, g = (n >> 8) & 255, b = n & 255 // 相对亮度(sRGB 简化) var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255 return lum < 0.55 ? '#ffffff' : '#1c2333' } // 脚本执行时就注入(不等 setup / mount,存档直入画布也有样式)。 ensureStyle() var ctxRef = null // ---------------- 节点体 ---------------- // 宿主契约:render(node, props, host) 往 host 这个真实 DOM 里画,不是返回 vDOM。 // host 由宿主固定、不重建;插件自己缓存骨架 + 幂等同步,编辑中的 textarea 不覆盖值。 function renderSticky(node, props, host) { ensureStyle() var data = (node && node.data) || {} var st = host.__sticky if (!st) { host.textContent = '' st = host.__sticky = buildSkeleton(host, props) } st.node = node st.props = props syncSkeleton(st, data, node) } function buildSkeleton(host, props) { var root = el('div', { className: 'iwb-sticky-node' }) var tools = el('div', { className: 'iwb-sticky-tools' }) var minus = el('button', { className: 'iwb-sticky-btn', type: 'button', title: '缩小字号', 'data-act': 'minus' }, '−') var plus = el('button', { className: 'iwb-sticky-btn', type: 'button', title: '放大字号', 'data-act': 'plus' }, '+') var bold = el('button', { className: 'iwb-sticky-btn', type: 'button', title: '切换粗细', 'data-act': 'bold' }, 'B') var dot = el('button', { className: 'iwb-sticky-dot', type: 'button', title: '背景色', 'data-act': 'palette' }) var pal = el('div', { className: 'iwb-sticky-pal', hidden: true }) var colorInput = el('input', { type: 'color', className: 'iwb-sticky-color-input', 'data-act': 'color' }) // rows=1 是关键:textarea 默认 rows=2,fitBody() 把高度设 auto 读 scrollHeight 时, // 单行文字也会被算成两行高(用户报的“预留了一行文本高度”)。显式 1 行后, // scrollHeight 才等于真实内容行数——多行文本仍会自动换行、高度跟着涨。 var body = el('textarea', { className: 'iwb-sticky-body', rows: '1', spellcheck: 'false', placeholder: '双击编辑…' }) // 对齐宿主文本节点:默认只读。宿主 CSS `.iwb-node textarea[readonly]{pointer-events:none}` // ⇒ 单击穿透给节点卡片(选中/拖动);双击由本插件在 root 上接管进入编辑。 body.readOnly = true body.setAttribute('readonly', '') tools.appendChild(minus); tools.appendChild(plus); tools.appendChild(bold); tools.appendChild(dot) // 色板挂在工具条内(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, pal: pal, colorInput: colorInput, body: body, data: null, palOpen: false, editing: false } function onTool(e) { var t = e.target || {} var act = t.getAttribute && t.getAttribute('data-act') if (!act) return e.stopPropagation && e.stopPropagation() var d = (st.node && st.node.data) || {} var up = st.props && st.props.onUpdate var id = st.node && st.node.id if (!up || !id) return if (act === 'minus' || act === 'plus') { var cur = normSize(d.fontSize == null ? DEFAULT_SIZE : d.fontSize) var next = act === 'minus' ? Math.max(MIN_SIZE, cur - SIZE_STEP) : cur + SIZE_STEP if (next !== cur) up(id, { fontSize: next }) } else if (act === 'bold') { up(id, { fontWeight: d.fontWeight === 600 ? 400 : 600 }) } else if (act === 'palette') { st.palOpen = !st.palOpen pal.hidden = !st.palOpen } } tools.addEventListener('click', onTool) // 色板里的色块 + 色轮(事件委托到 pal) for (var i = 0; i < PALETTE.length; i++) { var sw = el('button', { type: 'button', className: 'iwb-sticky-pal-sw', 'data-bg': PALETTE[i].value, title: PALETTE[i].name, style: 'background:' + PALETTE[i].value }) pal.appendChild(sw) } var wheelWrap = el('div', { className: 'iwb-sticky-wheel' }) var wheel = el('button', { type: 'button', className: 'iwb-sticky-wheel-btn', title: '自定义颜色', 'data-act': 'wheel' }) wheelWrap.appendChild(wheel); pal.appendChild(wheelWrap) function onPal(e) { var t = e.target || {} e.stopPropagation && e.stopPropagation() var up = st.props && st.props.onUpdate var id = st.node && st.node.id if (!up || !id) return var bg = t.getAttribute && t.getAttribute('data-bg') if (bg) { up(id, { bgColor: bg }) st.palOpen = false; pal.hidden = true return } var act = t.getAttribute && t.getAttribute('data-act') if (act === 'wheel') { // 点开系统取色器后这个浮层就完成使命:无论最终确认还是取消都先收起, // 避免取色器关闭后色板还压在便签上(用户报的“自定义调色后面板不退出”)。 st.palOpen = false; pal.hidden = true try { colorInput.click() } catch (e2) { /* noop */ } } } pal.addEventListener('click', onPal) function onColor(e) { var up = st.props && st.props.onUpdate var id = st.node && st.node.id if (up && id && e.target && e.target.value) up(id, { bgColor: e.target.value }) // 自定义色确认后兜底再收一次(个别平台 change 先于上面的收起逻辑到达)。 st.palOpen = false; pal.hidden = true } colorInput.addEventListener('change', onColor) // ---- 正文编辑状态机(照宿主文本节点:双击进编辑、失焦退出、只读态单击穿透)---- function setEditing(on) { st.editing = on body.readOnly = !on if (on) { try { body.removeAttribute('readonly') } catch (e2) { /* noop */ } } else body.setAttribute('readonly', '') body.className = 'iwb-sticky-body' + (on ? ' iwb-sticky-editing' : '') } // 双击进入编辑:绑在 root 上(不是 readonly textarea —— 它 pointer-events:none 收不到双击)。 // 宿主卡片 onDoubleClick 只处理内置文本类型,plugin: 节点它不接管,所以这里自己 stopPropagation。 function onDblClick(e) { // 工具条/色板/按钮上的双击不进编辑 var t = e.target || {} if (t.getAttribute && t.getAttribute('data-act')) return if (t.getAttribute && t.getAttribute('data-bg')) return e.stopPropagation && e.stopPropagation() setEditing(true) try { body.focus() } catch (e2) { /* noop */ } } root.addEventListener('dblclick', onDblClick) // 编辑态:按下/按键不冒泡给宿主(否则会拖节点 / 触发画布快捷键);失焦退出编辑。 ;['pointerdown', 'mousedown'].forEach(function (ev) { body.addEventListener(ev, function (e) { if (st.editing) e.stopPropagation && e.stopPropagation() // 只读态不拦:让宿主选中节点 + 拖动 }) }) body.addEventListener('keydown', function (e) { e.stopPropagation && e.stopPropagation() if (e.key === 'Escape') { try { body.blur() } catch (e2) { /* noop */ } } }) body.addEventListener('blur', function () { setEditing(false) }) // 输入即写回(防抖交给宿主的 commitCanvas),编辑中不重建 DOM 故不失焦。 function onInput(e) { var up = st.props && st.props.onUpdate var id = st.node && st.node.id if (up && id) up(id, { text: e.target.value }) e.stopPropagation && e.stopPropagation() } body.addEventListener('input', onInput) st._listeners = { tools: onTool, pal: onPal, color: onColor, input: onInput, dblclick: onDblClick, setEditing: setEditing } 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 } // 宿主 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: // - 插件自动跟随内容写出的 h:内容长高就涨、缩回就降(v1.0.6 修的「两行退回一行不缩」); // - 用户拖手柄改出的 h:h 相对上一帧变了 ⇒ 尊重手动尺寸,只在内容超过它时顶一下。 // 首次遇到的旧大 h 校正一次(staleH),之后进入自动跟随。 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 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 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 var text = data.text == null ? '' : String(data.text) var color = textColorOf(bg) // 幂等:只在值真的变了才写 DOM,避免重建/覆盖正在编辑的 textarea。 if (st.root.style.background !== bg) st.root.style.background = bg if (st.root.style.color !== color) st.root.style.color = color 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) 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' : '') } if (st.dot.style.background !== bg) st.dot.style.background = bg } // ---------------- 面板 ---------------- var ctxForPanel = null var noteList = [] function refreshList() { if (!ctxForPanel) return var host = ctxForPanel.__stickyPanelRoot if (!host || typeof host.innerHTML !== 'undefined' && !host.innerHTML && !host.appendChild) return var list = host.querySelector ? host.querySelector('.iwb-sticky-list') : null if (!list) return // 尽力从画布读回便利贴节点(只读画布能力给了 getNodes) var nodes = [] try { var api = ctxRef && ctxRef.canvas if (api && typeof api.getNodes === 'function') nodes = api.getNodes() || [] } catch (e) { nodes = [] } var mine = nodes.filter(function (n) { return String(n.type || '').indexOf('plugin:sticky:') === 0 }) // 用最近插入的本地记录补足(getNodes 快照不含自定义状态没关系,这里只显示文本) var seen = {} var rows = [] mine.concat(noteList).forEach(function (n) { var key = n.id if (!key || seen[key]) return seen[key] = 1 var text = String((n.data && n.data.text) || '').replace(/\s+/g, ' ').slice(0, 30) || '(空便利贴)' rows.push({ id: key, text: text }) }) while (list.firstChild) list.removeChild(list.firstChild) rows.slice(-20).forEach(function (r) { var row = el('div', { className: 'iwb-sticky-row' }) row.appendChild(el('span', { className: 'iwb-sticky-row-txt' }, r.text)) var go = el('button', { className: 'iwb-sticky-go', type: 'button' }, '定位') go.addEventListener('click', function () { try { if (ctxRef && ctxRef.canvas && typeof ctxRef.canvas.focusNode === 'function') ctxRef.canvas.focusNode(r.id) } catch (e) { /* noop */ } }) row.appendChild(go) list.appendChild(row) }) } function mountPanel(host, ctx) { ensureStyle() ctxForPanel = ctx host.textContent = '' host.style.minWidth = '0'; host.style.minHeight = '0'; host.style.height = '100%' var wrap = el('div', { className: 'iwb-sticky-panel' }) wrap.appendChild(el('div', { className: 'iwb-sticky-panel-tip' }, '在画布上放一张便利贴:字调大当说明标题,字调小放多行正文。点右下角拖动手柄可改变便签大小。')) var add = el('button', { className: 'iwb-sticky-add', type: 'button' }, '+ 插入便利贴') add.addEventListener('click', function () { insertNote() }) wrap.appendChild(add) var list = el('div', { className: 'iwb-sticky-list' }) wrap.appendChild(list) host.appendChild(wrap) ctxForPanel.__stickyPanelRoot = host refreshList() } function insertNote() { var api = ctxRef && ctxRef.canvas if (!api || typeof api.createNode !== 'function') return null var id = api.createNode('plugin:sticky:sticky', null, null, { text: '', fontSize: DEFAULT_SIZE, fontWeight: 400, bgColor: DEFAULT_BG, }) if (id) { noteList.push({ id: id, data: { text: '' } }) refreshList() } return id } function dispose() { ctxRef = null ctxForPanel = null noteList = [] } global.IWBPluginHost.register({ id: 'sticky', name: '便利贴', version: '1.0.7', apiVersion: 1, capabilities: ['panels', 'nodeTypes', 'canvas'], }, function (ctx) { ctxRef = ctx if (ctx.nodes && typeof ctx.nodes.register === 'function') { ctx.nodes.register({ id: 'sticky', title: '便利贴', defaults: {}, ports: { in: false, out: false }, height: 180, render: renderSticky, }) } if (ctx.ui && typeof ctx.ui.registerPanel === 'function') { ctx.ui.registerPanel({ id: 'sticky', title: '便利贴', icon: 'note', mount: mountPanel, }) } return { dispose: dispose } }) })(typeof window !== 'undefined' ? window : globalThis)