@wagq/dsh-obsidian 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +149 -0
- package/README.md +148 -2
- package/client.js +1303 -0
- package/cordis.patch.yml +5 -0
- package/docs/DETAIL.md +3 -0
- package/docs/DETAIL.zh.md +3 -0
- package/docs/preview.svg +58 -0
- package/icon.svg +6 -0
- package/index.js +931 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +76 -4
package/client.js
ADDED
|
@@ -0,0 +1,1303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器端:@wagq/dsh-obsidian —— 知识库工作区
|
|
3
|
+
*
|
|
4
|
+
* 三栏布局:
|
|
5
|
+
* 左:文件库(类型筛选 chip + 搜索 + 文件夹/笔记树)
|
|
6
|
+
* 中:文档(面包屑 + 标题 + 标签 + 元信息 chip + 正文渲染/编辑)
|
|
7
|
+
* 右:大纲(标题跳转)与关联笔记(反链/出链)
|
|
8
|
+
*/
|
|
9
|
+
window.__ModuleLoader__.load({
|
|
10
|
+
id: '@wagq/dsh-obsidian',
|
|
11
|
+
factory(require) {
|
|
12
|
+
const React = require('react');
|
|
13
|
+
const h = React.createElement;
|
|
14
|
+
|
|
15
|
+
const PANEL_ID = 'wagq-dsh-obsidian';
|
|
16
|
+
const NS = 'kb-panel';
|
|
17
|
+
const API = '/kb/api';
|
|
18
|
+
const RAW = '/kb/raw';
|
|
19
|
+
|
|
20
|
+
const DICT = {
|
|
21
|
+
zh: {
|
|
22
|
+
panel: '知识库', vault: '知识库',
|
|
23
|
+
files: '文件库', search: '搜标题、标签或正文…', all: '全部', notes: '笔记',
|
|
24
|
+
pdf: 'PDF', images: '图片', others: '其他', folders: '文件夹',
|
|
25
|
+
newNote: '新建笔记', newFolder: '新建文件夹', refresh: '刷新',
|
|
26
|
+
preview: '预览', outline: '大纲', related: '关联', edit: '编辑', save: '保存',
|
|
27
|
+
saved: '已保存', saving: '保存中…', cancel: '取消', delete: '删除', rename: '重命名',
|
|
28
|
+
fullscreen: '全屏', exitFullscreen: '退出全屏', more: '更多', unsaved: '未保存',
|
|
29
|
+
selectNote: '从左侧选择一篇笔记开始阅读', empty: '这里还没有内容',
|
|
30
|
+
noOutline: '这篇笔记还没有标题', noRelated: '还没有关联笔记',
|
|
31
|
+
noResult: '没有匹配的内容', notConnected: '未连接知识库',
|
|
32
|
+
notConnectedHint: '请先在 Obsidian 中打开一个 vault,再点刷新。',
|
|
33
|
+
words: '字', chars: '字符', lines: '行', modified: '修改',
|
|
34
|
+
reading: '只读渲染', editing: '编辑中', linkOut: '出链', linkIn: '反链',
|
|
35
|
+
pathPlaceholder: '例如:00-收件箱/新笔记', confirmDelete: '确定把这篇笔记移入回收站?',
|
|
36
|
+
today: '今天', yesterday: '昨天', previewNote: '预览', backlinksTitle: '关联笔记',
|
|
37
|
+
toc: '大纲', untitled: '未命名', loading: '加载中…',
|
|
38
|
+
ask: '问答', askPlaceholder: '问整个知识库…(Ctrl+Enter 发送)', askSend: '提问',
|
|
39
|
+
asking: '正在检索并回答…', evidenceOnly: '未接入模型,已返回最相关的笔记片段',
|
|
40
|
+
citations: '出处', noEvidence: '知识库里没有找到相关内容', clear: '清空',
|
|
41
|
+
discardChanges: '当前笔记有未保存的更改,确定放弃并打开其他笔记?', searching: '搜索中…',
|
|
42
|
+
reload: '重新检测', create: '创建', continue: '继续', created: '已创建',
|
|
43
|
+
switchVault: '切换知识库 vault', noVaults: '没有发现其它 vault。请先在 Obsidian 中打开一个库。',
|
|
44
|
+
newMenu: '新建', createVault: '新建知识库项目', createVaultTitle: '新建知识库项目 — 位置', createVaultName: '新建知识库项目 — 名称',
|
|
45
|
+
next: '下一步', confirmDeleteFolder: '确定删除这个文件夹?(整棵子树移入 .trash)', deleted: '已删除', renamed: '已重命名', renameFolder: '重命名文件夹',
|
|
46
|
+
noteNamePlaceholder: '输入笔记名,如:产品需求(自动加 .md)',
|
|
47
|
+
folderNamePlaceholder: '输入文件夹名,如:需求文档',
|
|
48
|
+
open: '打开', hintContextMenu: '右键:新建 / 重命名 / 删除',
|
|
49
|
+
},
|
|
50
|
+
en: {
|
|
51
|
+
panel: 'Knowledge base', vault: 'Knowledge base',
|
|
52
|
+
files: 'Library', search: 'Search titles, tags or text…', all: 'All', notes: 'Notes',
|
|
53
|
+
pdf: 'PDF', images: 'Images', others: 'Others', folders: 'Folders',
|
|
54
|
+
newNote: 'New note', newFolder: 'New folder', refresh: 'Refresh',
|
|
55
|
+
preview: 'Preview', outline: 'Outline', related: 'Related', edit: 'Edit', save: 'Save',
|
|
56
|
+
saved: 'Saved', saving: 'Saving…', cancel: 'Cancel', delete: 'Delete', rename: 'Rename',
|
|
57
|
+
fullscreen: 'Fullscreen', exitFullscreen: 'Exit fullscreen', more: 'More', unsaved: 'Unsaved',
|
|
58
|
+
selectNote: 'Pick a note on the left to read', empty: 'Nothing here yet',
|
|
59
|
+
noOutline: 'No headings in this note', noRelated: 'No related notes',
|
|
60
|
+
noResult: 'No matches', notConnected: 'Not connected',
|
|
61
|
+
notConnectedHint: 'Open a vault in Obsidian, then refresh.',
|
|
62
|
+
words: 'words', chars: 'chars', lines: 'lines', modified: 'Modified',
|
|
63
|
+
reading: 'Rendered', editing: 'Editing', linkOut: 'outgoing', linkIn: 'backlinks',
|
|
64
|
+
pathPlaceholder: 'e.g. 00-inbox/New note', confirmDelete: 'Move this note to trash?',
|
|
65
|
+
today: 'Today', yesterday: 'Yesterday', previewNote: 'Preview', backlinksTitle: 'Related notes',
|
|
66
|
+
toc: 'Outline', untitled: 'Untitled', loading: 'Loading…',
|
|
67
|
+
ask: 'Q&A', askPlaceholder: 'Ask across the vault… (Ctrl+Enter)', askSend: 'Ask',
|
|
68
|
+
asking: 'Retrieving and answering…', evidenceOnly: 'No model attached — showing the most relevant passages',
|
|
69
|
+
citations: 'Sources', noEvidence: 'Nothing relevant found in the vault', clear: 'Clear',
|
|
70
|
+
discardChanges: 'This note has unsaved changes. Discard and open another note?', searching: 'Searching…',
|
|
71
|
+
reload: 'Redetect', create: 'Create', continue: 'Continue', created: 'Created',
|
|
72
|
+
switchVault: 'Switch vault', noVaults: 'No other vault found. Open one in Obsidian first.',
|
|
73
|
+
newMenu: 'New', createVault: 'New knowledge base project', createVaultTitle: 'New knowledge base — location', createVaultName: 'New knowledge base — name',
|
|
74
|
+
next: 'Next', confirmDeleteFolder: 'Delete this folder? (whole subtree moves to .trash)', deleted: 'Deleted', renamed: 'Renamed', renameFolder: 'Rename folder',
|
|
75
|
+
noteNamePlaceholder: 'Note name, e.g. Product brief (.md added)',
|
|
76
|
+
folderNamePlaceholder: 'Folder name, e.g. Requirements',
|
|
77
|
+
open: 'Open', hintContextMenu: 'Right-click: new / rename / delete',
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
let t = k => (DICT.en[k] ?? k);
|
|
81
|
+
|
|
82
|
+
async function api(path, body) {
|
|
83
|
+
const opt = body === undefined
|
|
84
|
+
? { method: 'GET' }
|
|
85
|
+
: { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) };
|
|
86
|
+
const r = await fetch(`${API}/${path}`, opt);
|
|
87
|
+
const ct = r.headers.get('content-type') || '';
|
|
88
|
+
if (!ct.includes('json')) throw new Error(`HTTP ${r.status}`);
|
|
89
|
+
return r.json();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const fmtDate = ms => {
|
|
93
|
+
if (!ms) return '';
|
|
94
|
+
const d = new Date(ms);
|
|
95
|
+
const p = n => String(n).padStart(2, '0');
|
|
96
|
+
return `${d.getFullYear()}/${p(d.getMonth() + 1)}/${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
97
|
+
};
|
|
98
|
+
const fmtSize = n => (n < 1024 ? `${n} B` : n < 1024 * 1024 ? `${(n / 1024).toFixed(0)} KB` : `${(n / 1048576).toFixed(1)} MB`);
|
|
99
|
+
|
|
100
|
+
// ── 极简 Markdown 渲染(标题/段落/列表/引用/代码/表格/链接/wikilink)──
|
|
101
|
+
function inline(text, key) {
|
|
102
|
+
const nodes = [];
|
|
103
|
+
let rest = String(text);
|
|
104
|
+
let i = 0;
|
|
105
|
+
const push = (node) => nodes.push(node);
|
|
106
|
+
const patterns = [
|
|
107
|
+
{ re: /^`([^`]+)`/, make: m => h('code', { key: `c${key}-${i++}`, style: { background: 'var(--dsw-alias-bg-layer-2)', padding: '1px 5px', borderRadius: 4, fontSize: '0.92em', fontFamily: 'ui-monospace, monospace' } }, m[1]) },
|
|
108
|
+
{ re: /^\*\*([^*]+)\*\*/, make: m => h('strong', { key: `b${key}-${i++}` }, m[1]) },
|
|
109
|
+
{ re: /^\*([^*]+)\*/, make: m => h('em', { key: `i${key}-${i++}` }, m[1]) },
|
|
110
|
+
{ re: /^\[\[([^\]|#]+)(?:\|([^\]]+))?\]\]/, make: m => h('span', { key: `w${key}-${i++}`, style: { color: 'var(--dsw-alias-brand-primary)', borderBottom: '1px dashed currentColor' }, title: m[1] }, m[2] || m[1]) },
|
|
111
|
+
{ re: /^\[([^\]]+)\]\(([^)]+)\)/, make: m => h('a', { key: `a${key}-${i++}`, href: m[2], target: '_blank', rel: 'noreferrer', style: { color: 'var(--dsw-alias-brand-primary)' } }, m[1]) },
|
|
112
|
+
{ re: /^#([\p{L}\p{N}_\-/]+)/u, make: m => h('span', { key: `t${key}-${i++}`, style: { color: 'var(--dsw-alias-brand-primary)' } }, `#${m[1]}`) },
|
|
113
|
+
];
|
|
114
|
+
while (rest.length) {
|
|
115
|
+
let matched = false;
|
|
116
|
+
for (const p of patterns) {
|
|
117
|
+
const m = rest.match(p.re);
|
|
118
|
+
if (m) { push(p.make(m)); rest = rest.slice(m[0].length); matched = true; break; }
|
|
119
|
+
}
|
|
120
|
+
if (matched) continue;
|
|
121
|
+
|
|
122
|
+
// 找到最近的下一个可匹配起点,把之前的纯文本一次性输出
|
|
123
|
+
let cut = rest.length;
|
|
124
|
+
for (const p of patterns) {
|
|
125
|
+
const probe = rest.slice(1);
|
|
126
|
+
const idx = probe.search(new RegExp(p.re.source));
|
|
127
|
+
if (idx >= 0) cut = Math.min(cut, idx + 1);
|
|
128
|
+
}
|
|
129
|
+
if (cut <= 1) { push(rest[0]); rest = rest.slice(1); }
|
|
130
|
+
else { push(rest.slice(0, cut)); rest = rest.slice(cut); }
|
|
131
|
+
}
|
|
132
|
+
return nodes;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function renderMarkdown(src, onHeading) {
|
|
136
|
+
const lines = String(src || '').replace(/\r/g, '').split('\n');
|
|
137
|
+
const out = [];
|
|
138
|
+
let i = 0, k = 0, hIndex = -1;
|
|
139
|
+
const H = (level, children, id) => h(`h${level}`, {
|
|
140
|
+
key: `h${k++}`, id: id !== undefined ? `kb-h-${id}` : undefined,
|
|
141
|
+
onClick: id !== undefined && onHeading ? () => onHeading(id) : undefined,
|
|
142
|
+
style: {
|
|
143
|
+
margin: level <= 2 ? '22px 0 10px' : '18px 0 8px',
|
|
144
|
+
fontSize: level === 1 ? 26 : level === 2 ? 21 : level === 3 ? 17.5 : 15.5,
|
|
145
|
+
fontWeight: level <= 2 ? 700 : 600,
|
|
146
|
+
lineHeight: 1.35,
|
|
147
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
148
|
+
cursor: id && onHeading ? 'pointer' : 'default',
|
|
149
|
+
},
|
|
150
|
+
}, ...children);
|
|
151
|
+
|
|
152
|
+
while (i < lines.length) {
|
|
153
|
+
const line = lines[i];
|
|
154
|
+
if (/^\s*$/.test(line)) { i++; continue; }
|
|
155
|
+
|
|
156
|
+
if (/^\s*```/.test(line)) {
|
|
157
|
+
const buf = [];
|
|
158
|
+
i++;
|
|
159
|
+
while (i < lines.length && !/^\s*```/.test(lines[i])) buf.push(lines[i++]);
|
|
160
|
+
i++;
|
|
161
|
+
out.push(h('pre', {
|
|
162
|
+
key: `pre${k++}`,
|
|
163
|
+
style: {
|
|
164
|
+
background: 'var(--dsw-alias-bg-layer-2)', border: '1px solid var(--dsw-alias-border-l1)',
|
|
165
|
+
borderRadius: 8, padding: '12px 14px', overflowX: 'auto', margin: '12px 0',
|
|
166
|
+
fontSize: 12.5, lineHeight: 1.6, fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
167
|
+
},
|
|
168
|
+
}, h('code', null, buf.join('\n'))));
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const hm = line.match(/^(#{1,6})\s+(.+?)\s*$/);
|
|
173
|
+
if (hm) {
|
|
174
|
+
const level = hm[1].length;
|
|
175
|
+
const text = hm[2];
|
|
176
|
+
const hid = String(++hIndex);
|
|
177
|
+
out.push(H(level, inline(text, `hd${k}`), hid));
|
|
178
|
+
i++;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (/^\s*(---|\*\*\*|___)\s*$/.test(line)) {
|
|
183
|
+
out.push(h('hr', { key: `hr${k++}`, style: { border: 'none', borderTop: '1px solid var(--dsw-alias-border-l1)', margin: '20px 0' } }));
|
|
184
|
+
i++;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (/^\s*>\s?/.test(line)) {
|
|
189
|
+
const buf = [];
|
|
190
|
+
while (i < lines.length && /^\s*>\s?/.test(lines[i])) buf.push(lines[i++].replace(/^\s*>\s?/, ''));
|
|
191
|
+
out.push(h('blockquote', {
|
|
192
|
+
key: `bq${k++}`,
|
|
193
|
+
style: {
|
|
194
|
+
margin: '12px 0', padding: '8px 14px', borderLeft: '3px solid var(--dsw-alias-brand-primary)',
|
|
195
|
+
background: 'var(--dsw-alias-bg-layer-2)', borderRadius: '0 6px 6px 0',
|
|
196
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
197
|
+
},
|
|
198
|
+
}, ...buf.filter(Boolean).map((b, bi) => h('div', { key: bi, style: { margin: '3px 0' } }, inline(b, `bq${k}-${bi}`)))));
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (/^\s*([-*+]|\d+\.)\s+/.test(line)) {
|
|
203
|
+
const items = [];
|
|
204
|
+
const ordered = /^\s*\d+\./.test(line);
|
|
205
|
+
while (i < lines.length && /^\s*([-*+]|\d+\.)\s+/.test(lines[i])) {
|
|
206
|
+
items.push(lines[i].replace(/^\s*([-*+]|\d+\.)\s+/, ''));
|
|
207
|
+
i++;
|
|
208
|
+
}
|
|
209
|
+
const Tag = ordered ? 'ol' : 'ul';
|
|
210
|
+
out.push(h(Tag, {
|
|
211
|
+
key: `list${k++}`,
|
|
212
|
+
style: { margin: '10px 0', paddingLeft: 22, lineHeight: 1.75 },
|
|
213
|
+
}, ...items.map((it, ii) => h('li', { key: ii, style: { margin: '2px 0' } }, inline(it, `li${k}-${ii}`)))));
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 表格
|
|
218
|
+
if (/^\s*\|.*\|\s*$/.test(line) && i + 1 < lines.length && /^\s*\|[\s:|-]+\|\s*$/.test(lines[i + 1])) {
|
|
219
|
+
const head = line.split('|').slice(1, -1).map(s => s.trim());
|
|
220
|
+
i += 2;
|
|
221
|
+
const rows = [];
|
|
222
|
+
while (i < lines.length && /^\s*\|.*\|\s*$/.test(lines[i])) {
|
|
223
|
+
rows.push(lines[i].split('|').slice(1, -1).map(s => s.trim()));
|
|
224
|
+
i++;
|
|
225
|
+
}
|
|
226
|
+
out.push(h('div', { key: `tb${k++}`, style: { overflowX: 'auto', margin: '14px 0' } },
|
|
227
|
+
h('table', { style: { borderCollapse: 'collapse', width: '100%', fontSize: 13 } },
|
|
228
|
+
h('thead', null, h('tr', null, ...head.map((c, ci) => h('th', {
|
|
229
|
+
key: ci,
|
|
230
|
+
style: { textAlign: 'left', padding: '8px 10px', borderBottom: '1px solid var(--dsw-alias-border-l2)', background: 'var(--dsw-alias-bg-layer-2)', fontWeight: 600 },
|
|
231
|
+
}, inline(c, `th${k}-${ci}`))))),
|
|
232
|
+
h('tbody', null, ...rows.map((r, ri) => h('tr', { key: ri }, ...r.map((c, ci) => h('td', {
|
|
233
|
+
key: ci,
|
|
234
|
+
style: { padding: '7px 10px', borderBottom: '1px solid var(--dsw-alias-border-l1)' },
|
|
235
|
+
}, inline(c, `td${k}-${ri}-${ci}`)))))),
|
|
236
|
+
)));
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// 普通段落:合并连续行
|
|
241
|
+
const para = [line];
|
|
242
|
+
i++;
|
|
243
|
+
while (i < lines.length && !/^\s*$/.test(lines[i]) && !/^(#{1,6})\s|^\s*```|^\s*>|^\s*([-*+]|\d+\.)\s|^\s*\|/.test(lines[i])) {
|
|
244
|
+
para.push(lines[i++]);
|
|
245
|
+
}
|
|
246
|
+
out.push(h('p', {
|
|
247
|
+
key: `p${k++}`,
|
|
248
|
+
style: { margin: '11px 0', lineHeight: 1.85, fontSize: 14.5, color: 'var(--dsw-alias-label-primary)' },
|
|
249
|
+
}, ...inline(para.join(' '), `p${k}`)));
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function BookGlyph({ size = 15 }) {
|
|
255
|
+
return h('svg', {
|
|
256
|
+
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
257
|
+
stroke: 'currentColor', strokeWidth: 2.1, strokeLinecap: 'round',
|
|
258
|
+
strokeLinejoin: 'round', 'aria-hidden': true, focusable: false,
|
|
259
|
+
style: { display: 'block', flexShrink: 0 },
|
|
260
|
+
},
|
|
261
|
+
h('rect', { x: 3.5, y: 3.5, width: 17, height: 17, rx: 3 }),
|
|
262
|
+
h('path', { d: 'M8 3.5v17' }),
|
|
263
|
+
h('path', { d: 'M11.5 8h5.5M11.5 12h5.5M11.5 16h3.5' }),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const Icon = ({ d, size = 14 }) => h('svg', {
|
|
268
|
+
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
269
|
+
stroke: 'currentColor', strokeWidth: 1.8, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
270
|
+
'aria-hidden': true, focusable: false, style: { flexShrink: 0 },
|
|
271
|
+
}, h('path', { d }));
|
|
272
|
+
|
|
273
|
+
// ═══════════════ 主工作区 ═══════════════
|
|
274
|
+
function KnowledgePanel() {
|
|
275
|
+
const [status, setStatus] = React.useState(null);
|
|
276
|
+
const [files, setFiles] = React.useState([]);
|
|
277
|
+
const [filter, setFilter] = React.useState('all');
|
|
278
|
+
const [query, setQuery] = React.useState('');
|
|
279
|
+
const [results, setResults] = React.useState(null);
|
|
280
|
+
const [selected, setSelected] = React.useState('');
|
|
281
|
+
const [note, setNote] = React.useState(null);
|
|
282
|
+
const [related, setRelated] = React.useState([]);
|
|
283
|
+
const [draft, setDraft] = React.useState('');
|
|
284
|
+
const [mode, setMode] = React.useState('read');
|
|
285
|
+
const [rightTab, setRightTab] = React.useState('outline');
|
|
286
|
+
const [askQ, setAskQ] = React.useState('');
|
|
287
|
+
const [askBusy, setAskBusy] = React.useState(false);
|
|
288
|
+
const [askRes, setAskRes] = React.useState(null);
|
|
289
|
+
const [notice, setNotice] = React.useState('');
|
|
290
|
+
const [busy, setBusy] = React.useState(false);
|
|
291
|
+
const [collapsed, setCollapsed] = React.useState({});
|
|
292
|
+
const [full, setFull] = React.useState(false);
|
|
293
|
+
// 内联输入/确认:DSH 的 WebView 禁用了 window.prompt/confirm,必须自带
|
|
294
|
+
const [dialog, setDialog] = React.useState(null);
|
|
295
|
+
// dialog: { kind:'prompt'|'confirm', title, value, placeholder, confirmText, onOk }
|
|
296
|
+
const [picker, setPicker] = React.useState(null);
|
|
297
|
+
const [menu, setMenu] = React.useState(null);
|
|
298
|
+
const rootRef = React.useRef(null);
|
|
299
|
+
// 把视口坐标换算成面板内坐标
|
|
300
|
+
const toLocal = (clientX, clientY) => {
|
|
301
|
+
const r = rootRef.current?.getBoundingClientRect?.();
|
|
302
|
+
return r ? { x: clientX - r.left, y: clientY - r.top } : { x: 40, y: 40 };
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const dirty = note && draft !== note.body;
|
|
306
|
+
|
|
307
|
+
const loadTree = React.useCallback(async () => {
|
|
308
|
+
try {
|
|
309
|
+
const r = await api('tree');
|
|
310
|
+
if (r.ok) setFiles(r.files || []);
|
|
311
|
+
} catch (e) { setNotice(String(e)); }
|
|
312
|
+
}, []);
|
|
313
|
+
|
|
314
|
+
const loadStatus = React.useCallback(async () => {
|
|
315
|
+
try { setStatus(await api('status')); } catch (e) { setNotice(String(e)); }
|
|
316
|
+
}, []);
|
|
317
|
+
|
|
318
|
+
React.useEffect(() => { loadStatus(); loadTree(); }, [loadStatus, loadTree]);
|
|
319
|
+
|
|
320
|
+
// 全屏时:Esc 退出、锁定页面滚动、卸载时复位
|
|
321
|
+
React.useEffect(() => {
|
|
322
|
+
if (!full) return undefined;
|
|
323
|
+
const onKey = e => { if (e.key === 'Escape') setFull(false); };
|
|
324
|
+
window.addEventListener('keydown', onKey);
|
|
325
|
+
const prevOverflow = document.body.style.overflow;
|
|
326
|
+
document.body.style.overflow = 'hidden';
|
|
327
|
+
return () => {
|
|
328
|
+
window.removeEventListener('keydown', onKey);
|
|
329
|
+
document.body.style.overflow = prevOverflow;
|
|
330
|
+
};
|
|
331
|
+
}, [full]);
|
|
332
|
+
|
|
333
|
+
const openNote = async (p) => {
|
|
334
|
+
if (dirty && p !== selected) {
|
|
335
|
+
const ok = await askConfirm({ title: t('discardChanges'), confirmText: t('continue') });
|
|
336
|
+
if (!ok) return;
|
|
337
|
+
}
|
|
338
|
+
setBusy(true);
|
|
339
|
+
try {
|
|
340
|
+
const r = await api(`note?path=${encodeURIComponent(p)}`);
|
|
341
|
+
if (!r.ok) { setNotice(r.error || 'open failed'); return; }
|
|
342
|
+
setSelected(p); setNote(r.note); setDraft(r.note.body); setMode('read'); setNotice('');
|
|
343
|
+
const rel = await api(`related?path=${encodeURIComponent(p)}`);
|
|
344
|
+
setRelated(rel.ok ? rel.related : []);
|
|
345
|
+
} catch (e) { setNotice(String(e)); }
|
|
346
|
+
finally { setBusy(false); }
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const askVault = async () => {
|
|
350
|
+
const q = askQ.trim();
|
|
351
|
+
if (!q) return;
|
|
352
|
+
setAskBusy(true); setAskRes(null);
|
|
353
|
+
try {
|
|
354
|
+
const r = await api('ask', { question: q });
|
|
355
|
+
if (r.ok) setAskRes(r);
|
|
356
|
+
else setNotice(r.error || 'ask failed');
|
|
357
|
+
} catch (e) { setNotice(String(e)); }
|
|
358
|
+
finally { setAskBusy(false); }
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const runSearch = React.useCallback(async (q) => {
|
|
362
|
+
setQuery(q);
|
|
363
|
+
if (!q.trim()) { setResults(null); return; }
|
|
364
|
+
try {
|
|
365
|
+
const r = await api(`search?query=${encodeURIComponent(q)}`);
|
|
366
|
+
setResults(r.ok ? r.results : []);
|
|
367
|
+
} catch (e) { setNotice(String(e)); }
|
|
368
|
+
}, []);
|
|
369
|
+
|
|
370
|
+
const save = async () => {
|
|
371
|
+
if (!note) return;
|
|
372
|
+
setBusy(true);
|
|
373
|
+
try {
|
|
374
|
+
const r = await api('save', { path: note.path, content: draft });
|
|
375
|
+
if (r.ok) {
|
|
376
|
+
const fresh = await api(`note?path=${encodeURIComponent(note.path)}`);
|
|
377
|
+
if (fresh.ok) { setNote(fresh.note); setDraft(fresh.note.body); }
|
|
378
|
+
setMode('read');
|
|
379
|
+
setNotice(t('saved')); loadTree();
|
|
380
|
+
} else setNotice(r.error || 'save failed');
|
|
381
|
+
} catch (e) { setNotice(String(e)); }
|
|
382
|
+
finally { setBusy(false); }
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// DSH 的 WebView 禁用了 window.prompt / window.confirm,
|
|
386
|
+
// 所有需要输入的交互都走这里的内联对话框。
|
|
387
|
+
// 用 ref 保存 resolve 与当前值,避免在闭包里读到过期的 state。
|
|
388
|
+
const dialogRef = React.useRef(null);
|
|
389
|
+
const dialogValueRef = React.useRef('');
|
|
390
|
+
const askPrompt = (opts) => new Promise(resolve => {
|
|
391
|
+
dialogRef.current = { kind: 'prompt', resolve };
|
|
392
|
+
dialogValueRef.current = opts.value ?? '';
|
|
393
|
+
setDialog({
|
|
394
|
+
kind: 'prompt',
|
|
395
|
+
title: opts.title,
|
|
396
|
+
value: opts.value ?? '',
|
|
397
|
+
placeholder: opts.placeholder ?? '',
|
|
398
|
+
confirmText: opts.confirmText ?? t('save'),
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
const askConfirm = (opts) => new Promise(resolve => {
|
|
402
|
+
dialogRef.current = { kind: 'confirm', resolve };
|
|
403
|
+
dialogValueRef.current = '';
|
|
404
|
+
setDialog({
|
|
405
|
+
kind: 'confirm',
|
|
406
|
+
title: opts.title,
|
|
407
|
+
confirmText: opts.confirmText ?? t('delete'),
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
const closeDialog = (value) => {
|
|
411
|
+
const pending = dialogRef.current;
|
|
412
|
+
dialogRef.current = null;
|
|
413
|
+
setDialog(null);
|
|
414
|
+
try { pending?.resolve?.(value); } catch { /* ignore */ }
|
|
415
|
+
};
|
|
416
|
+
const submitDialog = () => {
|
|
417
|
+
const pending = dialogRef.current;
|
|
418
|
+
const kind = pending?.kind;
|
|
419
|
+
dialogRef.current = null;
|
|
420
|
+
setDialog(null);
|
|
421
|
+
const value = kind === 'confirm' ? true : dialogValueRef.current;
|
|
422
|
+
try { pending?.resolve?.(value); } catch { /* ignore */ }
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// 从当前 vault 路径推出父目录,作为新建项目的默认位置
|
|
426
|
+
const vaultParentGuess = () => {
|
|
427
|
+
const p = status?.vaultPath || '';
|
|
428
|
+
if (!p) return '';
|
|
429
|
+
const norm = p.replace(/\//g, '\\').replace(/\\+$/, '');
|
|
430
|
+
const idx = norm.lastIndexOf('\\');
|
|
431
|
+
return idx > 0 ? norm.slice(0, idx) : norm;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
const switchVault = async (p) => { setPicker(null);
|
|
435
|
+
setBusy(true);
|
|
436
|
+
try {
|
|
437
|
+
const r = await api('switch', { path: p });
|
|
438
|
+
if (r.ok) {
|
|
439
|
+
setStatus(r);
|
|
440
|
+
setSelected(''); setNote(null); setDraft(''); setRelated([]); setResults(null); setQuery('');
|
|
441
|
+
await loadTree();
|
|
442
|
+
setNotice('');
|
|
443
|
+
} else setNotice(r.error || 'switch failed');
|
|
444
|
+
} catch (e) { setNotice(String(e)); }
|
|
445
|
+
finally { setBusy(false); }
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const removeFolder = async (rel, name) => {
|
|
449
|
+
const ok = await askConfirm({
|
|
450
|
+
title: `${t('confirmDeleteFolder')}\n\n${rel}`,
|
|
451
|
+
confirmText: t('delete'),
|
|
452
|
+
});
|
|
453
|
+
if (!ok) return;
|
|
454
|
+
setBusy(true);
|
|
455
|
+
try {
|
|
456
|
+
const r = await api('rmdir', { path: rel });
|
|
457
|
+
if (r.ok) { await loadTree(); await loadStatus(); setNotice(`${t('deleted')}: ${rel}${r.result.files ? ` (${r.result.files} 个文件)` : ''}`); }
|
|
458
|
+
else setNotice(r.error || 'rmdir failed');
|
|
459
|
+
} catch (e) { setNotice(String(e)); }
|
|
460
|
+
finally { setBusy(false); }
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
const renameFolder = async (rel, name) => {
|
|
464
|
+
const to = await askPrompt({ title: t('renameFolder'), value: name, confirmText: t('save') });
|
|
465
|
+
if (!to || !to.trim() || to.trim() === name) return;
|
|
466
|
+
const parent = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : '';
|
|
467
|
+
const target = parent ? `${parent}/${to.trim()}` : to.trim();
|
|
468
|
+
setBusy(true);
|
|
469
|
+
try {
|
|
470
|
+
const r = await api('move', { from: rel, to: target });
|
|
471
|
+
if (r.ok) { await loadTree(); setNotice(`${t('renamed')}: ${target}`); }
|
|
472
|
+
else setNotice(r.error || 'rename failed');
|
|
473
|
+
} catch (e) { setNotice(String(e)); }
|
|
474
|
+
finally { setBusy(false); }
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const createVaultProject = async () => {
|
|
478
|
+
setPicker(null);
|
|
479
|
+
const parent = await askPrompt({
|
|
480
|
+
title: t('createVaultTitle'),
|
|
481
|
+
value: vaultParentGuess(),
|
|
482
|
+
placeholder: 'D:\\知识库',
|
|
483
|
+
confirmText: t('next'),
|
|
484
|
+
});
|
|
485
|
+
if (!parent || !parent.trim()) return;
|
|
486
|
+
const name = await askPrompt({
|
|
487
|
+
title: `${t('createVaultName')}\n${parent.trim()}`,
|
|
488
|
+
value: '',
|
|
489
|
+
placeholder: '我的新知识库',
|
|
490
|
+
confirmText: t('create'),
|
|
491
|
+
});
|
|
492
|
+
if (!name || !name.trim()) return;
|
|
493
|
+
setBusy(true);
|
|
494
|
+
try {
|
|
495
|
+
const r = await api('create-vault', { parent: parent.trim(), name: name.trim() });
|
|
496
|
+
if (r.ok) {
|
|
497
|
+
setStatus(r);
|
|
498
|
+
setSelected(''); setNote(null); setDraft(''); setRelated([]); setResults(null); setQuery('');
|
|
499
|
+
await loadTree();
|
|
500
|
+
setNotice(`${t('created')}: ${r.result.path}`);
|
|
501
|
+
} else setNotice(r.error || 'create vault failed');
|
|
502
|
+
} catch (e) { setNotice(String(e)); }
|
|
503
|
+
finally { setBusy(false); }
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
// parent:在哪个文件夹里新建(空字符串 = vault 根目录)
|
|
507
|
+
const joinPath = (parent, name) => (parent ? `${parent}/${name}` : name);
|
|
508
|
+
|
|
509
|
+
const createNote = async (parent = '') => {
|
|
510
|
+
const name = await askPrompt({
|
|
511
|
+
title: parent ? `${t('newNote')} · ${parent}` : t('newNote'),
|
|
512
|
+
placeholder: t('noteNamePlaceholder'),
|
|
513
|
+
confirmText: t('create'),
|
|
514
|
+
});
|
|
515
|
+
if (!name || !name.trim()) return;
|
|
516
|
+
const rel = joinPath(parent, name.trim().replace(/^\/+/, ''));
|
|
517
|
+
setBusy(true);
|
|
518
|
+
try {
|
|
519
|
+
const r = await api('create', { path: rel });
|
|
520
|
+
if (r.ok) { await loadTree(); await openNote(r.result.path); setNotice(''); }
|
|
521
|
+
else setNotice(r.error || 'create failed');
|
|
522
|
+
} catch (e) { setNotice(String(e)); }
|
|
523
|
+
finally { setBusy(false); }
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
const createFolder = async (parent = '') => {
|
|
527
|
+
const name = await askPrompt({
|
|
528
|
+
title: parent ? `${t('newFolder')} · ${parent}` : t('newFolder'),
|
|
529
|
+
placeholder: t('folderNamePlaceholder'),
|
|
530
|
+
confirmText: t('create'),
|
|
531
|
+
});
|
|
532
|
+
if (!name || !name.trim()) return;
|
|
533
|
+
const rel = joinPath(parent, name.trim().replace(/^\/+/, ''));
|
|
534
|
+
setBusy(true);
|
|
535
|
+
try {
|
|
536
|
+
const r = await api('mkdir', { path: rel });
|
|
537
|
+
if (r.ok) { await loadTree(); await loadStatus(); setNotice(`${t('created')}: ${r.result.path}`); }
|
|
538
|
+
else setNotice(r.error || 'mkdir failed');
|
|
539
|
+
} catch (e) { setNotice(String(e)); }
|
|
540
|
+
finally { setBusy(false); }
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
const removeNote = async () => {
|
|
544
|
+
if (!note) return;
|
|
545
|
+
if (!(await askConfirm({ title: t('confirmDelete'), confirmText: t('delete') }))) return;
|
|
546
|
+
try {
|
|
547
|
+
const r = await api('delete', { path: note.path });
|
|
548
|
+
if (r.ok) { setSelected(''); setNote(null); setDraft(''); setRelated([]); loadTree(); }
|
|
549
|
+
else setNotice(r.error || 'delete failed');
|
|
550
|
+
} catch (e) { setNotice(String(e)); }
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
const renameNote = async () => {
|
|
554
|
+
if (!note) return;
|
|
555
|
+
const to = await askPrompt({ title: t('rename'), value: note.path, confirmText: t('save') });
|
|
556
|
+
if (!to || to.trim() === note.path) return;
|
|
557
|
+
try {
|
|
558
|
+
if (dirty) await api('save', { path: note.path, content: draft });
|
|
559
|
+
const r = await api('move', { from: note.path, to: to.trim() });
|
|
560
|
+
if (r.ok) { await loadTree(); await openNote(r.result.to); }
|
|
561
|
+
else setNotice(r.error || 'rename failed');
|
|
562
|
+
} catch (e) { setNotice(String(e)); }
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// 右键菜单里按路径重命名笔记
|
|
566
|
+
const renameNoteAt = async (rel) => {
|
|
567
|
+
const base = rel.split('/').pop() || rel;
|
|
568
|
+
const name = await askPrompt({ title: t('rename'), value: base.replace(/\.md$/i, ''), confirmText: t('save') });
|
|
569
|
+
if (!name || !name.trim()) return;
|
|
570
|
+
const parent = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : '';
|
|
571
|
+
const target = joinPath(parent, name.trim().replace(/^\/+/, ''));
|
|
572
|
+
setBusy(true);
|
|
573
|
+
try {
|
|
574
|
+
if (selected === rel && dirty) await api('save', { path: rel, content: draft });
|
|
575
|
+
const r = await api('move', { from: rel, to: target });
|
|
576
|
+
if (r.ok) {
|
|
577
|
+
await loadTree();
|
|
578
|
+
if (selected === rel) await openNote(r.result.to);
|
|
579
|
+
setNotice(`${t('renamed')}: ${target}`);
|
|
580
|
+
} else setNotice(r.error || 'rename failed');
|
|
581
|
+
} catch (e) { setNotice(String(e)); }
|
|
582
|
+
finally { setBusy(false); }
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
// 右键菜单里按路径删除笔记
|
|
586
|
+
const removeNoteAt = async (rel) => {
|
|
587
|
+
const ok = await askConfirm({ title: `${t('confirmDelete')}\n\n${rel}`, confirmText: t('delete') });
|
|
588
|
+
if (!ok) return;
|
|
589
|
+
setBusy(true);
|
|
590
|
+
try {
|
|
591
|
+
const r = await api('delete', { path: rel });
|
|
592
|
+
if (r.ok) {
|
|
593
|
+
if (selected === rel) { setSelected(''); setNote(null); setDraft(''); setRelated([]); }
|
|
594
|
+
await loadTree(); await loadStatus();
|
|
595
|
+
setNotice(`${t('deleted')}: ${rel}`);
|
|
596
|
+
} else setNotice(r.error || 'delete failed');
|
|
597
|
+
} catch (e) { setNotice(String(e)); }
|
|
598
|
+
finally { setBusy(false); }
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// ── 样式 ──
|
|
602
|
+
const C = {
|
|
603
|
+
text: 'var(--dsw-alias-label-primary, CanvasText)',
|
|
604
|
+
text2: 'var(--dsw-alias-label-secondary, GrayText)',
|
|
605
|
+
bg: 'var(--dsw-alias-bg-base, Canvas)',
|
|
606
|
+
bg1: 'var(--dsw-alias-bg-layer-1, Canvas)',
|
|
607
|
+
bg2: 'var(--dsw-alias-bg-layer-2, ButtonFace)',
|
|
608
|
+
overlay: 'var(--dsw-alias-bg-overlay, ButtonFace)',
|
|
609
|
+
line: 'var(--dsw-alias-border-l1, rgba(128,128,128,.22))',
|
|
610
|
+
line2: 'var(--dsw-alias-border-l2, rgba(128,128,128,.38))',
|
|
611
|
+
brand: 'var(--dsw-alias-brand-primary, #6b5cff)',
|
|
612
|
+
err: 'var(--dsw-alias-state-error-primary, #d33)',
|
|
613
|
+
warn: 'var(--dsw-alias-state-warn-primary, #c98b00)',
|
|
614
|
+
ok: 'var(--dsw-alias-state-success-primary, #2ea043)',
|
|
615
|
+
side: 'var(--dsw-specific-sidebar-fill, Canvas)',
|
|
616
|
+
onBrand: 'var(--dsw-alias-bg-base, #ffffff)',
|
|
617
|
+
};
|
|
618
|
+
const S = {
|
|
619
|
+
root: {
|
|
620
|
+
display: 'flex', height: full ? 'calc(100vh - 34px)' : '100%', minHeight: 0, boxSizing: 'border-box',
|
|
621
|
+
color: C.text, background: C.bg, fontSize: 13,
|
|
622
|
+
overflow: 'hidden',
|
|
623
|
+
position: full ? 'fixed' : 'relative',
|
|
624
|
+
...(full
|
|
625
|
+
? { top: 34, left: 0, right: 0, zIndex: 95, width: '100vw' }
|
|
626
|
+
: {}),
|
|
627
|
+
},
|
|
628
|
+
colLeft: {
|
|
629
|
+
width: 268, flexShrink: 0, display: 'flex', flexDirection: 'column', minHeight: 0,
|
|
630
|
+
borderRight: `1px solid ${C.line}`, background: C.side,
|
|
631
|
+
},
|
|
632
|
+
colMid: { flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', minHeight: 0, background: C.bg },
|
|
633
|
+
colRight: {
|
|
634
|
+
width: 252, flexShrink: 0, display: 'flex', flexDirection: 'column', minHeight: 0,
|
|
635
|
+
borderLeft: `1px solid ${C.line}`, background: C.side,
|
|
636
|
+
},
|
|
637
|
+
bar: { display: 'flex', alignItems: 'center', gap: 6, padding: '9px 10px', borderBottom: `1px solid ${C.line}`, flexShrink: 0 },
|
|
638
|
+
barTitle: { fontWeight: 600, fontSize: 13.5, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
639
|
+
iconBtn: {
|
|
640
|
+
display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 4,
|
|
641
|
+
border: 'none', background: 'transparent', color: C.text2, cursor: 'pointer',
|
|
642
|
+
borderRadius: 6, padding: '5px 7px', fontSize: 12, fontFamily: 'inherit',
|
|
643
|
+
},
|
|
644
|
+
btn: {
|
|
645
|
+
display: 'inline-flex', alignItems: 'center', gap: 5,
|
|
646
|
+
border: `1px solid ${C.line2}`, background: C.bg2, color: C.text,
|
|
647
|
+
borderRadius: 6, padding: '5px 10px', cursor: 'pointer', fontSize: 12, fontFamily: 'inherit',
|
|
648
|
+
},
|
|
649
|
+
btnOn: { background: C.bg1, color: C.text, borderColor: C.brand },
|
|
650
|
+
btnPrimary: {
|
|
651
|
+
display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 5,
|
|
652
|
+
border: `1px solid ${C.brand}`, background: C.brand, color: C.onBrand,
|
|
653
|
+
borderRadius: 6, padding: '6px 12px', cursor: 'pointer', fontSize: 12,
|
|
654
|
+
fontFamily: 'inherit', fontWeight: 600,
|
|
655
|
+
},
|
|
656
|
+
input: {
|
|
657
|
+
width: '100%', boxSizing: 'border-box', background: C.bg1, color: C.text,
|
|
658
|
+
border: `1px solid ${C.line}`, borderRadius: 6, padding: '6px 9px',
|
|
659
|
+
fontSize: 12, fontFamily: 'inherit', outline: 'none',
|
|
660
|
+
},
|
|
661
|
+
chips: { display: 'flex', gap: 6, flexWrap: 'wrap', padding: '8px 10px', borderBottom: `1px solid ${C.line}`, flexShrink: 0 },
|
|
662
|
+
chip: {
|
|
663
|
+
border: 'none', background: 'transparent', color: C.text2, cursor: 'pointer',
|
|
664
|
+
borderRadius: 999, padding: '3px 9px', fontSize: 11.5, fontFamily: 'inherit',
|
|
665
|
+
},
|
|
666
|
+
chipOn: {
|
|
667
|
+
background: C.bg1, color: C.text, fontWeight: 600,
|
|
668
|
+
boxShadow: `inset 0 0 0 1.5px ${C.brand}`,
|
|
669
|
+
padding: '3px 9px',
|
|
670
|
+
}, tree: { flex: 1, minHeight: 0, overflowY: 'auto', padding: '6px 6px 14px' },
|
|
671
|
+
row: {
|
|
672
|
+
display: 'flex', alignItems: 'center', gap: 6, padding: '4px 7px',
|
|
673
|
+
borderRadius: 5, cursor: 'pointer', fontSize: 12.5, color: C.text,
|
|
674
|
+
whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
|
|
675
|
+
},
|
|
676
|
+
rowSel: { background: C.bg2, fontWeight: 600 },
|
|
677
|
+
rowText: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: 1, textAlign: 'left' },
|
|
678
|
+
doc: { flex: 1, minHeight: 0, overflowY: 'auto', padding: '22px 40px 80px' },
|
|
679
|
+
crumb: { display: 'flex', alignItems: 'center', gap: 5, fontSize: 11.5, color: C.text2, marginBottom: 10, flexWrap: 'wrap' },
|
|
680
|
+
docTitle: { fontSize: 27, fontWeight: 700, lineHeight: 1.3, margin: '0 0 10px', letterSpacing: '-0.3px' },
|
|
681
|
+
tagRow: { display: 'flex', gap: 6, flexWrap: 'wrap', marginBottom: 12 },
|
|
682
|
+
tag: {
|
|
683
|
+
fontSize: 11, padding: '2px 8px', borderRadius: 999,
|
|
684
|
+
border: `1px solid ${C.line2}`, color: C.text2, background: C.bg1,
|
|
685
|
+
},
|
|
686
|
+
metaRow: { display: 'flex', gap: 12, flexWrap: 'wrap', fontSize: 11.5, color: C.text2, marginBottom: 18, alignItems: 'center' },
|
|
687
|
+
loader: { fontSize: 11.5, color: C.text2, padding: '3px 8px', borderRadius: 5, background: C.bg2 },
|
|
688
|
+
textarea: {
|
|
689
|
+
display: 'block',
|
|
690
|
+
width: '100%',
|
|
691
|
+
boxSizing: 'border-box',
|
|
692
|
+
height: 460,
|
|
693
|
+
minHeight: 240,
|
|
694
|
+
resize: 'vertical',
|
|
695
|
+
background: C.bg2,
|
|
696
|
+
color: C.text,
|
|
697
|
+
border: `1px solid ${C.line2}`,
|
|
698
|
+
borderRadius: 8,
|
|
699
|
+
padding: 14,
|
|
700
|
+
fontSize: 13,
|
|
701
|
+
lineHeight: 1.85,
|
|
702
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
703
|
+
outline: 'none',
|
|
704
|
+
},
|
|
705
|
+
tabs: {
|
|
706
|
+
display: 'flex', gap: 2, padding: '0 6px', flexShrink: 0,
|
|
707
|
+
borderBottom: `1px solid ${C.line}`, minHeight: 41, alignItems: 'flex-end',
|
|
708
|
+
boxSizing: 'border-box',
|
|
709
|
+
},
|
|
710
|
+
tab: {
|
|
711
|
+
borderTop: 'none', borderLeft: 'none', borderRight: 'none',
|
|
712
|
+
background: 'transparent', color: C.text2, cursor: 'pointer',
|
|
713
|
+
padding: '8px 9px', fontSize: 12, fontFamily: 'inherit',
|
|
714
|
+
borderBottom: '2px solid transparent', marginBottom: -1, lineHeight: 1.5,
|
|
715
|
+
},
|
|
716
|
+
tabOn: { color: C.text, borderBottom: `2px solid ${C.brand}`, fontWeight: 600 },
|
|
717
|
+
rightBody: { flex: 1, minHeight: 0, overflowY: 'auto', padding: '8px 8px 20px' },
|
|
718
|
+
outRow: {
|
|
719
|
+
display: 'block', width: '100%', textAlign: 'left', border: 'none',
|
|
720
|
+
background: 'transparent', color: C.text, cursor: 'pointer',
|
|
721
|
+
padding: '4px 8px', borderRadius: 5, fontSize: 12.5, fontFamily: 'inherit',
|
|
722
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
723
|
+
},
|
|
724
|
+
relRow: {
|
|
725
|
+
display: 'flex', alignItems: 'center', gap: 6, width: '100%',
|
|
726
|
+
border: 'none', background: 'transparent', color: C.text, cursor: 'pointer',
|
|
727
|
+
padding: '6px 8px', borderRadius: 5, fontSize: 12, fontFamily: 'inherit',
|
|
728
|
+
textAlign: 'left',
|
|
729
|
+
},
|
|
730
|
+
rowBtn: {
|
|
731
|
+
display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
732
|
+
border: 'none', background: 'transparent', color: C.text2, cursor: 'pointer',
|
|
733
|
+
padding: 2, borderRadius: 4, opacity: 0.65, flexShrink: 0,
|
|
734
|
+
},
|
|
735
|
+
empty: { padding: 28, textAlign: 'center', color: C.text2, fontSize: 12.5, lineHeight: 1.8 },
|
|
736
|
+
notice: { margin: '0 10px 8px', fontSize: 11.5, color: C.warn, background: C.bg2, borderRadius: 6, padding: '6px 9px' },
|
|
737
|
+
badge: { fontSize: 10.5, padding: '1px 6px', borderRadius: 4, background: C.bg2, color: C.text2 },
|
|
738
|
+
menuGroup: { display: 'flex', flexDirection: 'column' },
|
|
739
|
+
menuSep: { height: 1, background: C.line, margin: '5px 4px' },
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// 右键菜单条目
|
|
743
|
+
const menuItem = (label, d, fn, color) => h('button', {
|
|
744
|
+
style: {
|
|
745
|
+
display: 'flex', alignItems: 'center', gap: 8, width: '100%', textAlign: 'left',
|
|
746
|
+
border: 'none', background: 'transparent', color: color || C.text,
|
|
747
|
+
cursor: 'pointer', padding: '7px 9px', borderRadius: 6,
|
|
748
|
+
fontFamily: 'inherit', fontSize: 12.5,
|
|
749
|
+
},
|
|
750
|
+
onClick: fn,
|
|
751
|
+
},
|
|
752
|
+
h(Icon, { d, size: 13 }),
|
|
753
|
+
h('span', null, label),
|
|
754
|
+
);
|
|
755
|
+
|
|
756
|
+
const connected = status?.connected;
|
|
757
|
+
|
|
758
|
+
// 文件树分组
|
|
759
|
+
const filtered = React.useMemo(() => {
|
|
760
|
+
if (results) return results.map(r => ({ ...r, type: 'note', fromSearch: true }));
|
|
761
|
+
const list = filter === 'all' ? files : files.filter(f => f.type === filter);
|
|
762
|
+
return list;
|
|
763
|
+
}, [files, filter, results]);
|
|
764
|
+
|
|
765
|
+
const tree = React.useMemo(() => {
|
|
766
|
+
const root = { name: '', dirs: new Map(), files: [] };
|
|
767
|
+
const ensureDir = (segs) => {
|
|
768
|
+
let node = root;
|
|
769
|
+
for (const s of segs) {
|
|
770
|
+
if (!node.dirs.has(s)) node.dirs.set(s, { name: s, dirs: new Map(), files: [] });
|
|
771
|
+
node = node.dirs.get(s);
|
|
772
|
+
}
|
|
773
|
+
return node;
|
|
774
|
+
};
|
|
775
|
+
for (const f of filtered) {
|
|
776
|
+
const segs = f.path.split('/');
|
|
777
|
+
if (f.type === 'folder') {
|
|
778
|
+
// 空文件夹也要建出节点
|
|
779
|
+
ensureDir(segs);
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
const name = segs.pop();
|
|
783
|
+
const node = ensureDir(segs);
|
|
784
|
+
node.files.push({ ...f, name });
|
|
785
|
+
}
|
|
786
|
+
return root;
|
|
787
|
+
}, [filtered]);
|
|
788
|
+
|
|
789
|
+
const renderTree = (node, depth, prefix = '') => {
|
|
790
|
+
const items = [];
|
|
791
|
+
const dirs = [...node.dirs.entries()].sort((a, b) => a[0].localeCompare(b[0]));
|
|
792
|
+
for (const [name, child] of dirs) {
|
|
793
|
+
const id = `${depth}:${name}`;
|
|
794
|
+
const isOpen = !collapsed[id];
|
|
795
|
+
const dirRel = prefix ? `${prefix}/${name}` : name;
|
|
796
|
+
items.push(h('div', {
|
|
797
|
+
key: `d-${id}`,
|
|
798
|
+
'data-kb-row': 'folder',
|
|
799
|
+
style: { ...S.row, paddingLeft: 7 + depth * 12, color: C.text2 },
|
|
800
|
+
onClick: () => setCollapsed(c => ({ ...c, [id]: !c[id] })),
|
|
801
|
+
onContextMenu: e => {
|
|
802
|
+
e.preventDefault();
|
|
803
|
+
e.stopPropagation();
|
|
804
|
+
setMenu({ kind: 'folder', path: dirRel, name, ...toLocal(e.clientX, e.clientY) });
|
|
805
|
+
},
|
|
806
|
+
title: `${dirRel}\n${t('hintContextMenu')}`,
|
|
807
|
+
},
|
|
808
|
+
h('span', { style: { width: 12, display: 'inline-flex', transform: isOpen ? 'rotate(90deg)' : 'none', transition: 'transform .12s' } },
|
|
809
|
+
h(Icon, { d: 'M9 6l6 6-6 6', size: 11 })),
|
|
810
|
+
h('span', { style: S.rowText }, name),
|
|
811
|
+
h('button', {
|
|
812
|
+
style: S.rowBtn,
|
|
813
|
+
title: t('delete'),
|
|
814
|
+
onClick: e => { e.stopPropagation(); removeFolder(dirRel, name); },
|
|
815
|
+
}, h(Icon, { d: 'M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14', size: 11 })),
|
|
816
|
+
h('button', {
|
|
817
|
+
style: S.rowBtn,
|
|
818
|
+
title: t('rename'),
|
|
819
|
+
onClick: e => { e.stopPropagation(); renameFolder(dirRel, name); },
|
|
820
|
+
}, h(Icon, { d: 'M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z', size: 11 })),
|
|
821
|
+
));
|
|
822
|
+
if (isOpen) items.push(...renderTree(child, depth + 1, dirRel));
|
|
823
|
+
}
|
|
824
|
+
const fs = node.files.slice().sort((a, b) => (b.mtime || 0) - (a.mtime || 0));
|
|
825
|
+
for (const f of fs) {
|
|
826
|
+
const isPdf = f.type === 'pdf';
|
|
827
|
+
const isImg = f.type === 'image';
|
|
828
|
+
const glyph = isPdf ? 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z'
|
|
829
|
+
: isImg ? 'M4 5h16v14H4zM4 15l5-5 5 5M15 12l5 5'
|
|
830
|
+
: 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5';
|
|
831
|
+
items.push(h('div', {
|
|
832
|
+
key: `f-${f.path}`,
|
|
833
|
+
'data-kb-row': 'note',
|
|
834
|
+
style: { ...S.row, paddingLeft: 7 + depth * 12 + 12, ...(selected === f.path ? S.rowSel : {}) },
|
|
835
|
+
onClick: () => openNote(f.path),
|
|
836
|
+
onContextMenu: e => {
|
|
837
|
+
e.preventDefault();
|
|
838
|
+
e.stopPropagation();
|
|
839
|
+
setMenu({ kind: 'note', path: f.path, name: f.title || f.name, ...toLocal(e.clientX, e.clientY) });
|
|
840
|
+
},
|
|
841
|
+
title: `${f.path}\n${t('hintContextMenu')}`,
|
|
842
|
+
},
|
|
843
|
+
h('span', { style: { color: C.text2, display: 'inline-flex' } }, h(Icon, { d: glyph, size: 12 })),
|
|
844
|
+
h('span', { style: S.rowText }, f.title || f.name),
|
|
845
|
+
f.fromSearch && f.line ? h('span', { style: { ...S.badge, fontSize: 10 } }, `:${f.line}`) : null,
|
|
846
|
+
));
|
|
847
|
+
}
|
|
848
|
+
return items;
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
const headings = note?.headings || [];
|
|
852
|
+
const jumpTo = (id) => {
|
|
853
|
+
const el = document.getElementById(`kb-h-${id}`);
|
|
854
|
+
if (el?.scrollIntoView) el.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
return h('div', { ref: rootRef, style: S.root, 'data-dsh-plugin': '@wagq/dsh-obsidian', 'data-dsh-part': 'kb-workspace' },
|
|
858
|
+
|
|
859
|
+
// ═════ 左:文件库 ═════
|
|
860
|
+
h('div', { style: S.colLeft },
|
|
861
|
+
h('div', { style: S.bar },
|
|
862
|
+
h('button', {
|
|
863
|
+
style: {
|
|
864
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, flex: 1, minWidth: 0,
|
|
865
|
+
border: 'none', background: 'transparent', color: C.text, cursor: 'pointer',
|
|
866
|
+
padding: '3px 4px', borderRadius: 6, fontFamily: 'inherit', textAlign: 'left',
|
|
867
|
+
},
|
|
868
|
+
title: status?.vaultPath || t('files'),
|
|
869
|
+
onClick: async () => {
|
|
870
|
+
try {
|
|
871
|
+
const r = await api('vaults');
|
|
872
|
+
setPicker(r.ok ? (r.vaults || []) : []);
|
|
873
|
+
} catch { setPicker([]); }
|
|
874
|
+
},
|
|
875
|
+
},
|
|
876
|
+
h('span', { style: { display: 'inline-flex', color: C.brand } }, h(BookGlyph)),
|
|
877
|
+
h('span', { style: { ...S.barTitle, flex: 1 } }, status?.name || t('files')),
|
|
878
|
+
h('span', { style: { display: 'inline-flex', color: C.text2, transform: 'rotate(90deg)' } },
|
|
879
|
+
h(Icon, { d: 'M9 6l6 6-6 6', size: 10 })),
|
|
880
|
+
),
|
|
881
|
+
h('button', {
|
|
882
|
+
style: S.iconBtn, title: t('newMenu'),
|
|
883
|
+
onClick: e => {
|
|
884
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
885
|
+
const p = toLocal(r.left, r.bottom + 4);
|
|
886
|
+
setMenu({ kind: 'new', x: p.x, y: p.y });
|
|
887
|
+
},
|
|
888
|
+
}, h(Icon, { d: 'M12 5v14M5 12h14' })),
|
|
889
|
+
h('button', { style: S.iconBtn, title: t('newFolder'), onClick: () => createFolder('') },
|
|
890
|
+
h(Icon, { d: 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2zM12 11v6M9 14h6' })),
|
|
891
|
+
h('button', { style: S.iconBtn, title: t('refresh'), onClick: () => { loadStatus(); loadTree(); } }, h(Icon, { d: 'M21 12a9 9 0 1 1-3-6.7M21 3v6h-6' })),
|
|
892
|
+
),
|
|
893
|
+
h('div', { style: { padding: '8px 10px 0' } },
|
|
894
|
+
h('input', {
|
|
895
|
+
style: S.input, value: query, placeholder: t('search'),
|
|
896
|
+
onChange: e => runSearch(e.target.value),
|
|
897
|
+
'aria-label': t('search'),
|
|
898
|
+
}),
|
|
899
|
+
),
|
|
900
|
+
h('div', { style: S.chips },
|
|
901
|
+
...[['all', t('all')], ['note', t('notes')], ['pdf', t('pdf')], ['image', t('images')], ['file', t('others')]].map(([k, label]) =>
|
|
902
|
+
h('button', {
|
|
903
|
+
key: k,
|
|
904
|
+
style: { ...S.chip, ...(filter === k && !results ? S.chipOn : {}) },
|
|
905
|
+
onClick: () => { setFilter(k); setResults(null); setQuery(''); },
|
|
906
|
+
}, label)),
|
|
907
|
+
),
|
|
908
|
+
!connected && status
|
|
909
|
+
? h('div', { style: { ...S.notice, margin: '8px 10px' } }, t('notConnectedHint'))
|
|
910
|
+
: null,
|
|
911
|
+
notice ? h('div', { style: S.notice }, notice) : null,
|
|
912
|
+
h('div', {
|
|
913
|
+
style: S.tree,
|
|
914
|
+
onContextMenu: e => {
|
|
915
|
+
if (e.target.closest && e.target.closest('[data-kb-row]')) return;
|
|
916
|
+
e.preventDefault();
|
|
917
|
+
setMenu({ kind: 'root', ...toLocal(e.clientX, e.clientY) });
|
|
918
|
+
},
|
|
919
|
+
},
|
|
920
|
+
filtered.length === 0
|
|
921
|
+
? h('div', { style: S.empty }, results ? t('noResult') : t('empty'))
|
|
922
|
+
: renderTree(tree, 0),
|
|
923
|
+
),
|
|
924
|
+
h('div', { style: { ...S.bar, borderBottom: 'none', borderTop: `1px solid ${C.line}`, justifyContent: 'space-between' } },
|
|
925
|
+
h('span', { style: { fontSize: 11, color: C.text2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
926
|
+
connected ? status?.vaultPath : t('notConnected')),
|
|
927
|
+
h('span', { style: { fontSize: 11, color: C.text2, flexShrink: 0 } },
|
|
928
|
+
connected ? `${status.counts.note} ${t('notes')} · ${status.counts.folders} ${t('folders')}` : ''),
|
|
929
|
+
),
|
|
930
|
+
),
|
|
931
|
+
|
|
932
|
+
// ═════ 中:文档 ═════
|
|
933
|
+
h('div', { style: S.colMid },
|
|
934
|
+
h('div', { style: S.bar },
|
|
935
|
+
h('span', { style: { ...S.barTitle, flex: 1 } }, note ? note.title : t('vault')),
|
|
936
|
+
note ? h('span', { style: { ...S.loader, color: dirty ? C.warn : C.text2 } },
|
|
937
|
+
dirty ? t('unsaved')
|
|
938
|
+
: note.kind === 'note' ? (mode === 'edit' ? t('editing') : t('reading'))
|
|
939
|
+
: note.kind === 'image' ? t('images')
|
|
940
|
+
: note.kind === 'pdf' ? t('pdf')
|
|
941
|
+
: fmtSize(note.size)) : null,
|
|
942
|
+
note ? h('button', {
|
|
943
|
+
style: { ...S.iconBtn, ...(mode === 'read' ? S.btnOn : {}) },
|
|
944
|
+
onClick: () => setMode('read'), title: t('preview'),
|
|
945
|
+
}, t('preview')) : null, note && note.kind === 'note' ? h('button', {
|
|
946
|
+
style: { ...S.iconBtn, ...(mode === 'edit' ? S.btnOn : {}) },
|
|
947
|
+
onClick: () => setMode('edit'), title: t('edit'),
|
|
948
|
+
}, t('edit')) : null,
|
|
949
|
+
note && dirty ? h('button', { style: { ...S.btn, borderColor: C.brand, color: C.brand }, onClick: save, disabled: busy }, busy ? t('saving') : t('save')) : null,
|
|
950
|
+
note ? h('button', { style: S.iconBtn, title: t('rename'), onClick: renameNote }, h(Icon, { d: 'M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z' })) : null,
|
|
951
|
+
note ? h('button', { style: S.iconBtn, title: t('delete'), onClick: removeNote }, h(Icon, { d: 'M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14', })) : null,
|
|
952
|
+
h('button', {
|
|
953
|
+
style: { ...S.iconBtn, ...(full ? S.btnOn : {}) },
|
|
954
|
+
title: full ? t('exitFullscreen') : t('fullscreen'),
|
|
955
|
+
onClick: () => setFull(f => !f),
|
|
956
|
+
}, h(Icon, { d: full ? 'M9 3v6H3M15 21v-6h6M3 15h6v6M21 9h-6V3' : 'M4 9V4h5M20 15v5h-5M20 9V4h-5M4 15v5h5' })),
|
|
957
|
+
),
|
|
958
|
+
|
|
959
|
+
note
|
|
960
|
+
? h('div', { style: S.doc },
|
|
961
|
+
h('div', { style: S.crumb },
|
|
962
|
+
...note.path.split('/').map((seg, si, arr) =>
|
|
963
|
+
h('span', { key: si, style: { display: 'inline-flex', alignItems: 'center', gap: 5 } },
|
|
964
|
+
si > 0 ? h('span', { style: { opacity: .5 } }, '/') : null,
|
|
965
|
+
h('span', { style: si === arr.length - 1 ? { color: C.text } : undefined }, seg.replace(/\.(md|markdown)$/i, '')))),
|
|
966
|
+
),
|
|
967
|
+
h('h1', { style: S.docTitle }, note.title),
|
|
968
|
+
note.tags.length
|
|
969
|
+
? h('div', { style: S.tagRow }, ...note.tags.map(tag => h('span', { key: tag, style: S.tag }, `#${tag}`)))
|
|
970
|
+
: null,
|
|
971
|
+
h('div', { style: S.metaRow },
|
|
972
|
+
h('span', null, `📄 ${note.words} ${t('words')}`),
|
|
973
|
+
h('span', null, `↩ ${note.chars} ${t('chars')}`),
|
|
974
|
+
h('span', null, `☰ ${note.lines} ${t('lines')}`),
|
|
975
|
+
h('span', null, `🕒 ${fmtDate(note.mtime)}`),
|
|
976
|
+
h('span', { style: S.badge }, fmtSize(note.size)),
|
|
977
|
+
),
|
|
978
|
+
mode === 'edit' && note.kind !== 'note' ? null : mode === 'edit'
|
|
979
|
+
? h('textarea', {
|
|
980
|
+
style: S.textarea, value: draft ?? '', spellCheck: false,
|
|
981
|
+
onChange: e => setDraft(e.target.value),
|
|
982
|
+
'aria-label': note.title,
|
|
983
|
+
})
|
|
984
|
+
: note.kind === 'image'
|
|
985
|
+
? h('div', { style: { padding: '10px 0' } },
|
|
986
|
+
h('img', {
|
|
987
|
+
src: `${RAW}?path=${encodeURIComponent(note.path)}`,
|
|
988
|
+
alt: note.title,
|
|
989
|
+
style: { maxWidth: '100%', borderRadius: 8, border: `1px solid ${C.line}` },
|
|
990
|
+
}))
|
|
991
|
+
: note.kind === 'pdf'
|
|
992
|
+
? h('div', null,
|
|
993
|
+
h('iframe', {
|
|
994
|
+
src: `${RAW}?path=${encodeURIComponent(note.path)}`,
|
|
995
|
+
title: note.title,
|
|
996
|
+
style: { width: '100%', height: 620, border: `1px solid ${C.line}`, borderRadius: 8, background: C.bg1 },
|
|
997
|
+
}),
|
|
998
|
+
h('div', { style: { marginTop: 8, fontSize: 12, color: C.text2 } },
|
|
999
|
+
h('a', { href: `${RAW}?path=${encodeURIComponent(note.path)}`, target: '_blank', rel: 'noreferrer', style: { color: C.brand } }, `↗ ${note.title}`)),
|
|
1000
|
+
)
|
|
1001
|
+
: note.kind && note.kind !== 'note'
|
|
1002
|
+
? h('div', { style: S.empty },
|
|
1003
|
+
h('div', null, `${note.title} · ${fmtSize(note.size)}`),
|
|
1004
|
+
h('a', { href: `${RAW}?path=${encodeURIComponent(note.path)}`, target: '_blank', rel: 'noreferrer', style: { color: C.brand, fontSize: 12 } }, '↗ 下载/打开'))
|
|
1005
|
+
: h('div', null, ...renderMarkdown(note.body, jumpTo)),
|
|
1006
|
+
mode === 'edit'
|
|
1007
|
+
? h('div', { style: { display: 'flex', gap: 8, justifyContent: 'flex-end', marginTop: 14 } },
|
|
1008
|
+
h('button', { style: S.btn, onClick: () => { setDraft(note.body); setMode('read'); }, disabled: !dirty }, t('cancel')),
|
|
1009
|
+
h('button', { style: { ...S.btn, borderColor: C.brand, color: C.brand }, onClick: save, disabled: busy || !dirty }, busy ? t('saving') : t('save')),
|
|
1010
|
+
)
|
|
1011
|
+
: null,
|
|
1012
|
+
)
|
|
1013
|
+
: h('div', { style: S.empty },
|
|
1014
|
+
h('div', { style: { fontSize: 34, marginBottom: 10, opacity: .35 } }, '📚'),
|
|
1015
|
+
h('div', null, t('selectNote')),
|
|
1016
|
+
),
|
|
1017
|
+
),
|
|
1018
|
+
|
|
1019
|
+
// ═════ 右:大纲 / 关联 ═════
|
|
1020
|
+
h('div', { style: S.colRight },
|
|
1021
|
+
h('div', { style: S.tabs },
|
|
1022
|
+
h('button', { style: { ...S.tab, ...(rightTab === 'outline' ? S.tabOn : {}) }, onClick: () => setRightTab('outline') }, t('outline')),
|
|
1023
|
+
h('button', { style: { ...S.tab, ...(rightTab === 'related' ? S.tabOn : {}) }, onClick: () => setRightTab('related') }, `${t('related')}${related.length ? ` ${related.length}` : ''}`),
|
|
1024
|
+
h('button', { style: { ...S.tab, ...(rightTab === 'ask' ? S.tabOn : {}) }, onClick: () => setRightTab('ask') }, t('ask')),
|
|
1025
|
+
),
|
|
1026
|
+
h('div', { style: S.rightBody },
|
|
1027
|
+
rightTab === 'ask'
|
|
1028
|
+
? h('div', { style: { padding: '10px 8px 0' } },
|
|
1029
|
+
h('textarea', {
|
|
1030
|
+
style: { ...S.input, height: 62, resize: 'vertical', lineHeight: 1.6, padding: '7px 9px' },
|
|
1031
|
+
value: askQ, placeholder: t('askPlaceholder'),
|
|
1032
|
+
onChange: e => setAskQ(e.target.value),
|
|
1033
|
+
onKeyDown: e => {
|
|
1034
|
+
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); askVault(); }
|
|
1035
|
+
},
|
|
1036
|
+
'aria-label': t('ask'),
|
|
1037
|
+
}),
|
|
1038
|
+
h('div', { style: { display: 'flex', gap: 6, marginTop: 6, alignItems: 'center' } },
|
|
1039
|
+
h('button', {
|
|
1040
|
+
style: {
|
|
1041
|
+
...S.btnPrimary, flex: 1,
|
|
1042
|
+
...(askBusy || !askQ.trim() ? { opacity: 0.55, cursor: 'default' } : {}),
|
|
1043
|
+
},
|
|
1044
|
+
onClick: askVault, disabled: askBusy || !askQ.trim(),
|
|
1045
|
+
}, askBusy ? t('asking') : t('askSend')),
|
|
1046
|
+
askRes ? h('button', { style: S.iconBtn, title: t('clear'), onClick: () => { setAskRes(null); setAskQ(''); } }, '✕') : null,
|
|
1047
|
+
),
|
|
1048
|
+
askBusy ? h('div', { style: { ...S.match, padding: '8px 0' } }, t('asking')) : null,
|
|
1049
|
+
askRes && !askRes.synthesized ? h('div', { style: { fontSize: 11, color: C.warn, marginTop: 8, lineHeight: 1.6 } },
|
|
1050
|
+
`${t('evidenceOnly')}${askRes.reason ? `(${askRes.reason})` : ''}`) : null,
|
|
1051
|
+
askRes && askRes.answer ? h('div', {
|
|
1052
|
+
style: { marginTop: 10, fontSize: 12.5, lineHeight: 1.85, color: C.text, whiteSpace: 'pre-wrap', wordBreak: 'break-word' },
|
|
1053
|
+
}, askRes.answer) : null,
|
|
1054
|
+
askRes && askRes.hits?.length
|
|
1055
|
+
? h('div', { style: { marginTop: 12 } },
|
|
1056
|
+
h('div', { style: { ...S.match, padding: '0 0 6px', fontSize: 11 } }, `${t('citations')} · ${askRes.citations?.length || 0}`),
|
|
1057
|
+
...askRes.hits.map((hh, hi) => h('button', {
|
|
1058
|
+
key: hi,
|
|
1059
|
+
style: { ...S.relRow, alignItems: 'flex-start' },
|
|
1060
|
+
onClick: () => openNote(hh.path),
|
|
1061
|
+
title: hh.text.slice(0, 240),
|
|
1062
|
+
},
|
|
1063
|
+
h('span', { style: { fontSize: 10, color: C.text2, minWidth: 16 } }, `[${hi + 1}]`),
|
|
1064
|
+
h('span', { style: { flex: 1, overflow: 'hidden' } },
|
|
1065
|
+
h('span', { style: { display: 'block', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, hh.title),
|
|
1066
|
+
h('span', { style: { display: 'block', fontSize: 10.5, color: C.text2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, hh.heading || ''),
|
|
1067
|
+
),
|
|
1068
|
+
)),
|
|
1069
|
+
)
|
|
1070
|
+
: null,
|
|
1071
|
+
askRes && !askRes.hits?.length ? h('div', { style: S.empty }, t('noEvidence')) : null,
|
|
1072
|
+
)
|
|
1073
|
+
: rightTab === 'outline'
|
|
1074
|
+
? (headings.length
|
|
1075
|
+
? headings.map((x, xi) => h('button', {
|
|
1076
|
+
key: xi,
|
|
1077
|
+
style: { ...S.outRow, paddingLeft: 8 + (x.level - 1) * 13, color: x.level <= 2 ? C.text : C.text2, fontWeight: x.level <= 2 ? 600 : 400 },
|
|
1078
|
+
onClick: () => jumpTo(String(xi)),
|
|
1079
|
+
title: x.text,
|
|
1080
|
+
}, x.text))
|
|
1081
|
+
: h('div', { style: S.empty }, t('noOutline')))
|
|
1082
|
+
: (related.length
|
|
1083
|
+
? related.map((r, ri) => h('button', {
|
|
1084
|
+
key: ri,
|
|
1085
|
+
style: S.relRow,
|
|
1086
|
+
onClick: () => openNote(r.path),
|
|
1087
|
+
title: r.path,
|
|
1088
|
+
},
|
|
1089
|
+
h('span', { style: { color: r.kind === 'in' ? C.brand : C.text2, display: 'inline-flex' } },
|
|
1090
|
+
h(Icon, { d: r.kind === 'in' ? 'M10 13a5 5 0 0 0 7 0l3-3a5 5 0 0 0-7-7l-1 1' : 'M14 11a5 5 0 0 0-7 0l-3 3a5 5 0 0 0 7 7l1-1', size: 12 })),
|
|
1091
|
+
h('span', { style: { flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, r.title),
|
|
1092
|
+
h('span', { style: { fontSize: 10, color: C.text2 } }, r.kind === 'in' ? t('linkIn') : t('linkOut')),
|
|
1093
|
+
))
|
|
1094
|
+
: h('div', { style: S.empty }, t('noRelated'))),
|
|
1095
|
+
),
|
|
1096
|
+
),
|
|
1097
|
+
|
|
1098
|
+
// 新建菜单(笔记 / 文件夹 / 知识库项目)
|
|
1099
|
+
menu && menu.kind === 'new'
|
|
1100
|
+
? h('div', {
|
|
1101
|
+
style: { position: 'absolute', inset: 0, zIndex: 280 },
|
|
1102
|
+
onClick: () => setMenu(null),
|
|
1103
|
+
},
|
|
1104
|
+
h('div', {
|
|
1105
|
+
style: {
|
|
1106
|
+
position: 'absolute', left: Math.max(8, menu.x), top: Math.max(8, menu.y),
|
|
1107
|
+
width: 200, background: C.bg2, border: `1px solid ${C.line2}`,
|
|
1108
|
+
borderRadius: 8, padding: 6, boxShadow: '0 12px 40px rgba(0,0,0,.45)',
|
|
1109
|
+
},
|
|
1110
|
+
onClick: e => e.stopPropagation(),
|
|
1111
|
+
},
|
|
1112
|
+
h('div', { style: S.menuGroup },
|
|
1113
|
+
menuItem(t('newNote'), 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5', () => { setMenu(null); createNote(''); }),
|
|
1114
|
+
menuItem(t('newFolder'), 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z', () => { setMenu(null); createFolder(''); }),
|
|
1115
|
+
),
|
|
1116
|
+
h('div', { style: S.menuSep }),
|
|
1117
|
+
h('div', { style: S.menuGroup },
|
|
1118
|
+
menuItem(t('createVault'), 'M12 3v18M3 12h18', () => { setMenu(null); createVaultProject(); }, C.brand),
|
|
1119
|
+
),
|
|
1120
|
+
),
|
|
1121
|
+
)
|
|
1122
|
+
: null,
|
|
1123
|
+
|
|
1124
|
+
// 右键菜单(文件夹 / 笔记 / 空白处)
|
|
1125
|
+
menu && menu.kind !== 'new'
|
|
1126
|
+
? h('div', {
|
|
1127
|
+
style: { position: 'absolute', inset: 0, zIndex: 280 },
|
|
1128
|
+
onClick: () => setMenu(null),
|
|
1129
|
+
},
|
|
1130
|
+
h('div', {
|
|
1131
|
+
style: {
|
|
1132
|
+
position: 'absolute', left: Math.max(8, menu.x), top: Math.max(8, menu.y),
|
|
1133
|
+
width: 196, background: C.bg2, border: `1px solid ${C.line2}`,
|
|
1134
|
+
borderRadius: 8, padding: 6, boxShadow: '0 12px 40px rgba(0,0,0,.45)',
|
|
1135
|
+
},
|
|
1136
|
+
onClick: e => e.stopPropagation(),
|
|
1137
|
+
},
|
|
1138
|
+
// 在文件夹里新建(仅文件夹菜单)
|
|
1139
|
+
menu.kind === 'folder'
|
|
1140
|
+
? h('div', null,
|
|
1141
|
+
h('div', { style: S.menuGroup },
|
|
1142
|
+
menuItem(t('newNote'), 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5', () => { setMenu(null); createNote(menu.path); }),
|
|
1143
|
+
menuItem(t('newFolder'), 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z', () => { setMenu(null); createFolder(menu.path); }),
|
|
1144
|
+
),
|
|
1145
|
+
h('div', { style: S.menuSep }),
|
|
1146
|
+
h('div', { style: S.menuGroup },
|
|
1147
|
+
menuItem(t('rename'), 'M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z', () => { setMenu(null); renameFolder(menu.path, menu.name); }),
|
|
1148
|
+
menuItem(t('delete'), 'M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14', () => { setMenu(null); removeFolder(menu.path, menu.name); }, C.err),
|
|
1149
|
+
),
|
|
1150
|
+
)
|
|
1151
|
+
: null,
|
|
1152
|
+
// 笔记菜单
|
|
1153
|
+
menu.kind === 'note'
|
|
1154
|
+
? h('div', null,
|
|
1155
|
+
h('div', { style: S.menuGroup },
|
|
1156
|
+
menuItem(t('open'), 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5', () => { setMenu(null); openNote(menu.path); }),
|
|
1157
|
+
),
|
|
1158
|
+
h('div', { style: S.menuSep }),
|
|
1159
|
+
h('div', { style: S.menuGroup },
|
|
1160
|
+
menuItem(t('rename'), 'M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z', () => { setMenu(null); renameNoteAt(menu.path); }),
|
|
1161
|
+
menuItem(t('delete'), 'M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14', () => { setMenu(null); removeNoteAt(menu.path); }, C.err),
|
|
1162
|
+
),
|
|
1163
|
+
)
|
|
1164
|
+
: null,
|
|
1165
|
+
// 空白处菜单(根目录)
|
|
1166
|
+
menu.kind === 'root'
|
|
1167
|
+
? h('div', null,
|
|
1168
|
+
h('div', { style: S.menuGroup },
|
|
1169
|
+
menuItem(t('newNote'), 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5', () => { setMenu(null); createNote(''); }),
|
|
1170
|
+
menuItem(t('newFolder'), 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z', () => { setMenu(null); createFolder(''); }),
|
|
1171
|
+
),
|
|
1172
|
+
h('div', { style: S.menuSep }),
|
|
1173
|
+
h('div', { style: S.menuGroup },
|
|
1174
|
+
menuItem(t('createVault'), 'M12 3v18M3 12h18', () => { setMenu(null); createVaultProject(); }, C.brand),
|
|
1175
|
+
),
|
|
1176
|
+
)
|
|
1177
|
+
: null,
|
|
1178
|
+
),
|
|
1179
|
+
)
|
|
1180
|
+
: null,
|
|
1181
|
+
|
|
1182
|
+
// vault 切换下拉
|
|
1183
|
+
picker
|
|
1184
|
+
? h('div', {
|
|
1185
|
+
style: {
|
|
1186
|
+
position: 'absolute', inset: 0, zIndex: 290,
|
|
1187
|
+
background: 'rgba(0,0,0,.35)', display: 'flex',
|
|
1188
|
+
alignItems: 'flex-start', justifyContent: 'flex-start', padding: 16,
|
|
1189
|
+
},
|
|
1190
|
+
onClick: e => { if (e.target === e.currentTarget) setPicker(null); },
|
|
1191
|
+
},
|
|
1192
|
+
h('div', {
|
|
1193
|
+
style: {
|
|
1194
|
+
width: 320, maxHeight: '70%', overflowY: 'auto',
|
|
1195
|
+
background: C.bg2, border: `1px solid ${C.line2}`, borderRadius: 10,
|
|
1196
|
+
padding: 8, boxShadow: '0 12px 40px rgba(0,0,0,.45)',
|
|
1197
|
+
},
|
|
1198
|
+
},
|
|
1199
|
+
h('div', { style: { fontSize: 11.5, color: C.text2, padding: '4px 8px 8px' } }, t('switchVault')),
|
|
1200
|
+
picker.length === 0
|
|
1201
|
+
? h('div', { style: { fontSize: 11.5, color: C.text2, padding: '6px 8px' } }, t('noVaults'))
|
|
1202
|
+
: picker.map(v => h('button', {
|
|
1203
|
+
key: v.path,
|
|
1204
|
+
style: {
|
|
1205
|
+
display: 'block', width: '100%', textAlign: 'left', border: 'none',
|
|
1206
|
+
background: v.path === status?.vaultPath ? C.bg1 : 'transparent',
|
|
1207
|
+
color: C.text, cursor: 'pointer', padding: '8px 9px',
|
|
1208
|
+
borderRadius: 6, fontFamily: 'inherit', fontSize: 12.5,
|
|
1209
|
+
},
|
|
1210
|
+
onClick: () => switchVault(v.path),
|
|
1211
|
+
title: v.path,
|
|
1212
|
+
},
|
|
1213
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
|
|
1214
|
+
h('span', { style: { fontWeight: 600 } }, v.name),
|
|
1215
|
+
v.open ? h('span', { style: S.badge }, 'open') : null,
|
|
1216
|
+
v.path === status?.vaultPath ? h('span', { style: { ...S.badge, color: C.brand } }, '✓') : null,
|
|
1217
|
+
),
|
|
1218
|
+
h('div', { style: { fontSize: 10.5, color: C.text2, marginTop: 2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, v.path),
|
|
1219
|
+
)),
|
|
1220
|
+
h('div', { style: { height: 1, background: C.line, margin: '8px 4px' } }),
|
|
1221
|
+
h('button', {
|
|
1222
|
+
style: {
|
|
1223
|
+
display: 'flex', alignItems: 'center', gap: 8, width: '100%', textAlign: 'left',
|
|
1224
|
+
border: 'none', background: 'transparent', color: C.brand, cursor: 'pointer',
|
|
1225
|
+
padding: '9px 9px', borderRadius: 6, fontFamily: 'inherit', fontSize: 12.5, fontWeight: 600,
|
|
1226
|
+
},
|
|
1227
|
+
onClick: createVaultProject,
|
|
1228
|
+
},
|
|
1229
|
+
h(Icon, { d: 'M12 5v14M5 12h14', size: 13 }),
|
|
1230
|
+
h('span', null, t('createVault')),
|
|
1231
|
+
),
|
|
1232
|
+
),
|
|
1233
|
+
)
|
|
1234
|
+
: null,
|
|
1235
|
+
|
|
1236
|
+
// 内联对话框(替代被禁用的 window.prompt / window.confirm)
|
|
1237
|
+
dialog
|
|
1238
|
+
? h('div', {
|
|
1239
|
+
style: {
|
|
1240
|
+
position: 'absolute', inset: 0, zIndex: 300,
|
|
1241
|
+
background: 'rgba(0,0,0,.42)', display: 'flex',
|
|
1242
|
+
alignItems: 'center', justifyContent: 'center', padding: 20,
|
|
1243
|
+
},
|
|
1244
|
+
onClick: e => { if (e.target === e.currentTarget) closeDialog(dialog.kind === 'confirm' ? false : null); },
|
|
1245
|
+
},
|
|
1246
|
+
h('div', {
|
|
1247
|
+
style: {
|
|
1248
|
+
width: 'min(420px, 100%)', background: C.bg2, color: C.text,
|
|
1249
|
+
border: `1px solid ${C.line2}`, borderRadius: 10, padding: 16,
|
|
1250
|
+
boxShadow: '0 12px 40px rgba(0,0,0,.45)', display: 'flex',
|
|
1251
|
+
flexDirection: 'column', gap: 12,
|
|
1252
|
+
},
|
|
1253
|
+
role: 'dialog',
|
|
1254
|
+
'aria-modal': 'true',
|
|
1255
|
+
},
|
|
1256
|
+
h('div', { style: { fontSize: 13.5, fontWeight: 600, lineHeight: 1.5 } }, dialog.title),
|
|
1257
|
+
dialog.kind === 'prompt'
|
|
1258
|
+
? h('input', {
|
|
1259
|
+
style: S.input,
|
|
1260
|
+
value: dialog.value,
|
|
1261
|
+
placeholder: dialog.placeholder,
|
|
1262
|
+
autoFocus: true,
|
|
1263
|
+
onChange: e => {
|
|
1264
|
+
const v = e.target.value;
|
|
1265
|
+
dialogValueRef.current = v;
|
|
1266
|
+
setDialog(d => d && { ...d, value: v });
|
|
1267
|
+
},
|
|
1268
|
+
onKeyDown: e => {
|
|
1269
|
+
e.stopPropagation();
|
|
1270
|
+
if (e.key === 'Enter' && !e.nativeEvent?.isComposing) submitDialog();
|
|
1271
|
+
if (e.key === 'Escape') closeDialog(null);
|
|
1272
|
+
},
|
|
1273
|
+
})
|
|
1274
|
+
: null,
|
|
1275
|
+
h('div', { style: { display: 'flex', gap: 8, justifyContent: 'flex-end' } },
|
|
1276
|
+
h('button', { style: S.btn, onClick: () => closeDialog(dialog.kind === 'confirm' ? false : null) }, t('cancel')),
|
|
1277
|
+
h('button', { style: S.btnPrimary, onClick: submitDialog }, dialog.confirmText || t('save')),
|
|
1278
|
+
),
|
|
1279
|
+
),
|
|
1280
|
+
)
|
|
1281
|
+
: null,
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function registerUi(ctx) {
|
|
1286
|
+
ctx.slots.inject('main', () =>
|
|
1287
|
+
ctx.slots.register({ name: 'main', key: PANEL_ID, locale: NS }, KnowledgePanel));
|
|
1288
|
+
ctx.slots.inject('sidebar.panellist', () =>
|
|
1289
|
+
ctx.slots.register(
|
|
1290
|
+
{ name: 'sidebar.panellist', id: PANEL_ID, order: 56, locale: NS, label: () => t('panel') },
|
|
1291
|
+
BookGlyph));
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
function apply(ctx) {
|
|
1295
|
+
const disposeLocale = ctx.locale.register(NS, DICT);
|
|
1296
|
+
t = ctx.locale.bind(NS);
|
|
1297
|
+
const ui = ctx.inject(['slots'], () => registerUi(ctx));
|
|
1298
|
+
return () => { ui?.dispose?.(); disposeLocale?.(); };
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
return { inject: ['slots', 'locale'], apply };
|
|
1302
|
+
},
|
|
1303
|
+
});
|