dsh-wiki-notes 1.1.3 → 1.3.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/lib/client.js CHANGED
@@ -21,9 +21,18 @@ window.__ModuleLoader__.load({
21
21
  var useCallback = React.useCallback
22
22
  var useEffect = React.useEffect
23
23
  var useState = React.useState
24
+ // 只为「同一时刻只允许一个操作」,真 React 一定有 useRef;测试 stub 没有时退回一个普通盒子。
25
+ var useRef = React.useRef || function (value) {
26
+ return { current: value }
27
+ }
24
28
 
25
29
  var API = '/api/wiki-notes'
26
30
 
31
+ // 请求最长等多久。宿主卡住时按钮不能永远停在「保存中…」。
32
+ var TIMEOUT_MS = 15000
33
+ // 扫一个库要读磁盘,给它比普通请求更长的耐心。
34
+ var SCAN_TIMEOUT_MS = 60000
35
+
27
36
  /* ------------------------------ HTTP ------------------------------ */
28
37
 
29
38
  function readJson(res) {
@@ -44,16 +53,66 @@ window.__ModuleLoader__.load({
44
53
  })
45
54
  }
46
55
 
47
- function apiGet(path) {
48
- return fetch(API + path, { cache: 'no-store' }).then(readJson)
56
+ /**
57
+ * fetch + 超时。没有超时的话,宿主一旦不回话,busy 永远是 true,
58
+ * 整个设置页所有按钮都会锁死,用户只能重开窗口。
59
+ */
60
+ function apiFetch(path, init, timeoutMs) {
61
+ var ms = typeof timeoutMs === 'number' && timeoutMs > 0 ? timeoutMs : TIMEOUT_MS
62
+ var controller = typeof AbortController === 'function' ? new AbortController() : null
63
+ var options = {}
64
+ Object.keys(init).forEach(function (key) {
65
+ options[key] = init[key]
66
+ })
67
+ if (controller !== null) options.signal = controller.signal
68
+ var timer =
69
+ controller === null
70
+ ? null
71
+ : setTimeout(function () {
72
+ controller.abort()
73
+ }, ms)
74
+ function done() {
75
+ if (timer !== null) clearTimeout(timer)
76
+ }
77
+ return fetch(API + path, options).then(
78
+ function (res) {
79
+ return readJson(res).then(
80
+ function (data) {
81
+ done()
82
+ return data
83
+ },
84
+ function (error) {
85
+ done()
86
+ throw error
87
+ },
88
+ )
89
+ },
90
+ function (error) {
91
+ done()
92
+ if (error !== null && error !== undefined && error.name === 'AbortError') {
93
+ var failure = new Error('宿主 ' + Math.round(ms / 1000) + ' 秒没有响应。确认 DSH 还在运行,或稍后再试。')
94
+ failure.timeout = true
95
+ throw failure
96
+ }
97
+ throw error
98
+ },
99
+ )
49
100
  }
50
101
 
51
- function apiPost(path, body) {
52
- return fetch(API + path, {
53
- method: 'POST',
54
- headers: { 'content-type': 'application/json' },
55
- body: JSON.stringify(body || {}),
56
- }).then(readJson)
102
+ function apiGet(path, timeoutMs) {
103
+ return apiFetch(path, { cache: 'no-store' }, timeoutMs)
104
+ }
105
+
106
+ function apiPost(path, body, timeoutMs) {
107
+ return apiFetch(
108
+ path,
109
+ {
110
+ method: 'POST',
111
+ headers: { 'content-type': 'application/json' },
112
+ body: JSON.stringify(body || {}),
113
+ },
114
+ timeoutMs,
115
+ )
57
116
  }
58
117
 
59
118
  /* ------------------------------ 小组件 ------------------------------ */
@@ -86,6 +145,26 @@ window.__ModuleLoader__.load({
86
145
  return 'ok'
87
146
  }
88
147
 
148
+ /** 字节数说人话:覆盖诊断里「读进来多少」比页数更能说明问题。 */
149
+ function bytesLabel(value) {
150
+ var bytes = typeof value === 'number' && value > 0 ? value : 0
151
+ if (bytes === 0) return '0 KB'
152
+ if (bytes < 1024) return bytes + ' B'
153
+ if (bytes < 1024 * 1024) return Math.round(bytes / 1024) + ' KB'
154
+ return (bytes / 1024 / 1024).toFixed(1) + ' MB'
155
+ }
156
+
157
+ /** ISO 时间转本机「月-日 时:分」;空值或坏值不编时间,直接给一个短横。 */
158
+ function shortTime(iso) {
159
+ if (typeof iso !== 'string' || iso === '') return '—'
160
+ var date = new Date(iso)
161
+ if (isNaN(date.getTime())) return '—'
162
+ function pad(value) {
163
+ return value < 10 ? '0' + value : String(value)
164
+ }
165
+ return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes())
166
+ }
167
+
89
168
  /* ------------------------------ 设置页 ------------------------------ */
90
169
 
91
170
  function WikiNotesSection(props) {
@@ -97,10 +176,20 @@ window.__ModuleLoader__.load({
97
176
  var draft = draftTuple[0]
98
177
  var setDraft = draftTuple[1]
99
178
 
100
- var busyTuple = useState('')
179
+ // 首帧就是 'load':否则刷新回来之前会先闪一下「未连接宿主」。
180
+ var busyTuple = useState('load')
101
181
  var busy = busyTuple[0]
102
182
  var setBusy = busyTuple[1]
103
183
 
184
+ // 同一时刻只允许一个操作。busy 是 state,连点两下时闭包里读到的还是旧值,
185
+ // 两次请求会一起发出去;用一个同步的盒子挡住。
186
+ var busyRef = useRef('')
187
+
188
+ // 清掉配置要按两次:第一次只把按钮变成「确认清掉?」,4 秒内不确认就复原。
189
+ var confirmResetTuple = useState(false)
190
+ var confirmReset = confirmResetTuple[0]
191
+ var setConfirmReset = confirmResetTuple[1]
192
+
104
193
  var noticeTuple = useState(null)
105
194
  var notice = noticeTuple[0]
106
195
  var setNotice = noticeTuple[1]
@@ -128,7 +217,8 @@ window.__ModuleLoader__.load({
128
217
  var refresh = useCallback(
129
218
  function () {
130
219
  setBusy('load')
131
- return apiGet('/state')
220
+ // 读状态可能会当场扫一遍库,给它和「测试」一样长的耐心。
221
+ return apiGet('/state', SCAN_TIMEOUT_MS)
132
222
  .then(function (data) {
133
223
  apply(data)
134
224
  setPromptDraft(function (current) {
@@ -156,6 +246,9 @@ window.__ModuleLoader__.load({
156
246
  // 统一管 busy 与异常。成功文案由各 handler 自己给 —— 早先这里塞一句通用
157
247
  // describe,会把 handler 刚写进去的「保存失败」错误提示盖成成功,必须各管各的。
158
248
  function run(kind, task) {
249
+ // 上一个操作还没回来,就当这次点击不存在——比让两个请求互相覆盖安全。
250
+ if (busyRef.current !== '') return Promise.resolve(null)
251
+ busyRef.current = kind
159
252
  setBusy(kind)
160
253
  setNotice(null)
161
254
  return Promise.resolve()
@@ -167,6 +260,7 @@ window.__ModuleLoader__.load({
167
260
  return null
168
261
  })
169
262
  .then(function (data) {
263
+ busyRef.current = ''
170
264
  setBusy('')
171
265
  return data
172
266
  })
@@ -183,32 +277,52 @@ window.__ModuleLoader__.load({
183
277
  }
184
278
 
185
279
  function onCheck() {
280
+ if (busyRef.current !== '') return null
186
281
  var target = draft.trim()
187
282
  setProbe(null)
283
+ if (target === '') {
284
+ setProbeNote({ tone: 'warn', text: '输入框是空的——先填一个笔记目录。' })
285
+ return null
286
+ }
188
287
  // 点下去立刻给一句反馈:否则请求慢或失败时,界面看起来像「点了没反应」。
189
- setProbeNote({ tone: '', text: target === '' ? '输入框是空的——先填一个笔记目录。' : '正在扫描:' + target + ' …' })
190
- if (target === '') return null
288
+ setProbeNote({ tone: '', text: '正在扫描:' + target + ' …' })
191
289
  return run('check', function () {
192
- return apiPost('/check', { root: target }).then(function (result) {
193
- setProbe(result)
194
- if (result && result.ok === true) {
195
- var stats = result.stats || {}
196
- setProbeNote({
197
- tone: 'ok',
198
- text: '这条路径能扫:' + (stats.pages || 0) + ' 页 / ' + (stats.resources || 0) + ' 份资料(' + (result.ms || 0) + ' ms)',
199
- })
200
- } else {
201
- setProbeNote({ tone: 'err', text: (result && result.error) || '这个路径扫不出页面' })
202
- }
203
- return result
204
- })
290
+ return apiPost('/check', { root: target }, SCAN_TIMEOUT_MS).then(
291
+ function (result) {
292
+ setProbe(result)
293
+ if (result && result.ok === true) {
294
+ var stats = result.stats || {}
295
+ setProbeNote({
296
+ tone: 'ok',
297
+ text:
298
+ '这条路径能扫:' + (stats.pages || 0) + ' 页 / ' + (stats.resources || 0) + ' 份资料(' + (result.ms || 0) + ' ms)',
299
+ })
300
+ } else {
301
+ setProbeNote({ tone: 'err', text: (result && result.error) || '这个路径扫不出页面' })
302
+ }
303
+ return result
304
+ },
305
+ function (error) {
306
+ setProbeNote({ tone: 'err', text: '这次没测成:' + error.message })
307
+ throw error
308
+ },
309
+ )
205
310
  })
206
311
  }
207
312
 
208
313
  function onSave() {
209
314
  var target = draft.trim()
315
+ // 空输入框直接提交等于「把配置写成空」=删掉库路径。这条口子必须堵在客户端,
316
+ // 光靠服务端「显式传空串才清」挡不住——用户清空输入框再点保存,传的就是空串。
317
+ if (target === '') {
318
+ setNotice({
319
+ tone: 'err',
320
+ text: '输入框是空的。想换库就填新路径;想清掉已保存的路径,请点右边那个「清掉配置」。',
321
+ })
322
+ return
323
+ }
210
324
  run('save', function () {
211
- return apiPost('/save', { root: target }).then(function (data) {
325
+ return apiPost('/save', { root: target }, SCAN_TIMEOUT_MS).then(function (data) {
212
326
  // 服务端拒绝保存(路径扫不出页面)时不会落盘,也不该改输入框里的草稿
213
327
  if (data && data.ok === false && data.saved === false) {
214
328
  setNotice({ tone: 'err', text: '没有保存:' + (data.error || '这个路径扫不出页面') })
@@ -229,7 +343,7 @@ window.__ModuleLoader__.load({
229
343
 
230
344
  function onRescan() {
231
345
  run('scan', function () {
232
- return apiPost('/rescan', {}).then(function (data) {
346
+ return apiPost('/rescan', {}, SCAN_TIMEOUT_MS).then(function (data) {
233
347
  // 只更新 state,不碰 draft —— 用户可能正在输入另一个路径
234
348
  setState(data)
235
349
  var stats = (data && data.stats) || {}
@@ -240,6 +354,17 @@ window.__ModuleLoader__.load({
240
354
  }
241
355
 
242
356
  function onReset() {
357
+ if (busyRef.current !== '') return
358
+ // 第一下只是上膛:这个按钮点错了要重配一遍,值得多问一句。
359
+ if (!confirmReset) {
360
+ setConfirmReset(true)
361
+ setNotice({ tone: 'warn', text: '再点一次「确认清掉?」才会删掉配置里的库路径(笔记本身不受影响)。' })
362
+ setTimeout(function () {
363
+ setConfirmReset(false)
364
+ }, 4000)
365
+ return
366
+ }
367
+ setConfirmReset(false)
243
368
  run('save', function () {
244
369
  return apiPost('/save', { root: '' }).then(function (data) {
245
370
  apply(data)
@@ -312,6 +437,41 @@ window.__ModuleLoader__.load({
312
437
  }),
313
438
  ),
314
439
 
440
+ // 覆盖诊断:发现多少、读到多少、漏在哪 —— 一行说清分母,不用猜。
441
+ state !== null && (stats.pages || 0) > 0
442
+ ? h(
443
+ 'div',
444
+ { className: 'wn-cov' },
445
+ h(
446
+ 'span',
447
+ { className: 'wn-cov-item' },
448
+ '发现 ' + (stats.discovered || 0) + ' 个文件 → 读到 ' + stats.pages + ' 页 / ' + (stats.sections || 0) + ' 小节',
449
+ ),
450
+ h('span', { className: 'wn-cov-item' }, bytesLabel(stats.bytes)),
451
+ (stats.skipped || 0) > 0 ? h('span', { className: 'wn-cov-item' }, '跳过 ' + stats.skipped + '(index/log/下划线开头)') : null,
452
+ (stats.unreadable || 0) > 0
453
+ ? h('span', { className: 'wn-cov-item wn-cov-warn' }, '读不了 ' + stats.unreadable + ' 个')
454
+ : null,
455
+ (stats.oversized || 0) > 0
456
+ ? h('span', { className: 'wn-cov-item wn-cov-warn' }, '超大未读 ' + stats.oversized + ' 个')
457
+ : null,
458
+ (stats.failuresTotal || 0) > 0
459
+ ? h('span', { className: 'wn-cov-item wn-cov-warn' }, '目录/stat 失败 ' + stats.failuresTotal)
460
+ : null,
461
+ stats.newestMtime
462
+ ? h('span', { className: 'wn-cov-item' }, '最新修改 ' + shortTime(stats.newestMtime))
463
+ : null,
464
+ stats.scannedAt
465
+ ? h(
466
+ 'span',
467
+ { className: 'wn-cov-item' },
468
+ '扫描于 ' + shortTime(stats.scannedAt) + ' · ' + (stats.scannedMs || 0) + ' ms',
469
+ )
470
+ : null,
471
+ h('span', { className: 'wn-cov-item wn-cov-path', title: '实际被扫描的目录' }, state.scannedRoot || state.root || ''),
472
+ )
473
+ : null,
474
+
315
475
  (state !== null && state.scale) ? h('div', { className: 'wn-scale' }, state.scale) : null,
316
476
 
317
477
  (state !== null && Array.isArray(state.topics) && state.topics.length > 0)
@@ -374,6 +534,20 @@ window.__ModuleLoader__.load({
374
534
  ? h(Notice, { tone: 'warn', message: '这个路径本身不是知识层,实际读的是它里面的:' + state.scannedRoot })
375
535
  : null,
376
536
 
537
+ // 只读检索面板:让人自己检查「模型会查到什么」,不用问模型。
538
+ // 库没配好也照常显示 —— 它自己会把错误说出来,不连累上面那些配置区。
539
+ h(
540
+ 'div',
541
+ { className: 'wn-card' },
542
+ h(
543
+ 'div',
544
+ { className: 'wn-card-head' },
545
+ h('span', { className: 'wn-card-title' }, '试一试检索'),
546
+ h('span', { className: 'wn-badge wn-badge-muted' }, '只读'),
547
+ ),
548
+ h(WikiSearchPanel, { compact: false }),
549
+ ),
550
+
377
551
  state === null
378
552
  ? null
379
553
  : h(
@@ -411,7 +585,10 @@ window.__ModuleLoader__.load({
411
585
  placeholder: '例如 D:\\笔记\\我的知识库 或 /home/me/notes',
412
586
  onChange: function (event) {
413
587
  setDraft(event.target.value)
588
+ // 换了路径,上一次「扫不出页面」的结论就不再指同一个东西了。
414
589
  setProbe(null)
590
+ setProbeNote(null)
591
+ setConfirmReset(false)
415
592
  },
416
593
  onKeyDown: function (event) {
417
594
  if (event.key === 'Enter' && busy === '') onCheck()
@@ -435,7 +612,15 @@ window.__ModuleLoader__.load({
435
612
  h('button', { className: 'wn-btn', disabled: busy !== '', onClick: onRescan }, busy === 'scan' ? '扫描中…' : '重新扫描'),
436
613
  h('span', { className: 'wn-actions-spacer' }),
437
614
  savedRoot !== ''
438
- ? h('button', { className: 'wn-btn wn-btn-ghost', disabled: busy !== '', onClick: onReset }, '清掉配置')
615
+ ? h(
616
+ 'button',
617
+ {
618
+ className: 'wn-btn wn-btn-ghost' + (confirmReset ? ' wn-btn-danger' : ''),
619
+ disabled: busy !== '',
620
+ onClick: onReset,
621
+ },
622
+ confirmReset ? '确认清掉?' : '清掉配置',
623
+ )
439
624
  : null,
440
625
  ),
441
626
 
@@ -483,6 +668,8 @@ window.__ModuleLoader__.load({
483
668
  h('textarea', {
484
669
  className: 'wn-textarea',
485
670
  rows: 3,
671
+ maxLength: 600,
672
+ 'aria-label': '覆盖整段注入文案(留空则用自动生成的主题词行)',
486
673
  disabled: promptDraft.enabled === false,
487
674
  value: promptDraft.override,
488
675
  placeholder: '留空 = 自动用下面这段(按标签算出来的)',
@@ -493,6 +680,8 @@ window.__ModuleLoader__.load({
493
680
  h('textarea', {
494
681
  className: 'wn-textarea',
495
682
  rows: 2,
683
+ maxLength: 600,
684
+ 'aria-label': '追加一句提示词',
496
685
  disabled: promptDraft.enabled === false,
497
686
  value: promptDraft.extra,
498
687
  placeholder: '追加一句(可选),例如:不确定时先查库再回答',
@@ -519,7 +708,7 @@ window.__ModuleLoader__.load({
519
708
  busy === 'prompt' ? '保存中…' : '保存提示词',
520
709
  ),
521
710
  h('span', { className: 'wn-actions-spacer' }),
522
- h('span', { className: 'wn-hint' }, '(预览是上一次扫描的结果,保存后刷新即更新)'),
711
+ h('span', { className: 'wn-hint' }, '(预览是上一次扫描的结果,保存后刷新即更新;两段各上限 600 字,超了会被截断)'),
523
712
  ),
524
713
  ),
525
714
 
@@ -550,6 +739,597 @@ window.__ModuleLoader__.load({
550
739
  h('div', { className: 'wn-foot' }, '只读:这个页面不会改知识库里的任何文件。'),
551
740
  ) }
552
741
 
742
+ /* ------------------------------ 检索面板 ------------------------------ */
743
+
744
+ // 通道名要能直接读出来。分数的权重写在 title 里 —— 想核的时候能核,
745
+ // 不想核的时候不占地方。
746
+ var VIA_LABEL = { name: '页名', alias: '别名', tag: '标签', body: '正文', index: '摘要' }
747
+ var VIA_HINT = {
748
+ name: '页名全等 120 分,页名含该词 60 分',
749
+ alias: '别名含该词 50 分',
750
+ tag: '标签含该词 25 分',
751
+ body: '正文每命中一次 3 分,最多算 12 次',
752
+ index: '只在其它通道都是 0 时按 index 摘要给 8 分(只补漏,不抢位)',
753
+ }
754
+
755
+ /**
756
+ * 把正文里命中查询词的地方标出来。
757
+ *
758
+ * 只做「标位置」,不改正文 —— 长词优先,否则「上下文工程」会被「上下文」切成两截。
759
+ * 返回数组(字符串与 <mark> 混排),交给 React 转义,不拼 HTML 字符串。
760
+ */
761
+ function highlightNodes(text, terms) {
762
+ var body = String(text === undefined || text === null ? '' : text)
763
+ if (body === '') return body
764
+ var words = []
765
+ for (var i = 0; i < (Array.isArray(terms) ? terms.length : 0); i += 1) {
766
+ var word = String(terms[i] === undefined || terms[i] === null ? '' : terms[i])
767
+ if (word !== '' && words.indexOf(word) === -1) words.push(word)
768
+ }
769
+ if (words.length === 0) return body
770
+ words.sort(function (a, b) {
771
+ return b.length - a.length
772
+ })
773
+ var pattern = words
774
+ .map(function (word) {
775
+ return word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
776
+ })
777
+ .join('|')
778
+ var re = new RegExp('(' + pattern + ')', 'gi')
779
+ var out = []
780
+ var last = 0
781
+ var match = re.exec(body)
782
+ var index = 0
783
+ while (match !== null) {
784
+ if (match.index > last) out.push(body.slice(last, match.index))
785
+ out.push(h('mark', { key: 'mk' + index, className: 'wn-mark' }, match[0]))
786
+ index += 1
787
+ last = match.index + match[0].length
788
+ if (match[0] === '') re.lastIndex += 1
789
+ match = re.exec(body)
790
+ }
791
+ if (last < body.length) out.push(body.slice(last))
792
+ return out
793
+ }
794
+
795
+ /**
796
+ * 通用「复制」按钮:citation、查询词都用它 —— 复制逻辑只写一份。
797
+ * 剪贴板不可用(http 下的老浏览器、或没有权限)时不假装成功文案骗人,
798
+ * 但也别弹错:按钮保持原样,用户再点一次也不会更糟。
799
+ */
800
+ function CopyButton(props) {
801
+ var text = String(props.text === undefined ? '' : props.text)
802
+ var copiedTuple = useState(false)
803
+ var copied = copiedTuple[0]
804
+ var setCopied = copiedTuple[1]
805
+ function copy() {
806
+ var clipboard = typeof navigator !== 'undefined' ? navigator.clipboard : undefined
807
+ if (clipboard === undefined || clipboard === null || typeof clipboard.writeText !== 'function') {
808
+ setCopied(true)
809
+ return
810
+ }
811
+ clipboard.writeText(text).then(
812
+ function () {
813
+ setCopied(true)
814
+ },
815
+ function () {},
816
+ )
817
+ }
818
+ return h(
819
+ 'button',
820
+ {
821
+ className: props.className || 'wn-cite-copy',
822
+ type: 'button',
823
+ title: props.title || '复制',
824
+ disabled: text === '',
825
+ onClick: copy,
826
+ },
827
+ copied ? props.doneLabel || '已复制' : props.label || '复制',
828
+ )
829
+ }
830
+
831
+ /** citation 那一行的「复制」:复制的是原文出处,方便贴进对话或别的地方核对。 */
832
+ function CiteRow(props) {
833
+ var text = String(props.text === undefined ? '' : props.text)
834
+ return h(
835
+ 'div',
836
+ { className: 'wn-hit-cite' },
837
+ h('span', { className: 'wn-hit-cite-text' }, text),
838
+ text === '' ? null : h(CopyButton, { text: text, title: '复制这行出处' }),
839
+ )
840
+ }
841
+
842
+ function TermChip(props) {
843
+ var term = props.term
844
+ var parts = term.parts || []
845
+ var detail =
846
+ parts.length === 0
847
+ ? '未命中'
848
+ : parts
849
+ .map(function (part) {
850
+ var name = VIA_LABEL[part.via] || part.via
851
+ return name + (part.via === 'body' ? ' ×' + part.count : '') + ' ' + part.score
852
+ })
853
+ .join(' · ')
854
+ var hint =
855
+ parts.length === 0
856
+ ? '这个词在这页里一次都没出现'
857
+ : parts.map(function (part) { return (VIA_HINT[part.via] || part.via) + ' → ' + part.score + ' 分' }).join('\n')
858
+ return h(
859
+ 'span',
860
+ {
861
+ className: 'wn-chip' + (parts.length === 0 ? ' wn-chip-dim' : ''),
862
+ title: hint,
863
+ },
864
+ h('span', { className: 'wn-chip-term' }, term.term),
865
+ h('span', { className: 'wn-chip-detail' }, detail),
866
+ )
867
+ }
868
+
869
+ function SearchHit(props) {
870
+ var hit = props.hit
871
+ var terms = Array.isArray(hit.termHits) ? hit.termHits : []
872
+ var rank = typeof props.rank === 'number' ? props.rank : 1
873
+ var top = typeof props.top === 'number' && props.top > 0 ? props.top : hit.score || 1
874
+ var ratio = top > 0 ? (hit.score || 0) / top : 0
875
+ // 太短的条看不见,但也不该假装它有分量:6% 只是让它视觉上存在。
876
+ var width = Math.max(6, Math.min(100, Math.round(ratio * 100)))
877
+ var termsText = terms.map(function (term) {
878
+ return term.term
879
+ })
880
+ var open = props.open === true
881
+ var detailsProps = { className: 'wn-hit' + (props.active === true ? ' wn-hit-active' : '') }
882
+ // 键盘上下选中的那一条默认展开:不然「选中」只剩一道边框,看不出选了干嘛
883
+ if (open) detailsProps.open = true
884
+ return h(
885
+ 'details',
886
+ detailsProps,
887
+ h(
888
+ 'summary',
889
+ { className: 'wn-hit-head' },
890
+ // summary 当 flex 容器在部分浏览器里会被 UA 样式顶掉(它自带 display 规则),
891
+ // 所以真正的行布局交给里面这层 span —— summary 只负责「可点、可展开」。
892
+ h(
893
+ 'span',
894
+ { className: 'wn-hit-row' },
895
+ h('span', { className: 'wn-hit-rank' }, String(rank)),
896
+ h('span', { className: 'wn-hit-name' }, hit.name),
897
+ hit.type !== '' && props.hideType !== true ? h('span', { className: 'wn-hit-type' }, hit.type) : null,
898
+ hit.allTerms === false ? h('span', { className: 'wn-hit-partial' }, '部分词') : null,
899
+ h(
900
+ 'span',
901
+ { className: 'wn-hit-meter', 'aria-hidden': 'true' },
902
+ h('span', { className: 'wn-hit-meter-fill', style: { width: width + '%' } }),
903
+ ),
904
+ h('span', { className: 'wn-hit-score' }, String(hit.score)),
905
+ ),
906
+ ),
907
+ h(
908
+ 'div',
909
+ { className: 'wn-hit-body' },
910
+ h(
911
+ 'div',
912
+ { className: 'wn-why' },
913
+ terms.map(function (term) {
914
+ return h(TermChip, { key: term.term, term: term })
915
+ }),
916
+ ),
917
+ hit.section && hit.section.title !== ''
918
+ ? h('div', { className: 'wn-hit-section' }, '§ ' + hit.section.title)
919
+ : null,
920
+ h(
921
+ 'div',
922
+ { className: 'wn-hit-text' },
923
+ hit.section && hit.section.text !== ''
924
+ ? highlightNodes(hit.section.text, termsText)
925
+ : '(这一页命中了,但没有命中某个正文小节)',
926
+ ),
927
+ h(CiteRow, { text: hit.citation || '' }),
928
+ ),
929
+ )
930
+ }
931
+
932
+ /* -------------------- 面板里可复用的几块(预览也用它) -------------------- */
933
+
934
+ /**
935
+ * 按「类型」分桶。只在真有两种以上类型时才分组:单一类型加标题只是多一层噪音。
936
+ * 纯函数,好测,也让预览不必再抄一遍分桶逻辑。
937
+ */
938
+ function groupHits(hits) {
939
+ var groups = []
940
+ for (var i = 0; i < hits.length; i += 1) {
941
+ var name = hits[i].type === '' ? '未标类型' : hits[i].type
942
+ var bucket = null
943
+ for (var j = 0; j < groups.length; j += 1) {
944
+ if (groups[j].name === name) bucket = groups[j]
945
+ }
946
+ if (bucket === null) {
947
+ bucket = { name: name, items: [] }
948
+ groups.push(bucket)
949
+ }
950
+ bucket.items.push({ hit: hits[i], rank: i + 1 })
951
+ }
952
+ return { groups: groups, grouped: groups.length > 1 }
953
+ }
954
+
955
+ /** 计数行:命中多少、参与打分多少、多快、库多大;OR 降级时多一句黄字。 */
956
+ function CountRow(props) {
957
+ var result = props.result
958
+ var shown = Array.isArray(result.hits) ? result.hits.length : 0
959
+ return h(
960
+ 'div',
961
+ { className: 'wn-count' },
962
+ h(
963
+ 'span',
964
+ { className: 'wn-count-main' },
965
+ '命中 ' + result.total + ' 页',
966
+ // 条数被 limit 截住时说清楚,否则「命中 7 页」下面只列 3 条会让人以为漏了。
967
+ shown < result.total ? '(显示前 ' + shown + ')' : '',
968
+ ),
969
+ result.degraded === true
970
+ ? h('span', { className: 'wn-count-degraded' }, '没有一条命中全部词,已降级为「命中任一」')
971
+ : null,
972
+ h(
973
+ 'span',
974
+ { className: 'wn-count-meta' },
975
+ '参与打分 ' +
976
+ result.considered +
977
+ ' 页 · ' +
978
+ (result.elapsedMs || 0) +
979
+ ' ms' +
980
+ (typeof result.scanned === 'number' ? ' · 库 ' + result.scanned + ' 页' : ''),
981
+ ),
982
+ )
983
+ }
984
+
985
+ /** 查询诊断:这次真正参与打分的词、各自命中多少页、AND 还是 OR。 */
986
+ function TermRow(props) {
987
+ var terms = Array.isArray(props.termPages) ? props.termPages : []
988
+ if (terms.length === 0) return null
989
+ return h(
990
+ 'div',
991
+ { className: 'wn-terms' },
992
+ h('span', { className: 'wn-terms-label' }, '实际用的词'),
993
+ terms.map(function (item) {
994
+ var missed = (item.pages || 0) === 0
995
+ return h(
996
+ 'span',
997
+ {
998
+ key: item.term,
999
+ className: 'wn-term' + (missed ? ' wn-term-miss' : ''),
1000
+ title: missed ? '这个词在库里一页都没沾到 —— 换个说法或换个词' : '这个词在 ' + item.pages + ' 页上拿到了分',
1001
+ },
1002
+ h('span', { className: 'wn-term-word' }, item.term),
1003
+ h('span', { className: 'wn-term-count' }, missed ? '没沾到' : item.pages + ' 页'),
1004
+ )
1005
+ }),
1006
+ h(
1007
+ 'span',
1008
+ { className: 'wn-terms-mode' },
1009
+ props.matchMode === 'or' ? '匹配:命中任一(OR)' : '匹配:全词都要(AND)',
1010
+ ),
1011
+ )
1012
+ }
1013
+
1014
+ /** 命中列表:分组与否、选中哪条、分数条按「相对第一名」缩放。 */
1015
+ function HitList(props) {
1016
+ var hits = Array.isArray(props.hits) ? props.hits : []
1017
+ if (hits.length === 0) return null
1018
+ var active = typeof props.active === 'number' ? props.active : -1
1019
+ var top = hits[0].score
1020
+ function node(item, hideType) {
1021
+ return h(SearchHit, {
1022
+ key: item.hit.name,
1023
+ hit: item.hit,
1024
+ rank: item.rank,
1025
+ top: top,
1026
+ hideType: hideType,
1027
+ active: active === item.rank - 1,
1028
+ open: active === item.rank - 1,
1029
+ })
1030
+ }
1031
+ var split = groupHits(hits)
1032
+ return h(
1033
+ 'div',
1034
+ { className: 'wn-hit-list' },
1035
+ split.grouped
1036
+ ? split.groups.map(function (group) {
1037
+ return h(
1038
+ 'div',
1039
+ { className: 'wn-group', key: group.name },
1040
+ h('div', { className: 'wn-group-label' }, group.name + '(' + group.items.length + ')'),
1041
+ group.items.map(function (item) {
1042
+ return node(item, true)
1043
+ }),
1044
+ )
1045
+ })
1046
+ : hits.map(function (hit, index) {
1047
+ return node({ hit: hit, rank: index + 1 }, false)
1048
+ }),
1049
+ )
1050
+ }
1051
+
1052
+ /** 空状态:库多大、覆盖到哪、以及几个能直接点的标签。 */
1053
+ function EmptyState(props) {
1054
+ var lib = props.lib
1055
+ var pick = typeof props.onPick === 'function' ? props.onPick : function () {}
1056
+ return h(
1057
+ 'div',
1058
+ { className: 'wn-empty' },
1059
+ lib !== null && lib !== undefined
1060
+ ? h('div', { className: 'wn-empty-lib' }, '库 ' + lib.pages + ' 页 · ' + lib.resources + ' 份资料 · 实时同步' + lib.watch)
1061
+ : null,
1062
+ lib !== null && lib !== undefined
1063
+ ? h(
1064
+ 'div',
1065
+ { className: 'wn-empty-cov' },
1066
+ '参与检索 ' +
1067
+ lib.pages +
1068
+ ' 页 / ' +
1069
+ lib.sections +
1070
+ ' 个小节' +
1071
+ (lib.oversized > 0 ? ' · 超大未读 ' + lib.oversized : '') +
1072
+ (lib.unreadable > 0 ? ' · 读不了 ' + lib.unreadable : '') +
1073
+ (lib.skipped > 0 ? ' · 跳过 ' + lib.skipped : ''),
1074
+ )
1075
+ : null,
1076
+ h('div', { className: 'wn-empty-line' }, '和模型调 wiki_recall 是同一条检索路径 —— 这儿查不到,模型也拿不到。'),
1077
+ lib !== null && lib !== undefined && Array.isArray(lib.topics) && lib.topics.length > 0
1078
+ ? h(
1079
+ 'div',
1080
+ { className: 'wn-suggest' },
1081
+ h('span', { className: 'wn-suggest-label' }, '试试'),
1082
+ lib.topics.map(function (topic) {
1083
+ return h(
1084
+ 'button',
1085
+ {
1086
+ key: topic.tag,
1087
+ className: 'wn-chip wn-suggest-chip',
1088
+ type: 'button',
1089
+ title: '库里 ' + topic.count + ' 个页面带这个标签',
1090
+ onClick: function () {
1091
+ pick(topic.tag)
1092
+ },
1093
+ },
1094
+ h('span', { className: 'wn-chip-term' }, topic.tag),
1095
+ )
1096
+ }),
1097
+ )
1098
+ : null,
1099
+ )
1100
+ }
1101
+
1102
+ /**
1103
+ * 检索面板:设置页与右侧栏共用同一个组件。
1104
+ *
1105
+ * 它只做一件事 —— 把人放进模型的位置:同一条 `/api/wiki-notes/search`,
1106
+ * 也就是同一个 run(mode:'search')。所以「面板上看到的」和「模型拿到的」不可能分叉。
1107
+ */
1108
+ function WikiSearchPanel(props) {
1109
+ var compact = props !== null && props !== undefined && props.compact === true
1110
+
1111
+ var queryTuple = useState('')
1112
+ var query = queryTuple[0]
1113
+ var setQuery = queryTuple[1]
1114
+
1115
+ var resultTuple = useState(null)
1116
+ var result = resultTuple[0]
1117
+ var setResult = resultTuple[1]
1118
+
1119
+ var busyTuple = useState(false)
1120
+ var busy = busyTuple[0]
1121
+ var setBusy = busyTuple[1]
1122
+
1123
+ var errorTuple = useState('')
1124
+ var error = errorTuple[0]
1125
+ var setError = errorTuple[1]
1126
+
1127
+ // 连按回车会发出多个请求;只认最后一次 —— 先发的慢响应不许盖掉后发的。
1128
+ var seqRef = useRef(0)
1129
+ // 边打边查的防抖:停手 350ms 才发,不然每敲一个字就扫一遍库。
1130
+ var timerRef = useRef(0)
1131
+ var activeTuple = useState(-1)
1132
+ var active = activeTuple[0]
1133
+ var setActive = activeTuple[1]
1134
+ // 快捷键要把焦点送进输入框,只能靠 DOM 引用
1135
+ var inputRef = useRef(null)
1136
+
1137
+ // 空状态得有点信息可看:库多大、同步开没开、库里的词长什么样。
1138
+ // 失败就算了 —— 建议是锦上添花,拿不到建议不该让面板看起来像坏了。
1139
+ var libTuple = useState(null)
1140
+ var lib = libTuple[0]
1141
+ var setLib = libTuple[1]
1142
+ useEffect(function () {
1143
+ var alive = true
1144
+ apiGet('/state', SCAN_TIMEOUT_MS)
1145
+ .then(function (data) {
1146
+ if (!alive || data === null || data === undefined) return
1147
+ var stats = data.stats || {}
1148
+ setLib({
1149
+ pages: stats.pages || 0,
1150
+ resources: stats.resources || 0,
1151
+ watch: data.watch === 'on' ? '开' : '关',
1152
+ topics: (Array.isArray(data.topics) ? data.topics : []).slice(0, 6),
1153
+ // 覆盖诊断:空结果时要说清「是库里没有,还是这次没读到」
1154
+ sections: stats.sections || 0,
1155
+ oversized: stats.oversized || 0,
1156
+ unreadable: stats.unreadable || 0,
1157
+ skipped: stats.skipped || 0,
1158
+ root: data.scannedRoot || data.root || '',
1159
+ })
1160
+ })
1161
+ .catch(function () {})
1162
+ return function () {
1163
+ alive = false
1164
+ }
1165
+ }, [])
1166
+
1167
+ // `/` 或 Ctrl/⌘+K 把焦点送进输入框:面板挂在侧栏时,手通常不在鼠标上。
1168
+ useEffect(function () {
1169
+ if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') return undefined
1170
+ function onKey(event) {
1171
+ var target = event.target
1172
+ var tag = target !== null && target !== undefined && target.tagName ? String(target.tagName).toLowerCase() : ''
1173
+ if (tag === 'input' || tag === 'textarea') return
1174
+ var isSlash = event.key === '/' && event.metaKey !== true && event.ctrlKey !== true
1175
+ var isCombo = (event.metaKey === true || event.ctrlKey === true) && String(event.key).toLowerCase() === 'k'
1176
+ if (!isSlash && !isCombo) return
1177
+ var node = inputRef.current
1178
+ if (node === null || node === undefined || typeof node.focus !== 'function') return
1179
+ event.preventDefault()
1180
+ node.focus()
1181
+ }
1182
+ window.addEventListener('keydown', onKey)
1183
+ return function () {
1184
+ window.removeEventListener('keydown', onKey)
1185
+ }
1186
+ }, [])
1187
+
1188
+ function runSearch(text) {
1189
+ var trimmed = String(text === undefined ? query : text).trim()
1190
+ if (trimmed === '' || busy) return
1191
+ setQuery(trimmed)
1192
+ setActive(-1)
1193
+ seqRef.current = seqRef.current + 1
1194
+ var seq = seqRef.current
1195
+ setBusy(true)
1196
+ setError('')
1197
+ // 和模型同样的默认条数(5 条),侧栏放宽到 8 条方便扫。
1198
+ apiGet('/search?q=' + encodeURIComponent(trimmed) + '&limit=' + (compact ? 8 : 5), SCAN_TIMEOUT_MS)
1199
+ .then(function (data) {
1200
+ if (seq !== seqRef.current) return
1201
+ setResult(data)
1202
+ })
1203
+ .catch(function (failure) {
1204
+ if (seq !== seqRef.current) return
1205
+ setResult(null)
1206
+ setError(failure && failure.message ? failure.message : '查询失败')
1207
+ })
1208
+ .then(function () {
1209
+ if (seq !== seqRef.current) return
1210
+ setBusy(false)
1211
+ })
1212
+ }
1213
+
1214
+ function submit() {
1215
+ // 手动提交要压掉待发的防抖请求,不然回车后还会再发一次同样的查询
1216
+ if (timerRef.current !== 0 && typeof clearTimeout === 'function') clearTimeout(timerRef.current)
1217
+ timerRef.current = 0
1218
+ runSearch(query)
1219
+ }
1220
+
1221
+ function scheduleSearch(text) {
1222
+ if (typeof setTimeout !== 'function') return
1223
+ if (timerRef.current !== 0 && typeof clearTimeout === 'function') clearTimeout(timerRef.current)
1224
+ timerRef.current = setTimeout(function () {
1225
+ timerRef.current = 0
1226
+ var trimmed = String(text === undefined ? '' : text).trim()
1227
+ // 一个字就查多半是噪音:中文单字命中面太宽,结果没人看
1228
+ if (trimmed.length < 2) return
1229
+ runSearch(trimmed)
1230
+ }, 350)
1231
+ }
1232
+
1233
+ function clearAll() {
1234
+ if (timerRef.current !== 0 && typeof clearTimeout === 'function') clearTimeout(timerRef.current)
1235
+ timerRef.current = 0
1236
+ seqRef.current = seqRef.current + 1
1237
+ setQuery('')
1238
+ setResult(null)
1239
+ setError('')
1240
+ setActive(-1)
1241
+ }
1242
+
1243
+ var hits = result !== null && Array.isArray(result.hits) ? result.hits : []
1244
+
1245
+ return h(
1246
+ 'div',
1247
+ { className: 'wn-panel' + (compact ? ' wn-panel-compact' : '') },
1248
+ h(
1249
+ 'div',
1250
+ { className: 'wn-search' },
1251
+ h('input', {
1252
+ className: 'wn-input wn-search-input',
1253
+ type: 'text',
1254
+ value: query,
1255
+ ref: inputRef,
1256
+ spellCheck: false,
1257
+ 'aria-label': '检索词',
1258
+ placeholder: '输入关键词,例如「上下文 预算」',
1259
+ onChange: function (event) {
1260
+ var next = event.target.value
1261
+ setQuery(next)
1262
+ scheduleSearch(next)
1263
+ },
1264
+ onKeyDown: function (event) {
1265
+ if (event.key === 'Enter') {
1266
+ event.preventDefault()
1267
+ submit()
1268
+ return
1269
+ }
1270
+ if (event.key === 'Escape') {
1271
+ event.preventDefault()
1272
+ clearAll()
1273
+ return
1274
+ }
1275
+ // 上下键在结果之间走:面板里不该非拿鼠标不可
1276
+ if (event.key === 'ArrowDown' && hits.length > 0) {
1277
+ event.preventDefault()
1278
+ setActive(function (prev) {
1279
+ return Math.min(prev + 1, hits.length - 1)
1280
+ })
1281
+ return
1282
+ }
1283
+ if (event.key === 'ArrowUp' && hits.length > 0) {
1284
+ event.preventDefault()
1285
+ setActive(function (prev) {
1286
+ return Math.max(prev - 1, 0)
1287
+ })
1288
+ }
1289
+ },
1290
+ }),
1291
+ // 查一次常常要换个地方再查:把这次的词原样带走,比让人重新敲一遍强。
1292
+ query.trim() === ''
1293
+ ? null
1294
+ : h(CopyButton, {
1295
+ className: 'wn-copy-query',
1296
+ text: query.trim(),
1297
+ label: '复制词',
1298
+ title: '复制这次的查询词',
1299
+ }),
1300
+ h(
1301
+ 'button',
1302
+ {
1303
+ className: 'wn-btn wn-btn-primary',
1304
+ type: 'button',
1305
+ disabled: busy || query.trim() === '',
1306
+ onClick: submit,
1307
+ },
1308
+ busy ? '查…' : '查一下',
1309
+ ),
1310
+ ),
1311
+
1312
+ error !== '' ? h(Notice, { tone: 'err', message: error }) : null,
1313
+
1314
+ busy ? h('div', { className: 'wn-busy' }, '检索中…') : null,
1315
+
1316
+ result !== null ? h(CountRow, { result: result }) : null,
1317
+
1318
+ result !== null ? h(TermRow, { termPages: result.termPages, matchMode: result.matchMode }) : null,
1319
+
1320
+ result !== null && hits.length === 0
1321
+ ? h(Notice, {
1322
+ tone: 'warn',
1323
+ message: '一条也没命中。要么库里确实没写这个说法,要么该换个词 —— 中文不分词,写整句只会一个词都匹配不上。',
1324
+ })
1325
+ : null,
1326
+
1327
+ h(HitList, { hits: hits, active: active }),
1328
+
1329
+ result === null && error === '' && busy === false ? h(EmptyState, { lib: lib, onPick: runSearch }) : null,
1330
+ )
1331
+ }
1332
+
553
1333
  /* ------------------------------ 样式 ------------------------------ */
554
1334
 
555
1335
  var CSS = [
@@ -571,20 +1351,24 @@ window.__ModuleLoader__.load({
571
1351
  'color:var(--wn-fg);display:flex;flex-direction:column;gap:14px;padding:2px;box-sizing:border-box;max-width:100%;}',
572
1352
  '.wn-title{font-size:15px;font-weight:650;letter-spacing:.2px;}',
573
1353
  '.wn-sub{font-size:12.5px;color:var(--wn-muted);margin-top:5px;line-height:1.6;}',
574
- '.wn-dim{font-size:12.5px;color:var(--wn-faint);}',
1354
+ '.wn-dim{font-size:12.5px;color:var(--wn-muted);}',
575
1355
  '.wn-stats{display:flex;gap:10px;flex-wrap:wrap;}',
576
1356
  '.wn-stat{flex:1 1 96px;min-width:96px;background:var(--wn-bar);border:1px solid var(--wn-line-soft);border-radius:10px;padding:10px 12px;}',
577
1357
  '.wn-stat-value{font-size:22px;font-weight:600;line-height:1.15;font-variant-numeric:tabular-nums;}',
578
1358
  '.wn-stat-label{font-size:11.5px;color:var(--wn-muted);margin-top:3px;}',
579
1359
  '.wn-stat-warn .wn-stat-value{color:var(--wn-warn);}',
580
- '.wn-scale{font-size:12px;color:var(--wn-faint);}',
1360
+ '.wn-scale{font-size:12px;color:var(--wn-muted);}',
1361
+ '.wn-cov{display:flex;flex-wrap:wrap;gap:5px 12px;font-size:11.5px;color:var(--wn-muted);font-variant-numeric:tabular-nums;}',
1362
+ '.wn-cov-item{white-space:nowrap;}',
1363
+ '.wn-cov-warn{color:var(--wn-warn);}',
1364
+ '.wn-cov-path{flex:1 1 100%;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11px;}',
581
1365
  '.wn-card{background:var(--wn-card);border:1px solid var(--wn-line);border-radius:12px;padding:13px 14px;display:flex;flex-direction:column;gap:10px;}',
582
1366
  '.wn-card-head{display:flex;align-items:center;gap:8px;}',
583
1367
  '.wn-card-title{font-size:12.5px;font-weight:600;color:var(--wn-muted);}',
584
1368
  '.wn-badge{font-size:11px;padding:2px 7px;border-radius:999px;border:1px solid var(--wn-line);color:var(--wn-muted);background:var(--wn-bar);white-space:nowrap;}',
585
1369
  '.wn-badge-ok{color:var(--wn-ok);border-color:color-mix(in srgb,var(--wn-ok) 35%,transparent);}',
586
1370
  '.wn-badge-warn{color:var(--wn-warn);border-color:color-mix(in srgb,var(--wn-warn) 35%,transparent);}',
587
- '.wn-badge-muted{color:var(--wn-faint);}',
1371
+ '.wn-badge-muted{color:var(--wn-muted);}',
588
1372
  '.wn-path{word-break:break-all;font-family:var(--dsw-font-family-mono,ui-monospace,SFMono-Regular,Menlo,Consolas,monospace);font-size:12px;background:var(--wn-field);border:1px solid var(--wn-line-soft);border-radius:8px;padding:8px 10px;}',
589
1373
  '.wn-field-row{display:flex;gap:8px;}',
590
1374
  '.wn-input{flex:1 1 auto;min-width:0;box-sizing:border-box;font-size:12.5px;font-family:var(--dsw-font-family-mono,ui-monospace,SFMono-Regular,Menlo,Consolas,monospace);color:var(--wn-fg);background:var(--wn-field);border:1px solid var(--wn-line);border-radius:8px;padding:8px 10px;outline:none;}',
@@ -598,12 +1382,13 @@ window.__ModuleLoader__.load({
598
1382
  '.wn-btn:focus-visible,.wn-input:focus-visible{outline:2px solid var(--wn-brand);outline-offset:2px;}',
599
1383
  '.wn-btn-primary{background:var(--wn-brand);color:var(--wn-brand-fg);border-color:transparent;}',
600
1384
  '.wn-btn-ghost{background:transparent;color:var(--wn-muted);}',
1385
+ '.wn-btn-danger{color:var(--wn-err);border-color:color-mix(in srgb,var(--wn-err) 45%,transparent);background:color-mix(in srgb,var(--wn-err) 8%,transparent);}',
601
1386
  '.wn-notice{font-size:12.5px;line-height:1.55;padding:8px 11px;border-radius:8px;border:1px solid var(--wn-line-soft);background:var(--wn-bar);color:var(--wn-muted);}',
602
1387
  '.wn-notice-ok{color:var(--wn-ok);border-color:color-mix(in srgb,var(--wn-ok) 30%,transparent);background:color-mix(in srgb,var(--wn-ok) 8%,transparent);}',
603
1388
  '.wn-notice-err{color:var(--wn-err);border-color:color-mix(in srgb,var(--wn-err) 30%,transparent);background:color-mix(in srgb,var(--wn-err) 8%,transparent);}',
604
1389
  '.wn-notice-warn{color:var(--wn-warn);border-color:color-mix(in srgb,var(--wn-warn) 30%,transparent);background:color-mix(in srgb,var(--wn-warn) 8%,transparent);}',
605
1390
  '.wn-probe{font-size:12px;color:var(--wn-muted);line-height:1.6;}',
606
- '.wn-hint{font-size:11.5px;color:var(--wn-faint);line-height:1.65;word-break:break-all;}',
1391
+ '.wn-hint{font-size:11.5px;color:var(--wn-muted);line-height:1.65;word-break:break-all;}',
607
1392
  '.wn-prompt{display:flex;flex-direction:column;gap:7px;border-top:1px dashed var(--wn-line-soft);padding-top:11px;}',
608
1393
  '.wn-prompt-head{display:flex;align-items:center;gap:7px;font-size:12.5px;color:var(--wn-fg);cursor:pointer;}',
609
1394
  '.wn-textarea{font:inherit;font-size:12.5px;line-height:1.6;padding:8px 10px;border-radius:8px;border:1px solid var(--wn-line);background:var(--wn-bar);color:var(--wn-fg);resize:vertical;}',
@@ -614,7 +1399,7 @@ window.__ModuleLoader__.load({
614
1399
  '.wn-hint-warn{color:var(--wn-warn);}',
615
1400
  '.wn-hint-dim{opacity:.75;}',
616
1401
  '.wn-head{display:flex;flex-direction:column;gap:3px;}',
617
- '.wn-statusbar{display:flex;align-items:center;gap:7px;flex-wrap:wrap;font-size:11.5px;color:var(--wn-faint);}',
1402
+ '.wn-statusbar{display:flex;align-items:center;gap:7px;flex-wrap:wrap;font-size:11.5px;color:var(--wn-muted);}',
618
1403
  '.wn-dot{width:7px;height:7px;border-radius:50%;flex:0 0 auto;}',
619
1404
  '.wn-dot-on{background:var(--wn-ok);box-shadow:0 0 0 3px color-mix(in srgb,var(--wn-ok) 16%,transparent);}',
620
1405
  '.wn-dot-off{background:var(--wn-faint);opacity:.5;}',
@@ -627,9 +1412,12 @@ window.__ModuleLoader__.load({
627
1412
  '.wn-topics-label{font-size:11.5px;color:var(--wn-muted);}',
628
1413
  '.wn-toggle{display:flex;align-items:baseline;gap:6px;flex-wrap:wrap;font-size:11.5px;color:var(--wn-muted);cursor:pointer;}',
629
1414
  '.wn-toggle input{cursor:pointer;}',
630
- '.wn-chip{font-size:11.5px;padding:2px 8px;border-radius:999px;background:var(--wn-bar);border:1px solid var(--wn-line-soft);color:var(--wn-muted);}',
1415
+ '.wn-chip{display:inline-flex;align-items:baseline;gap:6px;font-size:11.5px;padding:2px 8px;border-radius:6px;background:var(--wn-bar);border:1px solid var(--wn-line-soft);color:var(--wn-muted);}',
1416
+ '.wn-chip-term{font-weight:600;color:var(--wn-fg);}',
1417
+ '.wn-chip-detail{color:var(--wn-muted);font-variant-numeric:tabular-nums;}',
631
1418
  '.wn-chip-warn{color:var(--wn-warn);border-color:color-mix(in srgb,var(--wn-warn) 35%,transparent);}',
632
- '.wn-chip-dim{color:var(--wn-faint);}',
1419
+ '.wn-chip-dim{opacity:.7;}',
1420
+ '.wn-chip-dim .wn-chip-term{font-weight:500;color:var(--wn-faint);}',
633
1421
  '.wn-actions-spacer{flex:1 1 auto;}',
634
1422
  '.wn-details{border-top:1px solid var(--wn-line-soft);padding-top:9px;margin-top:2px;}',
635
1423
  '.wn-details summary{font-size:11.5px;color:var(--wn-muted);cursor:pointer;user-select:none;list-style:none;}',
@@ -637,13 +1425,99 @@ window.__ModuleLoader__.load({
637
1425
  '.wn-details summary::before{content:"▸ ";color:var(--wn-faint);}',
638
1426
  '.wn-details[open] summary::before{content:"▾ ";}',
639
1427
  '.wn-details-body{display:flex;flex-direction:column;gap:4px;padding-top:8px;}',
1428
+ '.wn-panel{display:flex;flex-direction:column;gap:10px;}',
1429
+ '.wn-search{display:flex;gap:8px;align-items:center;}',
1430
+ '.wn-search-input{flex:1 1 auto;min-width:0;font-family:inherit;font-size:13px;}',
1431
+ // 计数行:主信息在左,元信息推到行尾 —— 一眼看到「命中几页」,想核再看耗时。
1432
+ '.wn-count{display:flex;align-items:baseline;gap:10px;flex-wrap:wrap;font-size:12px;color:var(--wn-muted);font-variant-numeric:tabular-nums;}',
1433
+ '.wn-count-main{color:var(--wn-fg);font-weight:600;}',
1434
+ '.wn-count-degraded{font-size:11.5px;color:var(--wn-warn);}',
1435
+ '.wn-count-meta{margin-left:auto;font-size:11px;color:var(--wn-muted);}',
1436
+ // 结果按行排、不按卡片:一列的边框会把 1/2/3 名的强弱比较打断。
1437
+ '.wn-hit-list{display:flex;flex-direction:column;border-top:1px solid var(--wn-line-soft);}',
1438
+ '.wn-hit{border-bottom:1px solid var(--wn-line-soft);}',
1439
+ '.wn-hit[open]{background:var(--wn-bar);}',
1440
+ '.wn-hit-head{display:block;padding:0;cursor:pointer;list-style:none;user-select:none;}',
1441
+ '.wn-hit-head::-webkit-details-marker{display:none;}',
1442
+ '.wn-hit-row{display:flex;align-items:center;gap:9px;padding:9px 5px;font-size:13px;line-height:1.45;}',
1443
+ '.wn-hit-row:hover{background:var(--wn-bar);}',
1444
+ '.wn-hit[open] .wn-hit-row:hover{background:transparent;}',
1445
+ '.wn-hit-rank{flex:0 0 15px;text-align:right;font-size:11px;color:var(--wn-muted);font-variant-numeric:tabular-nums;}',
1446
+ '.wn-hit-name{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-weight:600;color:var(--wn-fg);}',
1447
+ '.wn-hit-type{font-size:10.5px;line-height:1.6;padding:0 5px;border-radius:5px;border:1px solid var(--wn-line-soft);color:var(--wn-muted);white-space:nowrap;}',
1448
+ '.wn-hit-partial{font-size:10.5px;color:var(--wn-warn);white-space:nowrap;}',
1449
+ '.wn-hit-meter{flex:0 0 44px;height:3px;border-radius:2px;margin-left:auto;background:var(--wn-line-soft);overflow:hidden;}',
1450
+ '.wn-hit-meter-fill{display:block;height:100%;border-radius:2px;background:color-mix(in srgb,var(--wn-brand) 58%,transparent);}',
1451
+ '.wn-hit-score{flex:0 0 32px;text-align:right;font-size:12.5px;font-weight:600;font-variant-numeric:tabular-nums;color:var(--wn-fg);}',
1452
+ '.wn-hit-body{display:flex;flex-direction:column;gap:7px;padding:2px 5px 12px 24px;}',
1453
+ '.wn-why{display:flex;flex-wrap:wrap;gap:5px;}',
1454
+ '.wn-hit-section{font-size:11px;color:var(--wn-muted);}',
1455
+ // 设置页正文不设高度上限(页面本来就长,截断反而多出滚动条);侧栏才封顶。
1456
+ '.wn-hit-text{font-size:12.5px;line-height:1.75;color:var(--wn-fg);white-space:pre-wrap;padding-right:4px;}',
1457
+ '.wn-hit-cite{font-size:10.5px;color:var(--wn-muted);word-break:break-all;display:flex;align-items:baseline;gap:8px;}',
1458
+ '.wn-hit-cite-text{flex:1 1 auto;min-width:0;}',
1459
+ '.wn-cite-copy{flex:0 0 auto;font-family:inherit;font-size:10.5px;line-height:1.6;padding:0 6px;border-radius:5px;border:1px solid var(--wn-line);background:var(--wn-bar);color:var(--wn-muted);cursor:pointer;}',
1460
+ '.wn-cite-copy:hover{background:var(--wn-field);color:var(--wn-fg);}',
1461
+ '.wn-copy-query{flex:0 0 auto;font-family:inherit;font-size:12px;line-height:1.9;padding:0 9px;border-radius:7px;border:1px solid var(--wn-line);background:transparent;color:var(--wn-muted);cursor:pointer;}',
1462
+ '.wn-copy-query:hover{background:var(--wn-bar);color:var(--wn-fg);}',
1463
+ // 键盘走一圈时要看得见落点:录不录用户无所谓,不能靠鼠标才知道焦点在哪。
1464
+ '.wn-cite-copy:focus-visible,.wn-copy-query:focus-visible,.wn-suggest-chip:focus-visible,.wn-hit-head:focus-visible,.wn-search-input:focus-visible,.wn-btn:focus-visible{outline:2px solid var(--wn-brand);outline-offset:1px;}',
1465
+ '.wn-cite-copy:disabled,.wn-copy-query:disabled{opacity:.55;cursor:default;}',
1466
+ '.wn-cite-copy:hover{color:var(--wn-fg);border-color:var(--wn-line);}',
1467
+ // 正文里的命中位置:底纹而不是改字色,正文照旧可读
1468
+ '.wn-mark{background:color-mix(in srgb,var(--wn-warn) 22%,transparent);color:inherit;border-radius:2px;padding:0 1px;}',
1469
+ '.wn-hit-active{background:var(--wn-bar);}',
1470
+ // 查询诊断:引擎到底拿哪几个词去找的
1471
+ '.wn-terms{display:flex;flex-wrap:wrap;align-items:center;gap:6px;font-size:11.5px;color:var(--wn-muted);}',
1472
+ '.wn-terms-label{color:var(--wn-muted);}',
1473
+ '.wn-term{display:inline-flex;align-items:baseline;gap:5px;padding:1px 6px;border-radius:5px;border:1px solid var(--wn-line-soft);background:var(--wn-bar);}',
1474
+ '.wn-term-word{font-weight:600;color:var(--wn-fg);}',
1475
+ '.wn-term-count{font-variant-numeric:tabular-nums;color:var(--wn-muted);}',
1476
+ '.wn-term-miss{border-style:dashed;border-color:var(--wn-err);}',
1477
+ '.wn-term-miss .wn-term-word{color:var(--wn-err);}',
1478
+ '.wn-terms-mode{margin-left:auto;font-size:11px;color:var(--wn-muted);}',
1479
+ '.wn-group{display:flex;flex-direction:column;}',
1480
+ '.wn-group-label{font-size:10.5px;letter-spacing:.4px;color:var(--wn-muted);padding:9px 2px 3px;}',
1481
+ '.wn-busy{font-size:11.5px;color:var(--wn-muted);}',
1482
+ '.wn-empty-cov{font-size:11px;color:var(--wn-muted);font-variant-numeric:tabular-nums;line-height:1.6;}',
1483
+ '.wn-panel-compact .wn-hit-text{max-height:220px;overflow:auto;}',
1484
+ '.wn-panel-compact .wn-hit-row{padding:8px 2px;}',
1485
+ '.wn-panel-compact .wn-hit-body{padding-left:24px;padding-right:2px;}',
1486
+ // 空状态:与其留一片白,不如把「库多大」和「库里的词长什么样」摆出来。
1487
+ '.wn-empty{display:flex;flex-direction:column;gap:8px;padding:12px 13px;border:1px dashed var(--wn-line);border-radius:10px;}',
1488
+ '.wn-empty-lib{font-size:11.5px;color:var(--wn-muted);font-variant-numeric:tabular-nums;}',
1489
+ '.wn-empty-line{font-size:12px;line-height:1.6;color:var(--wn-muted);}',
1490
+ '.wn-suggest{display:flex;flex-wrap:wrap;align-items:center;gap:6px;}',
1491
+ '.wn-suggest-label{font-size:11px;color:var(--wn-muted);}',
1492
+ '.wn-suggest-chip{font-family:inherit;cursor:pointer;}',
1493
+ '.wn-suggest-chip:hover{border-color:var(--wn-brand);}',
1494
+ '.wn-suggest-chip:focus-visible{outline:2px solid var(--wn-brand);outline-offset:2px;}',
640
1495
  '.wn-notice{display:flex;gap:7px;align-items:flex-start;}',
641
1496
  '.wn-notice-mark{flex:0 0 auto;font-weight:700;opacity:.85;}',
642
1497
  '.wn-notice-text{flex:1 1 auto;}',
643
- '.wn-foot{font-size:11.5px;color:var(--wn-faint);border-top:1px solid var(--wn-line-soft);padding-top:10px;}',
1498
+ '.wn-foot{font-size:11.5px;color:var(--wn-muted);border-top:1px solid var(--wn-line-soft);padding-top:10px;}',
644
1499
  '}',
645
1500
  ].join('')
646
1501
 
1502
+ /* ------------------------------ 右侧栏 ------------------------------ */
1503
+
1504
+ var SIDEBAR_TAB_ID = 'dsh-wiki-notes/search'
1505
+ var SIDEBAR_TAB_KIND = 'wiki-notes-search'
1506
+
1507
+ function WikiSearchSidebarTab() {
1508
+ return h(
1509
+ 'div',
1510
+ { className: 'wn-root wn-sidebar' },
1511
+ h(
1512
+ 'div',
1513
+ { className: 'wn-head' },
1514
+ h('div', { className: 'wn-title' }, '知识库'),
1515
+ h('div', { className: 'wn-sub' }, '在这儿查一遍,看模型为什么会、或者为什么不会想起你的笔记。'),
1516
+ ),
1517
+ h(WikiSearchPanel, { compact: true }),
1518
+ )
1519
+ }
1520
+
647
1521
  /* ------------------------------ 注册 ------------------------------ */
648
1522
 
649
1523
  function apply(ctx) {
@@ -661,7 +1535,7 @@ window.__ModuleLoader__.load({
661
1535
  }
662
1536
  }, 'dsh-wiki-notes: styles')
663
1537
 
664
- // settings.section = 设置弹层左侧的一级入口
1538
+ // 设置弹层左侧的一级入口
665
1539
  ctx.slots.inject('settings.section', function () {
666
1540
  return ctx.slots.register(
667
1541
  {
@@ -677,12 +1551,79 @@ window.__ModuleLoader__.load({
677
1551
  },
678
1552
  )
679
1553
  })
1554
+
1555
+ // 右侧栏入口:可选能力,坏了也不能连累设置页。
1556
+ // 服务不存在时 ctx.inject 的回调根本不会执行 —— 这就是要的故障隔离。
1557
+ // 也刻意没把它写进 package.json 的 dsh.client.inject:那会把「侧栏模块在不在」
1558
+ // 变成整个客户端包的加载前提,侧栏一缺,连设置页都进不去。
1559
+ if (typeof ctx.inject !== 'function') return
1560
+ ctx.inject(['sidebarRightTabs'], function (owner) {
1561
+ var tabs = owner.get('sidebarRightTabs')
1562
+ if (tabs === null || tabs === undefined || typeof tabs.register !== 'function') return
1563
+ owner.effect(function () {
1564
+ return tabs.register({
1565
+ id: SIDEBAR_TAB_ID,
1566
+ kind: SIDEBAR_TAB_KIND,
1567
+ priority: 'extension',
1568
+ title: function () {
1569
+ return '知识库'
1570
+ },
1571
+ guide: [
1572
+ {
1573
+ order: 100,
1574
+ // 侧栏显示的是 guide 这条的标题,不是上面的 title —— 两处必须同一个字符串,
1575
+ // 否则页签会写成一句说明("按关键词查一遍…")而不是入口名。
1576
+ title: function () {
1577
+ return '知识库'
1578
+ },
1579
+ },
1580
+ ],
1581
+ })
1582
+ }, 'dsh-wiki-notes: sidebar tab type')
1583
+
1584
+ // 必须用 owner 的槽位表登记:本模块的 ctx.slots 属于插件自己的作用域,
1585
+ // 往别人的插槽里注册会被静默丢掉。
1586
+ var ownerRegister = owner.slots.register.bind(owner.slots)
1587
+ owner.slots.inject('sidebar.right.pane.tab', function () {
1588
+ return ownerRegister(
1589
+ {
1590
+ name: 'sidebar.right.pane.tab',
1591
+ key: SIDEBAR_TAB_ID,
1592
+ inject: function () {
1593
+ return {}
1594
+ },
1595
+ },
1596
+ function () {
1597
+ return h(WikiSearchSidebarTab, {})
1598
+ },
1599
+ )
1600
+ })
1601
+ })
680
1602
  }
681
1603
 
682
1604
  module.exports = {
683
1605
  name: 'wiki-notes',
684
1606
  inject: ['slots'],
685
1607
  apply: apply,
1608
+ // 只给 tools/preview.mjs 用:把面板的零件交出去,好在真 CSS 下渲染一张静态预览。
1609
+ // 宿主不看这个键,多一个也不影响加载。
1610
+ __preview: {
1611
+ TermChip: TermChip,
1612
+ SearchHit: SearchHit,
1613
+ CiteRow: CiteRow,
1614
+ CopyButton: CopyButton,
1615
+ Notice: Notice,
1616
+ groupHits: groupHits,
1617
+ CountRow: CountRow,
1618
+ TermRow: TermRow,
1619
+ HitList: HitList,
1620
+ EmptyState: EmptyState,
1621
+ highlightNodes: highlightNodes,
1622
+ bytesLabel: bytesLabel,
1623
+ shortTime: shortTime,
1624
+ WikiSearchPanel: WikiSearchPanel,
1625
+ WikiSearchSidebarTab: WikiSearchSidebarTab,
1626
+ },
686
1627
  }
687
1628
  return module.exports
688
1629
  },