dsh-remote-vs 0.25.0 → 0.27.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.
Files changed (109) hide show
  1. package/README.en.md +12 -15
  2. package/README.md +11 -12
  3. package/THIRD-PARTY-NOTICES.md +193 -0
  4. package/lib/client.js +7313 -79961
  5. package/lib/index.js +1529 -964
  6. package/lib/p2-router-fs.js +1 -1
  7. package/lib/types/client/CodeEditor.d.ts +22 -0
  8. package/lib/types/client/OutlinePanel.d.ts +22 -0
  9. package/lib/types/client/ProcessesSection.d.ts +19 -0
  10. package/lib/types/client/RemoteExplorer.d.ts +53 -0
  11. package/lib/types/client/api.d.ts +42 -3
  12. package/lib/types/client/blame-model.d.ts +77 -0
  13. package/lib/types/client/changes/review-state.d.ts +19 -0
  14. package/lib/types/client/connection.d.ts +16 -0
  15. package/lib/types/client/i18n.d.ts +1 -1
  16. package/lib/types/client/outline-model.d.ts +59 -0
  17. package/lib/types/client/request-gate.d.ts +110 -0
  18. package/lib/types/client/scm/DiffView.d.ts +45 -7
  19. package/lib/types/client/scm/actions.d.ts +43 -4
  20. package/lib/types/client/store.d.ts +71 -0
  21. package/lib/types/client/vsc/FindWidget.d.ts +6 -0
  22. package/lib/types/client/vsc/blame.d.ts +19 -0
  23. package/lib/types/client/vsc/change-marks.d.ts +16 -6
  24. package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
  25. package/lib/types/client/vsc/kit.d.ts +11 -2
  26. package/lib/types/client/vsc/preview-document.d.ts +46 -0
  27. package/lib/types/client/vsc/preview-model.d.ts +31 -0
  28. package/lib/types/client/vsc/preview-services.d.ts +24 -0
  29. package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
  30. package/lib/types/client/vsc/terminal-theme.d.ts +28 -2
  31. package/lib/types/forward.d.ts +12 -0
  32. package/lib/types/fs-ops.d.ts +42 -0
  33. package/lib/types/git-ops.d.ts +18 -0
  34. package/lib/types/http.d.ts +2 -0
  35. package/lib/types/keygen.d.ts +9 -0
  36. package/lib/types/monitor.d.ts +8 -0
  37. package/lib/types/notices.d.ts +16 -1
  38. package/lib/types/p2/helper-rpc.d.ts +45 -0
  39. package/lib/types/processes.d.ts +43 -0
  40. package/lib/types/protocol.d.ts +34 -0
  41. package/lib/types/routes.d.ts +2 -0
  42. package/lib/types/store.d.ts +7 -0
  43. package/lib/types/terminal.d.ts +8 -0
  44. package/lib/types/view-settings.d.ts +5 -0
  45. package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
  46. package/package.json +3 -1
  47. package/src/client/CodeEditor.tsx +194 -9
  48. package/src/client/DirectoryFlow.tsx +16 -0
  49. package/src/client/DoctorPanel.tsx +70 -7
  50. package/src/client/EditorPane.tsx +574 -42
  51. package/src/client/HostsView.tsx +12 -3
  52. package/src/client/MonitorView.tsx +16 -5
  53. package/src/client/OutlinePanel.tsx +287 -0
  54. package/src/client/ProcessesSection.tsx +292 -0
  55. package/src/client/RemoteDirectoryPicker.tsx +12 -0
  56. package/src/client/RemoteExplorer.tsx +929 -40
  57. package/src/client/RemoteRoot.tsx +147 -22
  58. package/src/client/SearchView.tsx +8 -0
  59. package/src/client/TerminalSnippets.tsx +83 -8
  60. package/src/client/api.ts +114 -13
  61. package/src/client/blame-model.ts +242 -0
  62. package/src/client/changes/ChangesDock.tsx +188 -6
  63. package/src/client/changes/changes.css +10 -3
  64. package/src/client/changes/review-state.ts +34 -0
  65. package/src/client/connection.ts +29 -0
  66. package/src/client/downloads.ts +49 -4
  67. package/src/client/i18n.ts +276 -8
  68. package/src/client/index.tsx +4 -0
  69. package/src/client/outline-model.ts +775 -0
  70. package/src/client/request-gate.ts +221 -0
  71. package/src/client/scm/CommitPane.tsx +20 -2
  72. package/src/client/scm/DiffView.tsx +343 -117
  73. package/src/client/scm/HistoryPanel.tsx +61 -3
  74. package/src/client/scm/RepoPicker.tsx +1 -1
  75. package/src/client/scm/ScmPanel.tsx +391 -87
  76. package/src/client/scm/actions.ts +150 -14
  77. package/src/client/store.tsx +426 -14
  78. package/src/client/ui.tsx +4 -0
  79. package/src/client/vsc/FindWidget.tsx +24 -9
  80. package/src/client/vsc/blame.ts +147 -0
  81. package/src/client/vsc/breadcrumb.tsx +91 -3
  82. package/src/client/vsc/change-marks.ts +93 -44
  83. package/src/client/vsc/editor-keymap.ts +59 -1
  84. package/src/client/vsc/file-icons.tsx +17 -3
  85. package/src/client/vsc/kit.tsx +29 -20
  86. package/src/client/vsc/preview-document.tsx +323 -0
  87. package/src/client/vsc/preview-model.ts +216 -0
  88. package/src/client/vsc/preview-services.ts +118 -0
  89. package/src/client/vsc/save-shortcut.ts +47 -1
  90. package/src/client/vsc/terminal-theme.ts +44 -18
  91. package/src/client/vsc/vsc.css +6509 -5458
  92. package/src/forward.ts +47 -9
  93. package/src/fs-ops.ts +124 -4
  94. package/src/git-ops.ts +54 -3
  95. package/src/http.ts +35 -4
  96. package/src/keygen.ts +55 -4
  97. package/src/monitor.ts +13 -0
  98. package/src/notices.ts +23 -1
  99. package/src/p2/helper-rpc.ts +113 -7
  100. package/src/p2/worlds.ts +14 -1
  101. package/src/processes.ts +177 -0
  102. package/src/protocol.ts +37 -0
  103. package/src/routes.ts +330 -43
  104. package/src/sftp.ts +51 -14
  105. package/src/store.ts +10 -0
  106. package/src/terminal.ts +73 -26
  107. package/src/view-settings.ts +50 -10
  108. package/lib/types/client/Dialog.d.ts +0 -14
  109. package/src/client/Dialog.tsx +0 -76
@@ -250,6 +250,12 @@ export function stepFind(view: EditorView, forward: boolean, state?: FindState):
250
250
  export function FindWidget(props: {
251
251
  view: EditorView | undefined
252
252
  open: boolean
253
+ /**
254
+ * The page's dictionary translator. Required, not defaulted: the widget has
255
+ * no language of its own, and a silent Chinese fallback is exactly the
256
+ * hardcoded copy this prop replaced.
257
+ */
258
+ t: (key: string) => string
253
259
  /** A selection carried in by Ctrl+F (`id` bumps to re-trigger). */
254
260
  seed: { text: string; id: number }
255
261
  /**
@@ -419,11 +425,21 @@ export function FindWidget(props: {
419
425
  <input
420
426
  ref={inputRef}
421
427
  className="rvs-find-input"
422
- placeholder="查找"
428
+ placeholder={props.t('find.placeholder')}
423
429
  value={state.query}
424
430
  spellCheck={false}
425
431
  onChange={(e) => patch({ query: e.target.value })}
426
432
  onKeyDown={(e) => {
433
+ // An IME candidate window uses Enter to pick a character and Escape
434
+ // to drop it. Acting on them would seek to a half-composed query or
435
+ // close the widget mid-composition, and preventDefault here can
436
+ // swallow the candidate commit. Stopping propagation first keeps the
437
+ // host's editor/tree shortcuts out of the composing keystroke.
438
+ // `isComposing` is the platform-agnostic signal; `keyCode === 229` is
439
+ // the fallback for engines that dispatch compositionend before this
440
+ // keydown (Chrome), so one Enter cannot seek twice.
441
+ e.stopPropagation()
442
+ if (e.nativeEvent.isComposing === true || e.keyCode === 229) return
427
443
  if (e.key === 'Enter') {
428
444
  e.preventDefault()
429
445
  seek(!e.shiftKey)
@@ -435,30 +451,29 @@ export function FindWidget(props: {
435
451
  e.preventDefault()
436
452
  e.currentTarget.select()
437
453
  }
438
- e.stopPropagation()
439
454
  }}
440
455
  />
441
456
  <span className="rvs-toggles">
442
- {toggle('caseSensitive', '区分大小写(Aa)')}
443
- {toggle('wholeWord', '全字匹配(ab_)')}
444
- {toggle('regexp', '正则表达式(.*)')}
457
+ {toggle('caseSensitive', props.t('find.caseSensitive'))}
458
+ {toggle('wholeWord', props.t('find.wholeWord'))}
459
+ {toggle('regexp', props.t('find.regexp'))}
445
460
  </span>
446
461
  <span className={count.total === 0 ? 'rvs-find-count none' : 'rvs-find-count'}>
447
- {state.query === '' ? '' : count.total === 0 ? '无结果' : `${count.index || 1}/${count.total}`}
462
+ {state.query === '' ? '' : count.total === 0 ? props.t('find.noResults') : `${count.index || 1}/${count.total}`}
448
463
  </span>
449
464
  <IconButton
450
- title="上一个(Shift+Enter)"
465
+ title={props.t('find.prev')}
451
466
  onClick={() => seek(false)}
452
467
  >
453
468
  <span style={{ fontSize: 13 }}>↑</span>
454
469
  </IconButton>
455
470
  <IconButton
456
- title="下一个(Enter)"
471
+ title={props.t('find.next')}
457
472
  onClick={() => seek(true)}
458
473
  >
459
474
  <span style={{ fontSize: 13 }}>↓</span>
460
475
  </IconButton>
461
- <IconButton title="关闭(Esc)" onClick={props.onClose}>
476
+ <IconButton title={props.t('find.close')} onClick={props.onClose}>
462
477
  <IconClose size={11} />
463
478
  </IconButton>
464
479
  </div>
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Inline blame gutter for the CodeMirror editor.
3
+ *
4
+ * The data is fetched by the editor pane (one `-L` window at a time) and handed
5
+ * in as a line→commit map; this module only paints it. Keeping the fetch out of
6
+ * here is deliberate: the same model is used by the gate without CodeMirror,
7
+ * and a scroll must not be able to start a request from inside a gutter
8
+ * callback.
9
+ */
10
+ import {
11
+ RangeSetBuilder,
12
+ type Extension,
13
+ type RangeSet,
14
+ } from '@codemirror/state'
15
+ import {
16
+ EditorView,
17
+ GutterMarker,
18
+ ViewPlugin,
19
+ gutter,
20
+ type ViewUpdate,
21
+ } from '@codemirror/view'
22
+ import { blameLabel, blameRuns, blameTitle, isCommittedBlame, type BlameLine } from '../blame-model.ts'
23
+
24
+ /** One gutter cell's label; `null` text means "same commit as the line above". */
25
+ class BlameMarker extends GutterMarker {
26
+ constructor(
27
+ private readonly text: string,
28
+ private readonly title: string,
29
+ private readonly committed: boolean,
30
+ ) {
31
+ super()
32
+ }
33
+
34
+ override toDOM(): HTMLElement {
35
+ const node = document.createElement('span')
36
+ node.className = this.text === ''
37
+ ? 'rvs-blame-cont'
38
+ : `rvs-blame-chip${this.committed ? '' : ' rvs-blame-uncommitted'}`
39
+ node.textContent = this.text
40
+ if (this.title !== '') node.title = this.title
41
+ return node
42
+ }
43
+
44
+ override eq(other: GutterMarker): boolean {
45
+ return other instanceof BlameMarker && other.text === this.text && other.title === this.title
46
+ }
47
+ }
48
+
49
+ const EMPTY = new RangeSetBuilder<GutterMarker>().finish()
50
+
51
+ /** Fixed spacer: keeps the gutter from jumping between files. */
52
+ class SpacerMarker extends GutterMarker {
53
+ override toDOM(): HTMLElement {
54
+ const node = document.createElement('span')
55
+ node.className = 'rvs-blame-spacer'
56
+ node.textContent = 'author · 00y'
57
+ return node
58
+ }
59
+ }
60
+
61
+ const SPACER = new SpacerMarker()
62
+
63
+ /** Viewport-only markers; the gutter itself only renders visible lines. */
64
+ function viewportMarkers(
65
+ view: EditorView,
66
+ entries: ReadonlyMap<number, BlameLine>,
67
+ nowSeconds: number,
68
+ ): RangeSet<GutterMarker> {
69
+ const doc = view.state.doc
70
+ const first = Math.max(1, doc.lineAt(view.viewport.from).number)
71
+ const last = Math.min(doc.lines, doc.lineAt(view.viewport.to).number)
72
+ if (entries.size === 0 || last < first) return EMPTY
73
+ const runStarts = new Set(
74
+ blameRuns([...entries.values()].sort((left, right) => left.line - right.line)).map(run => run.from),
75
+ )
76
+ const builder = new RangeSetBuilder<GutterMarker>()
77
+ for (let number = first; number <= last; number += 1) {
78
+ const entry = entries.get(number)
79
+ if (entry === undefined) continue
80
+ const sameAsAbove = !runStarts.has(number)
81
+ const marker = sameAsAbove
82
+ ? new BlameMarker('', '', true)
83
+ : new BlameMarker(blameLabel(entry, nowSeconds), blameTitle(entry), isCommittedBlame(entry.hash))
84
+ const line = doc.line(number)
85
+ builder.add(line.from, line.to, marker)
86
+ }
87
+ return builder.finish()
88
+ }
89
+
90
+ /** Fire `report` when the viewport (or the document) changes. */
91
+ function viewportReporter(report: (first: number, last: number) => void): Extension {
92
+ const emit = (view: EditorView): void => {
93
+ const doc = view.state.doc
94
+ report(doc.lineAt(view.viewport.from).number, doc.lineAt(view.viewport.to).number)
95
+ }
96
+ return ViewPlugin.fromClass(class {
97
+ constructor(view: EditorView) {
98
+ // A freshly mounted editor gets no update event: report the first
99
+ // viewport here, or the pane would never fetch any blame until scroll.
100
+ emit(view)
101
+ }
102
+
103
+ update(update: ViewUpdate): void {
104
+ if (!update.viewportChanged && !update.docChanged) return
105
+ emit(update.view)
106
+ }
107
+ })
108
+ }
109
+
110
+ /**
111
+ * Build the gutter extension. Rebuilt by the editor's compartment whenever a
112
+ * blame window lands; `nowSeconds` is pinned so every cell in one render has
113
+ * the same clock.
114
+ */
115
+ export function blameExtension(
116
+ entries: ReadonlyMap<number, BlameLine>,
117
+ onOpen?: (entry: BlameLine) => void,
118
+ ): Extension {
119
+ const nowSeconds = Math.floor(Date.now() / 1000)
120
+ const activate = (view: EditorView, line: { from: number }, event: Event): boolean => {
121
+ const number = view.state.doc.lineAt(line.from).number
122
+ const entry = entries.get(number)
123
+ if (entry === undefined || !isCommittedBlame(entry.hash)) return false
124
+ event.preventDefault()
125
+ onOpen?.(entry)
126
+ return true
127
+ }
128
+ return [
129
+ gutter({
130
+ class: 'rvs-blame-gutter',
131
+ markers: view => viewportMarkers(view, entries, nowSeconds),
132
+ initialSpacer: () => SPACER,
133
+ domEventHandlers: onOpen === undefined
134
+ ? {}
135
+ : {
136
+ mousedown(view, line, event) {
137
+ return activate(view, line, event)
138
+ },
139
+ },
140
+ }),
141
+ ]
142
+ }
143
+
144
+ /** Internal export for the editor to forward viewport changes. */
145
+ export function blameViewportReporter(report: (first: number, last: number) => void): Extension {
146
+ return viewportReporter(report)
147
+ }
@@ -10,7 +10,15 @@
10
10
  * Read-only: it only calls `fileOp.list` on the remote. The remote path is the
11
11
  * Host's own business — no second validation layer here.
12
12
  */
13
- import { useEffect, useMemo, useRef, useState, type MouseEvent as ReactMouseEvent, type ReactNode } from 'react'
13
+ import {
14
+ useEffect,
15
+ useMemo,
16
+ useRef,
17
+ useState,
18
+ type KeyboardEvent as ReactKeyboardEvent,
19
+ type MouseEvent as ReactMouseEvent,
20
+ type ReactNode,
21
+ } from 'react'
14
22
  import { api } from '../api.ts'
15
23
  import type { RemoteFileEntry } from '../../protocol.ts'
16
24
 
@@ -73,6 +81,14 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
73
81
  const [error, setError] = useState<string | undefined>(undefined)
74
82
  /** Load token: two quick segment clicks must only let the last answer land. */
75
83
  const loadSeq = useRef(0)
84
+ /** The menu box, so opening it can hand over the keyboard. */
85
+ const menuRef = useRef<HTMLDivElement>(null)
86
+ /** The crumb that opened the menu; focus goes back here on an Esc dismissal. */
87
+ const openerRef = useRef<HTMLElement | null>(null)
88
+ /** True when the last close was Esc (only then is focus handed back). */
89
+ const restoreFocus = useRef(false)
90
+ /** The first item is focused once per opening, not on every directory reload. */
91
+ const firstItemFocused = useRef(false)
76
92
 
77
93
  const t = props.t
78
94
  const root = props.root.replace(/\/+$/, '')
@@ -138,7 +154,10 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
138
154
  const onKeyDown = (event: KeyboardEvent): void => {
139
155
  // During IME composition Esc cancels the candidate, not the menu.
140
156
  if (event.isComposing) return
141
- if (event.key === 'Escape') setMenu(undefined)
157
+ if (event.key === 'Escape') {
158
+ restoreFocus.current = true
159
+ setMenu(undefined)
160
+ }
142
161
  }
143
162
  document.addEventListener('mousedown', onMouseDown, true)
144
163
  document.addEventListener('keydown', onKeyDown, true)
@@ -153,6 +172,40 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
153
172
  setMenu(undefined)
154
173
  }, [props.path])
155
174
 
175
+ /**
176
+ * Menu keyboard model (audit I-12): the menu declared `role="menu"` but had no
177
+ * arrow keys and no initial focus, so a screen reader announced a menu whose
178
+ * items could only be reached with Tab. Opening hands the keyboard to the menu
179
+ * box, and the first item takes it as soon as the (async) listing lands.
180
+ */
181
+ useEffect(() => {
182
+ if (menu === undefined) return
183
+ firstItemFocused.current = false
184
+ menuRef.current?.focus()
185
+ }, [menu])
186
+
187
+ useEffect(() => {
188
+ if (menu === undefined || loading || error !== undefined || entries.length === 0) return
189
+ if (firstItemFocused.current) return
190
+ const box = menuRef.current
191
+ if (box === null) return
192
+ const active = document.activeElement
193
+ // Never pull focus away from a control the user already reached.
194
+ if (active !== box && box.contains(active)) return
195
+ firstItemFocused.current = true
196
+ box.querySelector<HTMLElement>('.rvs-crumb-menu-item')?.focus()
197
+ }, [menu, loading, error, entries])
198
+
199
+ // Esc is a dismissal, so it gives the keyboard back to the crumb that opened
200
+ // the menu instead of dropping focus on <body>. An outside click keeps focus
201
+ // where the user clicked, and picking an entry moves it on by itself.
202
+ useEffect(() => {
203
+ if (menu !== undefined || !restoreFocus.current) return
204
+ restoreFocus.current = false
205
+ const opener = openerRef.current
206
+ if (opener !== null && document.body.contains(opener)) opener.focus()
207
+ }, [menu])
208
+
156
209
  if (!underRoot) {
157
210
  return (
158
211
  <nav className="rvs-breadcrumb" aria-label={tr(t, 'editor.crumbLabel', '文件路径')}>
@@ -163,6 +216,7 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
163
216
 
164
217
  /** Click a segment: a directory lists itself; the file lists its parent. */
165
218
  const clickCrumb = (crumb: Crumb, index: number, event: ReactMouseEvent<HTMLButtonElement>): void => {
219
+ openerRef.current = event.currentTarget
166
220
  const isFile = index === crumbs.length - 1
167
221
  const dir = isFile ? crumb.path.slice(0, Math.max(0, crumb.path.lastIndexOf('/'))) : crumb.path
168
222
  openMenu(dir === '' ? '/' : dir, Math.max(0, event.currentTarget.offsetLeft))
@@ -175,6 +229,30 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
175
229
  return parts.length === 0 ? undefined : `/${parts.join('/')}`
176
230
  }
177
231
 
232
+ /** The enabled entries of the open menu, in DOM order. */
233
+ const menuItems = (): HTMLElement[] => {
234
+ const box = menuRef.current
235
+ if (box === null) return []
236
+ return Array.from(box.querySelectorAll<HTMLElement>('.rvs-crumb-menu-item'))
237
+ }
238
+
239
+ /** ↑/↓/Home/End between entries (Enter/Space is the button's own activation). */
240
+ const onMenuKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>): void => {
241
+ if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp' && event.key !== 'Home' && event.key !== 'End') return
242
+ const items = menuItems()
243
+ if (items.length === 0) return
244
+ event.preventDefault()
245
+ const at = items.indexOf(document.activeElement as HTMLElement)
246
+ const next = event.key === 'Home'
247
+ ? 0
248
+ : event.key === 'End'
249
+ ? items.length - 1
250
+ : event.key === 'ArrowDown'
251
+ ? (at + 1) % items.length
252
+ : (at <= 0 ? items.length - 1 : at - 1)
253
+ items[next]?.focus()
254
+ }
255
+
178
256
  return (
179
257
  <nav className="rvs-breadcrumb" ref={navRef} aria-label={tr(t, 'editor.crumbLabel', '文件路径')}>
180
258
  {crumbs.map((crumb, index) => {
@@ -199,7 +277,17 @@ export function Breadcrumb(props: BreadcrumbProps): ReactNode {
199
277
  })}
200
278
 
201
279
  {menu !== undefined && (
202
- <div className="rvs-crumb-menu" role="menu" style={{ left: menu.left }}>
280
+ <div
281
+ ref={menuRef}
282
+ className="rvs-crumb-menu"
283
+ role="menu"
284
+ // A non-modal picker: Ctrl+S stays live while it is open (the editor is
285
+ // still the surface in use). `isModalLayerOpen()` reads this marker.
286
+ data-rvs-nonmodal=""
287
+ tabIndex={-1}
288
+ onKeyDown={onMenuKeyDown}
289
+ style={{ left: menu.left }}
290
+ >
203
291
  <div className="rvs-crumb-menu-head">
204
292
  <button
205
293
  type="button"
@@ -42,6 +42,14 @@ import {
42
42
  /** 改动点的三种形态。 */
43
43
  export type ChangeKind = 'add' | 'mod' | 'del'
44
44
 
45
+ /**
46
+ * 字典翻译函数,由调用方传入(`CodeEditor` 把页面语言解析好的那个 `t` 交下来)。
47
+ *
48
+ * 本模块自己不选语言:同样的 `t` 与同样的数据永远画出同样的 DOM,纯函数性得以
49
+ * 保持;语言变了由调用方重装扩展(`changeMarksExtension(t)`)。
50
+ */
51
+ export type Translate = (key: string) => string
52
+
45
53
  /**
46
54
  * 新增/修改行里"真正变化的字符区间":相对所属行的列偏移,**0-based、半开区间**。
47
55
  *
@@ -158,7 +166,6 @@ const KIND_PRIORITY: Record<ChangeKind, number> = { add: 0, mod: 1, del: 2 }
158
166
 
159
167
  /** Gutter 符号与说明(三种形态靠符号 + 颜色双重区分)。 */
160
168
  const GLYPH: Record<ChangeKind, string> = { add: '+', mod: '●', del: '−' }
161
- const LABEL: Record<ChangeKind, string> = { add: '新增行', mod: '修改行', del: '删除行' }
162
169
 
163
170
  /** 空标记集。构造一次,避免每次清空都新建一个 RangeSet。 */
164
171
  const NO_MARKERS = new RangeSetBuilder<GutterMarker>().finish()
@@ -184,12 +191,6 @@ function lineDecoration(kind: ChangeKind): Decoration {
184
191
  */
185
192
  const WORD_ADD = Decoration.mark({ class: 'rvs-change-word-add' })
186
193
 
187
- /** 删除 chip 的悬浮说明。与 gutter 的 `LABEL` 一样,是编辑器侧的固定文案。 */
188
- const CHIP_TITLE = '已删除的内容'
189
-
190
- /** 整块删除那种胶囊的悬浮说明(后面跟行数)。 */
191
- const BLOCK_CHIP_TITLE = '已删除的整块内容'
192
-
193
194
  /**
194
195
  * 被删内容的零宽 widget。
195
196
  *
@@ -201,12 +202,22 @@ class RemovedChipWidget extends WidgetType {
201
202
  private readonly text: string
202
203
  private readonly truncated: boolean
203
204
  private readonly count: number
205
+ /**
206
+ * 文案在构造时按调用方的 `t` 解析好,并参与 `eq`:语言变了而 DOM 被复用,
207
+ * tooltip 就会停在旧语言上。
208
+ */
209
+ private readonly chipTitle: string
210
+ private readonly blockTitle: string
211
+ private readonly removedLines: string
204
212
 
205
- constructor(text: string, truncated: boolean, count = 0) {
213
+ constructor(text: string, truncated: boolean, count: number, t: Translate) {
206
214
  super()
207
215
  this.text = text
208
216
  this.truncated = truncated
209
217
  this.count = count
218
+ this.chipTitle = t('changes.deletedChip')
219
+ this.blockTitle = t('changes.deletedBlock')
220
+ this.removedLines = t('changes.deletedLines')
210
221
  }
211
222
 
212
223
  eq(other: WidgetType): boolean {
@@ -214,6 +225,9 @@ class RemovedChipWidget extends WidgetType {
214
225
  && other.text === this.text
215
226
  && other.truncated === this.truncated
216
227
  && other.count === this.count
228
+ && other.chipTitle === this.chipTitle
229
+ && other.blockTitle === this.blockTitle
230
+ && other.removedLines === this.removedLines
217
231
  }
218
232
 
219
233
  toDOM(): HTMLElement {
@@ -228,8 +242,12 @@ class RemovedChipWidget extends WidgetType {
228
242
  */
229
243
  const block = this.count > 1
230
244
  chip.className = block ? 'rvs-change-word-del rvs-change-block-del' : 'rvs-change-word-del'
231
- chip.textContent = block ? `−${this.count} 行已删除` : (this.truncated ? `${this.text}…` : this.text)
232
- chip.title = block ? `${BLOCK_CHIP_TITLE}(${this.count} 行)\n${this.text}` : CHIP_TITLE
245
+ chip.textContent = block
246
+ ? this.removedLines.replace('{count}', String(this.count))
247
+ : (this.truncated ? `${this.text}…` : this.text)
248
+ chip.title = block
249
+ ? `${this.blockTitle.replace('{count}', String(this.count))}\n${this.text}`
250
+ : this.chipTitle
233
251
  chip.setAttribute('aria-label', chip.title)
234
252
  return chip
235
253
  }
@@ -240,8 +258,8 @@ class RemovedChipWidget extends WidgetType {
240
258
  * 区间之前,`RangeSetBuilder` 只接受按 `from` + `startSide` 升序的输入(行装饰
241
259
  * `-2e8`、chip `-1e8`、字符区间 `-1`)。
242
260
  */
243
- function chipDecoration(chip: RemovedChip): Decoration {
244
- return Decoration.widget({ widget: new RemovedChipWidget(chip.text, chip.truncated, chip.count ?? 0), side: -1 })
261
+ function chipDecoration(chip: RemovedChip, t: Translate): Decoration {
262
+ return Decoration.widget({ widget: new RemovedChipWidget(chip.text, chip.truncated, chip.count ?? 0, t), side: -1 })
245
263
  }
246
264
 
247
265
  /** 一行的行内片段:新增字符区间 + 删除 chip。 */
@@ -312,11 +330,14 @@ function inlineIndexOf(set: ChangeSet | null, doc: Text): Map<number, LineInline
312
330
  class KindMarker extends GutterMarker {
313
331
  readonly kind: ChangeKind
314
332
  readonly elementClass: string
333
+ /** 已解析的说明(title 与 aria-label 共用),由工厂按 `t` 传进来。 */
334
+ private readonly label: string
315
335
 
316
- constructor(kind: ChangeKind) {
336
+ constructor(kind: ChangeKind, label: string) {
317
337
  super()
318
338
  this.kind = kind
319
339
  this.elementClass = `rvs-change-gutter-${kind}`
340
+ this.label = label
320
341
  }
321
342
 
322
343
  eq(other: GutterMarker): boolean {
@@ -327,17 +348,23 @@ class KindMarker extends GutterMarker {
327
348
  const span = document.createElement('span')
328
349
  span.className = `rvs-change-mark rvs-change-mark-${this.kind}`
329
350
  span.textContent = GLYPH[this.kind]
330
- span.title = LABEL[this.kind]
331
- span.setAttribute('aria-label', LABEL[this.kind])
351
+ span.title = this.label
352
+ span.setAttribute('aria-label', this.label)
332
353
  return span
333
354
  }
334
355
  }
335
356
 
336
- /** 三种 kind 的共享实例(`eq` 让相邻同 kind 的行合并成一条 gutter 色条)。 */
337
- const MARKERS: Record<ChangeKind, GutterMarker> = {
338
- add: new KindMarker('add'),
339
- mod: new KindMarker('mod'),
340
- del: new KindMarker('del'),
357
+ /**
358
+ * 三种 kind 的 gutter 标记(`eq` 让相邻同 kind 的行合并成一条 gutter 色条)。
359
+ *
360
+ * 每次装扩展重建一份:文案按调用方的 `t` 解析,不能再做成模块级常量。
361
+ */
362
+ function markersFor(t: Translate): Record<ChangeKind, GutterMarker> {
363
+ return {
364
+ add: new KindMarker('add', t('changes.markAdd')),
365
+ mod: new KindMarker('mod', t('changes.markMod')),
366
+ del: new KindMarker('del', t('changes.markDel')),
367
+ }
341
368
  }
342
369
 
343
370
  /**
@@ -485,9 +512,12 @@ function firstSpanFrom(spans: readonly Span[], line: number): number {
485
512
  class ChangeMarkHighlighter {
486
513
  decorations: DecorationSet = Decoration.none
487
514
  private readonly view: EditorView
515
+ /** 解析标记文案的翻译函数;由 `changeMarkPlugin(t)` 装进来。 */
516
+ private readonly t: Translate
488
517
 
489
- constructor(view: EditorView) {
518
+ constructor(view: EditorView, t: Translate) {
490
519
  this.view = view
520
+ this.t = t
491
521
  this.rebuild()
492
522
  }
493
523
 
@@ -527,7 +557,7 @@ class ChangeMarkHighlighter {
527
557
  // chip 的 `at` 是列偏移,越过行尾的(基线变了)夹到行尾,绝不越界。
528
558
  for (const chip of detail.chips) {
529
559
  const at = line.from + Math.min(chip.at, line.length)
530
- pieces.push({ from: at, to: at, decoration: chipDecoration(chip) })
560
+ pieces.push({ from: at, to: at, decoration: chipDecoration(chip, this.t) })
531
561
  }
532
562
  for (const word of detail.ranges) {
533
563
  const from = line.from + Math.min(word.from, line.length)
@@ -549,13 +579,20 @@ class ChangeMarkHighlighter {
549
579
  /**
550
580
  * 视口内行装饰的 ViewPlugin。
551
581
  *
552
- * 导出是为了让自检脚本能直接读 `view.plugin(changeMarkPlugin).decorations.size`
553
- * —— "只装饰视口内的行"这件事在 DOM 上看不出来(CodeMirror 本来就让 DOM 只保留
554
- * 可见行),只有读这份 RangeSet 的条数才有判别力。
582
+ * 用工厂而不是模块级常量:标记与 chip 的文案由调用方的 `t` 解析。仍然导出是为了
583
+ * 让自检脚本能直接读 `view.plugin(changeMarkPlugin(t)).decorations.size` ——
584
+ * "只装饰视口内的行"这件事在 DOM 上看不出来(CodeMirror 本来就让 DOM 只保留可见
585
+ * 行),只有读这份 RangeSet 的条数才有判别力。
555
586
  */
556
- export const changeMarkPlugin = ViewPlugin.fromClass(ChangeMarkHighlighter, {
557
- decorations: value => value.decorations,
558
- })
587
+ export function changeMarkPlugin(t: Translate): ViewPlugin<ChangeMarkHighlighter> {
588
+ return ViewPlugin.fromClass(class extends ChangeMarkHighlighter {
589
+ constructor(view: EditorView) {
590
+ super(view, t)
591
+ }
592
+ }, {
593
+ decorations: value => value.decorations,
594
+ })
595
+ }
559
596
 
560
597
  /**
561
598
  * 视口内的 gutter 标记,带一条缓存。
@@ -563,15 +600,24 @@ export const changeMarkPlugin = ViewPlugin.fromClass(ChangeMarkHighlighter, {
563
600
  * gutter 的 `markers` 回调在**每一次**视图更新都会跑(选中、滚动、输入都算),
564
601
  * 而且它比较的是"视口内那一段"的 RangeSet。所以这里按"数据 + 视口"做单条缓存:
565
602
  * 没有新数据、没滚动时直接返回已有结果,滚动时只重建视口那几十行。
603
+ *
604
+ * 缓存键里带上标记实例集合(`owner`):每个编辑器按自己的 `t` 建一份标记,语言
605
+ * 不同的两个编辑器同时挂载时不能互相复用。
566
606
  */
567
- let gutterCache: { state: MarksState; from: number; to: number; markers: RangeSet<GutterMarker> } | undefined
607
+ let gutterCache: {
608
+ state: MarksState
609
+ from: number
610
+ to: number
611
+ owner: Record<ChangeKind, GutterMarker>
612
+ markers: RangeSet<GutterMarker>
613
+ } | undefined
568
614
 
569
- function viewportMarkers(view: EditorView): RangeSet<GutterMarker> {
615
+ function viewportMarkers(view: EditorView, owner: Record<ChangeKind, GutterMarker>): RangeSet<GutterMarker> {
570
616
  const marks = view.state.field(marksField, false) ?? EMPTY_STATE
571
617
  const from = view.viewport.from
572
618
  const to = view.viewport.to
573
619
  const cached = gutterCache
574
- if (cached !== undefined && cached.state === marks && cached.from === from && cached.to === to) {
620
+ if (cached !== undefined && cached.state === marks && cached.from === from && cached.to === to && cached.owner === owner) {
575
621
  return cached.markers
576
622
  }
577
623
  let markers = NO_MARKERS
@@ -585,7 +631,7 @@ function viewportMarkers(view: EditorView): RangeSet<GutterMarker> {
585
631
  if (span.first > lastLine) break
586
632
  const start = Math.max(span.first, firstLine)
587
633
  const end = Math.min(span.last, lastLine)
588
- const marker = MARKERS[span.kind]
634
+ const marker = owner[span.kind]
589
635
  // 每一行必须是**独立**的标记区间:gutter 只在 `marker.from === line.from`
590
636
  // 时才把该标记放进这一行(view 的 `advanceCursor`),一个跨多行的区间只会
591
637
  // 出现在第一行上。
@@ -596,7 +642,7 @@ function viewportMarkers(view: EditorView): RangeSet<GutterMarker> {
596
642
  }
597
643
  markers = builder.finish()
598
644
  }
599
- gutterCache = { state: marks, from, to, markers }
645
+ gutterCache = { state: marks, from, to, owner, markers }
600
646
  return markers
601
647
  }
602
648
 
@@ -606,14 +652,17 @@ function viewportMarkers(view: EditorView): RangeSet<GutterMarker> {
606
652
  * `CodeEditor` 只在调用方真的传了 `changeSet` 时把它装进编辑器(Compartment),
607
653
  * 没传时不装 —— 这样"没有会话改动"的文件连一条 gutter 都不会多出来。
608
654
  */
609
- export const changeMarksExtension: Extension = [
610
- marksField,
611
- changeStaleField,
612
- gutter({
613
- class: 'rvs-change-gutter',
614
- markers: view => viewportMarkers(view),
615
- initialSpacer: () => SPACER,
616
- lineMarkerChange: update => update.startState.field(marksField, false) !== update.state.field(marksField, false),
617
- }),
618
- changeMarkPlugin,
619
- ]
655
+ export function changeMarksExtension(t: Translate): Extension {
656
+ const markers = markersFor(t)
657
+ return [
658
+ marksField,
659
+ changeStaleField,
660
+ gutter({
661
+ class: 'rvs-change-gutter',
662
+ markers: view => viewportMarkers(view, markers),
663
+ initialSpacer: () => SPACER,
664
+ lineMarkerChange: update => update.startState.field(marksField, false) !== update.state.field(marksField, false),
665
+ }),
666
+ changeMarkPlugin(t),
667
+ ]
668
+ }