- 内存:首帧前预热显示缩略图(避免整屏解码原图)、带图节点视口裁剪 240→80、
DISPLAY_THUMB_MAX 640→1024、放大后不再切原图解码(上限 2× 缩略图长边)
- 参考图:修复 url 引用对象未解析导致的 [object Object] 破图;失效图改为明确占位(双击重新导入)
- 新增 {__cache}→{__blob} 迁移(只迁移可读字节,读不到不动)
- 图片节点按图片比例收边、内边距收窄
- 诊断:帧时长探针(正式包默认关闭,IWB_PERF_PROBE=1 才启用)、edge-drop 换板误报过滤、缓存计数
- 插件 sticky 1.0.10:移除左侧栏面板
428 lines
23 KiB
JavaScript
428 lines
23 KiB
JavaScript
// 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 }
|
||
|
||
// 宽度变化 → 重新算行数(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。
|
||
// 设 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) {
|
||
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 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
|
||
}
|
||
|
||
// 宿主 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
|
||
}
|
||
|
||
// ⚠️ 不要给便签加左侧栏面板(用户 2026-11-29 再次确认,此前 11-28 做过一次又被改回来):
|
||
// 一旦向宿主登记「面板」,资源面板就会多出一个「便利贴」标签页。插入便利贴走画布右键菜单,不走面板。
|
||
|
||
function dispose() {
|
||
ctxRef = null
|
||
}
|
||
|
||
global.IWBPluginHost.register({
|
||
id: 'sticky',
|
||
name: '便利贴',
|
||
version: '1.0.10',
|
||
apiVersion: 1,
|
||
capabilities: ['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,
|
||
})
|
||
}
|
||
return { dispose: dispose }
|
||
})
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|