/* DevBox · 应用外壳:导航、搜索、路由、命令面板 */
(function () {
'use strict'
const { el, icon, tools, groups } = DB
const view = document.getElementById('view')
const nav = document.getElementById('nav')
const crumb = document.getElementById('crumb')
const navSearch = document.getElementById('navSearch')
const palette = document.getElementById('palette')
const paletteInput = document.getElementById('paletteInput')
const paletteList = document.getElementById('paletteList')
const byId = (id) => tools.find((t) => t.id === id)
const groupOf = (id) => groups.find((g) => g.id === id) || groups[0]
/* ---------------- 侧栏 ---------------- */
function renderNav(keyword = '') {
nav.textContent = ''
const kw = keyword.trim().toLowerCase()
let shown = 0
for (const g of groups) {
const list = tools.filter((t) => t.group === g.id && match(t, kw))
if (!list.length) continue
const box = el('div', { class: 'nav-group' })
box.append(el('div', { class: 'nav-title', text: g.name }))
for (const t of list) {
box.append(el('button', {
class: 'nav-item' + (currentId === t.id ? ' active' : ''),
onclick: () => go(t.id),
html: icon(t.icon) + '',
}))
const span = box.lastChild.querySelector('span')
span.textContent = t.name
shown++
}
nav.append(box)
}
if (!shown) nav.append(el('div', { class: 'nav-empty', text: '没有匹配的工具,换个词试试' }))
if (DB.auth.user?.admin) {
const box = el('div', { class: 'nav-group' })
box.append(el('div', { class: 'nav-title', text: '管理' }))
box.append(el('button', {
class: 'nav-item' + (currentId === 'admin' ? ' active' : ''),
onclick: () => go('admin'),
html: icon('i-lock') + ''
}))
box.lastChild.querySelector('span').textContent = '管理后台'
nav.append(box)
}
}
function match(t, kw) {
if (!kw) return true
const hay = [t.name, t.desc, t.id, ...(t.keywords || []), groupOf(t.group).name].join(' ').toLowerCase()
return kw.split(/\s+/).every((w) => hay.includes(w))
}
/* ---------------- 首页 ---------------- */
function renderHome() {
crumb.innerHTML = '工具箱'
view.textContent = ''
const wrap = el('div', { class: 'wrap' })
wrap.append(el('header', { class: 'hero', html: `
一个人人可用的 开发者工具箱
图片转换、哈希与加密、JWT、时间戳、JSON、正则、Diff、颜色、CIDR⋯⋯
全部在浏览器本地算完,不上传任何数据,断网也能用。
` }))
wrap.append(el('div', { class: 'hero-stats' }, ...[
[tools.length + ' 个工具', '个'],
['6 类', '类'],
['0 依赖', '零'],
['100% 本地', '本'],
].map(([a, b]) => el('span', { class: 'chip', html: `${a.split(' ')[0]}${a.slice(a.indexOf(' ') + 1)}` }))))
for (const g of groups) {
const list = tools.filter((t) => t.group === g.id)
const sec = el('section', { class: 'home-group' })
sec.append(el('div', { class: 'home-head' }, el('h2', { text: g.name }), el('span', { text: `${list.length} 个 · ${g.desc}` })))
const cards = el('div', { class: 'cards' })
for (const t of list) {
cards.append(el('button', { class: 'card', onclick: () => go(t.id), html: `
${icon(t.icon)}
${g.name}
` }))
const c = cards.lastChild
c.querySelector('h3').textContent = t.name
c.querySelector('p').textContent = t.desc
}
sec.append(cards)
wrap.append(sec)
}
view.append(wrap)
}
/* ---------------- 工具页 ---------------- */
let currentId = null
function paywall(message) {
crumb.innerHTML = '工具箱 / 积分不足'
view.textContent = ''
const wrap = el('div', { class: 'wrap' })
wrap.append(el('div', { class: 'panel paywall-inline' },
el('h2', { text: '积分不够了' }),
el('p', { class: 'muted', text: message || '每次用一个工具扣 1 积分,用 CDK 兑换一些再继续。' }),
el('div', { class: 'row' },
el('button', { class: 'btn primary', text: '去兑换 CDK', onclick: () => DB.auth.openRedeem() }),
el('button', { class: 'btn', text: '回首页', onclick: () => go('') })
)))
view.append(wrap)
}
async function renderTool(t) {
crumb.innerHTML = '工具箱 / ' + groupOf(t.group).name + ' / ' + t.name + ''
view.textContent = ''
const wrap = el('div', { class: 'wrap' })
const head = el('header', { class: 'tool-head' }, el('div', { class: 'card-ico', html: icon(t.icon) }))
const tt = el('div', {}, el('h1', { text: t.name }), el('p', { text: t.desc }))
head.append(tt)
wrap.append(head)
const body = el('div', { class: 'tool-body' })
wrap.append(body)
view.append(wrap)
view.scrollTop = 0
let charged
try {
charged = await DB.auth.charge(t.id)
} catch (e) {
if (e.status === 402) return paywall(e.message)
DB.toast(e.message, true)
return paywall(e.message)
}
if (charged?.user) DB.toast(`−${charged.cost || 1} 积分,剩 ${charged.user.credits}`)
try {
t.mount(body)
} catch (e) {
console.error(e)
body.append(el('div', { class: 'panel err', text: '这个工具出错了:' + e.message }))
}
}
function go(id) {
if (location.hash === '#/' + id) { route() } else { location.hash = '#/' + id }
document.body.classList.remove('nav-open')
}
async function route() {
if (!DB.auth.user) { DB.auth.showAuth(); return }
const id = (location.hash || '').replace(/^#\//, '')
if (id === 'about') return renderAbout()
if (id === 'admin') {
currentId = 'admin'
renderNav(navSearch.value)
if (!DB.auth.user.admin) { DB.toast('只有管理员能进', true); location.hash = ''; return }
crumb.innerHTML = '工具箱 / 管理后台'
return DB.admin.render(view)
}
const t = byId(id)
currentId = t ? t.id : null
renderNav(navSearch.value)
if (t) await renderTool(t)
else if (id) { location.hash = ''; return }
else renderHome()
}
function renderAbout() {
crumb.innerHTML = '关于'
currentId = null
renderNav(navSearch.value)
view.textContent = ''
const wrap = el('div', { class: 'wrap' })
wrap.innerHTML = `
- 技术
- 原生 HTML / CSS / JavaScript,0 依赖
- 隐私
- 所有计算在浏览器内完成,文件不离开你的设备
- 离线
- 双击 index.html 即可使用(file:// 也支持)
- 快捷键
- ⌘K / Ctrl+K 搜索工具 · Esc 关闭弹层
- 作者
- syc65535
加密工具用的是浏览器内置 Web Crypto(AES-GCM、PBKDF2、SHA-2),不是自造的算法;哈希不可逆,AES 才是可解密的。
`
view.append(wrap)
}
/* ---------------- 命令面板 ---------------- */
let palIndex = 0
let palItems = []
function openPalette() {
palette.hidden = false
paletteInput.value = ''
paletteInput.focus()
renderPalette('')
}
function closePalette() { palette.hidden = true }
function renderPalette(kw) {
paletteList.textContent = ''
palItems = tools.filter((t) => match(t, kw.trim().toLowerCase()))
palIndex = 0
if (!palItems.length) { paletteList.append(el('div', { class: 'nav-empty', text: '没有找到,换个关键词' })); return }
palItems.forEach((t, i) => {
const item = el('button', { class: 'palette-item' + (i === 0 ? ' sel' : ''), onclick: () => { closePalette(); go(t.id) }, html: `${icon(t.icon)}${groupOf(t.group).name}` })
item.querySelector('span').textContent = t.name
paletteList.append(item)
})
}
function palMove(d) {
const items = [...paletteList.querySelectorAll('.palette-item')]
if (!items.length) return
palIndex = (palIndex + d + items.length) % items.length
items.forEach((n, i) => n.classList.toggle('sel', i === palIndex))
items[palIndex].scrollIntoView({ block: 'nearest' })
}
/* ---------------- 事件 ---------------- */
window.addEventListener('hashchange', route)
navSearch.addEventListener('input', () => renderNav(navSearch.value))
document.getElementById('paletteBtn').addEventListener('click', openPalette)
document.getElementById('openNav').addEventListener('click', () => document.body.classList.add('nav-open'))
document.getElementById('closeNav').addEventListener('click', () => document.body.classList.remove('nav-open'))
document.getElementById('scrim').addEventListener('click', () => document.body.classList.remove('nav-open'))
palette.addEventListener('click', (e) => { if (e.target === palette) closePalette() })
paletteInput.addEventListener('input', () => renderPalette(paletteInput.value))
paletteInput.addEventListener('keydown', (e) => {
if (e.key === 'ArrowDown') { e.preventDefault(); palMove(1) }
else if (e.key === 'ArrowUp') { e.preventDefault(); palMove(-1) }
else if (e.key === 'Enter') { const t = palItems[palIndex]; if (t) { closePalette(); go(t.id) } }
})
document.addEventListener('keydown', (e) => {
const mod = e.metaKey || e.ctrlKey
if (mod && e.key.toLowerCase() === 'k') { e.preventDefault(); palette.hidden ? openPalette() : closePalette() }
else if (e.key === 'Escape') { closePalette(); document.body.classList.remove('nav-open') }
else if (e.key === '/' && !/input|textarea|select/i.test(document.activeElement.tagName)) { e.preventDefault(); openPalette() }
})
/* ---------------- 启动 ---------------- */
DB.reroute = () => { if (DB.auth.user) route() }
DB.auth.mount()
let booted = false
const start = () => { if (booted) return; booted = true; route() }
DB.auth.onChange((user) => { if (user) start() })
DB.auth.ready().then((user) => { if (user) start() })
DB.go = go
})()