dsh-remote-vs 0.24.0 → 0.26.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 (125) hide show
  1. package/README.en.md +50 -16
  2. package/README.md +40 -15
  3. package/THIRD-PARTY-NOTICES.md +188 -0
  4. package/lib/client.js +6512 -77887
  5. package/lib/index.js +1461 -1024
  6. package/lib/p2-router-fs.js +1 -1
  7. package/lib/types/client/CodeEditor.d.ts +25 -0
  8. package/lib/types/client/EditorPane.d.ts +16 -5
  9. package/lib/types/client/FloatingPlaceholder.d.ts +16 -5
  10. package/lib/types/client/RemoteEditorView.d.ts +13 -10
  11. package/lib/types/client/RemoteExplorer.d.ts +53 -0
  12. package/lib/types/client/RemoteRoot.d.ts +5 -5
  13. package/lib/types/client/TerminalSnippets.d.ts +36 -0
  14. package/lib/types/client/TerminalView.d.ts +1 -1
  15. package/lib/types/client/XTerm.d.ts +33 -0
  16. package/lib/types/client/api.d.ts +37 -2
  17. package/lib/types/client/changes/review-state.d.ts +19 -0
  18. package/lib/types/client/connection.d.ts +16 -0
  19. package/lib/types/client/editor-capability.d.ts +27 -0
  20. package/lib/types/client/i18n.d.ts +1 -1
  21. package/lib/types/client/request-gate.d.ts +110 -0
  22. package/lib/types/client/scm/DiffView.d.ts +45 -7
  23. package/lib/types/client/scm/actions.d.ts +31 -3
  24. package/lib/types/client/store.d.ts +112 -4
  25. package/lib/types/client/terminal-input.d.ts +23 -0
  26. package/lib/types/client/terminal-snippets.d.ts +86 -0
  27. package/lib/types/client/vsc/FindWidget.d.ts +6 -0
  28. package/lib/types/client/vsc/breadcrumb.d.ts +30 -0
  29. package/lib/types/client/vsc/change-marks.d.ts +16 -6
  30. package/lib/types/client/vsc/editor-caret.d.ts +32 -0
  31. package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
  32. package/lib/types/client/vsc/editor-langs.d.ts +9 -0
  33. package/lib/types/client/vsc/editor-settings.d.ts +55 -0
  34. package/lib/types/client/vsc/image-preview.d.ts +36 -0
  35. package/lib/types/client/vsc/kit.d.ts +13 -0
  36. package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
  37. package/lib/types/client/vsc/terminal-clipboard.d.ts +15 -0
  38. package/lib/types/client/vsc/terminal-line.d.ts +25 -0
  39. package/lib/types/client/vsc/terminal-links.d.ts +62 -0
  40. package/lib/types/client/vsc/terminal-theme.d.ts +48 -0
  41. package/lib/types/client/vsc/terminal-writer.d.ts +52 -0
  42. package/lib/types/forward.d.ts +12 -0
  43. package/lib/types/fs-ops.d.ts +42 -0
  44. package/lib/types/http.d.ts +2 -0
  45. package/lib/types/keygen.d.ts +9 -0
  46. package/lib/types/monitor.d.ts +8 -0
  47. package/lib/types/notices.d.ts +16 -1
  48. package/lib/types/p2/helper-rpc.d.ts +45 -0
  49. package/lib/types/pool.d.ts +25 -0
  50. package/lib/types/routes.d.ts +2 -0
  51. package/lib/types/store.d.ts +7 -0
  52. package/lib/types/terminal.d.ts +33 -1
  53. package/lib/types/view-settings.d.ts +5 -0
  54. package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
  55. package/package.json +2 -1
  56. package/src/client/CodeEditor.tsx +205 -10
  57. package/src/client/DirectoryFlow.tsx +16 -0
  58. package/src/client/DoctorPanel.tsx +70 -7
  59. package/src/client/EditorPane.tsx +545 -118
  60. package/src/client/FloatingPlaceholder.tsx +34 -5
  61. package/src/client/HostsView.tsx +12 -3
  62. package/src/client/RemoteDirectoryPicker.tsx +12 -0
  63. package/src/client/RemoteEditorView.tsx +14 -11
  64. package/src/client/RemoteExplorer.tsx +929 -40
  65. package/src/client/RemoteRoot.tsx +167 -147
  66. package/src/client/SearchView.tsx +8 -0
  67. package/src/client/TerminalSnippets.tsx +432 -0
  68. package/src/client/TerminalView.tsx +50 -1
  69. package/src/client/XTerm.tsx +228 -58
  70. package/src/client/api.ts +106 -13
  71. package/src/client/auto-open-remote.ts +11 -9
  72. package/src/client/changes/ChangesDock.tsx +188 -6
  73. package/src/client/changes/changes.css +10 -3
  74. package/src/client/changes/review-state.ts +34 -0
  75. package/src/client/connection.ts +29 -0
  76. package/src/client/downloads.ts +49 -4
  77. package/src/client/editor-capability.ts +42 -0
  78. package/src/client/i18n.ts +228 -12
  79. package/src/client/index.tsx +22 -6
  80. package/src/client/request-gate.ts +221 -0
  81. package/src/client/scm/CommitPane.tsx +20 -2
  82. package/src/client/scm/DiffView.tsx +343 -117
  83. package/src/client/scm/HistoryPanel.tsx +23 -2
  84. package/src/client/scm/RepoPicker.tsx +1 -1
  85. package/src/client/scm/ScmPanel.tsx +391 -87
  86. package/src/client/scm/actions.ts +95 -12
  87. package/src/client/store.tsx +537 -22
  88. package/src/client/terminal-input.ts +44 -0
  89. package/src/client/terminal-snippets.ts +216 -0
  90. package/src/client/ui.tsx +4 -0
  91. package/src/client/vsc/FindWidget.tsx +24 -9
  92. package/src/client/vsc/breadcrumb.tsx +339 -0
  93. package/src/client/vsc/change-marks.ts +93 -44
  94. package/src/client/vsc/editor-caret.ts +30 -0
  95. package/src/client/vsc/editor-keymap.ts +59 -1
  96. package/src/client/vsc/editor-langs.ts +20 -0
  97. package/src/client/vsc/editor-settings.ts +146 -0
  98. package/src/client/vsc/editor-theme.ts +4 -5
  99. package/src/client/vsc/file-icons.tsx +17 -3
  100. package/src/client/vsc/image-preview.tsx +110 -0
  101. package/src/client/vsc/kit.tsx +46 -10
  102. package/src/client/vsc/save-shortcut.ts +47 -1
  103. package/src/client/vsc/terminal-clipboard.ts +19 -0
  104. package/src/client/vsc/terminal-line.ts +101 -0
  105. package/src/client/vsc/terminal-links.ts +166 -0
  106. package/src/client/vsc/terminal-theme.ts +97 -0
  107. package/src/client/vsc/terminal-writer.ts +112 -0
  108. package/src/client/vsc/vsc.css +1078 -181
  109. package/src/forward.ts +47 -9
  110. package/src/fs-ops.ts +124 -4
  111. package/src/git-ops.ts +20 -3
  112. package/src/http.ts +35 -4
  113. package/src/keygen.ts +55 -4
  114. package/src/monitor.ts +13 -0
  115. package/src/notices.ts +23 -1
  116. package/src/p2/helper-rpc.ts +113 -7
  117. package/src/p2/worlds.ts +14 -1
  118. package/src/pool.ts +99 -27
  119. package/src/routes.ts +212 -43
  120. package/src/sftp.ts +51 -14
  121. package/src/store.ts +10 -0
  122. package/src/terminal.ts +225 -24
  123. package/src/view-settings.ts +50 -10
  124. package/lib/types/client/Dialog.d.ts +0 -14
  125. package/src/client/Dialog.tsx +0 -76
@@ -0,0 +1,432 @@
1
+ /**
2
+ * The "常用指令" panel above the terminal: a personal library of NAME + COMMAND
3
+ * pairs that paste into the active terminal's command line WITHOUT pressing
4
+ * Enter (misclick safety: the user still reviews and runs it).
5
+ *
6
+ * Why it is a popover rather than a dock: the terminal needs the whole column
7
+ * for output; the list is opened deliberately, used once, and dismissed. The
8
+ * panel is portaled with fixed coordinates so the terminal's own scroller and
9
+ * the sidebar's `overflow: hidden` cannot clip it.
10
+ *
11
+ * Snapshot + subscribe come from `terminal-snippets.ts` (one list shared with the
12
+ * Ctrl+B quick-add shortcut, which `XTerm` reports as a counter prop).
13
+ */
14
+ import {
15
+ useCallback,
16
+ useEffect,
17
+ useMemo,
18
+ useRef,
19
+ useState,
20
+ useSyncExternalStore,
21
+ type KeyboardEvent as ReactKeyboardEvent,
22
+ type ReactNode,
23
+ } from 'react'
24
+ import { createPortal } from 'react-dom'
25
+ import {
26
+ addSnippet,
27
+ moveSnippet,
28
+ removeSnippet,
29
+ snippetsSnapshot,
30
+ subscribeSnippets,
31
+ updateSnippet,
32
+ updateSnippets,
33
+ type TerminalSnippet,
34
+ } from './terminal-snippets.ts'
35
+ import { confirmDialog, focusables } from './vsc/kit.tsx'
36
+
37
+ /** Props of the panel (plus the button that opens it). */
38
+ export interface TerminalSnippetsProps {
39
+ /** Translator. */
40
+ t: (key: string) => string
41
+ /** Insert one command into the active terminal; false when none is mounted. */
42
+ onInsert: (command: string) => boolean
43
+ /** Bumped by the Ctrl+B shortcut; each change opens the ADD dialog. */
44
+ addRequest: number
45
+ /**
46
+ * Draft the Ctrl+B form starts with (the terminal selection, else the line
47
+ * reconstructed from keystrokes). Empty for the panel's own "+" button.
48
+ */
49
+ addContent: string
50
+ }
51
+
52
+ /** Where the popover is anchored (viewport coordinates of the button). */
53
+ interface Anchor {
54
+ left: number
55
+ top: number
56
+ width: number
57
+ }
58
+
59
+ /** What the form dialog is doing. */
60
+ type FormState = { mode: 'add'; content: string } | { mode: 'edit'; snippet: TerminalSnippet }
61
+
62
+ /** One row, identified for drag & drop. */
63
+ interface DragState {
64
+ from: number
65
+ }
66
+
67
+ /**
68
+ * The panel.
69
+ *
70
+ * Kept in one component on purpose: the button, the popover, the form and their
71
+ * shared focus/dismissal rules are one interaction, and splitting them would
72
+ * need a context for four pieces of state.
73
+ */
74
+ export function TerminalSnippets(props: TerminalSnippetsProps): ReactNode {
75
+ const t = props.t
76
+ const items = useSyncExternalStore(subscribeSnippets, snippetsSnapshot, snippetsSnapshot)
77
+ const [anchor, setAnchor] = useState<Anchor | undefined>(undefined)
78
+ const [form, setForm] = useState<FormState | undefined>(undefined)
79
+ const [drag, setDrag] = useState<DragState | undefined>(undefined)
80
+ const [dropIndex, setDropIndex] = useState<number | undefined>(undefined)
81
+ const buttonRef = useRef<HTMLButtonElement>(null)
82
+ const panelRef = useRef<HTMLDivElement>(null)
83
+ /** Latest `addRequest` already handled (the prop is a counter, not an event). */
84
+ const handledAdd = useRef(props.addRequest)
85
+
86
+ /** Open the panel under the button. */
87
+ const openPanel = useCallback((): void => {
88
+ const rect = buttonRef.current?.getBoundingClientRect()
89
+ if (rect === undefined || rect === null) return
90
+ setAnchor({ left: Math.max(4, rect.left), top: rect.bottom + 4, width: 300 })
91
+ }, [])
92
+
93
+ const closePanel = useCallback((): void => {
94
+ setAnchor(undefined)
95
+ setDrag(undefined)
96
+ setDropIndex(undefined)
97
+ }, [])
98
+
99
+ /**
100
+ * The open panel owns the keyboard.
101
+ *
102
+ * It is portaled to the END of `<body>`, so a keyboard user who opened it from
103
+ * the ⚡ button otherwise had to Tab through the whole page to reach the first
104
+ * row. The first command is where the user wants to be; an empty library
105
+ * starts on the add button. Closing hands focus back to the trigger — but only
106
+ * when the user did not already put it somewhere else (closing by clicking the
107
+ * terminal must not steal the caret back).
108
+ */
109
+ useEffect(() => {
110
+ if (anchor === undefined) return
111
+ const panel = panelRef.current
112
+ if (panel === null) return
113
+ const first = panel.querySelector<HTMLElement>('.rvs-snips-insert') ?? focusables(panel)[0]
114
+ first?.focus()
115
+ return () => {
116
+ const active = document.activeElement
117
+ if (active === null || active === document.body || panelRef.current?.contains(active) === true) {
118
+ buttonRef.current?.focus()
119
+ }
120
+ }
121
+ }, [anchor])
122
+
123
+ /**
124
+ * List navigation inside the panel (audit I-9): ↑/↓ walk the command rows,
125
+ * Enter is the focused row's native button activation (so it inserts), and Esc
126
+ * is handled HERE, not on `document`: it used to be document-wide, so pressing
127
+ * Esc in the terminal closed the panel while the shell received the same ESC.
128
+ */
129
+ const onPanelKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>): void => {
130
+ if (event.nativeEvent.isComposing) return
131
+ if (event.key === 'Escape') {
132
+ event.preventDefault()
133
+ event.stopPropagation()
134
+ closePanel()
135
+ return
136
+ }
137
+ // Alt+↑/↓ is the grip's reorder chord; leave it to the row.
138
+ if (event.altKey) return
139
+ if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') return
140
+ const panel = panelRef.current
141
+ if (panel === null) return
142
+ const rows = Array.from(panel.querySelectorAll<HTMLElement>('.rvs-snips-insert'))
143
+ if (rows.length === 0) return
144
+ event.preventDefault()
145
+ const at = rows.indexOf(document.activeElement as HTMLElement)
146
+ const next = event.key === 'ArrowDown' ? (at + 1) % rows.length : (at <= 0 ? rows.length - 1 : at - 1)
147
+ rows[next]?.focus()
148
+ }
149
+
150
+ // Ctrl+B from the terminal: open the ADD form (and the panel behind it) with
151
+ // the draft the terminal handed over — selection first, else the typed line.
152
+ useEffect(() => {
153
+ if (props.addRequest === handledAdd.current) return
154
+ handledAdd.current = props.addRequest
155
+ openPanel()
156
+ setForm({ mode: 'add', content: props.addContent })
157
+ }, [props.addRequest, props.addContent, openPanel])
158
+
159
+ // Close on outside click / window resize (a fixed popover would be left
160
+ // pointing at nothing). Capture phase so xterm's own mousedown cannot refocus
161
+ // the terminal first, which would make the panel look unclosable. Esc lives on
162
+ // the panel itself (see `onPanelKeyDown`): the keystroke must not be taken from
163
+ // the shell.
164
+ useEffect(() => {
165
+ if (anchor === undefined) return
166
+ const onMouseDown = (event: MouseEvent): void => {
167
+ const target = event.target
168
+ if (!(target instanceof Node)) return
169
+ if (panelRef.current?.contains(target) === true) return
170
+ if (buttonRef.current?.contains(target) === true) return
171
+ closePanel()
172
+ }
173
+ const onResize = (): void => closePanel()
174
+ /**
175
+ * Esc while the focus is anywhere EXCEPT the panel.
176
+ *
177
+ * The panel handles its own Esc when it has focus (`onPanelKeyDown`), but it
178
+ * can be visibly open while the focus sits elsewhere (clicking its title or
179
+ * scrolling it leaves the activeElement outside) — the old document-level
180
+ * handler closed it from anywhere. Restore that, but never steal Esc from
181
+ * the shell: a focus inside `.xterm` means the key belongs to the terminal.
182
+ */
183
+ const onKeyDown = (event: KeyboardEvent): void => {
184
+ if (event.key !== 'Escape') return
185
+ const active = document.activeElement
186
+ if (active instanceof Node && panelRef.current?.contains(active) === true) return
187
+ if (active instanceof Element && active.closest('.xterm') !== null) return
188
+ closePanel()
189
+ }
190
+ document.addEventListener('mousedown', onMouseDown, true)
191
+ document.addEventListener('keydown', onKeyDown, true)
192
+ window.addEventListener('resize', onResize)
193
+ return () => {
194
+ document.removeEventListener('mousedown', onMouseDown, true)
195
+ document.removeEventListener('keydown', onKeyDown, true)
196
+ window.removeEventListener('resize', onResize)
197
+ }
198
+ }, [anchor, closePanel])
199
+
200
+ /** Paste one command and get out of the way. */
201
+ const insert = (snippet: TerminalSnippet): void => {
202
+ if (!props.onInsert(snippet.command)) {
203
+ // No mounted terminal for the active tab: the caller says so; the panel
204
+ // stays open so the click can be retried.
205
+ window.setTimeout(() => { setForm(undefined) }, 0)
206
+ return
207
+ }
208
+ closePanel()
209
+ }
210
+
211
+ const requestDelete = (snippet: TerminalSnippet): void => {
212
+ void confirmDialog({
213
+ title: t('term.snippetDeleteTitle'),
214
+ text: snippet.name,
215
+ danger: true,
216
+ okText: t('term.snippetDelete'),
217
+ }).then((ok) => {
218
+ if (ok) updateSnippets(list => removeSnippet(list, snippet.id))
219
+ })
220
+ }
221
+
222
+ /** Move one row by keyboard (Alt+Arrow) — the accessible twin of dragging. */
223
+ const moveBy = (index: number, direction: -1 | 1): void => {
224
+ updateSnippets(list => moveSnippet(list, index, index + direction))
225
+ }
226
+
227
+ const panel = anchor === undefined
228
+ ? undefined
229
+ : createPortal(
230
+ <div
231
+ ref={panelRef}
232
+ className="rvs-portal rvs-snips-panel"
233
+ role="dialog"
234
+ aria-label={t('term.snippets')}
235
+ onKeyDown={onPanelKeyDown}
236
+ style={{ left: `${anchor.left}px`, top: `${anchor.top}px`, width: `${anchor.width}px` }}
237
+ >
238
+ <div className="rvs-snips-head">
239
+ <span className="rvs-snips-title">⚡ {t('term.snippets')}</span>
240
+ <span className="rvs-snips-spacer" />
241
+ <button
242
+ type="button"
243
+ className="rvs-snips-add"
244
+ title={t('term.snippetAdd')}
245
+ aria-label={t('term.snippetAdd')}
246
+ onClick={() => setForm({ mode: 'add', content: '' })}
247
+ >+</button>
248
+ </div>
249
+ <div className="rvs-snips-list">
250
+ {items.length === 0 && (
251
+ <div className="rvs-snips-empty">{t('term.snippetEmpty')}</div>
252
+ )}
253
+ {items.map((snippet, index) => (
254
+ <div
255
+ key={snippet.id}
256
+ className={`rvs-snips-row${dropIndex === index && drag !== undefined ? ' drop' : ''}`}
257
+ draggable
258
+ onDragStart={(event) => {
259
+ setDrag({ from: index })
260
+ event.dataTransfer.effectAllowed = 'move'
261
+ event.dataTransfer.setData('text/plain', String(index))
262
+ }}
263
+ onDragOver={(event) => {
264
+ if (drag === undefined) return
265
+ event.preventDefault()
266
+ event.dataTransfer.dropEffect = 'move'
267
+ setDropIndex(index)
268
+ }}
269
+ onDrop={(event) => {
270
+ event.preventDefault()
271
+ const from = Number(event.dataTransfer.getData('text/plain'))
272
+ setDrag(undefined)
273
+ setDropIndex(undefined)
274
+ if (Number.isFinite(from)) updateSnippets(list => moveSnippet(list, from, index))
275
+ }}
276
+ onDragEnd={() => { setDrag(undefined); setDropIndex(undefined) }}
277
+ >
278
+ <span
279
+ className="rvs-snips-grip"
280
+ title={t('term.snippetDragHint')}
281
+ role="button"
282
+ aria-label={t('term.snippetDragHint')}
283
+ tabIndex={0}
284
+ onKeyDown={(event) => {
285
+ if (event.altKey && event.key === 'ArrowUp') { event.preventDefault(); moveBy(index, -1) }
286
+ if (event.altKey && event.key === 'ArrowDown') { event.preventDefault(); moveBy(index, 1) }
287
+ }}
288
+ >⠿</span>
289
+ <button
290
+ type="button"
291
+ className="rvs-snips-insert"
292
+ title={`${t('term.snippetInsertHint')}\n${snippet.command}`}
293
+ onClick={() => insert(snippet)}
294
+ >
295
+ <span className="rvs-snips-name">{snippet.name}</span>
296
+ <span className="rvs-snips-arrow" aria-hidden="true">--&gt;</span>
297
+ <span className="rvs-snips-text">{snippet.command}</span>
298
+ </button>
299
+ <button
300
+ type="button"
301
+ className="rvs-snips-icon"
302
+ title={t('term.snippetEdit')}
303
+ aria-label={t('term.snippetEdit')}
304
+ onClick={() => setForm({ mode: 'edit', snippet })}
305
+ >✎</button>
306
+ <button
307
+ type="button"
308
+ className="rvs-snips-icon danger"
309
+ title={t('term.snippetDelete')}
310
+ aria-label={t('term.snippetDelete')}
311
+ onClick={() => requestDelete(snippet)}
312
+ >×</button>
313
+ </div>
314
+ ))}
315
+ </div>
316
+ <div className="rvs-snips-foot">{t('term.snippetFootHint')}</div>
317
+ </div>,
318
+ document.body,
319
+ )
320
+
321
+ return (
322
+ <>
323
+ <button
324
+ ref={buttonRef}
325
+ type="button"
326
+ className="rvs-hbtn rvs-snips-btn"
327
+ title={t('term.snippets')}
328
+ aria-label={t('term.snippets')}
329
+ onClick={() => { if (anchor === undefined) openPanel(); else closePanel() }}
330
+ >
331
+ <span aria-hidden="true">⚡</span> {t('term.snippets')}
332
+ </button>
333
+ {panel}
334
+ {form !== undefined && (
335
+ <SnippetForm
336
+ t={t}
337
+ mode={form.mode}
338
+ {...(form.mode === 'edit' ? { snippet: form.snippet } : { initialCommand: form.content })}
339
+ onCancel={() => setForm(undefined)}
340
+ onSubmit={(input) => {
341
+ if (form.mode === 'add') {
342
+ updateSnippets(list => addSnippet(list, input))
343
+ } else {
344
+ updateSnippets(list => updateSnippet(list, form.snippet.id, input))
345
+ }
346
+ setForm(undefined)
347
+ closePanel()
348
+ }}
349
+ />
350
+ )}
351
+ </>
352
+ )
353
+ }
354
+
355
+ /** The add / edit form (name + command), portaled like the shared dialogs. */
356
+ function SnippetForm(props: {
357
+ t: (key: string) => string
358
+ mode: 'add' | 'edit'
359
+ snippet?: TerminalSnippet
360
+ /** Draft for a NEW snippet (Ctrl+B hands over the selection / typed line). */
361
+ initialCommand?: string
362
+ onCancel: () => void
363
+ onSubmit: (input: { name: string; command: string }) => void
364
+ }): ReactNode {
365
+ const t = props.t
366
+ const [name, setName] = useState(props.snippet?.name ?? '')
367
+ const [command, setCommand] = useState(props.snippet?.command ?? props.initialCommand ?? '')
368
+ const [error, setError] = useState<string | undefined>(undefined)
369
+ const nameRef = useRef<HTMLInputElement>(null)
370
+ const openerRef = useRef<HTMLElement | null>(document.activeElement as HTMLElement | null)
371
+
372
+ useEffect(() => {
373
+ nameRef.current?.focus()
374
+ nameRef.current?.select()
375
+ return () => {
376
+ const opener = openerRef.current
377
+ if (opener !== null && document.body.contains(opener)) opener.focus()
378
+ }
379
+ }, [])
380
+
381
+ useEffect(() => {
382
+ const onKey = (event: KeyboardEvent): void => {
383
+ if (event.isComposing) return
384
+ if (event.key === 'Escape') {
385
+ event.preventDefault()
386
+ props.onCancel()
387
+ }
388
+ }
389
+ document.addEventListener('keydown', onKey, true)
390
+ return () => document.removeEventListener('keydown', onKey, true)
391
+ }, [props])
392
+
393
+ const submit = (): void => {
394
+ if (name.trim() === '') { setError(t('term.snippetNameRequired')); return }
395
+ if (command.trim() === '') { setError(t('term.snippetCommandRequired')); return }
396
+ props.onSubmit({ name, command })
397
+ }
398
+
399
+ return createPortal(
400
+ <div className="rvs-portal rvs-modal-mask" onMouseDown={(event) => { if (event.target === event.currentTarget) props.onCancel() }}>
401
+ <div className="rvs-modal-box" role="dialog" aria-modal="true" aria-label={props.mode === 'add' ? t('term.snippetAdd') : t('term.snippetEdit')}>
402
+ <div className="rvs-modal-title">{props.mode === 'add' ? t('term.snippetAdd') : t('term.snippetEdit')}</div>
403
+ <label className="rvs-snips-field">
404
+ <span>{t('term.snippetName')}</span>
405
+ <input
406
+ ref={nameRef}
407
+ value={name}
408
+ placeholder={t('term.snippetNamePlaceholder')}
409
+ onChange={(event) => { setName(event.target.value); setError(undefined) }}
410
+ onKeyDown={(event) => { if (event.key === 'Enter' && !event.nativeEvent.isComposing) { event.preventDefault(); submit() } }}
411
+ />
412
+ </label>
413
+ <label className="rvs-snips-field">
414
+ <span>{t('term.snippetCommand')}</span>
415
+ <textarea
416
+ className="rvs-snips-command"
417
+ value={command}
418
+ placeholder={t('term.snippetCommandPlaceholder')}
419
+ rows={4}
420
+ onChange={(event) => { setCommand(event.target.value); setError(undefined) }}
421
+ />
422
+ </label>
423
+ {error !== undefined && <div className="rvs-snips-error" role="alert">{error}</div>}
424
+ <div className="rvs-modal-row">
425
+ <button type="button" className="rvs-modal-btn" onClick={props.onCancel}>{t('common.cancel')}</button>
426
+ <button type="button" className="rvs-modal-btn primary" onClick={submit}>{t('common.save')}</button>
427
+ </div>
428
+ </div>
429
+ </div>,
430
+ document.body,
431
+ )
432
+ }
@@ -11,8 +11,10 @@ import { useCallback, useEffect, useRef, useState, useSyncExternalStore, type Re
11
11
  import type { RemoteHostSummary } from '../protocol.ts'
12
12
  import { RemoteApiError, api } from './api.ts'
13
13
  import { XTerm } from './XTerm.tsx'
14
+ import { TerminalSnippets } from './TerminalSnippets.tsx'
15
+ import { pasteIntoTerminal } from './terminal-input.ts'
14
16
  import { cdCommand } from './shell.ts'
15
- import type { TerminalRequest } from './store.tsx'
17
+ import { jumpToTerminalPath, type TerminalRequest } from './store.tsx'
16
18
  import { optionStyle } from './ui.tsx'
17
19
  import {
18
20
  IconButton,
@@ -20,6 +22,7 @@ import {
20
22
  IconPlus,
21
23
  IconTerminal,
22
24
  confirmDialog,
25
+ toast,
23
26
  } from './vsc/kit.tsx'
24
27
 
25
28
  const ALIAS_KEY = 'dsh-remote-vs.term.alias'
@@ -221,6 +224,15 @@ export function TerminalView(props: {
221
224
  connected: true,
222
225
  })))
223
226
  const [activeId, setActiveId] = useState<string | undefined>(() => tabs[0]?.id)
227
+ /**
228
+ * The "quick add snippet" request from Ctrl+B: a counter (so the same draft
229
+ * repeated still opens the form) plus the draft to pre-fill.
230
+ *
231
+ * A value rather than a callback prop: the shortcut lives in `XTerm` while the
232
+ * form lives in the header, and this is the one value both sides can share
233
+ * without lifting xterm state into React.
234
+ */
235
+ const [snippetAdd, setSnippetAdd] = useState<{ id: number; content: string } | undefined>(undefined)
224
236
 
225
237
  const effectiveAlias = (alias !== '' && props.hosts.some(host => host.alias === alias))
226
238
  ? alias
@@ -370,6 +382,18 @@ export function TerminalView(props: {
370
382
 
371
383
  const active = tabs.find(tab => tab.id === activeId) ?? tabs[0]
372
384
 
385
+ /**
386
+ * Remote home of a tab's host, for `~/path` tokens printed in its terminal.
387
+ *
388
+ * The SSH user is the only honest source (the PTY protocol carries no home):
389
+ * root lives in `/root`, everyone else in `/home/<user>`.
390
+ */
391
+ const homeFor = (hostAlias: string): string | undefined => {
392
+ const host = props.hosts.find(item => item.alias === hostAlias)
393
+ if (host === undefined) return undefined
394
+ return host.user === 'root' ? '/root' : `/home/${host.user}`
395
+ }
396
+
373
397
  const labelFor = (tab: TermTab): string => {
374
398
  const siblings = tabs.filter(item => item.alias === tab.alias)
375
399
  const index = siblings.findIndex(item => item.id === tab.id) + 1
@@ -392,6 +416,18 @@ export function TerminalView(props: {
392
416
  <div className="rvs-pane grow">
393
417
  <div className="rvs-header">
394
418
  <span className="rvs-title">{t('tab.terminal')}</span>
419
+ {/* The "常用指令" library: a personal list of name+command pairs that
420
+ paste into the active terminal without pressing Enter. */}
421
+ <TerminalSnippets
422
+ t={t}
423
+ addRequest={snippetAdd?.id ?? 0}
424
+ addContent={snippetAdd?.content ?? ''}
425
+ onInsert={(command) => {
426
+ if (pasteIntoTerminal(active?.id, command)) return true
427
+ toast(t('term.snippetNoTerminal'), true)
428
+ return false
429
+ }}
430
+ />
395
431
  <select
396
432
  className="rvs-select"
397
433
  value={effectiveAlias}
@@ -481,6 +517,19 @@ export function TerminalView(props: {
481
517
  })}
482
518
  visible={props.visible && tab.id === active?.id}
483
519
  t={t}
520
+ tabId={tab.id}
521
+ onQuickAddSnippet={(draft) => {
522
+ // Ctrl+B: pre-fill the form with what the user selected, else
523
+ // with the line reconstructed from this session's keystrokes.
524
+ const content = (draft.selection ?? draft.line ?? '').trim()
525
+ setSnippetAdd(previous => ({ id: (previous?.id ?? 0) + 1, content }))
526
+ }}
527
+ {...(homeFor(tab.alias) === undefined ? {} : { home: homeFor(tab.alias) })}
528
+ onPathClick={(token, context) => {
529
+ // Ctrl+Click in the terminal: validated against the host, then
530
+ // revealed in the tree (see `jumpToTerminalPath`).
531
+ void jumpToTerminalPath(tab.alias, token, context)
532
+ }}
484
533
  {...(tab.initialCommand === undefined ? {} : { initialInput: tab.initialCommand })}
485
534
  onReady={(id) => patchTab(tab.id, { session: id, connected: true, note: undefined })}
486
535
  onExit={(code, message) => {