diff --git a/promptlib/package.json b/promptlib/package.json index 288bb27..05383e2 100644 --- a/promptlib/package.json +++ b/promptlib/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/promptlib", "private": true, - "version": "1.6.23", + "version": "1.7.0", "description": "提示词库:右栏提示词列表(搜索/分组/收藏/新建编辑/插入画布/复制/删除),数据由宿主 promptlib 能力提供", "ibwPlugin": { "id": "promptlib", diff --git a/promptlib/src/index.js b/promptlib/src/index.js index 370f17d..7f19477 100644 --- a/promptlib/src/index.js +++ b/promptlib/src/index.js @@ -968,54 +968,69 @@ // ===== v1.5.0:变量编辑器(新建/编辑弹窗用)===== // 变量来自正文的 {名字}:自动检测、可改标签/默认值/说明;留空时渲染保留 {原名}。 - function makeVarsEditor (text, initial) { + function makeVarsEditor (text, initial, onTextChange) { var box = el('div', { class: 'iwb-plp-vars' }) - var head = el('div', { class: 'iwb-plp-vhead' }) - head.appendChild(el('span', { text: '变量' })) - head.appendChild(el('span', { class: 'iwb-plp-vhint', text: '从正文 {…} 自动检测,可改标签 / 默认值 / 说明' })) - var chips = el('div', { class: 'iwb-plp-vchips' }) + var head = el('div', { class: 'iwb-plp-vhead' }, + el('span', { text: '变量' }), + el('span', { class: 'iwb-plp-vhint', text: '与正文双向同步 · 点 i 写说明' })) var rows = el('div', { class: 'iwb-plp-vrows' }) - var prev = el('div', { class: 'iwb-plp-vprev' }) - box.appendChild(head); box.appendChild(chips); box.appendChild(rows); box.appendChild(prev) + box.appendChild(head); box.appendChild(rows) var list = [] - if (Array.isArray(initial) && initial.length) list = initial.map(function (v) { return { key: String(v.key || ''), label: String(v.label || v.key || ''), desc: String(v.desc || ''), def: String(v.def || '') } }) + var open = {} + var norm = function (v) { return { key: String(v.key || ''), desc: String(v.desc || ''), def: String(v.def || '') } } + if (Array.isArray(initial) && initial.length) list = initial.filter(function (v) { return v && v.key }).map(norm) function values () { var m = {} list.forEach(function (v) { if (v.def) m[v.key] = v.def }) return m } function paint () { - clear(chips); clear(rows) + clear(rows) list.forEach(function (v) { - var chip = el('span', { class: 'iwb-plp-vchip', text: v.label || v.key }) - var x = el('i', { class: 'iwb-plp-vx', text: '×', title: '忽略这个变量' }) - x.addEventListener('click', function () { list = list.filter(function (z) { return z.key !== v.key }); paint() }) - chip.appendChild(x) - chips.appendChild(chip) - var row = el('div', { class: 'iwb-plp-vrow' }) - row.appendChild(el('span', { class: 'iwb-plp-vk', text: '{' + v.key + '}' })) - var def = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: '默认值(可留空)' }) - def.value = v.def || '' - def.addEventListener('input', function () { v.def = def.value; paintPrev() }) - var desc = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: '说明(如:如「11.9」)' }) - desc.value = v.desc || '' - desc.addEventListener('input', function () { v.desc = desc.value }) - row.appendChild(def); row.appendChild(desc) - rows.appendChild(row) + var item = el('div', { class: 'iwb-plp-vitem' }) + var row = el('div', { class: 'iwb-plp-vrow2' }) + var nameIn = el('input', { class: 'iwb-input iwb-plp-vname', placeholder: '变量名' }) + nameIn.value = v.key + var iBtn = el('button', { class: 'iwb-plp-vi' + (open[v.key] ? ' is-open' : ''), type: 'button', title: '说明', text: 'i' }) + iBtn.addEventListener('click', function () { open[v.key] = !open[v.key]; paint() }) + nameIn.addEventListener('blur', function () { renameTo(v, nameIn.value) }) + nameIn.addEventListener('keydown', function (e) { if (e.key === 'Enter') { e.preventDefault(); nameIn.blur() } }) + row.appendChild(nameIn); row.appendChild(iBtn) + item.appendChild(row) + if (open[v.key]) { + var d = el('input', { class: 'iwb-input iwb-plp-vdesc', placeholder: '说明(会作为节点输入框的占位提示)' }) + d.value = v.desc || '' + d.addEventListener('input', function () { v.desc = d.value }) + item.appendChild(d) + } + rows.appendChild(item) }) - if (!list.length) rows.appendChild(el('div', { class: 'iwb-hint', text: '正文里没有 {变量};写入 {产品名} 这样的大括号即会被识别。' })) - paintPrev() + if (!list.length) rows.appendChild(el('div', { class: 'iwb-hint', text: '正文里没有 {变量};输入 {产品名} 即会被识别。' })) + } + function renameTo (v, raw) { + var next = String(raw || '').replace(/[{}]/g, '').trim() + if (!next || next === v.key) { paint(); return } + if (list.some(function (x) { return x !== v && x.key === next })) { notify('变量名重复:' + next, 'err'); paint(); return } + var old = v.key + v.key = next + if (open[old] !== undefined) { open[next] = open[old]; delete open[old] } + var nt = String(text || '').split('{' + old + '}').join('{' + next + '}') + text = nt + if (typeof onTextChange === 'function') onTextChange(nt) + paint() } - function paintPrev () { prev.textContent = fillTpl(text, values()) } function sync (nextText) { text = String(nextText || '') var found = detectVars(text) var old = {} list.forEach(function (v) { old[v.key] = v }) - list = found.map(function (v) { return old[v.key] ? Object.assign({}, v, old[v.key]) : v }) + list = found.map(function (v) { + var prev = old[v.key] + return { key: v.key, desc: prev ? prev.desc : v.desc, def: prev ? prev.def : '' } + }) paint() } - if (!list.length) list = detectVars(text) + if (!list.length) list = detectVars(text).map(norm) paint() return { node: box, sync: sync, list: function () { return list }, values: values } } @@ -1332,7 +1347,7 @@ var dlg = openDialog({ key: 'detail', title: p.name || '未命名', - width: 880, height: 640, + width: 1180, height: 720, actions: [edit, closeBtn], build: function (bodyEl, footEl) { var cov = el('div', { class: 'iwb-plp-dcov' }) @@ -1341,22 +1356,18 @@ right.appendChild(el('div', { class: 'iwb-hint', text: [p.group || '未分组'].concat(p.tags || []).join(' · ') + ' · ' + String(p.text || '').length + ' 字' })) var vlist = varsOf(p) var vals = {} - var txt = el('div', { class: 'iwb-plp-dtext', text: p.text || '' }) + var txt = el('div', { class: 'iwb-plp-dtext' }) + ;(function paintText () { + var str = String(p.text || '') + var re = /[{]([^{}]{1,24})[}]/g, last = 0, m + while ((m = re.exec(str))) { + if (m.index > last) txt.appendChild(document.createTextNode(str.slice(last, m.index))) + txt.appendChild(el('span', { class: 'iwb-plp-dvar', text: m[0] })) + last = m.index + m[0].length + } + if (last < str.length) txt.appendChild(document.createTextNode(str.slice(last))) + })() function filled () { return vlist.length ? fillTpl(p.text, vals) : String(p.text || '') } - if (vlist.length) { - var vbox = el('div', { class: 'iwb-plp-dvars' }) - vlist.forEach(function (v) { - var row = el('div', { class: 'iwb-plp-vrow' }) - row.appendChild(el('span', { class: 'iwb-plp-vk', text: v.label || v.key })) - var inp = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: v.desc || v.def || '' }) - inp.value = v.def || '' - inp.addEventListener('input', function () { vals[v.key] = inp.value; txt.textContent = filled() }) - row.appendChild(inp) - vbox.appendChild(row) - }) - right.appendChild(vbox) - right.appendChild(el('div', { class: 'iwb-hint', text: '预览(留空的变量保留 {原名})' })) - } right.appendChild(txt) bodyEl.appendChild(cov); bodyEl.appendChild(right) var del = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-danger', text: '删除' }) @@ -1456,7 +1467,7 @@ left.appendChild(el('label', { class: 'iwb-plp-lrow' }, el('span', { text: '标签' }), tagsIn)) var textIn = el('textarea', { class: 'iwb-input iwb-plp-etext', placeholder: '提示词正文' }) textIn.value = draft.text || '' - var varEd = makeVarsEditor(draft.text || '', draft.vars || []) + var varEd = makeVarsEditor(draft.text || '', draft.vars || [], function (nt) { textIn.value = nt; dirty = true }) var syncTimer = 0 textIn.addEventListener('input', function () { dirty = true @@ -1482,8 +1493,25 @@ var mid = el('div', { class: 'iwb-plp-emid' }) mid.appendChild(el('div', { class: 'iwb-plp-etool' }, insVarBtn, el('span', { class: 'iwb-hint', text: '在光标处插入 {变量},并自动聚焦变量名' }))) mid.appendChild(textIn) - mid.appendChild(varEd.node) - bodyEl.appendChild(left); bodyEl.appendChild(mid) + var split = el('div', { class: 'iwb-plp-esplit', title: '拖动调整变量列宽度' }) + var right = el('div', { class: 'iwb-plp-eright' }) + right.appendChild(varEd.node) + split.addEventListener('pointerdown', function (e) { + e.preventDefault() + var startX = e.clientX + var startW = right.getBoundingClientRect().width + function mv (ev) { + var w = Math.max(190, Math.min(460, startW + (startX - ev.clientX))) + right.style.width = w + 'px' + } + function up () { + document.removeEventListener('pointermove', mv, true) + document.removeEventListener('pointerup', up, true) + } + document.addEventListener('pointermove', mv, true) + document.addEventListener('pointerup', up, true) + }) + bodyEl.appendChild(left); bodyEl.appendChild(mid); bodyEl.appendChild(split); bodyEl.appendChild(right) var dupBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '另存副本' }) var insBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '插入画布' }) footEl.appendChild(dupBtn); footEl.appendChild(el('span', { class: 'iwb-drawer-spacer' })); footEl.appendChild(insBtn) @@ -1656,7 +1684,7 @@ global.IWBPluginHost.register({ id: 'promptlib', name: '提示词库', - version: '1.6.23', + version: '1.7.0', apiVersion: 1, capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvas', 'canvasWrite'], data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 }, @@ -1788,7 +1816,7 @@ defs.forEach(function (v) { var row = el('div', { class: 'iwb-plp-nrow' }) row.appendChild(el('span', { class: 'iwb-plp-nk', text: v.label || v.key })) - var inp = el('input', { class: 'iwb-input iwb-plp-nin', placeholder: v.def || ('填写 ' + (v.label || v.key)) }) + var inp = el('input', { class: 'iwb-input iwb-plp-nin', placeholder: v.desc || v.def || ('填写 ' + (v.label || v.key)) }) inp.value = String((d.vars && d.vars[v.key]) || v.def || '') inp.addEventListener('input', function () { scheduleUpdate(st, d) }) st.inputs[v.key] = inp diff --git a/promptlib/styles.css b/promptlib/styles.css index 3232964..17455f1 100644 --- a/promptlib/styles.css +++ b/promptlib/styles.css @@ -396,3 +396,20 @@ body.iwb-plp-dragging, body.iwb-plp-dragging *{user-select:none!important;-webki /* v1.6.23:放置指示改为灰色小方框 */ .iwb-plp-placebox{border:2px dashed #9aa3ad;border-radius:10px;background:rgba(122,130,142,.10);box-shadow:none} .iwb-plp-placebox-t{font-size:11px;color:#6b7280;background:rgba(255,255,255,.75);padding:2px 8px} +/* v1.7.0:编辑面板三栏 + 可拖宽;详情封面放大、变量高亮 */ +.iwb-plp-eleft{flex:0 0 236px;width:236px} +.iwb-plp-emid{flex:1 1 auto;min-width:220px;display:flex;flex-direction:column;min-height:0} +.iwb-plp-emid .iwb-plp-etext{flex:1 1 auto;min-height:0;resize:none} +.iwb-plp-esplit{flex:0 0 10px;cursor:col-resize;display:flex;align-items:center;justify-content:center;border-radius:6px;background:var(--iwb-plugin-inset,#f3f4f7)} +.iwb-plp-esplit:hover{background:var(--iwb-plugin-hover,#e9ecf1)} +.iwb-plp-eright{flex:0 0 auto;width:300px;min-width:190px;max-width:460px;display:flex;flex-direction:column;min-height:0;background:var(--iwb-plugin-surface,#fafbfc);border:1px solid var(--iwb-border,#e6e8ec);border-radius:10px;padding:8px} +.iwb-plp-vars{flex:1 1 auto;min-height:0;display:flex;flex-direction:column} +.iwb-plp-vrows{flex:1 1 auto;min-height:0;overflow:auto} +.iwb-plp-vitem{padding:4px 0;border-bottom:1px dashed var(--iwb-border,#eceef2)} +.iwb-plp-vrow2{display:flex;align-items:center;gap:6px} +.iwb-plp-vname{flex:1 1 auto;min-width:0;height:32px} +.iwb-plp-vi{flex:0 0 auto;width:24px;height:24px;padding:0;border-radius:50%;border:1px solid var(--iwb-border,#dfe3e9);background:#fff;color:#9aa1ac;font-size:11px;line-height:22px;text-align:center} +.iwb-plp-vi.is-open{background:#f1ecff;border-color:#d8cbff;color:#5a3fd0} +.iwb-plp-vdesc{width:100%;height:28px;margin-top:5px;border-style:dashed;background:#fbfcfd;font-size:11.5px;color:#6b7280} +.iwb-plp-dcov{flex:0 0 auto;width:180px;height:180px;border-radius:12px;overflow:hidden} +.iwb-plp-dvar{background:#f1ecff;color:#5a3fd0;border-radius:4px;padding:0 4px;font-weight:500}