This commit is contained in:
1 parent
8670619690
commit
57efde3eea
3 files changed
+245
-6
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/promptlib",
|
||||
"private": true,
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "提示词库:右栏提示词列表(搜索/分组/收藏/新建编辑/插入画布/复制/删除),数据由宿主 promptlib 能力提供",
|
||||
"ibwPlugin": {
|
||||
"id": "promptlib",
|
||||
|
||||
+216
-5
@@ -410,7 +410,7 @@
|
||||
var sep = el('div', { class: 'iwb-ctx-sep' })
|
||||
menu.appendChild(sep)
|
||||
var ed = el('button', { class: 'iwb-ctx-item', text: '编辑…' })
|
||||
ed.addEventListener('click', function () { closeMenu(); openEditor(p) })
|
||||
ed.addEventListener('click', function () { closeMenu(); openEditModal(p, false) })
|
||||
menu.appendChild(ed)
|
||||
document.body.appendChild(menu)
|
||||
menuEl = menu
|
||||
@@ -464,7 +464,7 @@
|
||||
var back = el('button', { class: 'iwb-btn iwb-btn-sm', text: '‹ 返回' })
|
||||
back.addEventListener('click', backToList)
|
||||
var edit = el('button', { class: 'iwb-btn iwb-btn-sm', text: '编辑' })
|
||||
edit.addEventListener('click', function () { openEditor(p) })
|
||||
edit.addEventListener('click', function () { openEditModal(p, false) })
|
||||
head.appendChild(back); head.appendChild(el('span', { class: 'iwb-drawer-spacer' })); head.appendChild(edit)
|
||||
var foot = el('div', { class: 'iwb-plp-dfoot' })
|
||||
var ins = el('button', { class: 'iwb-btn iwb-btn-sm', text: '插入画布' })
|
||||
@@ -483,6 +483,217 @@
|
||||
scrollBox.appendChild(head); scrollBox.appendChild(el('div', { class: 'iwb-row iwb-plp-dtop' }, thumb, meta)); scrollBox.appendChild(textEl); scrollBox.appendChild(foot)
|
||||
}
|
||||
|
||||
|
||||
// ===== v1.2.0:可拖动 / 可缩放弹窗(详情 & 编辑)=====
|
||||
function readDlg (key, w, h) {
|
||||
try {
|
||||
var raw = global.localStorage.getItem('iwb_plp_dlg_' + key)
|
||||
if (raw) { var o = JSON.parse(raw); if (o && o.w && o.h) return o }
|
||||
} catch (e) { /* ignore */ }
|
||||
var vw = global.innerWidth || 1440
|
||||
var vh = global.innerHeight || 900
|
||||
var W = Math.min(w, vw - 80), H = Math.min(h, vh - 80)
|
||||
return { x: Math.round((vw - W) / 2), y: Math.round((vh - H) / 2), w: W, h: H }
|
||||
}
|
||||
function saveDlg (key, o) { try { global.localStorage.setItem('iwb_plp_dlg_' + key, JSON.stringify(o)) } catch (e) { /* ignore */ } }
|
||||
|
||||
function openDialog (opts) {
|
||||
var key = opts.key
|
||||
var box = readDlg(key, opts.width || 880, opts.height || 640)
|
||||
var mask = el('div', { class: 'iwb-modal-mask iwb-plp-mask' })
|
||||
var card = el('div', { class: 'iwb-modal iwb-plp-dialog iwb-plp-dialog-' + key })
|
||||
card.style.position = 'fixed'; card.style.margin = '0'
|
||||
var head = el('div', { class: 'iwb-plp-dhead iwb-plp-dragh' })
|
||||
var grip = el('span', { class: 'iwb-plp-grip', title: '按住拖动' })
|
||||
for (var gi = 0; gi < 6; gi++) grip.appendChild(el('i'))
|
||||
head.appendChild(grip)
|
||||
head.appendChild(el('span', { class: 'iwb-toolbar-title', text: opts.title || '' }))
|
||||
head.appendChild(el('span', { class: 'iwb-drawer-spacer' }))
|
||||
;(opts.actions || []).forEach(function (b) { head.appendChild(b) })
|
||||
var bodyEl = el('div', { class: 'iwb-plp-dbody' })
|
||||
var footEl = el('div', { class: 'iwb-plp-dfooter' })
|
||||
var grip2 = el('div', { class: 'iwb-plp-resize', title: '拖动调整大小' })
|
||||
card.appendChild(head); card.appendChild(bodyEl); card.appendChild(footEl); card.appendChild(grip2)
|
||||
mask.appendChild(card)
|
||||
document.body.appendChild(mask)
|
||||
var live = { x: box.x, y: box.y, w: box.w, h: box.h }
|
||||
function apply () {
|
||||
card.style.left = live.x + 'px'; card.style.top = live.y + 'px'
|
||||
card.style.width = live.w + 'px'; card.style.height = live.h + 'px'
|
||||
}
|
||||
head.addEventListener('pointerdown', function (e) {
|
||||
if (e.target && e.target.tagName === 'BUTTON') return
|
||||
e.preventDefault(); e.stopPropagation()
|
||||
var sx = e.clientX, sy = e.clientY, ox = live.x, oy = live.y
|
||||
function mv (ev) {
|
||||
var vw = global.innerWidth || 1440, vh = global.innerHeight || 900
|
||||
live.x = Math.max(-live.w + 120, Math.min(vw - 120, ox + ev.clientX - sx))
|
||||
live.y = Math.max(0, Math.min(vh - 60, oy + ev.clientY - sy))
|
||||
apply()
|
||||
}
|
||||
function up () { global.removeEventListener('pointermove', mv); global.removeEventListener('pointerup', up); saveDlg(key, live) }
|
||||
global.addEventListener('pointermove', mv); global.addEventListener('pointerup', up)
|
||||
})
|
||||
grip2.addEventListener('pointerdown', function (e) {
|
||||
e.preventDefault(); e.stopPropagation()
|
||||
var sx = e.clientX, sy = e.clientY, ow = live.w, oh = live.h
|
||||
function mv (ev) {
|
||||
live.w = Math.max(640, Math.min((global.innerWidth || 1440) - 40, ow + ev.clientX - sx))
|
||||
live.h = Math.max(420, Math.min((global.innerHeight || 900) - 40, oh + ev.clientY - sy))
|
||||
apply()
|
||||
}
|
||||
function up () { global.removeEventListener('pointermove', mv); global.removeEventListener('pointerup', up); saveDlg(key, live) }
|
||||
global.addEventListener('pointermove', mv); global.addEventListener('pointerup', up)
|
||||
})
|
||||
mask.addEventListener('pointerdown', function (e) { if (e.target === mask) close() })
|
||||
function onKey (e) {
|
||||
if (e.key === 'Escape') { e.stopPropagation(); close() } else if ((e.metaKey || e.ctrlKey) && e.key === 'Enter' && opts.onSaveKey) { e.preventDefault(); opts.onSaveKey() }
|
||||
}
|
||||
function close () {
|
||||
saveDlg(key, live)
|
||||
if (mask.parentNode) mask.parentNode.removeChild(mask)
|
||||
document.removeEventListener('keydown', onKey, true)
|
||||
}
|
||||
document.addEventListener('keydown', onKey, true)
|
||||
opts.build(bodyEl, footEl, close)
|
||||
apply()
|
||||
return { close: close, card: card, body: bodyEl, foot: footEl }
|
||||
}
|
||||
|
||||
function paintCoverInto (node, p) {
|
||||
node.textContent = ''
|
||||
var url = coverOf(p)
|
||||
node.style.background = url ? 'var(--iwb-plugin-inset)' : gradOf(p)
|
||||
if (url) node.appendChild(el('img', { class: 'iwb-plp-cover', src: url, alt: '' }))
|
||||
}
|
||||
|
||||
function openDetailModal (p) {
|
||||
var edit = el('button', { class: 'iwb-btn iwb-btn-sm', text: '编辑' })
|
||||
var closeBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '关闭', text: '✕' })
|
||||
var dlg = openDialog({
|
||||
key: 'detail',
|
||||
title: p.name || '未命名',
|
||||
width: 880, height: 640,
|
||||
actions: [edit, closeBtn],
|
||||
build: function (bodyEl, footEl) {
|
||||
var cov = el('div', { class: 'iwb-plp-dcov' })
|
||||
paintCoverInto(cov, p)
|
||||
var right = el('div', { class: 'iwb-plp-dright' })
|
||||
right.appendChild(el('div', { class: 'iwb-hint', text: [p.group || '未分组'].concat(p.tags || []).join(' · ') + ' · ' + String(p.text || '').length + ' 字' }))
|
||||
var txt = el('div', { class: 'iwb-plp-dtext', text: p.text || '' })
|
||||
right.appendChild(txt)
|
||||
bodyEl.appendChild(cov); bodyEl.appendChild(right)
|
||||
var del = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-danger', text: '删除' })
|
||||
var cp = el('button', { class: 'iwb-btn iwb-btn-sm', text: '复制全文' })
|
||||
var ins = el('button', { class: 'iwb-btn iwb-btn-sm', text: '插入画布' })
|
||||
del.addEventListener('click', function () {
|
||||
ask('删除提示词「' + (p.name || '未命名') + '」?', { title: '删除提示词', okText: '删除' }).then(function (ok) {
|
||||
if (!ok) return
|
||||
dlg.close()
|
||||
save(prompts.filter(function (x) { return x.id !== p.id })).then(function () { notify('已删除') })
|
||||
})
|
||||
})
|
||||
cp.addEventListener('click', function () { Promise.resolve(api.copy(p.text || '')).then(function (r) { if (r && r.ok === false) notify('复制失败', 'err'); else notify('已复制全文') }) })
|
||||
ins.addEventListener('click', function () { Promise.resolve(api.insert(p.text || '', p.name || '')).then(function () { notify('已插入画布:' + (p.name || '提示词')) }).catch(function () { notify('插入画布失败', 'err') }) })
|
||||
footEl.appendChild(del); footEl.appendChild(el('span', { class: 'iwb-drawer-spacer' })); footEl.appendChild(cp); footEl.appendChild(ins)
|
||||
},
|
||||
})
|
||||
edit.addEventListener('click', function () { dlg.close(); openEditModal(p, false) })
|
||||
closeBtn.addEventListener('click', function () { dlg.close() })
|
||||
}
|
||||
|
||||
function openEditModal (p, isNew) {
|
||||
var draft = p ? Object.assign({}, p) : { id: uid('q'), name: '', text: '', tags: [], group: (filter === 'all' || filter === '__none__') ? '' : filter, cover: '' }
|
||||
var cancelBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '取消' })
|
||||
var saveBtn = el('button', { class: 'iwb-btn iwb-btn-primary iwb-btn-sm', text: isNew ? '创建' : '保存' })
|
||||
var dirty = false
|
||||
openDialog({
|
||||
key: 'edit',
|
||||
title: isNew ? '新建提示词' : '编辑提示词',
|
||||
width: 880, height: 640,
|
||||
actions: [cancelBtn, saveBtn],
|
||||
onSaveKey: function () { doSave() },
|
||||
build: function (bodyEl, footEl, close) {
|
||||
var left = el('div', { class: 'iwb-plp-eleft' })
|
||||
var cov = el('div', { class: 'iwb-plp-ecov' })
|
||||
var fileIn = el('input', { type: 'file', accept: 'image/*', style: 'display:none' })
|
||||
var upBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '上传图片' })
|
||||
var delCover = el('button', { class: 'iwb-btn iwb-btn-sm', text: '删除封面' })
|
||||
function repaint () { paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group }) }
|
||||
upBtn.addEventListener('click', function () { try { fileIn.click() } catch (e) { /* ignore */ } })
|
||||
fileIn.addEventListener('change', function () {
|
||||
var f = fileIn.files && fileIn.files[0]
|
||||
if (!f) return
|
||||
var rd = new FileReader()
|
||||
rd.onload = function () {
|
||||
Promise.resolve(api.putCover(String(rd.result || ''))).then(function (r) {
|
||||
if (!(r && r.ok && r.ref)) { notify('封面上传失败', 'err'); return }
|
||||
draft.cover = r.ref
|
||||
dirty = true
|
||||
delete coverCache[draft.id]
|
||||
Promise.resolve(api.coverUrl(r.ref)).then(function (u) {
|
||||
if (u && u.url) coverCache[draft.id] = u.url
|
||||
repaint(); notify('封面已更新')
|
||||
})
|
||||
}).catch(function () { notify('封面上传失败', 'err') })
|
||||
}
|
||||
rd.readAsDataURL(f)
|
||||
})
|
||||
delCover.addEventListener('click', function () { draft.cover = ''; dirty = true; delete coverCache[draft.id]; repaint() })
|
||||
left.appendChild(cov); left.appendChild(el('div', { class: 'iwb-row' }, upBtn, delCover)); left.appendChild(fileIn)
|
||||
var nameIn = el('input', { class: 'iwb-input', placeholder: '名称', value: draft.name || '' })
|
||||
var groupIn = el('input', { class: 'iwb-input', placeholder: '分组', value: draft.group || '' })
|
||||
var tagsIn = el('input', { class: 'iwb-input', placeholder: '标签,逗号分隔', value: (draft.tags || []).join(', ') })
|
||||
;[nameIn, groupIn, tagsIn].forEach(function (i) { i.addEventListener('input', function () { dirty = true }) })
|
||||
left.appendChild(el('label', { class: 'iwb-plp-lrow' }, el('span', { text: '名称' }), nameIn))
|
||||
left.appendChild(el('label', { class: 'iwb-plp-lrow' }, el('span', { text: '分组' }), groupIn))
|
||||
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 || ''
|
||||
textIn.addEventListener('input', function () { dirty = true })
|
||||
bodyEl.appendChild(left); bodyEl.appendChild(textIn)
|
||||
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)
|
||||
function collect () {
|
||||
return {
|
||||
id: draft.id,
|
||||
name: nameIn.value,
|
||||
group: groupIn.value,
|
||||
tags: String(tagsIn.value || '').split(',').map(function (s) { return s.trim() }).filter(Boolean),
|
||||
text: textIn.value,
|
||||
cover: draft.cover || '',
|
||||
}
|
||||
}
|
||||
function doSave () {
|
||||
var c = collect()
|
||||
var text = String(c.text || '').trim()
|
||||
if (!text) { notify('提示词内容不能为空', 'err'); return }
|
||||
var name = String(c.name || '').trim() || ('提示词 ' + text.slice(0, 8) + '…')
|
||||
var item = { id: c.id, name: name, text: text, tags: c.tags, group: String(c.group || '').trim(), cover: c.cover }
|
||||
var list = isNew ? prompts.concat([item]) : prompts.map(function (x) { return x.id === item.id ? item : x })
|
||||
var gs = groupsOf().slice()
|
||||
if (item.group && !gs.some(function (g) { return g.name === item.group })) gs.push(groupRef(item.group))
|
||||
close()
|
||||
save(list, gs).then(function () { notify(isNew ? '已创建' : '已保存') })
|
||||
}
|
||||
saveBtn.addEventListener('click', doSave)
|
||||
cancelBtn.addEventListener('click', function () { close() })
|
||||
dupBtn.addEventListener('click', function () {
|
||||
var c = collect()
|
||||
var copy = Object.assign({}, c, { id: uid('q'), name: (String(c.name || '提示词').trim()) + ' 副本' })
|
||||
close()
|
||||
save(prompts.concat([copy])).then(function () { notify('已另存副本') })
|
||||
})
|
||||
insBtn.addEventListener('click', function () {
|
||||
var c = collect()
|
||||
var t = String(c.text || '').trim()
|
||||
if (!t) { notify('提示词内容不能为空', 'err'); return }
|
||||
Promise.resolve(api.insert(t, c.name)).then(function () { notify('已插入画布:' + (c.name || '提示词')) }).catch(function () { notify('插入画布失败', 'err') })
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
function render () {
|
||||
clear(fixed); clear(scrollBox)
|
||||
if (view.mode === 'detail') { renderDetail(); return }
|
||||
@@ -493,7 +704,7 @@
|
||||
var search = el('input', { class: 'iwb-input iwb-grow', placeholder: '搜索名称 / 内容 / 标签…', value: query })
|
||||
search.addEventListener('input', function () { query = search.value; render(); var n = wrap.querySelector('input.iwb-input'); if (n) { n.focus(); try { n.setSelectionRange(n.value.length, n.value.length) } catch (e) { /* ignore */ } } })
|
||||
var newBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '新建提示词', text: '+ 新建' })
|
||||
newBtn.addEventListener('click', function () { openEditor(null) })
|
||||
newBtn.addEventListener('click', function () { openEditModal(null, true) })
|
||||
top.appendChild(search); top.appendChild(newBtn)
|
||||
fixed.appendChild(top)
|
||||
|
||||
@@ -581,7 +792,7 @@
|
||||
e.dataTransfer.effectAllowed = 'copy'
|
||||
} catch (err) { /* ignore */ }
|
||||
})
|
||||
card.addEventListener('click', function (e) { e.stopPropagation(); openDetail(p.id) })
|
||||
card.addEventListener('click', function (e) { e.stopPropagation(); openDetailModal(p) })
|
||||
card.addEventListener('contextmenu', function (e) { openMenu(e, p) })
|
||||
cards.appendChild(card)
|
||||
})
|
||||
@@ -616,7 +827,7 @@
|
||||
global.IWBPluginHost.register({
|
||||
id: 'promptlib',
|
||||
name: '提示词库',
|
||||
version: '1.1.0',
|
||||
version: '1.2.0',
|
||||
apiVersion: 1,
|
||||
capabilities: ['panels', 'promptlib'],
|
||||
data: { schemaVersion: 1, maxBytes: 65536 },
|
||||
|
||||
@@ -64,3 +64,31 @@
|
||||
.iwb-plp-editor-cover { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
|
||||
.iwb-plp-editor-thumb { flex: 0 0 96px; width: 96px; height: 128px; border-radius: 8px; overflow: hidden; border: 1px solid var(--iwb-border, #d5d9e0); }
|
||||
.iwb-plp-editor-coverops { display: flex; flex-direction: column; gap: 8px; }
|
||||
|
||||
/* v1.2.0:可拖动 / 可缩放的详情与编辑弹窗(同宽 880×640,最小 640×420) */
|
||||
.iwb-plp-mask { background: transparent; display: block; }
|
||||
.iwb-plp-dialog { display: flex; flex-direction: column; overflow: hidden; border-radius: 16px;
|
||||
box-shadow: 0 24px 60px rgba(0,0,0,.28); }
|
||||
.iwb-plp-dhead { display: flex; align-items: center; gap: 6px; padding: 10px 14px;
|
||||
border-bottom: 1px solid var(--iwb-border, #d5d9e0); cursor: move; user-select: none; }
|
||||
.iwb-plp-grip { display: grid; grid-template-columns: repeat(3, 3px); gap: 2px; margin-right: 6px; }
|
||||
.iwb-plp-grip i { width: 3px; height: 3px; border-radius: 50%; background: var(--iwb-border2, #b9bfca); display: block; }
|
||||
.iwb-plp-dbody { flex: 1 1 auto; min-height: 0; display: flex; gap: 14px; padding: 14px; }
|
||||
.iwb-plp-dcov { flex: 0 0 210px; width: 210px; height: 280px; border-radius: 10px; overflow: hidden;
|
||||
border: 1px solid var(--iwb-border, #d5d9e0); background: var(--iwb-plugin-inset, #f5f6f7); }
|
||||
.iwb-plp-dright { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
|
||||
.iwb-plp-dtext { flex: 1 1 auto; min-height: 0; overflow: auto; white-space: pre-wrap; word-break: break-word;
|
||||
font-size: 12px; line-height: 1.9; color: var(--iwb-text2, #4d5668); }
|
||||
.iwb-plp-dfooter { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 10px 14px;
|
||||
border-top: 1px solid var(--iwb-border, #d5d9e0); }
|
||||
.iwb-plp-resize { position: absolute; right: 0; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize;
|
||||
background: linear-gradient(135deg, transparent 0 45%, var(--iwb-border2, #b9bfca) 45% 50%, transparent 50% 62%, var(--iwb-border2, #b9bfca) 62% 67%, transparent 67%); }
|
||||
/* 编辑:左栏固定 240,右栏大文本框 */
|
||||
.iwb-plp-eleft { flex: 0 0 240px; width: 240px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
|
||||
.iwb-plp-ecov { width: 240px; height: 300px; border-radius: 10px; overflow: hidden;
|
||||
border: 1px solid var(--iwb-border, #d5d9e0); background: var(--iwb-plugin-inset, #f5f6f7); }
|
||||
.iwb-plp-lrow { display: flex; flex-direction: column; gap: 4px; font-size: 10px; color: var(--iwb-text2, #4d5668); }
|
||||
.iwb-plp-etext { flex: 1 1 auto; min-height: 0; height: auto; resize: none; font-size: 12px; line-height: 1.9; }
|
||||
/* 输入框边框柔化(用户反馈现在的边框太深) */
|
||||
.iwb-plp-dialog .iwb-input { border-color: color-mix(in srgb, var(--iwb-border, #d5d9e0) 72%, transparent); }
|
||||
.iwb-plp-dialog .iwb-input:focus { border-color: var(--iwb-brand, #6b7280); box-shadow: 0 0 0 2px color-mix(in srgb, var(--iwb-brand, #6b7280) 14%, transparent); }
|
||||
Reference in new issue
Block a user