feat(promptlib): 1.5.9 数据备份(导出/导入 JSON,含封面与变量,默认合并去重)
This commit is contained in:
1 parent
5c34856c6a
commit
501e46965a
1 file changed
+103
@@ -648,6 +648,106 @@
|
||||
return { node: box, sync: sync, list: function () { return list }, values: values }
|
||||
}
|
||||
|
||||
// ===== v1.5.9:数据备份(导出/导入 JSON,含封面)=====
|
||||
function backupFileName () {
|
||||
var d = new Date()
|
||||
var mm = String(d.getMonth() + 1).padStart(2, '0')
|
||||
var dd = String(d.getDate()).padStart(2, '0')
|
||||
return 'promptlib-backup-' + d.getFullYear() + '-' + mm + '-' + dd + '.json'
|
||||
}
|
||||
function coverToDataUrl (p) {
|
||||
var c = p && p.cover
|
||||
if (!c) return Promise.resolve('')
|
||||
if (typeof c === 'string') return Promise.resolve(c.indexOf('data:') === 0 ? c : '')
|
||||
if (c.img && FILEAPI) return Promise.resolve(FILEAPI.get(String(c.img))).then(function (r) { return (r && r.ok && r.url) || '' }).catch(function () { return '' })
|
||||
return Promise.resolve('')
|
||||
}
|
||||
function openBackup () {
|
||||
var exportBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', text: '导出备份…' })
|
||||
var importBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '导入备份…' })
|
||||
var overwrite = el('input', { type: 'checkbox' })
|
||||
var status = el('div', { class: 'iwb-hint', text: '' })
|
||||
openDialog({
|
||||
key: 'backup',
|
||||
title: '提示词库 · 数据备份',
|
||||
width: 620, height: 420,
|
||||
actions: [el('span', { class: 'iwb-drawer-spacer' })],
|
||||
build: function (bodyEl, footEl) {
|
||||
var box = el('div', { class: 'iwb-plp-backup' })
|
||||
box.appendChild(el('div', { class: 'iwb-hint', text: '导出为 JSON(含封面图片),可发给他人或换机导入。' }))
|
||||
box.appendChild(el('div', { class: 'iwb-hint', text: '导入默认合并:按 id 去重,已存在的不覆盖;勾选下方选项则用备份覆盖同名条目。' }))
|
||||
var line = el('label', { class: 'iwb-plp-bkopt' })
|
||||
line.appendChild(overwrite)
|
||||
line.appendChild(el('span', { text: '导入时覆盖同名条目' }))
|
||||
box.appendChild(line)
|
||||
box.appendChild(status)
|
||||
bodyEl.appendChild(box)
|
||||
footEl.appendChild(exportBtn)
|
||||
footEl.appendChild(importBtn)
|
||||
exportBtn.addEventListener('click', function () {
|
||||
exportBtn.disabled = true
|
||||
status.textContent = '正在导出…'
|
||||
Promise.all(prompts.map(function (p) {
|
||||
return coverToDataUrl(p).then(function (url) {
|
||||
var item = Object.assign({}, p)
|
||||
if (url) item.cover = url
|
||||
else delete item.cover
|
||||
return item
|
||||
})
|
||||
})).then(function (list) {
|
||||
var payload = { app: 'image-workbench', kind: 'promptlib-backup', version: 1, exportedAt: Date.now(), prompts: list, groups: groupsOf() }
|
||||
return Promise.resolve(api.exportText(backupFileName(), JSON.stringify(payload, null, 2)))
|
||||
}).then(function (r) {
|
||||
exportBtn.disabled = false
|
||||
if (r && r.ok) { status.textContent = '已导出:' + (r.path || backupFileName()); notify('已导出提示词备份') }
|
||||
else if (r && r.canceled) status.textContent = '已取消'
|
||||
else { status.textContent = '导出失败:' + ((r && r.error) || '未知错误'); notify('导出失败', 'err') }
|
||||
}).catch(function (e) { exportBtn.disabled = false; status.textContent = '导出失败:' + ((e && e.message) || e); notify('导出失败', 'err') })
|
||||
})
|
||||
importBtn.addEventListener('click', function () {
|
||||
importBtn.disabled = true
|
||||
status.textContent = '等待选择文件…'
|
||||
Promise.resolve(api.importText()).then(function (r) {
|
||||
if (!r || !r.ok) { importBtn.disabled = false; status.textContent = (r && r.canceled) ? '已取消' : ('导入失败:' + ((r && r.error) || '')); return }
|
||||
var payload = null
|
||||
try { payload = JSON.parse(r.text) } catch (e) { payload = null }
|
||||
if (!payload || !Array.isArray(payload.prompts)) { importBtn.disabled = false; status.textContent = '不是有效的提示词备份文件'; notify('导入失败:文件格式不对', 'err'); return }
|
||||
return Promise.all(payload.prompts.map(function (p) {
|
||||
var c = p && p.cover
|
||||
if (typeof c !== 'string' || c.indexOf('data:') !== 0 || !FILEAPI) return Promise.resolve(p)
|
||||
var h = 5381
|
||||
for (var i2 = 0; i2 < c.length; i2++) h = ((h << 5) + h + c.charCodeAt(i2)) >>> 0
|
||||
var name = 'c' + h.toString(36) + '.jpg'
|
||||
return Promise.resolve(FILEAPI.put(name, c)).then(function (res) {
|
||||
return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p
|
||||
}).catch(function () { return p })
|
||||
})).then(function (incoming) {
|
||||
var byId = {}
|
||||
prompts.forEach(function (p) { byId[p.id] = p })
|
||||
var added = 0
|
||||
incoming.forEach(function (p) {
|
||||
if (!p || !p.id) return
|
||||
if (byId[p.id] && !overwrite.checked) return
|
||||
if (!byId[p.id]) added++
|
||||
byId[p.id] = normalize(p)
|
||||
})
|
||||
prompts = Object.keys(byId).map(function (k) { return byId[k] })
|
||||
var gs = groupsOf().slice()
|
||||
;(payload.groups || []).forEach(function (g) {
|
||||
var name = typeof g === 'string' ? g : (g && g.name)
|
||||
if (name && !gs.some(function (x) { return x.name === name })) gs.push({ id: name, name: name })
|
||||
})
|
||||
groups = gs
|
||||
importBtn.disabled = false
|
||||
status.textContent = '已导入 ' + incoming.length + ' 条(新增 ' + added + ' 条)'
|
||||
return persist().then(function () { render(); notify('已导入 ' + incoming.length + ' 条提示词') })
|
||||
})
|
||||
}).catch(function (e) { importBtn.disabled = false; status.textContent = '导入失败:' + ((e && e.message) || e); notify('导入失败', 'err') })
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function paintCoverInto (node, p) {
|
||||
node.textContent = ''
|
||||
var url = p && p._coverUrl ? p._coverUrl : coverOf(p)
|
||||
@@ -919,6 +1019,9 @@
|
||||
chips.appendChild(addChip)
|
||||
var mgChip = el('button', { class: 'iwb-chip iwb-plp-add iwb-chip-add', title: '管理分组(重命名 / 排序 / 删除)', text: '⚙' })
|
||||
mgChip.addEventListener('click', openManager)
|
||||
var bkChip = el('button', { class: 'iwb-chip iwb-plp-add iwb-chip-add', title: '数据备份(导出/导入)', text: '⇅' })
|
||||
bkChip.addEventListener('click', openBackup)
|
||||
chips.appendChild(bkChip)
|
||||
chips.appendChild(mgChip)
|
||||
fixed.appendChild(chips)
|
||||
|
||||
|
||||
Reference in new issue
Block a user