feat(plugins): open_data_subpath 命令 + promptlib 设置页(分组/备份/数据位置)1.5.12
This commit is contained in:
1 parent
404aa08cc6
commit
3cb25875b3
6 files changed
+258
-11
No files matched your search
+2
-2
@@ -46,8 +46,8 @@
|
||||
"style": "styles.css",
|
||||
"version": "1.5.0",
|
||||
"files": {
|
||||
"main.js": "26ff14eea709e6c129c20ed6fee8546576892dfd65416c6308a9722ecc8576c2",
|
||||
"styles.css": "28a267866d81708fc51aa61a40f922decd917c9ea907c52c8c916fe6398cad68"
|
||||
"main.js": "f09f32c0432a2441232dcf072bac9a4d36c310c7dc0d0bd15001d460073c759e",
|
||||
"styles.css": "f43fa0e8fca78281616de8ea574422bccc7a2dba3951f648edad021aa6730a0b"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
+203
-6
@@ -648,6 +648,205 @@
|
||||
return { node: box, sync: sync, list: function () { return list }, values: values }
|
||||
}
|
||||
|
||||
// ===== v1.5.10:设置页(分组管理 / 数据备份 / 数据位置)=====
|
||||
function plpLastExport (v) {
|
||||
try {
|
||||
if (v === undefined) return String(global.localStorage.getItem('iwb_plp_last_export') || '')
|
||||
global.localStorage.setItem('iwb_plp_last_export', String(v))
|
||||
} catch (e) { /* ignore */ }
|
||||
return ''
|
||||
}
|
||||
function renameGroup (from, to) {
|
||||
var n = String(to || '').trim()
|
||||
if (!n || n === from) return
|
||||
prompts = prompts.map(function (p) { return p.group === from ? Object.assign({}, p, { group: n }) : p })
|
||||
groups = groupsOf().map(function (g) { return g.name === from ? { id: n, name: n } : g })
|
||||
}
|
||||
function deleteGroup (name) {
|
||||
prompts = prompts.map(function (p) { return p.group === name ? Object.assign({}, p, { group: '' }) : p })
|
||||
groups = groupsOf().filter(function (g) { return g.name !== name })
|
||||
if (filter === name) filter = 'all'
|
||||
}
|
||||
function moveGroup (from, dir) {
|
||||
var list = groupsOf().slice()
|
||||
var i = -1
|
||||
list.forEach(function (g, k) { if (g.name === from) i = k })
|
||||
var j = i + dir
|
||||
if (i < 0 || j < 0 || j >= list.length) return
|
||||
var item = list.splice(i, 1)[0]
|
||||
list.splice(j, 0, item)
|
||||
groups = list
|
||||
}
|
||||
function runExport () {
|
||||
return 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) {
|
||||
if (r && r.ok) { plpLastExport((r.path || backupFileName()) + '|' + Date.now()); notify('已导出提示词备份') }
|
||||
return r
|
||||
})
|
||||
}
|
||||
function runImport (overwrite) {
|
||||
return Promise.resolve(api.importText()).then(function (r) {
|
||||
if (!r || !r.ok) return r || { ok: false }
|
||||
var payload = null
|
||||
try { payload = JSON.parse(r.text) } catch (e) { payload = null }
|
||||
if (!payload || !Array.isArray(payload.prompts)) return { ok: false, error: '不是有效的提示词备份文件' }
|
||||
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) 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
|
||||
return persist().then(function () { render(); notify('已导入 ' + incoming.length + ' 条提示词'); return { ok: true, count: incoming.length, added: added } })
|
||||
})
|
||||
})
|
||||
}
|
||||
function openSettings () {
|
||||
var overwrite = el('input', { type: 'checkbox' })
|
||||
var bkStatus = el('div', { class: 'iwb-hint', text: '' })
|
||||
var usage = el('div', { class: 'iwb-hint', text: '读取中…' })
|
||||
var groupsBox = el('div', { class: 'iwb-plp-sgroups' })
|
||||
var addInput = el('input', { class: 'iwb-input', placeholder: '新分组名称' })
|
||||
var addBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '新建' })
|
||||
|
||||
function paintGroups () {
|
||||
clear(groupsBox)
|
||||
groupsOf().forEach(function (g) {
|
||||
var row = el('div', { class: 'iwb-plp-sgrow' })
|
||||
row.appendChild(el('span', { class: 'iwb-plp-sgrip', title: '排序' }))
|
||||
var nameIn = el('input', { class: 'iwb-input iwb-plp-sgname' })
|
||||
nameIn.value = g.name
|
||||
nameIn.addEventListener('change', function () {
|
||||
renameGroup(g.name, nameIn.value)
|
||||
save(prompts, groups).then(function () { paintGroups() })
|
||||
})
|
||||
row.appendChild(nameIn)
|
||||
row.appendChild(el('span', { class: 'iwb-plp-sgcount', text: String(countOf(g.name)) + ' 条' }))
|
||||
row.appendChild(el('span', { class: 'iwb-drawer-spacer' }))
|
||||
var up = el('button', { class: 'iwb-btn iwb-btn-sm', title: '上移', text: '↑' })
|
||||
up.addEventListener('click', function () { moveGroup(g.name, -1); save(prompts, groups).then(function () { paintGroups() }) })
|
||||
var down = el('button', { class: 'iwb-btn iwb-btn-sm', title: '下移', text: '↓' })
|
||||
down.addEventListener('click', function () { moveGroup(g.name, 1); save(prompts, groups).then(function () { paintGroups() }) })
|
||||
var del = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-danger', text: '删除' })
|
||||
del.addEventListener('click', function () {
|
||||
ask('删除分组「' + g.name + '」?分组里的提示词会回到「未分组」。', { title: '删除分组', okText: '删除' }).then(function (ok) {
|
||||
if (!ok) return
|
||||
deleteGroup(g.name)
|
||||
save(prompts, groups).then(function () { paintGroups() })
|
||||
})
|
||||
})
|
||||
row.appendChild(up); row.appendChild(down); row.appendChild(del)
|
||||
groupsBox.appendChild(row)
|
||||
})
|
||||
if (!groupsOf().length) groupsBox.appendChild(el('div', { class: 'iwb-hint', text: '还没有分组' }))
|
||||
}
|
||||
addBtn.addEventListener('click', function () {
|
||||
var n = String(addInput.value || '').trim()
|
||||
if (!n) return
|
||||
if (!groupsOf().some(function (g) { return g.name === n })) groups = groupsOf().concat([{ id: n, name: n }])
|
||||
addInput.value = ''
|
||||
save(prompts, groups).then(function () { paintGroups() })
|
||||
})
|
||||
|
||||
function refreshUsage () {
|
||||
var dirP = (typeof api.fileDir === 'function') ? Promise.resolve(api.fileDir()).catch(function () { return '' }) : Promise.resolve('')
|
||||
var statP = (typeof api.fileStat === 'function') ? Promise.resolve(api.fileStat()).catch(function () { return null }) : Promise.resolve(null)
|
||||
Promise.all([dirP, statP]).then(function (rs) {
|
||||
var dir = rs[0] || 'plugin-data/promptlib'
|
||||
var st = rs[1] || {}
|
||||
var covers = prompts.filter(function (p) { return p && p.cover }).length
|
||||
var size = st.bytes || 0
|
||||
usage.textContent = dir + ' · ' + (size > 1024 ? (size / 1024).toFixed(0) + ' KB' : size + ' B') + ' · ' + prompts.length + ' 条 · ' + groupsOf().length + ' 个分组 · ' + covers + ' 张封面'
|
||||
})
|
||||
}
|
||||
|
||||
openDialog({
|
||||
key: 'settings',
|
||||
title: '提示词库 · 设置',
|
||||
width: 760, height: 720,
|
||||
actions: [el('span', { class: 'iwb-drawer-spacer' })],
|
||||
build: function (bodyEl, footEl) {
|
||||
var box = el('div', { class: 'iwb-plp-settings' })
|
||||
var s1 = el('div', { class: 'iwb-plp-ssec' })
|
||||
s1.appendChild(el('div', { class: 'iwb-plp-stitle', text: '分组管理' }))
|
||||
s1.appendChild(el('div', { class: 'iwb-hint', text: '改名后条目自动跟随;删除分组不会删除提示词(回到「未分组」)。' }))
|
||||
s1.appendChild(groupsBox)
|
||||
s1.appendChild(el('div', { class: 'iwb-plp-sadd' }, addInput, addBtn))
|
||||
box.appendChild(s1)
|
||||
var s2 = el('div', { class: 'iwb-plp-ssec' })
|
||||
s2.appendChild(el('div', { class: 'iwb-plp-stitle', text: '数据备份' }))
|
||||
s2.appendChild(el('div', { class: 'iwb-hint', text: '导出为 JSON(含封面与变量),可发给他人或换机导入;导入默认按 id 合并去重。' }))
|
||||
var expBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', text: '导出备份…' })
|
||||
var impBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '导入备份…' })
|
||||
expBtn.addEventListener('click', function () {
|
||||
expBtn.disabled = true; bkStatus.textContent = '正在导出…'
|
||||
runExport().then(function (r) {
|
||||
expBtn.disabled = false
|
||||
bkStatus.textContent = (r && r.ok) ? ('已导出:' + (r.path || '')) : ((r && r.canceled) ? '已取消' : ('导出失败:' + ((r && r.error) || '未知错误')))
|
||||
if (r && r.ok) refreshUsage()
|
||||
}).catch(function (e) { expBtn.disabled = false; bkStatus.textContent = '导出失败:' + ((e && e.message) || e) })
|
||||
})
|
||||
impBtn.addEventListener('click', function () {
|
||||
impBtn.disabled = true; bkStatus.textContent = '等待选择文件…'
|
||||
runImport(Boolean(overwrite.checked)).then(function (r) {
|
||||
impBtn.disabled = false
|
||||
if (r && r.ok) { bkStatus.textContent = '已导入 ' + r.count + ' 条(新增 ' + r.added + ' 条)'; paintGroups(); refreshUsage() }
|
||||
else bkStatus.textContent = (r && r.canceled) ? '已取消' : ('导入失败:' + ((r && r.error) || '文件格式不对'))
|
||||
}).catch(function (e) { impBtn.disabled = false; bkStatus.textContent = '导入失败:' + ((e && e.message) || e) })
|
||||
})
|
||||
s2.appendChild(el('div', { class: 'iwb-plp-sbtns' }, expBtn, impBtn))
|
||||
s2.appendChild(el('label', { class: 'iwb-plp-sopt' }, overwrite, el('span', { text: '导入时覆盖同名条目' })))
|
||||
s2.appendChild(bkStatus)
|
||||
var last = plpLastExport()
|
||||
if (last) {
|
||||
var parts = last.split('|')
|
||||
s2.appendChild(el('div', { class: 'iwb-hint', text: '上次导出:' + new Date(Number(parts[1]) || Date.now()).toLocaleString() + ' · ' + String(parts[0]).split('/').pop() }))
|
||||
}
|
||||
box.appendChild(s2)
|
||||
var s3 = el('div', { class: 'iwb-plp-ssec' })
|
||||
s3.appendChild(el('div', { class: 'iwb-plp-stitle', text: '数据位置' }))
|
||||
s3.appendChild(usage)
|
||||
var openBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '打开数据目录' })
|
||||
openBtn.addEventListener('click', function () {
|
||||
if (typeof api.openPath !== 'function') { notify('当前客户端不支持打开目录', 'err'); return }
|
||||
Promise.resolve(api.openPath('plugin-data/promptlib')).then(function () { notify('已打开数据目录') }).catch(function (e) { notify('打开失败:' + ((e && e.message) || e), 'err') })
|
||||
})
|
||||
s3.appendChild(el('div', { class: 'iwb-plp-sbtns' }, openBtn))
|
||||
box.appendChild(s3)
|
||||
bodyEl.appendChild(box)
|
||||
paintGroups(); refreshUsage()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ===== v1.5.9:数据备份(导出/导入 JSON,含封面)=====
|
||||
function backupFileName () {
|
||||
var d = new Date()
|
||||
@@ -953,8 +1152,10 @@
|
||||
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 iwb-btn-primary', title: '新建提示词', text: '+ 新建提示词' })
|
||||
var setBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '设置(分组管理 / 数据备份)', text: '⚙' })
|
||||
setBtn.addEventListener('click', openSettings)
|
||||
newBtn.addEventListener('click', function () { openEditModal(null, true) })
|
||||
top.appendChild(search); top.appendChild(newBtn)
|
||||
top.appendChild(search); top.appendChild(newBtn); top.appendChild(setBtn)
|
||||
fixed.appendChild(top)
|
||||
|
||||
if (words().length) {
|
||||
@@ -1018,11 +1219,7 @@
|
||||
})
|
||||
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)
|
||||
|
||||
// 卡片列表
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
"提示词"
|
||||
],
|
||||
"files": {
|
||||
"main.js": "26ff14eea709e6c129c20ed6fee8546576892dfd65416c6308a9722ecc8576c2",
|
||||
"styles.css": "28a267866d81708fc51aa61a40f922decd917c9ea907c52c8c916fe6398cad68"
|
||||
"main.js": "f09f32c0432a2441232dcf072bac9a4d36c310c7dc0d0bd15001d460073c759e",
|
||||
"styles.css": "f43fa0e8fca78281616de8ea574422bccc7a2dba3951f648edad021aa6730a0b"
|
||||
}
|
||||
}
|
||||
@@ -174,3 +174,18 @@
|
||||
.iwb-plp-nmeta{font-size:10.5px;color:var(--iwb-text2,#8b929c)}
|
||||
.iwb-plp-nempty{font-size:11px;color:var(--iwb-text2,#8b929c);padding:2px 0}
|
||||
.iwb-plp-nwarn{font-size:11px;line-height:1.5;color:var(--iwb-warn,#d97706)}
|
||||
|
||||
/* v1.5.10:设置页 */
|
||||
.iwb-plp-settings{display:flex;flex-direction:column;gap:18px}
|
||||
.iwb-plp-ssec{border:1px solid var(--iwb-border,#e3e5e9);border-radius:12px;padding:12px 14px;display:flex;flex-direction:column;gap:8px}
|
||||
.iwb-plp-stitle{font-weight:600;font-size:12.5px}
|
||||
.iwb-plp-sgroups{display:flex;flex-direction:column;border:1px solid var(--iwb-border,#eef0f2);border-radius:10px;overflow:hidden}
|
||||
.iwb-plp-sgrow{display:flex;align-items:center;gap:8px;padding:7px 10px;border-top:1px solid var(--iwb-border,#f1f3f5)}
|
||||
.iwb-plp-sgrow:first-child{border-top:0}
|
||||
.iwb-plp-sgrip{width:12px;height:12px;background:radial-gradient(var(--iwb-text2,#c2c7cf) 1px,transparent 1px);background-size:4px 4px;border-radius:3px;flex:0 0 auto}
|
||||
.iwb-plp-sgname{width:170px;height:26px;font-size:12px}
|
||||
.iwb-plp-sgcount{font-size:11.5px;color:var(--iwb-text2,#8b929c);width:56px}
|
||||
.iwb-plp-sadd{display:flex;gap:8px;align-items:center}
|
||||
.iwb-plp-sbtns{display:flex;gap:8px;align-items:center}
|
||||
.iwb-plp-sopt{display:flex;gap:8px;align-items:center;font-size:12px;color:var(--iwb-text2,#4d5668)}
|
||||
.iwb-plp-settings{max-height:calc(100vh - 230px);overflow:auto;padding-right:4px}
|
||||
+34
-1
@@ -241,6 +241,39 @@ fn copy_tree(src: String, dst: String) -> Result<u64, String> {
|
||||
}
|
||||
|
||||
// 打开应用数据目录(设置里的「打开数据目录」按钮用;macOS 访达 / Windows 资源管理器 / Linux xdg-open)
|
||||
#[tauri::command]
|
||||
fn open_data_subpath(app: AppHandle, rel: String) -> Result<String, String> {
|
||||
// 只允许打开「资料目录」下的子路径:rel 由宿主/插件传入,必须做规范化 + 前缀校验,
|
||||
// 否则等于给插件一个任意路径的打开权限。
|
||||
let recorded = app
|
||||
.path()
|
||||
.app_config_dir()
|
||||
.ok()
|
||||
.and_then(|c| std::fs::read_to_string(c.join("data-dir.txt")).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty());
|
||||
let base = match recorded {
|
||||
Some(p) => std::path::PathBuf::from(p),
|
||||
None => app.path().app_data_dir().map_err(|e| e.to_string())?,
|
||||
};
|
||||
let base = base.canonicalize().map_err(|e| e.to_string())?;
|
||||
let target = base.join(rel.trim_start_matches('/')).canonicalize().map_err(|e| e.to_string())?;
|
||||
if !target.starts_with(&base) { return Err("路径不在资料目录内".into()); }
|
||||
std::fs::create_dir_all(&target).map_err(|e| e.to_string())?;
|
||||
let path = target.to_string_lossy().to_string();
|
||||
#[cfg(target_os = "macos")]
|
||||
let cmd = "open";
|
||||
#[cfg(target_os = "windows")]
|
||||
let cmd = "explorer";
|
||||
#[cfg(target_os = "linux")]
|
||||
let cmd = "xdg-open";
|
||||
std::process::Command::new(cmd)
|
||||
.arg(&path)
|
||||
.spawn()
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn open_app_data_dir(app: AppHandle) -> Result<String, String> {
|
||||
// 资料目录(用户选的那个)优先;没设置过才退回应用私有目录
|
||||
@@ -889,7 +922,7 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())
|
||||
.invoke_handler(tauri::generate_handler![open_app_data_dir, grant_dir, copy_tree, check_update, install_update, clipboard_read_image, clipboard_write_image, clipboard_write_text, clipboard_read_text, open_external_url, open_browser_window, browser_control, browser_state, canvas_webview_ensure, canvas_webview_bounds, canvas_webview_navigate, canvas_webview_action, canvas_webview_state, canvas_webview_close_all, canvas_webview_metrics, notify_desktop,
|
||||
.invoke_handler(tauri::generate_handler![open_app_data_dir, open_data_subpath, grant_dir, copy_tree, check_update, install_update, clipboard_read_image, clipboard_write_image, clipboard_write_text, clipboard_read_text, open_external_url, open_browser_window, browser_control, browser_state, canvas_webview_ensure, canvas_webview_bounds, canvas_webview_navigate, canvas_webview_action, canvas_webview_state, canvas_webview_close_all, canvas_webview_metrics, notify_desktop,
|
||||
app_memory_mb])
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
|
||||
@@ -64,6 +64,8 @@
|
||||
fileList: fileOp('list'),
|
||||
fileRemove: fileOp('remove'),
|
||||
fileStat: fileOp('stat'),
|
||||
fileDir: function () { const F = (typeof global !== 'undefined' && global.IWBStorageFile) ? global.IWBStorageFile.pluginFiles : null; return F ? F.dir('promptlib') : Promise.reject(new Error('文件能力未就绪')) },
|
||||
openPath: function (rel) { const T = (typeof global !== 'undefined') ? global.__TAURI__ : null; if (!T || !T.core) return Promise.reject(new Error('桌面能力不可用')); return T.core.invoke('open_data_subpath', { rel: String(rel || '') }) },
|
||||
// 备份文件读写(宿主的另存为/打开对话框)
|
||||
exportText: function (name, text) { const B = (typeof global !== 'undefined') ? global.IWBPromptlibBackup : null; return (B && typeof B.saveText === 'function') ? B.saveText(text, name) : Promise.reject(new Error('备份能力未就绪')) },
|
||||
importText: function () { const B = (typeof global !== 'undefined') ? global.IWBPromptlibBackup : null; return (B && typeof B.pickText === 'function') ? B.pickText() : Promise.reject(new Error('备份能力未就绪')) },
|
||||
|
||||
Reference in new issue
Block a user