- 首页:hero(标题/副文案/两个行动按钮)→ 搜索框前置 → 最近使用 → 按类别浏览(14 类磁贴)→ 完整工具目录
- 补齐 27 个缺失的 SVG symbol(i-edit/i-monitor/i-book/i-file/i-grid/i-abc/i-users/i-star/i-music/i-info/i-table/i-keyboard/i-mouse/i-type/i-diff/i-print/i-id/i-phone/i-mail/i-archive/i-cookie/i-braces/i-database/i-link/i-minimize/i-filter/i-lock)
- 修 icon('i-lock') 双前缀导致图标失效(app.js / admin.js)
- 补 --muted/--text/--brand/--panel-2 四个未定义 CSS 变量别名,跟随主题切换
- 修 server.mjs 在 Windows 下 path.normalize 返回反斜杠导致 /js/*.js、/css/*.css 全 404
- 收藏按钮不再重建工具页(避免重复扣积分)
387 lines
18 KiB
JavaScript
387 lines
18 KiB
JavaScript
/* 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 FAV = 'devbox.fav', REC = 'devbox.recent'
|
||
const readList = (k) => { try { return JSON.parse(localStorage.getItem(k) || '[]') } catch (e) { return [] } }
|
||
const writeList = (k, v) => localStorage.setItem(k, JSON.stringify(v.slice(0, 40)))
|
||
const isFav = (id) => readList(FAV).includes(id)
|
||
const toggleFav = (id) => {
|
||
const list = readList(FAV)
|
||
const i = list.indexOf(id)
|
||
if (i >= 0) list.splice(i, 1); else list.unshift(id)
|
||
writeList(FAV, list)
|
||
DB.toast(i >= 0 ? '已取消收藏' : '已收藏')
|
||
renderNav(navSearch.value)
|
||
document.querySelectorAll('.fav-btn').forEach((button) => {
|
||
button.classList.toggle('on', isFav(currentId))
|
||
button.textContent = isFav(currentId) ? '★ 已收藏' : '☆ 收藏'
|
||
button.setAttribute('aria-pressed', String(isFav(currentId)))
|
||
})
|
||
}
|
||
const pushRecent = (id) => {
|
||
if (!id || ['admin', 'about'].includes(id)) return
|
||
const list = readList(REC).filter((x) => x !== id)
|
||
list.unshift(id); writeList(REC, list)
|
||
}
|
||
const groupOf = (id) => groups.find((g) => g.id === id) || groups[0]
|
||
|
||
/* ---------------- 侧栏 ---------------- */
|
||
const NAV_CLOSED = 'devbox.navclosed'
|
||
const closedSet = () => {
|
||
try { return new Set(JSON.parse(localStorage.getItem(NAV_CLOSED) || '[]')) } catch (e) { return new Set() }
|
||
}
|
||
const toggleGroup = (id) => {
|
||
const set = closedSet()
|
||
if (set.has(id)) set.delete(id); else set.add(id)
|
||
localStorage.setItem(NAV_CLOSED, JSON.stringify([...set]))
|
||
renderNav(navSearch.value)
|
||
}
|
||
|
||
function renderNav(keyword = '') {
|
||
nav.textContent = ''
|
||
const kw = keyword.trim().toLowerCase()
|
||
const closed = closedSet()
|
||
let shown = 0
|
||
/* 顶部快捷区:最近使用 / 我的收藏 */
|
||
const quick = [
|
||
['devbox-quick-recent', '最近使用', readList(REC).map((id) => tools.find((t) => t.id === id)).filter(Boolean).slice(0, 8)],
|
||
['devbox-quick-fav', '我的收藏', readList(FAV).map((id) => tools.find((t) => t.id === id)).filter(Boolean)],
|
||
]
|
||
for (const [qid, qname, list] of quick) {
|
||
const hit = list.filter((t) => match(t, kw))
|
||
if (!hit.length || kw) continue
|
||
const box = el('div', { class: 'nav-group' + (closed.has(qid) ? ' closed' : '') })
|
||
const h = el('button', { class: 'nav-title', onclick: () => toggleGroup(qid) })
|
||
h.append(el('span', { class: 'nav-caret', text: closed.has(qid) ? '▸' : '▾' }))
|
||
h.append(el('span', { class: 'nav-gname', text: qname }))
|
||
h.append(el('span', { class: 'nav-count', text: String(hit.length) }))
|
||
box.append(h)
|
||
for (const t of hit) {
|
||
box.append(el('button', { class: 'nav-item' + (currentId === t.id ? ' active' : ''), onclick: () => go(t.id), html: icon(t.icon) + '<span></span>' }))
|
||
box.lastChild.querySelector('span').textContent = t.name
|
||
}
|
||
nav.append(box)
|
||
}
|
||
for (const g of groups) {
|
||
const list = tools.filter((t) => t.group === g.id && match(t, kw))
|
||
if (!list.length) continue
|
||
/* 有关键词或当前工具在这一组时强制展开 */
|
||
const forceOpen = !!kw || list.some((t) => t.id === currentId)
|
||
const isClosed = !forceOpen && closed.has(g.id)
|
||
const box = el('div', { class: 'nav-group' + (isClosed ? ' closed' : '') })
|
||
const head = el('button', { class: 'nav-title', onclick: () => toggleGroup(g.id) })
|
||
head.append(el('span', { class: 'nav-caret', text: isClosed ? '▸' : '▾' }))
|
||
head.append(el('span', { class: 'nav-gname', text: g.name }))
|
||
head.append(el('span', { class: 'nav-count', text: String(list.length) }))
|
||
box.append(head)
|
||
for (const t of list) {
|
||
box.append(el('button', {
|
||
class: 'nav-item' + (currentId === t.id ? ' active' : ''),
|
||
onclick: () => go(t.id),
|
||
html: icon(t.icon) + '<span></span>',
|
||
}))
|
||
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('lock') + '<span></span>'
|
||
}))
|
||
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))
|
||
}
|
||
|
||
/* ---------------- 首页 ---------------- */
|
||
let homeFilter = { group: 'all', kw: '' }
|
||
|
||
function renderHome() {
|
||
crumb.innerHTML = '工具箱'
|
||
view.textContent = ''
|
||
const wrap = el('div', { class: 'wrap' })
|
||
wrap.append(el('header', { class: 'hero', html: `
|
||
<div class="hero-kicker"><span class="pulse-dot"></span> DevBox · 本地优先的开发工具箱</div>
|
||
<h1>把重复的开发工作,<em>交给工具</em></h1>
|
||
<p>JSON、哈希、图片、时间、网络与文本工具集中在一个工作台。打开即用,处理结果默认留在浏览器里。</p>
|
||
<div class="hero-actions">
|
||
<button class="btn primary hero-cta" data-focus-search>开始找工具 <span>⌘K</span></button>
|
||
<button class="btn hero-secondary" data-open-recent>继续上次工作</button>
|
||
</div>
|
||
` }))
|
||
wrap.append(el('div', { class: 'hero-stats' }, ...[
|
||
[tools.length + ' 个工具', '个'],
|
||
[groups.length + ' 类', '类'],
|
||
['0 依赖', '零'],
|
||
['100% 本地', '本'],
|
||
].map(([a, b]) => el('span', { class: 'chip', html: `<b>${a.split(' ')[0]}</b>${a.slice(a.indexOf(' ') + 1)}` }))))
|
||
|
||
// 找工具搜索置于首屏,分类与完整列表按优先级后置
|
||
const tiles = el('div', { class: 'cat-tiles' })
|
||
const tileIcons = { image: 'image', encode: 'copy', crypto: 'lock', text: 'text', time: 'clock', web: 'globe', format: 'box', creator: 'bolt', random: 'swap', device: 'monitor', study: 'book', office: 'file', life: 'sun', fun: 'grid' }
|
||
for (const g of groups) {
|
||
const n = tools.filter((t) => t.group === g.id).length
|
||
if (!n) continue
|
||
const tile = el('button', { class: 'cat-tile', html: `<div class="cat-tile-ico">${icon(tileIcons[g.id] || 'box')}</div><div class="cat-tile-txt"><b>${g.name}</b><span>${n} 个 · ${g.desc}</span></div>` })
|
||
tile.addEventListener('click', () => { homeFilter.group = g.id; homeFilter.kw = ''; input.value = ''; paintChips(); paintList(); listWrap.scrollIntoView({ behavior: 'smooth', block: 'start' }) })
|
||
tiles.append(tile)
|
||
}
|
||
// 找工具:首屏优先搜索,其次最近使用、分类入口、完整目录
|
||
const input = el('input', { type: 'search', placeholder: '搜索 500+ 个工具:JSON、哈希、图片、时间戳…', autocomplete: 'off' })
|
||
const chipRow = el('div', { class: 'find-chips' })
|
||
wrap.querySelector('[data-focus-search]')?.addEventListener('click', () => { input.focus(); input.scrollIntoView({ behavior: 'smooth', block: 'center' }) })
|
||
wrap.querySelector('[data-open-recent]')?.addEventListener('click', () => {
|
||
const recent = readList(REC).map((id) => byId(id)).filter(Boolean)
|
||
if (recent[0]) go(recent[0].id); else { input.focus(); input.scrollIntoView({ behavior: 'smooth', block: 'center' }) }
|
||
})
|
||
const listWrap = el('div', { class: 'find-list' })
|
||
const searchBox = el('div', { class: 'find-search' }, el('svg', { html: '<use href="#i-search"/>' }), input)
|
||
wrap.append(el('div', { class: 'findbar' }, searchBox, chipRow))
|
||
const recentRow = el('div', { class: 'recent-row' })
|
||
wrap.append(recentRow)
|
||
wrap.append(el('div', { class: 'section-label', html: '<span>按类别浏览</span><small>先选方向,再从工具列表继续</small>' }))
|
||
wrap.append(tiles)
|
||
wrap.append(el('div', { class: 'section-label tool-directory-label', html: '<span>完整工具目录</span><small>搜索或按类别筛选</small>' }))
|
||
wrap.append(listWrap)
|
||
const paintRecent = () => {
|
||
recentRow.textContent = ''
|
||
const list = readList(REC).map((id) => byId(id)).filter(Boolean).slice(0, 10)
|
||
if (!list.length) return
|
||
recentRow.append(el('span', { class: 'recent-cap', text: '最近用过' }))
|
||
for (const t of list) recentRow.append(el('button', { class: 'find-chip ghost', text: t.name, onclick: () => go(t.id) }))
|
||
}
|
||
|
||
const paintChips = () => {
|
||
chipRow.textContent = ''
|
||
const mk = (id, name, n) => {
|
||
const b = el('button', { class: 'find-chip' + (homeFilter.group === id ? ' on' : ''), text: n ? `${name} ${n}` : name })
|
||
b.addEventListener('click', () => { homeFilter.group = id; paintChips(); paintList() })
|
||
return b
|
||
}
|
||
chipRow.append(mk('all', '全部', tools.length))
|
||
for (const g of groups) {
|
||
const n = tools.filter((t) => t.group === g.id).length
|
||
if (n) chipRow.append(mk(g.id, g.name, n))
|
||
}
|
||
}
|
||
|
||
const paintList = () => {
|
||
listWrap.textContent = ''
|
||
const kw = homeFilter.kw.trim().toLowerCase()
|
||
let total = 0
|
||
for (const g of groups) {
|
||
if (homeFilter.group !== 'all' && homeFilter.group !== g.id) continue
|
||
const list = tools.filter((t) => t.group === g.id && match(t, kw))
|
||
if (!list.length) continue
|
||
total += list.length
|
||
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) {
|
||
const card = el('button', { class: 'card', onclick: () => go(t.id), html: `
|
||
<div class="card-ico">${icon(t.icon || 'box')}</div>
|
||
<h3></h3><p></p><div class="card-tag">${g.name}</div>` })
|
||
const c = card
|
||
c.querySelector('h3').textContent = t.name
|
||
c.querySelector('p').textContent = t.desc
|
||
const star = el('span', { class: 'card-star' + (isFav(t.id) ? ' on' : ''), text: '★', title: '收藏' })
|
||
star.addEventListener('click', (e) => { e.stopPropagation(); toggleFav(t.id); star.classList.toggle('on') })
|
||
c.append(star)
|
||
cards.append(card)
|
||
}
|
||
sec.append(cards)
|
||
listWrap.append(sec)
|
||
}
|
||
if (!total) listWrap.append(el('div', { class: 'nav-empty', text: '没找到,换个关键词试试(比如「时间」「随机」「图片」)' }))
|
||
}
|
||
|
||
input.addEventListener('input', () => { homeFilter.kw = input.value; paintList() })
|
||
paintChips(); paintList(); paintRecent()
|
||
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 + ' / <b>' + t.name + '</b>'
|
||
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 }))
|
||
const fav = el('button', { class: 'btn fav-btn' + (isFav(t.id) ? ' on' : ''), text: isFav(t.id) ? '★ 已收藏' : '☆ 收藏' })
|
||
fav.addEventListener('click', () => toggleFav(t.id))
|
||
tt.append(fav)
|
||
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) {
|
||
pushRecent(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 = '工具箱 / <b>管理后台</b>'
|
||
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 = `
|
||
<header class="hero">
|
||
<h1>关于 <em>DevBox</em></h1>
|
||
<p>一个纯前端的开发者工具箱。没有后端、没有依赖、没有追踪 —— 打开就能用,关掉就什么也不留。</p>
|
||
</header>
|
||
<div class="panel stack">
|
||
<dl class="kv">
|
||
<dt>技术</dt><dd>原生 HTML / CSS / JavaScript,0 依赖</dd>
|
||
<dt>隐私</dt><dd>所有计算在浏览器内完成,文件不离开你的设备</dd>
|
||
<dt>离线</dt><dd>双击 index.html 即可使用(file:// 也支持)</dd>
|
||
<dt>快捷键</dt><dd>⌘K / Ctrl+K 搜索工具 · Esc 关闭弹层</dd>
|
||
<dt>作者</dt><dd>syc65535</dd>
|
||
</dl>
|
||
<p class="note">加密工具用的是浏览器内置 Web Crypto(AES-GCM、PBKDF2、SHA-2),不是自造的算法;哈希不可逆,AES 才是可解密的。</p>
|
||
</div>`
|
||
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)}<span></span><small>${groupOf(t.group).name}</small>` })
|
||
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
|
||
})()
|