// Notebooks.jsx — 新建本子(建空本子 / 建子本子)· 浅色道林纸体系 // 保存三态:idle「创建」→ saving(转圈·禁用)→ done(「已创建」后关闭)→ err(红字·保留输入) const { useState: useStateNb, useEffect: useEffectNb, useRef: useRefNb } = React; function NewNotebookModal({ parent, notebooks, onCreate, onClose, onDeleted }) { const tops = (notebooks || []).filter(nb => !nb.parent); const [name, setName] = useStateNb(''); const [par, setPar] = useStateNb(parent || ''); const [status, setStatus] = useStateNb('idle'); // idle | saving | done | err const [msg, setMsg] = useStateNb(''); const inputRef = useRefNb(null); useEffectNb(() => { if (inputRef.current) inputRef.current.focus(); }, []); useEffectNb(() => { const onKey = e => { if (e.key === 'Escape' && status !== 'saving') onClose(); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [status, onClose]); const parNb = tops.find(t => t.id === par); function submit() { const nm = name.trim(); if (!nm || status === 'saving') return; setStatus('saving'); setMsg(''); onCreate(nm, par || null).then(res => { if (res && res.existed) { setStatus('done'); setMsg('已有同名本子,已为你选中'); setTimeout(onClose, 900); } else { setStatus('done'); setMsg('已创建'); setTimeout(onClose, 650); } }).catch(() => { setStatus('err'); setMsg('没创建成功,检查网络再试'); }); } const saving = status === 'saving'; const done = status === 'done'; return (
{ if (e.target === e.currentTarget && !saving) onClose(); }}>
{parNb ? '新建子本子' : '新建本子'}
{parNb ? 住在「{parNb.name}」下面的一格 : '一格新的本子,住进你的本子树'}
{parNb ? 子本子继承「{parNb.name}」的色系,更浅一档 : 颜色与图标会按名字自动落定,先不用挑}
{status === 'err' ? {msg} : done ? {msg} : }
{!parNb && (
管理已有本子(分享整本 = 生成目录链接,随本子更新;删除后,里面的页会移到「生活」)
{(() => { const all = notebooks || []; const tops = all.filter(n => !n.parent && n.name !== '生活'); const rows = []; tops.forEach(t2 => { rows.push(t2); all.filter(c => c.parent === t2.id).forEach(c => rows.push(c)); }); return rows.map(n => (
{n.parent ? '› ' : ''}{n.name}{n.count ? ' · ' + n.count + ' 页' : ''} {!n.parent && ( )}
)); })()}
)}
); } Object.assign(window, { NewNotebookModal });