@workbench-kit/shell-react 0.0.2-prototype.0.2.10
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.md +98 -0
- package/package.json +51 -0
- package/src/chat/ai-mock-proposals.ts +39 -0
- package/src/chat/command-input.ts +52 -0
- package/src/chat/command-policy.ts +54 -0
- package/src/chat/command-surface.tsx +155 -0
- package/src/chat/use-chat-command-proposals.ts +194 -0
- package/src/chat/view-data.ts +20 -0
- package/src/chat/view.tsx +220 -0
- package/src/commands/inspector-surface.tsx +46 -0
- package/src/commands/use-command-descriptors.ts +41 -0
- package/src/commands/use-context-key-revision.ts +18 -0
- package/src/commands/view-data.ts +17 -0
- package/src/commands/view.tsx +43 -0
- package/src/devtools/WorkbenchDevtoolsPanel.tsx +124 -0
- package/src/devtools/WorkbenchDevtoolsShell.tsx +17 -0
- package/src/devtools/index.ts +11 -0
- package/src/devtools/use-workbench-devtools-snapshot.ts +84 -0
- package/src/devtools/workbench-devtools-snapshot.ts +140 -0
- package/src/devtools/workbench-devtools.css +82 -0
- package/src/editor/area-dnd.ts +69 -0
- package/src/editor/area-model.ts +71 -0
- package/src/editor/area.css +454 -0
- package/src/editor/area.tsx +676 -0
- package/src/editor/host-surface.tsx +691 -0
- package/src/editor/markdown-document-view-data.ts +16 -0
- package/src/editor/pane-visibility.ts +130 -0
- package/src/editor/resource.ts +36 -0
- package/src/editor/state-storage.ts +182 -0
- package/src/editor/tab-context-menu.ts +118 -0
- package/src/editor/use-editor.ts +138 -0
- package/src/editor/view-providers.tsx +504 -0
- package/src/editor/workspace-file-availability.ts +15 -0
- package/src/editor/workspace-reconcile.tsx +31 -0
- package/src/explorer/context-menu.ts +114 -0
- package/src/explorer/create-command-workspace-explorer-port.ts +86 -0
- package/src/explorer/reveal.ts +112 -0
- package/src/explorer/search-view-data.ts +15 -0
- package/src/explorer/search-view.tsx +65 -0
- package/src/explorer/use-active-workspace-path.ts +15 -0
- package/src/explorer/view-data.ts +58 -0
- package/src/explorer/view.tsx +227 -0
- package/src/extensions/context-menu.ts +52 -0
- package/src/extensions/management-model.ts +313 -0
- package/src/extensions/use-extension-management.ts +241 -0
- package/src/extensions/view-data.ts +19 -0
- package/src/extensions/view.tsx +63 -0
- package/src/field-remap/chrome-labels.ts +74 -0
- package/src/field-remap/convert-note-editor.tsx +206 -0
- package/src/field-remap/convert-palette.tsx +126 -0
- package/src/field-remap/demo.tsx +105 -0
- package/src/field-remap/detail-panel.tsx +659 -0
- package/src/field-remap/editor-surface.tsx +36 -0
- package/src/field-remap/flow-adapter.ts +838 -0
- package/src/field-remap/flow-ops.ts +442 -0
- package/src/field-remap/flow.tsx +948 -0
- package/src/field-remap/graph.tsx +559 -0
- package/src/field-remap/index.ts +88 -0
- package/src/field-remap/io-class-browse.tsx +145 -0
- package/src/field-remap/jsonata-transform.ts +166 -0
- package/src/field-remap/panel.tsx +466 -0
- package/src/field-remap/samples.ts +507 -0
- package/src/field-remap/shape-io-editor.tsx +170 -0
- package/src/field-remap/table-mapping-adapter.ts +59 -0
- package/src/field-remap/transform-options-editor.tsx +312 -0
- package/src/field-remap/tree.tsx +404 -0
- package/src/field-remap/view-data.ts +16 -0
- package/src/field-remap/view.css +1367 -0
- package/src/field-remap/view.tsx +86 -0
- package/src/index.ts +294 -0
- package/src/jdw/document-view-data.ts +31 -0
- package/src/jdw/json-form-source-patch.ts +323 -0
- package/src/jdw/lab-view-data.ts +25 -0
- package/src/jdw/lab-view.css +21 -0
- package/src/jdw/lab-view.tsx +79 -0
- package/src/jdw/widget-form-view.tsx +68 -0
- package/src/jdw/widget-preview-view.tsx +54 -0
- package/src/management/keybinding-overrides-storage.ts +49 -0
- package/src/management/keybinding-settings.tsx +16 -0
- package/src/management/palette-commands.ts +38 -0
- package/src/management/preference-settings-storage.ts +50 -0
- package/src/management/settings-ids.ts +12 -0
- package/src/management/settings.tsx +96 -0
- package/src/management/use-command-management.ts +191 -0
- package/src/management/use-keybinding-management.ts +130 -0
- package/src/shell/model.tsx +149 -0
- package/src/shell/provider.tsx +606 -0
- package/src/shell/secondary-actions.tsx +59 -0
- package/src/shell/settings-constants.ts +9 -0
- package/src/shell/settings.tsx +599 -0
- package/src/shell/shell.tsx +680 -0
- package/src/shell/titlebar-layout-controls.tsx +4 -0
- package/src/shell/view-host.tsx +212 -0
- package/src/workbench/appearance-storage.ts +107 -0
- package/src/workbench/command-host.tsx +243 -0
- package/src/workbench/command-palette.ts +208 -0
- package/src/workbench/keybinding-bridge.ts +54 -0
- package/src/workbench/layout-storage.ts +134 -0
- package/src/workbench/profile-modal.tsx +118 -0
- package/src/workbench/shell-command-registration.ts +58 -0
- package/src/workbench/startup-gate.tsx +136 -0
- package/src/workbench/status-sections.ts +43 -0
- package/src/workbench/use-persisted-appearance.ts +33 -0
- package/src/workbench/user-commands.ts +46 -0
- package/src/workbench/workspace-view-state.ts +35 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export type EditorViewMode = 'code' | 'form' | 'preview';
|
|
2
|
+
|
|
3
|
+
export type EditorPaneKind = 'code' | 'form' | 'preview';
|
|
4
|
+
|
|
5
|
+
export interface EditorPaneVisibility {
|
|
6
|
+
readonly code: boolean;
|
|
7
|
+
readonly form: boolean;
|
|
8
|
+
readonly preview: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_EDITOR_PANE_VISIBILITY: EditorPaneVisibility = {
|
|
12
|
+
code: true,
|
|
13
|
+
form: false,
|
|
14
|
+
preview: false,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function editorViewModeToPaneVisibility(mode: EditorViewMode): EditorPaneVisibility {
|
|
18
|
+
switch (mode) {
|
|
19
|
+
case 'form':
|
|
20
|
+
return {
|
|
21
|
+
code: false,
|
|
22
|
+
form: true,
|
|
23
|
+
preview: false,
|
|
24
|
+
};
|
|
25
|
+
case 'preview':
|
|
26
|
+
return {
|
|
27
|
+
code: false,
|
|
28
|
+
form: false,
|
|
29
|
+
preview: true,
|
|
30
|
+
};
|
|
31
|
+
case 'code':
|
|
32
|
+
default:
|
|
33
|
+
return DEFAULT_EDITOR_PANE_VISIBILITY;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveDefaultEditorPaneVisibility(
|
|
38
|
+
defaultViewMode: EditorViewMode | undefined,
|
|
39
|
+
options: { readonly formEligible?: boolean | undefined } = {},
|
|
40
|
+
): EditorPaneVisibility {
|
|
41
|
+
if (defaultViewMode !== undefined) {
|
|
42
|
+
return editorViewModeToPaneVisibility(defaultViewMode);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (options.formEligible) {
|
|
46
|
+
return editorViewModeToPaneVisibility('form');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return DEFAULT_EDITOR_PANE_VISIBILITY;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Maps pane visibility to the exclusive icon-select value (viewport).
|
|
54
|
+
* If multiple panes are somehow visible, prefers preview → form → code.
|
|
55
|
+
*/
|
|
56
|
+
export function paneVisibilityToPrimaryViewMode(visibility: EditorPaneVisibility): EditorViewMode {
|
|
57
|
+
const exclusive =
|
|
58
|
+
Number(visibility.code) + Number(visibility.form) + Number(visibility.preview) === 1;
|
|
59
|
+
if (exclusive) {
|
|
60
|
+
if (visibility.preview) return 'preview';
|
|
61
|
+
if (visibility.form) return 'form';
|
|
62
|
+
return 'code';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (visibility.preview) return 'preview';
|
|
66
|
+
if (visibility.form) return 'form';
|
|
67
|
+
return 'code';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function countVisibleEditorPanes(visibility: EditorPaneVisibility): number {
|
|
71
|
+
return Number(visibility.code) + Number(visibility.form) + Number(visibility.preview);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function toggleEditorPaneVisibility(
|
|
75
|
+
visibility: EditorPaneVisibility,
|
|
76
|
+
pane: EditorPaneKind,
|
|
77
|
+
): EditorPaneVisibility {
|
|
78
|
+
const nextValue = !visibility[pane];
|
|
79
|
+
|
|
80
|
+
if (!nextValue && countVisibleEditorPanes(visibility) <= 1) {
|
|
81
|
+
return visibility;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
...visibility,
|
|
86
|
+
[pane]: nextValue,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function sanitizeEditorPaneVisibility(
|
|
91
|
+
visibility: EditorPaneVisibility,
|
|
92
|
+
options: { formEligible: boolean; previewEligible: boolean },
|
|
93
|
+
): EditorPaneVisibility {
|
|
94
|
+
return resolveEffectiveEditorPaneVisibility(visibility, options);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function resolveEffectiveEditorPaneVisibility(
|
|
98
|
+
preference: EditorPaneVisibility,
|
|
99
|
+
options: { formEligible: boolean; previewEligible: boolean },
|
|
100
|
+
): EditorPaneVisibility {
|
|
101
|
+
const effective: EditorPaneVisibility = {
|
|
102
|
+
code: preference.code,
|
|
103
|
+
form: preference.form && options.formEligible,
|
|
104
|
+
preview: preference.preview && options.previewEligible,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (countVisibleEditorPanes(effective) === 0) {
|
|
108
|
+
return DEFAULT_EDITOR_PANE_VISIBILITY;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return effective;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function getVisibleEditorPaneKinds(visibility: EditorPaneVisibility): EditorPaneKind[] {
|
|
115
|
+
const kinds: EditorPaneKind[] = [];
|
|
116
|
+
|
|
117
|
+
if (visibility.code) {
|
|
118
|
+
kinds.push('code');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (visibility.form) {
|
|
122
|
+
kinds.push('form');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (visibility.preview) {
|
|
126
|
+
kinds.push('preview');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return kinds;
|
|
130
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JDW_SCHEMA_DOCUMENT_FILE_EXTENSION,
|
|
3
|
+
JDW_SCHEMA_DOCUMENT_MIME,
|
|
4
|
+
JDW_WIDGET_DOCUMENT_FILE_EXTENSION,
|
|
5
|
+
JDW_WIDGET_DOCUMENT_MIME,
|
|
6
|
+
} from '@workbench-kit/react/jdw/document';
|
|
7
|
+
import { parseWorkspaceResourceUri } from '@workbench-kit/workspace';
|
|
8
|
+
|
|
9
|
+
export function pathForResource(resourceUri: string): string {
|
|
10
|
+
const workspaceUri = parseWorkspaceResourceUri(resourceUri);
|
|
11
|
+
return workspaceUri?.kind === 'file' ? workspaceUri.path : resourceUri;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function copyResourcePath(resourceUri: string): void {
|
|
15
|
+
const path = pathForResource(resourceUri);
|
|
16
|
+
const clipboard = globalThis.navigator?.clipboard;
|
|
17
|
+
if (!clipboard) return;
|
|
18
|
+
|
|
19
|
+
void clipboard.writeText(path).catch(() => undefined);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function mimeTypeForResource(resourceUri: string): string | undefined {
|
|
23
|
+
const path = pathForResource(resourceUri).toLowerCase();
|
|
24
|
+
|
|
25
|
+
if (path.endsWith(JDW_SCHEMA_DOCUMENT_FILE_EXTENSION)) return JDW_SCHEMA_DOCUMENT_MIME;
|
|
26
|
+
if (path.endsWith(JDW_WIDGET_DOCUMENT_FILE_EXTENSION)) return JDW_WIDGET_DOCUMENT_MIME;
|
|
27
|
+
if (path.endsWith('.json')) return 'application/json';
|
|
28
|
+
if (path.endsWith('.ts') || path.endsWith('.tsx')) return 'text/typescript';
|
|
29
|
+
if (path.endsWith('.js') || path.endsWith('.jsx')) return 'text/javascript';
|
|
30
|
+
if (path.endsWith('.css')) return 'text/css';
|
|
31
|
+
if (path.endsWith('.html')) return 'text/html';
|
|
32
|
+
if (path.endsWith('.md') || path.endsWith('.mdx')) return 'text/markdown';
|
|
33
|
+
if (path.endsWith('.yml') || path.endsWith('.yaml')) return 'text/yaml';
|
|
34
|
+
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createBrowserWorkbenchStorage,
|
|
3
|
+
type EditorGroupState,
|
|
4
|
+
type EditorLayoutDirection,
|
|
5
|
+
type EditorLayoutNode,
|
|
6
|
+
type EditorState,
|
|
7
|
+
type EditorTabState,
|
|
8
|
+
type WorkbenchStorageReader,
|
|
9
|
+
type WorkbenchStorageWriter,
|
|
10
|
+
} from '@workbench-kit/workbench-core';
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_WORKBENCH_EDITOR_STATE_STORAGE_KEY = 'workbench-kit/.workbench/editors';
|
|
13
|
+
|
|
14
|
+
export function isWorkbenchEditorStatePersistenceAvailable(): boolean {
|
|
15
|
+
return createBrowserWorkbenchStorage({ kind: 'local' }) !== undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function editorStateToStorageValue(state: EditorState): EditorState {
|
|
19
|
+
return {
|
|
20
|
+
activeGroupId: state.activeGroupId,
|
|
21
|
+
groups: state.groups.map((group) => ({
|
|
22
|
+
activeTabId: group.activeTabId,
|
|
23
|
+
id: group.id,
|
|
24
|
+
tabs: group.tabs.map((tab) => ({
|
|
25
|
+
dirty: false,
|
|
26
|
+
editorId: tab.editorId,
|
|
27
|
+
icon: tab.icon,
|
|
28
|
+
id: tab.id,
|
|
29
|
+
pinned: tab.pinned,
|
|
30
|
+
preview: tab.preview,
|
|
31
|
+
resourceUri: tab.resourceUri,
|
|
32
|
+
title: tab.title,
|
|
33
|
+
})),
|
|
34
|
+
})),
|
|
35
|
+
layout: cloneEditorLayoutForStorage(state.layout),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function readPersistedEditorState(
|
|
40
|
+
storageKey = DEFAULT_WORKBENCH_EDITOR_STATE_STORAGE_KEY,
|
|
41
|
+
storage?: WorkbenchStorageReader,
|
|
42
|
+
): EditorState | undefined {
|
|
43
|
+
const resolvedStorage = storage ?? createBrowserWorkbenchStorage({ kind: 'local' });
|
|
44
|
+
if (!resolvedStorage) return undefined;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const raw = resolvedStorage.getItem(storageKey);
|
|
48
|
+
if (!raw) return undefined;
|
|
49
|
+
|
|
50
|
+
return parseEditorStateStorageValue(JSON.parse(raw) as unknown);
|
|
51
|
+
} catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function writePersistedEditorState(
|
|
57
|
+
state: EditorState,
|
|
58
|
+
storageKey = DEFAULT_WORKBENCH_EDITOR_STATE_STORAGE_KEY,
|
|
59
|
+
storage?: WorkbenchStorageWriter,
|
|
60
|
+
): void {
|
|
61
|
+
const resolvedStorage = storage ?? createBrowserWorkbenchStorage({ kind: 'local' });
|
|
62
|
+
if (!resolvedStorage) return;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
resolvedStorage.setItem(storageKey, JSON.stringify(editorStateToStorageValue(state), null, 2));
|
|
66
|
+
} catch {
|
|
67
|
+
// Ignore quota and security errors so the shell keeps working offline.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseEditorStateStorageValue(value: unknown): EditorState | undefined {
|
|
72
|
+
if (!isRecord(value)) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const groups = Array.isArray(value.groups)
|
|
77
|
+
? value.groups.flatMap(parseEditorGroupStorageValue)
|
|
78
|
+
: [];
|
|
79
|
+
const layout = parseEditorLayoutStorageValue(value.layout);
|
|
80
|
+
if (groups.length === 0 || !layout) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
activeGroupId: typeof value.activeGroupId === 'string' ? value.activeGroupId : undefined,
|
|
86
|
+
groups,
|
|
87
|
+
layout,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function parseEditorGroupStorageValue(value: unknown): EditorGroupState[] {
|
|
92
|
+
if (!isRecord(value) || typeof value.id !== 'string' || !Array.isArray(value.tabs)) {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return [
|
|
97
|
+
{
|
|
98
|
+
activeTabId: typeof value.activeTabId === 'string' ? value.activeTabId : undefined,
|
|
99
|
+
id: value.id,
|
|
100
|
+
tabs: value.tabs.flatMap(parseEditorTabStorageValue),
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function parseEditorTabStorageValue(value: unknown): EditorTabState[] {
|
|
106
|
+
if (
|
|
107
|
+
!isRecord(value) ||
|
|
108
|
+
typeof value.id !== 'string' ||
|
|
109
|
+
typeof value.editorId !== 'string' ||
|
|
110
|
+
typeof value.resourceUri !== 'string'
|
|
111
|
+
) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return [
|
|
116
|
+
{
|
|
117
|
+
dirty: false,
|
|
118
|
+
editorId: value.editorId,
|
|
119
|
+
icon: typeof value.icon === 'string' ? value.icon : undefined,
|
|
120
|
+
id: value.id,
|
|
121
|
+
pinned: typeof value.pinned === 'boolean' ? value.pinned : true,
|
|
122
|
+
preview: typeof value.preview === 'boolean' ? value.preview : false,
|
|
123
|
+
resourceUri: value.resourceUri,
|
|
124
|
+
title: typeof value.title === 'string' ? value.title : undefined,
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function parseEditorLayoutStorageValue(value: unknown): EditorLayoutNode | undefined {
|
|
130
|
+
if (!isRecord(value) || typeof value.type !== 'string') {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (value.type === 'group') {
|
|
135
|
+
return typeof value.groupId === 'string'
|
|
136
|
+
? {
|
|
137
|
+
groupId: value.groupId,
|
|
138
|
+
type: 'group',
|
|
139
|
+
}
|
|
140
|
+
: undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (value.type !== 'split' || !Array.isArray(value.children)) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const direction = parseEditorLayoutDirection(value.direction);
|
|
148
|
+
const children = value.children
|
|
149
|
+
.map(parseEditorLayoutStorageValue)
|
|
150
|
+
.filter((child): child is EditorLayoutNode => child !== undefined);
|
|
151
|
+
if (!direction || children.length === 0) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
children,
|
|
157
|
+
direction,
|
|
158
|
+
...(typeof value.primarySizePercent === 'number' && Number.isFinite(value.primarySizePercent)
|
|
159
|
+
? { primarySizePercent: value.primarySizePercent }
|
|
160
|
+
: {}),
|
|
161
|
+
type: 'split',
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseEditorLayoutDirection(value: unknown): EditorLayoutDirection | undefined {
|
|
166
|
+
return value === 'horizontal' || value === 'vertical' ? value : undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function cloneEditorLayoutForStorage(layout: EditorLayoutNode): EditorLayoutNode {
|
|
170
|
+
if (layout.type === 'group') {
|
|
171
|
+
return { ...layout };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
...layout,
|
|
176
|
+
children: layout.children.map(cloneEditorLayoutForStorage),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
181
|
+
return typeof value === 'object' && value !== null;
|
|
182
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCommandRegistry,
|
|
3
|
+
executeCommand as executeRegisteredCommand,
|
|
4
|
+
resolveCommandMenuItems,
|
|
5
|
+
} from '@workbench-kit/platform';
|
|
6
|
+
import type { ContextMenuItem } from '@workbench-kit/react/overlay';
|
|
7
|
+
import {
|
|
8
|
+
WORKBENCH_COMMAND_SURFACE_EDITOR,
|
|
9
|
+
WORKBENCH_EDITOR_DELETE_COMMAND_ID,
|
|
10
|
+
commandMenuItemsToContextMenuItems,
|
|
11
|
+
createWorkbenchEditorCommands,
|
|
12
|
+
createWorkbenchEditorTabMenuEntries,
|
|
13
|
+
type WorkbenchEditorCommandContext,
|
|
14
|
+
} from '@workbench-kit/react/workbench/commands';
|
|
15
|
+
import type {
|
|
16
|
+
EditorService,
|
|
17
|
+
EditorTabState,
|
|
18
|
+
ExtensionRegistry,
|
|
19
|
+
} from '@workbench-kit/workbench-core';
|
|
20
|
+
|
|
21
|
+
import { copyResourcePath, pathForResource } from './resource.js';
|
|
22
|
+
import {
|
|
23
|
+
appendExtensionContextMenuItems,
|
|
24
|
+
createExtensionContextMenuItems,
|
|
25
|
+
} from '../extensions/context-menu.js';
|
|
26
|
+
|
|
27
|
+
const editorCommandRegistry = createCommandRegistry(createWorkbenchEditorCommands());
|
|
28
|
+
const editorTabMenuEntries = createWorkbenchEditorTabMenuEntries().filter((entry) =>
|
|
29
|
+
entry.type === 'separator'
|
|
30
|
+
? entry.id !== 'tab-danger-separator'
|
|
31
|
+
: entry.commandId !== WORKBENCH_EDITOR_DELETE_COMMAND_ID,
|
|
32
|
+
);
|
|
33
|
+
const EDITOR_TAB_CONTEXT_MENU = 'editor/tab/context';
|
|
34
|
+
const EDITOR_TAB_EXTENSION_MENU_SEPARATOR_ID = 'editor-tab-extension-separator';
|
|
35
|
+
|
|
36
|
+
export function createEditorTabContextMenuItems({
|
|
37
|
+
editorService,
|
|
38
|
+
executeExtensionCommand,
|
|
39
|
+
extensionRegistry,
|
|
40
|
+
groupId,
|
|
41
|
+
tab,
|
|
42
|
+
tabs,
|
|
43
|
+
}: {
|
|
44
|
+
editorService: EditorService;
|
|
45
|
+
executeExtensionCommand?: ((commandId: string) => unknown) | undefined;
|
|
46
|
+
extensionRegistry?: ExtensionRegistry | undefined;
|
|
47
|
+
groupId: string;
|
|
48
|
+
tab: EditorTabState;
|
|
49
|
+
tabs: readonly EditorTabState[];
|
|
50
|
+
}): ContextMenuItem[] {
|
|
51
|
+
const closeableTabs = tabs.filter((candidate) => candidate.id !== tab.id);
|
|
52
|
+
const context: WorkbenchEditorCommandContext = {
|
|
53
|
+
canCloseAll: tabs.length > 0,
|
|
54
|
+
canCloseOthers: closeableTabs.length > 0,
|
|
55
|
+
canClosePath: true,
|
|
56
|
+
canCopyPath: true,
|
|
57
|
+
canDeletePath: false,
|
|
58
|
+
canDiscardFile: false,
|
|
59
|
+
canSaveFile: false,
|
|
60
|
+
canSplitDown: true,
|
|
61
|
+
canSplitRight: true,
|
|
62
|
+
canTogglePinned: true,
|
|
63
|
+
closeAll: () => tabs.forEach((candidate) => editorService.closeEditor(candidate.id)),
|
|
64
|
+
closeOthers: () =>
|
|
65
|
+
closeableTabs.forEach((candidate) => editorService.closeEditor(candidate.id)),
|
|
66
|
+
closePath: () => editorService.closeEditor(tab.id),
|
|
67
|
+
copyPath: () => copyResourcePath(tab.resourceUri),
|
|
68
|
+
deletePath: () => undefined,
|
|
69
|
+
discardFile: () => undefined,
|
|
70
|
+
filePath: pathForResource(tab.resourceUri),
|
|
71
|
+
hasMultipleOpenFiles: tabs.length > 1,
|
|
72
|
+
hasOpenFiles: tabs.length > 0,
|
|
73
|
+
hasUnsavedChanges: tab.dirty,
|
|
74
|
+
isPinned: tab.pinned,
|
|
75
|
+
saveFile: () => undefined,
|
|
76
|
+
splitDown: () =>
|
|
77
|
+
editorService.splitEditor({
|
|
78
|
+
afterGroupId: groupId,
|
|
79
|
+
direction: 'vertical',
|
|
80
|
+
tabId: tab.id,
|
|
81
|
+
}),
|
|
82
|
+
splitRight: () =>
|
|
83
|
+
editorService.splitEditor({
|
|
84
|
+
afterGroupId: groupId,
|
|
85
|
+
direction: 'horizontal',
|
|
86
|
+
tabId: tab.id,
|
|
87
|
+
}),
|
|
88
|
+
togglePinned: () => editorService.togglePinnedEditor(tab.id),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const contextKeys = {
|
|
92
|
+
'editor.hasResource': Boolean(tab.resourceUri),
|
|
93
|
+
'editor.isDirty': tab.dirty,
|
|
94
|
+
'editor.isPinned': tab.pinned,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const baseItems = commandMenuItemsToContextMenuItems(
|
|
98
|
+
resolveCommandMenuItems({
|
|
99
|
+
context,
|
|
100
|
+
contextKeys,
|
|
101
|
+
entries: editorTabMenuEntries,
|
|
102
|
+
registry: editorCommandRegistry,
|
|
103
|
+
surface: WORKBENCH_COMMAND_SURFACE_EDITOR,
|
|
104
|
+
}),
|
|
105
|
+
(commandId) => executeRegisteredCommand(editorCommandRegistry, commandId, context, contextKeys),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return appendExtensionContextMenuItems(
|
|
109
|
+
baseItems,
|
|
110
|
+
createExtensionContextMenuItems({
|
|
111
|
+
contextKeys,
|
|
112
|
+
executeCommand: executeExtensionCommand,
|
|
113
|
+
extensionRegistry,
|
|
114
|
+
menu: EDITOR_TAB_CONTEXT_MENU,
|
|
115
|
+
}),
|
|
116
|
+
EDITOR_TAB_EXTENSION_MENU_SEPARATOR_ID,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { useEffect, useMemo, useReducer } from 'react';
|
|
2
|
+
import type { EditorHost, EditorService, EditorTabState } from '@workbench-kit/workbench-core';
|
|
3
|
+
|
|
4
|
+
import { useWorkbench } from '../shell/provider.js';
|
|
5
|
+
import type { EditorDocumentViewProvider } from './view-providers.js';
|
|
6
|
+
|
|
7
|
+
export function useEditorService(): EditorService {
|
|
8
|
+
return useWorkbench().editorService;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useEditorState(): ReturnType<EditorService['getState']> {
|
|
12
|
+
const editorService = useEditorService();
|
|
13
|
+
const forceRender = useForceRender();
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const disposable = editorService.onDidChangeEditors(forceRender);
|
|
17
|
+
return () => {
|
|
18
|
+
disposable.dispose();
|
|
19
|
+
};
|
|
20
|
+
}, [editorService, forceRender]);
|
|
21
|
+
|
|
22
|
+
return editorService.getState();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useActiveEditorTab(): EditorTabState | undefined {
|
|
26
|
+
const editorService = useEditorService();
|
|
27
|
+
const forceRender = useForceRender();
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const disposable = editorService.onDidChangeEditors(forceRender);
|
|
31
|
+
return () => {
|
|
32
|
+
disposable.dispose();
|
|
33
|
+
};
|
|
34
|
+
}, [editorService, forceRender]);
|
|
35
|
+
|
|
36
|
+
return editorService.getActiveTab();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function useEditorHost(tabId?: string): EditorHost | undefined {
|
|
40
|
+
const editorService = useEditorService();
|
|
41
|
+
const { extensionRegistry, waitForExtensionStartup } = useWorkbench();
|
|
42
|
+
const editorState = useEditorState();
|
|
43
|
+
const forceRender = useForceRender();
|
|
44
|
+
const activeGroup = editorState.groups.find((group) => group.id === editorState.activeGroupId);
|
|
45
|
+
const resolvedTabId = tabId ?? activeGroup?.activeTabId;
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const disposable = editorService.onDidChangeEditors(forceRender);
|
|
49
|
+
return () => {
|
|
50
|
+
disposable.dispose();
|
|
51
|
+
};
|
|
52
|
+
}, [editorService, forceRender]);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
let cancelled = false;
|
|
56
|
+
|
|
57
|
+
void waitForExtensionStartup().then(() => {
|
|
58
|
+
if (!cancelled) {
|
|
59
|
+
forceRender();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return () => {
|
|
64
|
+
cancelled = true;
|
|
65
|
+
};
|
|
66
|
+
}, [forceRender, waitForExtensionStartup]);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
// Late onView / onCommand activations register editor host factories after the
|
|
70
|
+
// first paint that restores persisted tabs. Retry host creation when they land.
|
|
71
|
+
const disposable = extensionRegistry.onDidActivateExtension(forceRender);
|
|
72
|
+
return () => {
|
|
73
|
+
disposable.dispose();
|
|
74
|
+
};
|
|
75
|
+
}, [extensionRegistry, forceRender]);
|
|
76
|
+
|
|
77
|
+
if (!resolvedTabId) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Do not memoize createEditorHost — a failed lookup before factory registration
|
|
82
|
+
// must be retried after extension activation / startup re-renders.
|
|
83
|
+
// EditorService already caches successful hosts per tab id.
|
|
84
|
+
return editorService.createEditorHost(resolvedTabId);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function useEditorDocumentViewProviders(
|
|
88
|
+
localProviders?: readonly EditorDocumentViewProvider[] | undefined,
|
|
89
|
+
): readonly EditorDocumentViewProvider[] {
|
|
90
|
+
const { editorDocumentViewProviders, extensionRegistry, waitForExtensionStartup } =
|
|
91
|
+
useWorkbench();
|
|
92
|
+
const forceRender = useForceRender();
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const disposable = editorDocumentViewProviders.onDidChangeProviders(forceRender);
|
|
96
|
+
return () => {
|
|
97
|
+
disposable.dispose();
|
|
98
|
+
};
|
|
99
|
+
}, [editorDocumentViewProviders, forceRender]);
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
let cancelled = false;
|
|
103
|
+
|
|
104
|
+
// Document view providers often register during onStartup activate().
|
|
105
|
+
// Re-render after startup so Form/Preview panes are not stuck code-only.
|
|
106
|
+
void waitForExtensionStartup().then(() => {
|
|
107
|
+
if (!cancelled) {
|
|
108
|
+
forceRender();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return () => {
|
|
113
|
+
cancelled = true;
|
|
114
|
+
};
|
|
115
|
+
}, [forceRender, waitForExtensionStartup]);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
const disposable = extensionRegistry.onDidActivateExtension(forceRender);
|
|
119
|
+
return () => {
|
|
120
|
+
disposable.dispose();
|
|
121
|
+
};
|
|
122
|
+
}, [extensionRegistry, forceRender]);
|
|
123
|
+
|
|
124
|
+
const registryProviders = editorDocumentViewProviders.getProviders();
|
|
125
|
+
|
|
126
|
+
return useMemo(
|
|
127
|
+
() =>
|
|
128
|
+
localProviders && localProviders.length > 0
|
|
129
|
+
? [...localProviders, ...registryProviders]
|
|
130
|
+
: registryProviders,
|
|
131
|
+
[localProviders, registryProviders],
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function useForceRender() {
|
|
136
|
+
const [, forceRender] = useReducer((count: number) => count + 1, 0);
|
|
137
|
+
return forceRender;
|
|
138
|
+
}
|