dsh-remote-vs 0.23.0 → 0.25.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/README.en.md +49 -15
- package/README.md +40 -14
- package/lib/client.js +45978 -43338
- package/lib/index.js +388 -90
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/CodeEditor.d.ts +17 -0
- package/lib/types/client/EditorPane.d.ts +16 -5
- package/lib/types/client/FloatingPlaceholder.d.ts +16 -5
- package/lib/types/client/RemoteEditorView.d.ts +13 -10
- package/lib/types/client/RemoteRoot.d.ts +5 -5
- package/lib/types/client/SettingsHosts.d.ts +3 -0
- package/lib/types/client/TerminalSnippets.d.ts +36 -0
- package/lib/types/client/TerminalView.d.ts +1 -1
- package/lib/types/client/XTerm.d.ts +33 -0
- package/lib/types/client/api.d.ts +4 -1
- package/lib/types/client/auto-open-remote.d.ts +41 -0
- package/lib/types/client/editor-capability.d.ts +27 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/store.d.ts +59 -4
- package/lib/types/client/terminal-input.d.ts +23 -0
- package/lib/types/client/terminal-snippets.d.ts +86 -0
- package/lib/types/client/use-remote-chat-target.d.ts +2 -0
- package/lib/types/client/view-settings.d.ts +40 -0
- package/lib/types/client/vsc/breadcrumb.d.ts +30 -0
- package/lib/types/client/vsc/editor-caret.d.ts +32 -0
- package/lib/types/client/vsc/editor-langs.d.ts +9 -0
- package/lib/types/client/vsc/editor-settings.d.ts +55 -0
- package/lib/types/client/vsc/image-preview.d.ts +36 -0
- package/lib/types/client/vsc/kit.d.ts +2 -0
- package/lib/types/client/vsc/terminal-clipboard.d.ts +15 -0
- package/lib/types/client/vsc/terminal-line.d.ts +25 -0
- package/lib/types/client/vsc/terminal-links.d.ts +62 -0
- package/lib/types/client/vsc/terminal-theme.d.ts +22 -0
- package/lib/types/client/vsc/terminal-writer.d.ts +52 -0
- package/lib/types/paths.d.ts +9 -0
- package/lib/types/pool.d.ts +25 -0
- package/lib/types/protocol.d.ts +35 -0
- package/lib/types/routes.d.ts +7 -0
- package/lib/types/terminal.d.ts +25 -1
- package/lib/types/view-settings.d.ts +26 -0
- package/lib/{worlds-DXdP4SMn.js → worlds-B7gEDBzC.js} +13 -1
- package/package.json +36 -36
- package/src/client/CodeEditor.tsx +54 -2
- package/src/client/EditorPane.tsx +485 -107
- package/src/client/FloatingPlaceholder.tsx +34 -5
- package/src/client/RemoteEditorView.tsx +14 -11
- package/src/client/RemoteRoot.tsx +20 -125
- package/src/client/SettingsHosts.tsx +81 -3
- package/src/client/TerminalSnippets.tsx +357 -0
- package/src/client/TerminalView.tsx +50 -1
- package/src/client/XTerm.tsx +228 -58
- package/src/client/api.ts +7 -0
- package/src/client/auto-open-remote.ts +439 -0
- package/src/client/editor-capability.ts +42 -0
- package/src/client/i18n.ts +158 -6
- package/src/client/index.tsx +30 -6
- package/src/client/store.tsx +165 -13
- package/src/client/terminal-input.ts +44 -0
- package/src/client/terminal-snippets.ts +216 -0
- package/src/client/use-remote-chat-target.ts +1 -1
- package/src/client/view-settings.ts +99 -0
- package/src/client/vsc/breadcrumb.tsx +251 -0
- package/src/client/vsc/editor-caret.ts +30 -0
- package/src/client/vsc/editor-langs.ts +20 -0
- package/src/client/vsc/editor-settings.ts +146 -0
- package/src/client/vsc/editor-theme.ts +4 -5
- package/src/client/vsc/image-preview.tsx +110 -0
- package/src/client/vsc/kit.tsx +20 -1
- package/src/client/vsc/terminal-clipboard.ts +19 -0
- package/src/client/vsc/terminal-line.ts +101 -0
- package/src/client/vsc/terminal-links.ts +166 -0
- package/src/client/vsc/terminal-theme.ts +71 -0
- package/src/client/vsc/terminal-writer.ts +112 -0
- package/src/client/vsc/vsc.css +687 -48
- package/src/index.ts +6 -0
- package/src/paths.ts +13 -0
- package/src/pool.ts +99 -27
- package/src/protocol.ts +45 -0
- package/src/routes.ts +47 -0
- package/src/terminal.ts +160 -6
- package/src/view-settings.ts +133 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common-command snippets: the little "常用指令" library above the terminal.
|
|
3
|
+
*
|
|
4
|
+
* A snippet is a NAME plus the COMMAND it stands for. Clicking one in the panel
|
|
5
|
+
* pastes the command into the active terminal's command line WITHOUT pressing
|
|
6
|
+
* Enter — the user reviews and runs it themselves, so a misclick cannot execute
|
|
7
|
+
* `rm -rf`.
|
|
8
|
+
*
|
|
9
|
+
* Storage is per browser (localStorage), like the port-forward rules and the
|
|
10
|
+
* editor preferences: this is a personal convenience list, not workspace state,
|
|
11
|
+
* and the host is told nothing about it. The pure half is exported so the gate
|
|
12
|
+
* can drive parsing / CRUD / ordering without a DOM, and the module-level store
|
|
13
|
+
* (snapshot + subscribe) is what makes the popover and the Ctrl+B shortcut share
|
|
14
|
+
* one list.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** One saved command. */
|
|
18
|
+
export interface TerminalSnippet {
|
|
19
|
+
/** Stable identity (never reused, so drag/reorder cannot swap rows). */
|
|
20
|
+
id: string
|
|
21
|
+
/** Short label shown in the list. */
|
|
22
|
+
name: string
|
|
23
|
+
/** The command text pasted into the terminal (not executed). */
|
|
24
|
+
command: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** localStorage key (one JSON array, so the list cannot half-update). */
|
|
28
|
+
export const SNIPPETS_KEY = 'dsh-remote-vs.term.snippets'
|
|
29
|
+
|
|
30
|
+
/** Hard caps: a snippet list is a convenience, not a dataset. */
|
|
31
|
+
export const SNIPPETS_MAX = 200
|
|
32
|
+
export const SNIPPET_NAME_MAX = 80
|
|
33
|
+
export const SNIPPET_COMMAND_MAX = 4000
|
|
34
|
+
|
|
35
|
+
/** A fresh identity for one snippet. */
|
|
36
|
+
export function createSnippetId(): string {
|
|
37
|
+
return `s${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Normalize one user-entered snippet; `undefined` when it is unusable.
|
|
42
|
+
*
|
|
43
|
+
* Both fields are required (an unnamed command is unfindable, an empty command
|
|
44
|
+
* does nothing) and both are trimmed — a leading/trailing newline in a pasted
|
|
45
|
+
* command would otherwise send an empty line and RUN the command, which is
|
|
46
|
+
* exactly what this feature promises not to do.
|
|
47
|
+
*/
|
|
48
|
+
export function sanitizeSnippetInput(input: { name?: string; command?: string }): { name: string; command: string } | undefined {
|
|
49
|
+
const name = (input.name ?? '').trim().slice(0, SNIPPET_NAME_MAX)
|
|
50
|
+
const command = (input.command ?? '').trim().slice(0, SNIPPET_COMMAND_MAX)
|
|
51
|
+
if (name === '' || command === '') return undefined
|
|
52
|
+
return { name, command }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Parse the persisted JSON; anything malformed is dropped, never repaired silently. */
|
|
56
|
+
export function parseSnippets(raw: string | null | undefined): TerminalSnippet[] {
|
|
57
|
+
if (typeof raw !== 'string' || raw === '') return []
|
|
58
|
+
let parsed: unknown
|
|
59
|
+
try {
|
|
60
|
+
parsed = JSON.parse(raw) as unknown
|
|
61
|
+
} catch {
|
|
62
|
+
return []
|
|
63
|
+
}
|
|
64
|
+
if (!Array.isArray(parsed)) return []
|
|
65
|
+
const list: TerminalSnippet[] = []
|
|
66
|
+
const seen = new Set<string>()
|
|
67
|
+
for (const item of parsed) {
|
|
68
|
+
if (typeof item !== 'object' || item === null) continue
|
|
69
|
+
const record = item as Record<string, unknown>
|
|
70
|
+
const sanitized = sanitizeSnippetInput({
|
|
71
|
+
name: typeof record.name === 'string' ? record.name : '',
|
|
72
|
+
command: typeof record.command === 'string' ? record.command : '',
|
|
73
|
+
})
|
|
74
|
+
if (sanitized === undefined) continue
|
|
75
|
+
// A duplicated id would make two rows share a React key and a drag target.
|
|
76
|
+
let id = typeof record.id === 'string' && record.id !== '' ? record.id : createSnippetId()
|
|
77
|
+
if (seen.has(id)) id = createSnippetId()
|
|
78
|
+
seen.add(id)
|
|
79
|
+
list.push({ id, ...sanitized })
|
|
80
|
+
if (list.length >= SNIPPETS_MAX) break
|
|
81
|
+
}
|
|
82
|
+
return list
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Serialize only the known fields. */
|
|
86
|
+
export function serializeSnippets(list: readonly TerminalSnippet[]): string {
|
|
87
|
+
return JSON.stringify(list.map(item => ({ id: item.id, name: item.name, command: item.command })))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Append one snippet (newest last); returns the SAME array when it is invalid. */
|
|
91
|
+
export function addSnippet(
|
|
92
|
+
list: readonly TerminalSnippet[],
|
|
93
|
+
input: { name?: string; command?: string },
|
|
94
|
+
id: string = createSnippetId(),
|
|
95
|
+
): SnippetList {
|
|
96
|
+
const sanitized = sanitizeSnippetInput(input)
|
|
97
|
+
if (sanitized === undefined) return list
|
|
98
|
+
if (list.some(item => item.id === id)) id = createSnippetId()
|
|
99
|
+
const next = [...list, { id, ...sanitized }]
|
|
100
|
+
// The cap drops the OLDEST entry: the newest command is the one being used.
|
|
101
|
+
return next.length > SNIPPETS_MAX ? next.slice(next.length - SNIPPETS_MAX) : next
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Replace one snippet's fields; an invalid edit leaves the list untouched. */
|
|
105
|
+
export function updateSnippet(
|
|
106
|
+
list: readonly TerminalSnippet[],
|
|
107
|
+
id: string,
|
|
108
|
+
input: { name?: string; command?: string },
|
|
109
|
+
): SnippetList {
|
|
110
|
+
const index = list.findIndex(item => item.id === id)
|
|
111
|
+
if (index < 0) return list
|
|
112
|
+
const sanitized = sanitizeSnippetInput(input)
|
|
113
|
+
if (sanitized === undefined) return list
|
|
114
|
+
const current = list[index] as TerminalSnippet
|
|
115
|
+
if (current.name === sanitized.name && current.command === sanitized.command) return list
|
|
116
|
+
const next = [...list]
|
|
117
|
+
next[index] = { id, ...sanitized }
|
|
118
|
+
return next
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Drop one snippet. */
|
|
122
|
+
export function removeSnippet(list: readonly TerminalSnippet[], id: string): SnippetList {
|
|
123
|
+
const next = list.filter(item => item.id !== id)
|
|
124
|
+
return next.length === list.length ? list : next
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Move one snippet to another index (drag & drop), clamping both ends.
|
|
129
|
+
*
|
|
130
|
+
* Returning the SAME list for a no-op keeps the store's "did it change" test
|
|
131
|
+
* cheap and stops a drag that lands on its own row from re-rendering everything.
|
|
132
|
+
*/
|
|
133
|
+
export function moveSnippet(list: readonly TerminalSnippet[], from: number, to: number): SnippetList {
|
|
134
|
+
if (list.length < 2) return list
|
|
135
|
+
const source = Math.max(0, Math.min(list.length - 1, from))
|
|
136
|
+
const target = Math.max(0, Math.min(list.length - 1, to))
|
|
137
|
+
if (source === target) return list
|
|
138
|
+
const next = [...list]
|
|
139
|
+
const [moved] = next.splice(source, 1)
|
|
140
|
+
if (moved === undefined) return list
|
|
141
|
+
next.splice(target, 0, moved)
|
|
142
|
+
return next
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A list type that is either the caller's unchanged value or a new array. */
|
|
146
|
+
type SnippetList = readonly TerminalSnippet[]
|
|
147
|
+
|
|
148
|
+
// ----------------------------------------------------------------- store
|
|
149
|
+
|
|
150
|
+
const listeners = new Set<() => void>()
|
|
151
|
+
/** Lazy current list; undefined = localStorage not read yet. */
|
|
152
|
+
let current: readonly TerminalSnippet[] | undefined
|
|
153
|
+
|
|
154
|
+
function readFromStorage(): readonly TerminalSnippet[] {
|
|
155
|
+
try {
|
|
156
|
+
return parseSnippets(localStorage.getItem(SNIPPETS_KEY))
|
|
157
|
+
} catch {
|
|
158
|
+
// Private mode / storage disabled: the list works for this session only.
|
|
159
|
+
return []
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function persist(list: readonly TerminalSnippet[]): void {
|
|
164
|
+
try {
|
|
165
|
+
localStorage.setItem(SNIPPETS_KEY, serializeSnippets(list))
|
|
166
|
+
} catch {
|
|
167
|
+
// Same as above: failing to persist does not affect this session.
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function emit(): void {
|
|
172
|
+
for (const listener of [...listeners]) {
|
|
173
|
+
try {
|
|
174
|
+
listener()
|
|
175
|
+
} catch {
|
|
176
|
+
// One broken surface must not starve the others.
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Current list (identity-stable while unchanged, safe for useSyncExternalStore). */
|
|
182
|
+
export function snippetsSnapshot(): readonly TerminalSnippet[] {
|
|
183
|
+
if (current === undefined) current = readFromStorage()
|
|
184
|
+
return current
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Subscribe to snippet changes; returns the unsubscribe. */
|
|
188
|
+
export function subscribeSnippets(listener: () => void): () => void {
|
|
189
|
+
listeners.add(listener)
|
|
190
|
+
return () => {
|
|
191
|
+
listeners.delete(listener)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Apply one pure transition and persist it when it really changed.
|
|
197
|
+
*
|
|
198
|
+
* The updater must be pure and return either the same array (no change → no
|
|
199
|
+
* notification, no write) or a new one; every CRUD helper above already does.
|
|
200
|
+
*/
|
|
201
|
+
export function updateSnippets(
|
|
202
|
+
updater: (list: readonly TerminalSnippet[]) => readonly TerminalSnippet[],
|
|
203
|
+
): void {
|
|
204
|
+
const before = snippetsSnapshot()
|
|
205
|
+
const next = updater(before)
|
|
206
|
+
if (next === before) return
|
|
207
|
+
current = next
|
|
208
|
+
persist(next)
|
|
209
|
+
emit()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Reset the module store (the gate drives it directly; production never calls it). */
|
|
213
|
+
export function resetSnippetsForTests(list?: readonly TerminalSnippet[]): void {
|
|
214
|
+
current = list ?? []
|
|
215
|
+
listeners.clear()
|
|
216
|
+
}
|
|
@@ -33,7 +33,7 @@ function asSubscribe(value: unknown): SubscribeFn | undefined {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/** The on-screen session id, or undefined (no seat mounted / no feed). */
|
|
36
|
-
function mountedSessionId(ctx: Context): string | undefined {
|
|
36
|
+
export function mountedSessionId(ctx: Context): string | undefined {
|
|
37
37
|
try {
|
|
38
38
|
const get = (ctx as unknown as { get?(name: string): unknown }).get
|
|
39
39
|
if (typeof get !== 'function') return undefined
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client cache of the two view preferences (`/api/dsh-remote/view-settings`).
|
|
3
|
+
*
|
|
4
|
+
* Two very different consumers read the same values and must agree:
|
|
5
|
+
* - the Settings page renders the controls and writes them;
|
|
6
|
+
* - the frame-wide auto-open behaviour (`auto-open-remote.ts`) reads them while
|
|
7
|
+
* the Settings page is closed — often it is the ONLY reader, because a page
|
|
8
|
+
* that never opened Settings still has to know whether to open the panel.
|
|
9
|
+
*
|
|
10
|
+
* So the cache lives at module scope with a one-listener-set subscription: a
|
|
11
|
+
* save from the Settings page is visible to the behaviour in the SAME page
|
|
12
|
+
* without a reload. Every read is soft — an older host without the route answers
|
|
13
|
+
* nothing, `loadViewSettings` reports `undefined`, and the caller keeps its
|
|
14
|
+
* defaults instead of crashing the surface that asked.
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
REMOTE_VIEW_SETTINGS_DEFAULTS,
|
|
18
|
+
clampViewWidthRatio,
|
|
19
|
+
type RemoteViewSettings,
|
|
20
|
+
} from '../protocol.ts'
|
|
21
|
+
import { api } from './api.ts'
|
|
22
|
+
|
|
23
|
+
let cached: RemoteViewSettings | undefined
|
|
24
|
+
let inflight: Promise<RemoteViewSettings | undefined> | undefined
|
|
25
|
+
/** Where one published value came from: a load answers the page, a save is a user action. */
|
|
26
|
+
export type ViewSettingsSource = 'load' | 'save'
|
|
27
|
+
const listeners = new Set<(settings: RemoteViewSettings, source: ViewSettingsSource) => void>()
|
|
28
|
+
|
|
29
|
+
/** The last known preferences, or `undefined` until the first successful load. */
|
|
30
|
+
export function currentViewSettings(): RemoteViewSettings | undefined {
|
|
31
|
+
return cached
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Subscribe to every loaded/saved value; returns the unsubscribe.
|
|
36
|
+
*
|
|
37
|
+
* The source matters: only a SAVE is an explicit user action, and only that may
|
|
38
|
+
* resize a panel the user already has open (a page-load answer must not).
|
|
39
|
+
*/
|
|
40
|
+
export function subscribeViewSettings(
|
|
41
|
+
listener: (settings: RemoteViewSettings, source: ViewSettingsSource) => void,
|
|
42
|
+
): () => void {
|
|
43
|
+
listeners.add(listener)
|
|
44
|
+
return () => {
|
|
45
|
+
listeners.delete(listener)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Normalize one answer: an older host may omit or round a field. */
|
|
50
|
+
function normalize(raw: RemoteViewSettings | undefined): RemoteViewSettings {
|
|
51
|
+
return {
|
|
52
|
+
autoOpenRemoteWorkspace: typeof raw?.autoOpenRemoteWorkspace === 'boolean'
|
|
53
|
+
? raw.autoOpenRemoteWorkspace
|
|
54
|
+
: REMOTE_VIEW_SETTINGS_DEFAULTS.autoOpenRemoteWorkspace,
|
|
55
|
+
remoteWorkspaceWidthRatio: typeof raw?.remoteWorkspaceWidthRatio === 'number'
|
|
56
|
+
&& Number.isFinite(raw.remoteWorkspaceWidthRatio)
|
|
57
|
+
? clampViewWidthRatio(raw.remoteWorkspaceWidthRatio)
|
|
58
|
+
: REMOTE_VIEW_SETTINGS_DEFAULTS.remoteWorkspaceWidthRatio,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function publish(next: RemoteViewSettings, source: ViewSettingsSource): RemoteViewSettings {
|
|
63
|
+
cached = next
|
|
64
|
+
for (const listener of [...listeners]) {
|
|
65
|
+
try {
|
|
66
|
+
listener(next, source)
|
|
67
|
+
} catch {
|
|
68
|
+
// One broken surface must not starve the others.
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return next
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Load the preferences once (cached), or force a reload.
|
|
76
|
+
*
|
|
77
|
+
* @param force - ignore the cache (the retry path behind a failed first load).
|
|
78
|
+
* @returns the current values, or `undefined` when the host cannot answer.
|
|
79
|
+
*/
|
|
80
|
+
export function loadViewSettings(force = false): Promise<RemoteViewSettings | undefined> {
|
|
81
|
+
if (!force && cached !== undefined) return Promise.resolve(cached)
|
|
82
|
+
if (!force && inflight !== undefined) return inflight
|
|
83
|
+
const request = api.getViewSettings()
|
|
84
|
+
.then(raw => publish(normalize(raw), 'load'), () => undefined)
|
|
85
|
+
.finally(() => {
|
|
86
|
+
if (inflight === request) inflight = undefined
|
|
87
|
+
})
|
|
88
|
+
inflight = request
|
|
89
|
+
return request
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Persist a flat patch and publish the answer.
|
|
94
|
+
*
|
|
95
|
+
* @throws RemoteApiError when the host refuses (the settings page shows it).
|
|
96
|
+
*/
|
|
97
|
+
export async function saveViewSettings(patch: Partial<RemoteViewSettings>): Promise<RemoteViewSettings> {
|
|
98
|
+
return publish(normalize(await api.setViewSettings(patch)), 'save')
|
|
99
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Breadcrumb above the editor in the full-width (conversation view) variant.
|
|
3
|
+
*
|
|
4
|
+
* Every path segment is clickable and opens a VS Code-style sibling picker: a
|
|
5
|
+
* directory segment lists itself, the file segment lists its parent directory,
|
|
6
|
+
* so "open the file next to this one" costs one click instead of a trip to the
|
|
7
|
+
* tree (whose expansion state lives in another view and cannot be expressed in
|
|
8
|
+
* a URL).
|
|
9
|
+
*
|
|
10
|
+
* Read-only: it only calls `fileOp.list` on the remote. The remote path is the
|
|
11
|
+
* Host's own business — no second validation layer here.
|
|
12
|
+
*/
|
|
13
|
+
import { useEffect, useMemo, useRef, useState, type MouseEvent as ReactMouseEvent, type ReactNode } from 'react'
|
|
14
|
+
import { api } from '../api.ts'
|
|
15
|
+
import type { RemoteFileEntry } from '../../protocol.ts'
|
|
16
|
+
|
|
17
|
+
/** One breadcrumb segment. */
|
|
18
|
+
interface Crumb {
|
|
19
|
+
/** Display text. */
|
|
20
|
+
label: string
|
|
21
|
+
/** Absolute remote path. */
|
|
22
|
+
path: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Split an absolute POSIX path into segments (empty path = empty list). */
|
|
26
|
+
function pathSegments(path: string): string[] {
|
|
27
|
+
return path.replace(/\\/g, '/').split('/').filter(part => part !== '')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Root display name: the binding's name, else the last segment of the root. */
|
|
31
|
+
function rootLabel(root: string, rootName: string, fallback: string): string {
|
|
32
|
+
if (rootName !== '') return rootName
|
|
33
|
+
const parts = pathSegments(root)
|
|
34
|
+
return parts[parts.length - 1] ?? fallback
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Open dropdown state. */
|
|
38
|
+
interface MenuState {
|
|
39
|
+
/** Absolute directory being listed. */
|
|
40
|
+
dir: string
|
|
41
|
+
/** Menu's left edge, relative to the breadcrumb container (px). */
|
|
42
|
+
left: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Props of {@link Breadcrumb}. */
|
|
46
|
+
export interface BreadcrumbProps {
|
|
47
|
+
/** i18n lookup (falls back to inline copy when a key is missing). */
|
|
48
|
+
t: (key: string) => string
|
|
49
|
+
/** SSH host alias the workspace lives on (the file route's first argument). */
|
|
50
|
+
alias: string
|
|
51
|
+
/** Remote workspace root, absolute. */
|
|
52
|
+
root: string
|
|
53
|
+
/** Display name of the workspace root. */
|
|
54
|
+
rootName: string
|
|
55
|
+
/** Absolute path of the file on screen. */
|
|
56
|
+
path: string
|
|
57
|
+
/** Open one remote file in the editor. */
|
|
58
|
+
onOpen: (fullPath: string) => void
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Translate one key, falling back to the inline copy when the dictionary lacks it. */
|
|
62
|
+
function tr(t: (key: string) => string, key: string, fallback: string): string {
|
|
63
|
+
const value = t(key)
|
|
64
|
+
return value === undefined || value === '' || value === key ? fallback : value
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Breadcrumb: path segments + sibling dropdown. */
|
|
68
|
+
export function Breadcrumb(props: BreadcrumbProps): ReactNode {
|
|
69
|
+
const navRef = useRef<HTMLElement>(null)
|
|
70
|
+
const [menu, setMenu] = useState<MenuState | undefined>(undefined)
|
|
71
|
+
const [entries, setEntries] = useState<RemoteFileEntry[]>([])
|
|
72
|
+
const [loading, setLoading] = useState(false)
|
|
73
|
+
const [error, setError] = useState<string | undefined>(undefined)
|
|
74
|
+
/** Load token: two quick segment clicks must only let the last answer land. */
|
|
75
|
+
const loadSeq = useRef(0)
|
|
76
|
+
|
|
77
|
+
const t = props.t
|
|
78
|
+
const root = props.root.replace(/\/+$/, '')
|
|
79
|
+
/**
|
|
80
|
+
* A path OUTSIDE the workspace root (a repo sitting next to the binding, a
|
|
81
|
+
* diff path that left the tree) cannot be split into root-relative crumbs:
|
|
82
|
+
* doing it anyway would build `/root/home/...` and the sibling picker would
|
|
83
|
+
* list the wrong directory. Fall back to the plain path line.
|
|
84
|
+
*/
|
|
85
|
+
const underRoot = root !== ''
|
|
86
|
+
&& (props.path === root || props.path.startsWith(`${root}/`))
|
|
87
|
+
const segments = useMemo(() => {
|
|
88
|
+
const full = props.path.replace(/\/+$/, '')
|
|
89
|
+
if (root !== '' && full.startsWith(`${root}/`)) return pathSegments(full.slice(root.length + 1))
|
|
90
|
+
return pathSegments(full)
|
|
91
|
+
}, [props.path, root])
|
|
92
|
+
const crumbs = useMemo<Crumb[]>(() => {
|
|
93
|
+
const list: Crumb[] = [{ label: rootLabel(root, props.rootName, tr(t, 'editor.crumbRoot', '工作区')), path: root }]
|
|
94
|
+
for (const [index, segment] of segments.entries()) {
|
|
95
|
+
list.push({ label: segment, path: `${root}/${segments.slice(0, index + 1).join('/')}` })
|
|
96
|
+
}
|
|
97
|
+
return list
|
|
98
|
+
}, [root, props.rootName, segments, t])
|
|
99
|
+
|
|
100
|
+
/** List one directory and show the menu; `left` keeps the current position. */
|
|
101
|
+
const openMenu = (dir: string, left: number): void => {
|
|
102
|
+
setMenu({ dir, left })
|
|
103
|
+
setLoading(true)
|
|
104
|
+
setError(undefined)
|
|
105
|
+
loadSeq.current += 1
|
|
106
|
+
const token = loadSeq.current
|
|
107
|
+
void (async () => {
|
|
108
|
+
try {
|
|
109
|
+
const listed = await api.fileOp.list(props.alias, dir)
|
|
110
|
+
if (loadSeq.current !== token) return
|
|
111
|
+
// Directories first, then names: same order as the tree, faster to scan.
|
|
112
|
+
const sorted = [...listed].sort((a, b) => {
|
|
113
|
+
const aDir = a.isDir ? 0 : 1
|
|
114
|
+
const bDir = b.isDir ? 0 : 1
|
|
115
|
+
if (aDir !== bDir) return aDir - bDir
|
|
116
|
+
return a.name.localeCompare(b.name)
|
|
117
|
+
})
|
|
118
|
+
setEntries(sorted)
|
|
119
|
+
} catch (caught) {
|
|
120
|
+
if (loadSeq.current !== token) return
|
|
121
|
+
setEntries([])
|
|
122
|
+
setError(caught instanceof Error ? caught.message : String(caught))
|
|
123
|
+
} finally {
|
|
124
|
+
if (loadSeq.current === token) setLoading(false)
|
|
125
|
+
}
|
|
126
|
+
})()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Close on an outside click / Esc. Capture phase: CodeMirror's own mousedown
|
|
130
|
+
// handling must not run first (it grabs focus back and the menu looks stuck).
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
if (menu === undefined) return
|
|
133
|
+
const onMouseDown = (event: MouseEvent): void => {
|
|
134
|
+
const nav = navRef.current
|
|
135
|
+
if (nav !== null && event.target instanceof Node && nav.contains(event.target)) return
|
|
136
|
+
setMenu(undefined)
|
|
137
|
+
}
|
|
138
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
139
|
+
// During IME composition Esc cancels the candidate, not the menu.
|
|
140
|
+
if (event.isComposing) return
|
|
141
|
+
if (event.key === 'Escape') setMenu(undefined)
|
|
142
|
+
}
|
|
143
|
+
document.addEventListener('mousedown', onMouseDown, true)
|
|
144
|
+
document.addEventListener('keydown', onKeyDown, true)
|
|
145
|
+
return () => {
|
|
146
|
+
document.removeEventListener('mousedown', onMouseDown, true)
|
|
147
|
+
document.removeEventListener('keydown', onKeyDown, true)
|
|
148
|
+
}
|
|
149
|
+
}, [menu])
|
|
150
|
+
|
|
151
|
+
// Another file closes the menu: a stale dropdown over a new file is misaligned.
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
setMenu(undefined)
|
|
154
|
+
}, [props.path])
|
|
155
|
+
|
|
156
|
+
if (!underRoot) {
|
|
157
|
+
return (
|
|
158
|
+
<nav className="rvs-breadcrumb" aria-label={tr(t, 'editor.crumbLabel', '文件路径')}>
|
|
159
|
+
<span className="rvs-einfo-path" title={props.path}>{props.path}</span>
|
|
160
|
+
</nav>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Click a segment: a directory lists itself; the file lists its parent. */
|
|
165
|
+
const clickCrumb = (crumb: Crumb, index: number, event: ReactMouseEvent<HTMLButtonElement>): void => {
|
|
166
|
+
const isFile = index === crumbs.length - 1
|
|
167
|
+
const dir = isFile ? crumb.path.slice(0, Math.max(0, crumb.path.lastIndexOf('/'))) : crumb.path
|
|
168
|
+
openMenu(dir === '' ? '/' : dir, Math.max(0, event.currentTarget.offsetLeft))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const parentDirOf = (dir: string): string | undefined => {
|
|
172
|
+
if (dir === '' || dir === root || dir === '/') return undefined
|
|
173
|
+
const parts = pathSegments(dir)
|
|
174
|
+
parts.pop()
|
|
175
|
+
return parts.length === 0 ? undefined : `/${parts.join('/')}`
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<nav className="rvs-breadcrumb" ref={navRef} aria-label={tr(t, 'editor.crumbLabel', '文件路径')}>
|
|
180
|
+
{crumbs.map((crumb, index) => {
|
|
181
|
+
const isFile = index === crumbs.length - 1
|
|
182
|
+
return (
|
|
183
|
+
<span className="rvs-crumb-wrap" key={`${crumb.path}:${index}`}>
|
|
184
|
+
{index > 0 && <span className="rvs-crumb-sep" aria-hidden="true">›</span>}
|
|
185
|
+
<button
|
|
186
|
+
type="button"
|
|
187
|
+
className={`rvs-crumb${isFile ? ' rvs-crumb-file' : ''}`}
|
|
188
|
+
// The last segment is the FILE (its menu lists siblings); a
|
|
189
|
+
// directory segment lists ITSELF, so the hint must not promise
|
|
190
|
+
// sibling files there.
|
|
191
|
+
title={isFile ? props.path : crumb.path}
|
|
192
|
+
aria-haspopup="menu"
|
|
193
|
+
onClick={event => clickCrumb(crumb, index, event)}
|
|
194
|
+
>
|
|
195
|
+
{crumb.label}
|
|
196
|
+
</button>
|
|
197
|
+
</span>
|
|
198
|
+
)
|
|
199
|
+
})}
|
|
200
|
+
|
|
201
|
+
{menu !== undefined && (
|
|
202
|
+
<div className="rvs-crumb-menu" role="menu" style={{ left: menu.left }}>
|
|
203
|
+
<div className="rvs-crumb-menu-head">
|
|
204
|
+
<button
|
|
205
|
+
type="button"
|
|
206
|
+
className="rvs-crumb-menu-up"
|
|
207
|
+
title={tr(t, 'editor.crumbUp', '上一级')}
|
|
208
|
+
disabled={parentDirOf(menu.dir) === undefined}
|
|
209
|
+
onClick={() => {
|
|
210
|
+
const parent = parentDirOf(menu.dir)
|
|
211
|
+
if (parent === undefined) return
|
|
212
|
+
openMenu(parent, menu.left)
|
|
213
|
+
}}
|
|
214
|
+
>
|
|
215
|
+
↑
|
|
216
|
+
</button>
|
|
217
|
+
<span className="rvs-crumb-menu-path" title={menu.dir}>{menu.dir}</span>
|
|
218
|
+
</div>
|
|
219
|
+
<div className="rvs-crumb-menu-list">
|
|
220
|
+
{loading && <div className="rvs-crumb-menu-note">{tr(t, 'common.loading', '加载中…')}</div>}
|
|
221
|
+
{!loading && error !== undefined && <div className="rvs-crumb-menu-note rvs-status-error">{error}</div>}
|
|
222
|
+
{!loading && error === undefined && entries.length === 0 && (
|
|
223
|
+
<div className="rvs-crumb-menu-note">{tr(t, 'editor.crumbEmpty', '空目录')}</div>
|
|
224
|
+
)}
|
|
225
|
+
{!loading && error === undefined && entries.map(entry => (
|
|
226
|
+
<button
|
|
227
|
+
key={entry.path}
|
|
228
|
+
type="button"
|
|
229
|
+
role="menuitem"
|
|
230
|
+
className={`rvs-crumb-menu-item${entry.isDir ? ' rvs-dir' : ''}`}
|
|
231
|
+
title={entry.path}
|
|
232
|
+
onClick={() => {
|
|
233
|
+
if (entry.isDir) {
|
|
234
|
+
openMenu(entry.path, menu.left)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
// Stay on the surface this breadcrumb belongs to: no sidebar jump.
|
|
238
|
+
props.onOpen(entry.path)
|
|
239
|
+
setMenu(undefined)
|
|
240
|
+
}}
|
|
241
|
+
>
|
|
242
|
+
<span className="rvs-crumb-menu-glyph" aria-hidden="true">{entry.isDir ? '▸' : '·'}</span>
|
|
243
|
+
<span className="rvs-crumb-menu-name">{entry.name}</span>
|
|
244
|
+
</button>
|
|
245
|
+
))}
|
|
246
|
+
</div>
|
|
247
|
+
</div>
|
|
248
|
+
)}
|
|
249
|
+
</nav>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caret readout maths for the editor status bar (pure, no CodeMirror/DOM).
|
|
3
|
+
*
|
|
4
|
+
* Kept out of `CodeEditor.tsx` so the gate can assert the 1-based conversion and
|
|
5
|
+
* the "selected characters" count without mounting an editor.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Caret readout for the status bar (1-based, matching what the user sees). */
|
|
9
|
+
export interface CodeEditorCaret {
|
|
10
|
+
/** 1-based line of the caret (the selection's head). */
|
|
11
|
+
line: number
|
|
12
|
+
/** 1-based column of the caret. */
|
|
13
|
+
column: number
|
|
14
|
+
/** Number of selected characters (0 = empty selection). */
|
|
15
|
+
selected: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The caret reading of one editor state. */
|
|
19
|
+
export function caretOf(state: {
|
|
20
|
+
doc: { lineAt(offset: number): { number: number; from: number; to: number } }
|
|
21
|
+
selection: { main: { head: number; from: number; to: number } }
|
|
22
|
+
}): CodeEditorCaret {
|
|
23
|
+
const main = state.selection.main
|
|
24
|
+
const line = state.doc.lineAt(main.head)
|
|
25
|
+
return {
|
|
26
|
+
line: line.number,
|
|
27
|
+
column: main.head - line.from + 1,
|
|
28
|
+
selected: Math.max(0, main.to - main.from),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -135,3 +135,23 @@ export function langFor(path: string): Extension[] {
|
|
|
135
135
|
return []
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Guess the indentation style from the file body (status-bar hint only).
|
|
141
|
+
*
|
|
142
|
+
* Only the first stretch of lines that really starts with whitespace is
|
|
143
|
+
* inspected: the goal is a quick hint, not a formatter. `undefined` means "no
|
|
144
|
+
* indented line in the head", and the status bar then omits the item instead of
|
|
145
|
+
* inventing a default.
|
|
146
|
+
*/
|
|
147
|
+
export function detectIndentLabel(text: string): string | undefined {
|
|
148
|
+
const lines = text.split('\n', 200)
|
|
149
|
+
for (const line of lines) {
|
|
150
|
+
if (line.trim() === '') continue
|
|
151
|
+
if (line.startsWith('\t')) return 'Tab'
|
|
152
|
+
const spaces = /^ +/.exec(line)
|
|
153
|
+
if (spaces === null) continue
|
|
154
|
+
return `Spaces: ${Math.min(8, spaces[0].length)}`
|
|
155
|
+
}
|
|
156
|
+
return undefined
|
|
157
|
+
}
|