- app.js: 首页粘性搜索条 + 分类 chip(带数量),实时过滤,不再翻半天 - 新增 lib-simple.js(textTool / convTool / genTool 工厂) - 新增 9 个 catalog:cat-text / cat-encode / cat-convert / cat-random / cat-dev / cat-css / cat-device / cat-office / cat-life - 新增分组:随机 / 检测 / 办公 / 生活 - 工具数 44 → 201;全部 mount 通过(jsdom 201/201)
108 lines
5.4 KiB
JavaScript
108 lines
5.4 KiB
JavaScript
/* DevBox · 通用工具工厂:一个输入 → 输出的轻量工具都从这儿生成 */
|
||
(function () {
|
||
'use strict'
|
||
const { el, panelTitle, copy, toast } = DB
|
||
const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn })
|
||
const pane = (title, right) => { const p = el('div', { class: 'panel' }); if (title) p.append(panelTitle(title, right)); return p }
|
||
|
||
DB.ta = (ph, rows) => el('textarea', { class: 'ta', placeholder: ph, rows: rows || 10, spellcheck: 'false' })
|
||
DB.btn = btn
|
||
DB.pane = pane
|
||
|
||
/** 单框文本工具:输入即时转换,输出带复制 */
|
||
DB.textTool = function ({ id, name, group, icon, desc, keywords, ph, rows, fn, actions, live = true }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'type', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const src = DB.ta(ph || '在这里输入…', rows || 10)
|
||
const out = el('pre', { class: 'out' })
|
||
const stat = el('div', { class: 'hint' })
|
||
const run = () => {
|
||
try {
|
||
const r = fn(src.value)
|
||
out.textContent = typeof r === 'string' ? r : (r && r.text) || ''
|
||
if (r && r.hint) { stat.textContent = r.hint; stat.className = 'hint good' }
|
||
else stat.textContent = ''
|
||
} catch (e) { out.textContent = ''; stat.textContent = '出错了:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
const bar = el('div', { class: 'row' })
|
||
for (const [label, f] of Object.entries(actions || {})) bar.append(btn(label, () => { src.value = f(src.value); run() }))
|
||
bar.append(btn('清空', () => { src.value = ''; run() }, 'ghost'))
|
||
bar.append(btn('复制结果', () => copy(out.textContent), 'ghost'))
|
||
root.append(pane(name, bar), src, stat, out)
|
||
if (live) src.addEventListener('input', run)
|
||
run()
|
||
},
|
||
})
|
||
}
|
||
|
||
/** 文本 ↔ 文本 双向转换 */
|
||
DB.convTool = function ({ id, name, group, icon, desc, keywords, left = 'A', right = 'B', a2b, b2a, phA, phB }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'swap', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const src = DB.ta(phA || left + '…', 9)
|
||
const dst = DB.ta(phB || right + '…', 9)
|
||
const stat = el('div', { class: 'hint' })
|
||
const go = (dir) => {
|
||
try {
|
||
dst.value = dir === 'a' ? a2b(src.value) : b2a(src.value)
|
||
stat.textContent = dir === 'a' ? left + ' → ' + right : right + ' → ' + left
|
||
stat.className = 'hint good'
|
||
} catch (e) { stat.textContent = '转换失败:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
root.append(pane(name, el('div', { class: 'row' },
|
||
btn(left + ' → ' + right, () => go('a')), btn(right + ' → ' + left, () => go('b')),
|
||
btn('交换', () => { const t = src.value; src.value = dst.value; dst.value = t }, 'ghost'),
|
||
btn('复制结果', () => copy(dst.value), 'ghost'))),
|
||
el('div', { class: 'grid2' }, src, dst), stat)
|
||
},
|
||
})
|
||
}
|
||
|
||
/** 生成器:参数(select / number / text)+ 一键生成 */
|
||
DB.genTool = function ({ id, name, group, icon, desc, keywords, fields, run, outputLabel = '结果' }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'bolt', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const refs = {}
|
||
const row = el('div', { class: 'row' })
|
||
for (const f of fields || []) {
|
||
if (f.type === 'select') {
|
||
const s = el('select', { class: 'input' }, ...f.options.map(([v, t]) => el('option', { value: v, text: t || v })))
|
||
if (f.value) s.value = f.value
|
||
refs[f.key] = () => s.value
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), s))
|
||
} else if (f.type === 'number') {
|
||
const n = el('input', { type: 'number', value: f.value ?? 1, min: f.min ?? 0, max: f.max ?? 1000000, step: f.step || 1, style: { width: '110px' } })
|
||
refs[f.key] = () => Number(n.value)
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), n))
|
||
} else if (f.type === 'text') {
|
||
const i = el('input', { type: 'text', value: f.value || '', placeholder: f.ph || '' })
|
||
refs[f.key] = () => i.value
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), i))
|
||
} else if (f.type === 'check') {
|
||
const c = el('input', { type: 'checkbox' })
|
||
c.checked = !!f.value
|
||
refs[f.key] = () => c.checked
|
||
row.append(el('label', { class: 'inline-field row' }, c, el('span', { text: f.label })))
|
||
}
|
||
}
|
||
const out = el('pre', { class: 'out' })
|
||
const stat = el('div', { class: 'hint' })
|
||
const go = async () => {
|
||
try {
|
||
const args = Object.fromEntries(Object.entries(refs).map(([k, f]) => [k, f()]))
|
||
const r = await run(args)
|
||
out.textContent = typeof r === 'string' ? r : (r && r.text) || ''
|
||
if (r && r.hint) { stat.textContent = r.hint; stat.className = 'hint good' }
|
||
} catch (e) { stat.textContent = '生成失败:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
const bar = el('div', { class: 'row' }, btn('生成', go), btn('复制', () => copy(out.textContent), 'ghost'))
|
||
root.append(pane(name, bar), (fields || []).length ? row : null, stat, out)
|
||
go()
|
||
},
|
||
})
|
||
}
|
||
})()
|