@workbench-kit/shell-react 0.0.2-prototype.0.2.6 → 0.0.2-prototype.0.2.8

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 (95) hide show
  1. package/README.md +41 -0
  2. package/package.json +9 -9
  3. package/src/{chat-ai-mock-proposals.ts → chat/ai-mock-proposals.ts} +39 -39
  4. package/src/{chat-command-input.ts → chat/command-input.ts} +52 -52
  5. package/src/{chat-command-policy.ts → chat/command-policy.ts} +54 -54
  6. package/src/{chat-command-surface.tsx → chat/command-surface.tsx} +155 -155
  7. package/src/{use-workbench-chat-command-proposals.ts → chat/use-chat-command-proposals.ts} +194 -194
  8. package/src/{chat-view-data.ts → chat/view-data.ts} +20 -20
  9. package/src/{chat-view.tsx → chat/view.tsx} +220 -220
  10. package/src/{command-inspector-surface.tsx → commands/inspector-surface.tsx} +46 -46
  11. package/src/{use-workbench-command-descriptors.ts → commands/use-command-descriptors.ts} +41 -41
  12. package/src/{use-context-key-revision.ts → commands/use-context-key-revision.ts} +18 -18
  13. package/src/{commands-view-data.ts → commands/view-data.ts} +17 -17
  14. package/src/{commands-view.tsx → commands/view.tsx} +43 -43
  15. package/src/devtools/WorkbenchDevtoolsPanel.tsx +124 -124
  16. package/src/devtools/WorkbenchDevtoolsShell.tsx +17 -17
  17. package/src/devtools/index.ts +11 -11
  18. package/src/devtools/use-workbench-devtools-snapshot.ts +84 -84
  19. package/src/devtools/workbench-devtools-snapshot.ts +140 -140
  20. package/src/devtools/workbench-devtools.css +82 -82
  21. package/src/{editor-area-dnd.ts → editor/area-dnd.ts} +69 -69
  22. package/src/{editor-area-model.ts → editor/area-model.ts} +71 -71
  23. package/src/{editor-area.css → editor/area.css} +454 -454
  24. package/src/{editor-area.tsx → editor/area.tsx} +676 -676
  25. package/src/{editor-host-surface.tsx → editor/host-surface.tsx} +631 -634
  26. package/src/{editor-pane-visibility.ts → editor/pane-visibility.ts} +130 -130
  27. package/src/{editor-resource.ts → editor/resource.ts} +36 -36
  28. package/src/{editor-state-storage.ts → editor/state-storage.ts} +182 -193
  29. package/src/{editor-tab-context-menu.ts → editor/tab-context-menu.ts} +118 -118
  30. package/src/{use-editor.ts → editor/use-editor.ts} +114 -107
  31. package/src/{editor-view-providers.tsx → editor/view-providers.tsx} +552 -552
  32. package/src/{editor-workspace-file-availability.ts → editor/workspace-file-availability.ts} +15 -15
  33. package/src/{editor-workspace-reconcile.tsx → editor/workspace-reconcile.tsx} +31 -28
  34. package/src/{explorer-context-menu.ts → explorer/context-menu.ts} +114 -114
  35. package/src/{createCommandWorkspaceExplorerPort.ts → explorer/create-command-workspace-explorer-port.ts} +86 -86
  36. package/src/{explorer-reveal.ts → explorer/reveal.ts} +112 -112
  37. package/src/{search-view-data.ts → explorer/search-view-data.ts} +15 -15
  38. package/src/{search-view.tsx → explorer/search-view.tsx} +65 -62
  39. package/src/{use-active-workspace-path.ts → explorer/use-active-workspace-path.ts} +15 -15
  40. package/src/{explorer-view-data.ts → explorer/view-data.ts} +58 -58
  41. package/src/{explorer-view.tsx → explorer/view.tsx} +227 -224
  42. package/src/{extension-context-menu.ts → extensions/context-menu.ts} +52 -52
  43. package/src/{extension-management-model.ts → extensions/management-model.ts} +313 -313
  44. package/src/{use-extension-management.ts → extensions/use-extension-management.ts} +241 -193
  45. package/src/{extensions-view-data.ts → extensions/view-data.ts} +19 -19
  46. package/src/{extensions-view.tsx → extensions/view.tsx} +63 -38
  47. package/src/{field-remap-demo.tsx → field-remap/demo.tsx} +18 -18
  48. package/src/{field-remap-editor-surface.tsx → field-remap/editor-surface.tsx} +36 -36
  49. package/src/{field-remap-flow-adapter.ts → field-remap/flow-adapter.ts} +240 -240
  50. package/src/{field-remap-flow.tsx → field-remap/flow.tsx} +369 -369
  51. package/src/{field-remap-graph.tsx → field-remap/graph.tsx} +559 -559
  52. package/src/field-remap/jsonata-transform.ts +166 -0
  53. package/src/{field-remap-panel.tsx → field-remap/panel.tsx} +193 -175
  54. package/src/{field-remap-samples.ts → field-remap/samples.ts} +352 -352
  55. package/src/{table-mapping-adapter.ts → field-remap/table-mapping-adapter.ts} +59 -59
  56. package/src/{field-remap-tree.tsx → field-remap/tree.tsx} +404 -404
  57. package/src/{field-remap-view-data.ts → field-remap/view-data.ts} +16 -16
  58. package/src/{field-remap-view.css → field-remap/view.css} +676 -676
  59. package/src/{field-remap-view.tsx → field-remap/view.tsx} +86 -86
  60. package/src/index.ts +212 -199
  61. package/src/{json-form-source-patch.ts → jdw/json-form-source-patch.ts} +323 -323
  62. package/src/{jdw-lab-view-data.ts → jdw/lab-view-data.ts} +25 -25
  63. package/src/{jdw-lab-view.css → jdw/lab-view.css} +21 -21
  64. package/src/{jdw-lab-view.tsx → jdw/lab-view.tsx} +79 -79
  65. package/src/{jdw-widget-form-view.tsx → jdw/widget-form-view.tsx} +68 -65
  66. package/src/{jdw-widget-preview-view.tsx → jdw/widget-preview-view.tsx} +54 -51
  67. package/src/{keybinding-overrides-storage.ts → management/keybinding-overrides-storage.ts} +49 -57
  68. package/src/{keybinding-management-settings.tsx → management/keybinding-settings.tsx} +16 -16
  69. package/src/{management-palette-commands.ts → management/palette-commands.ts} +38 -38
  70. package/src/{preference-settings-storage.ts → management/preference-settings-storage.ts} +50 -58
  71. package/src/{management-settings-ids.ts → management/settings-ids.ts} +12 -12
  72. package/src/{management-settings.tsx → management/settings.tsx} +96 -75
  73. package/src/{use-command-management.ts → management/use-command-management.ts} +191 -191
  74. package/src/{use-keybinding-management.ts → management/use-keybinding-management.ts} +130 -130
  75. package/src/{shell-model.tsx → shell/model.tsx} +149 -149
  76. package/src/{provider.tsx → shell/provider.tsx} +603 -573
  77. package/src/{shell-secondary-actions.tsx → shell/secondary-actions.tsx} +59 -59
  78. package/src/{shell-settings-constants.ts → shell/settings-constants.ts} +9 -9
  79. package/src/{shell-settings.tsx → shell/settings.tsx} +599 -599
  80. package/src/{shell.tsx → shell/shell.tsx} +680 -649
  81. package/src/{shell-titlebar-layout-controls.tsx → shell/titlebar-layout-controls.tsx} +4 -4
  82. package/src/{shell-view-host.tsx → shell/view-host.tsx} +212 -199
  83. package/src/{workbench-appearance-storage.ts → workbench/appearance-storage.ts} +107 -115
  84. package/src/{workbench-command-host.tsx → workbench/command-host.tsx} +243 -243
  85. package/src/{workbench-command-palette.ts → workbench/command-palette.ts} +208 -208
  86. package/src/{workbench-keybinding-bridge.ts → workbench/keybinding-bridge.ts} +54 -54
  87. package/src/{workbench-layout-storage.ts → workbench/layout-storage.ts} +134 -145
  88. package/src/{workbench-profile-modal.tsx → workbench/profile-modal.tsx} +118 -118
  89. package/src/{workbench-shell-command-registration.ts → workbench/shell-command-registration.ts} +58 -58
  90. package/src/{workbench-startup-gate.tsx → workbench/startup-gate.tsx} +136 -136
  91. package/src/{workbench-status-sections.ts → workbench/status-sections.ts} +43 -43
  92. package/src/{use-persisted-workbench-appearance.ts → workbench/use-persisted-appearance.ts} +33 -33
  93. package/src/{workbench-user-commands.ts → workbench/user-commands.ts} +46 -46
  94. package/src/{workspace-view-state.ts → workbench/workspace-view-state.ts} +35 -35
  95. package/src/jsonata-transform.ts +0 -36
@@ -1,4 +1,4 @@
1
- export {
2
- WorkbenchShellTitleBarLayoutControls,
3
- type WorkbenchShellTitleBarLayoutControlsProps,
4
- } from '@workbench-kit/react/workbench/shell';
1
+ export {
2
+ WorkbenchShellTitleBarLayoutControls,
3
+ type WorkbenchShellTitleBarLayoutControlsProps,
4
+ } from '@workbench-kit/react/workbench/shell';
@@ -1,199 +1,212 @@
1
- import { isValidElement, useEffect, useMemo, useRef, type FocusEvent, type ReactNode } from 'react';
2
- import type {
3
- ExtensionRegistry,
4
- ViewHost,
5
- ViewHostFactoryRegistry,
6
- ViewProvider,
7
- } from '@workbench-kit/workbench-core';
8
-
9
- import { BuiltinChatView } from './chat-view.js';
10
- import { isBuiltinChatViewRenderData } from './chat-view-data.js';
11
- import { BuiltinCommandsView } from './commands-view.js';
12
- import { isBuiltinCommandsViewRenderData } from './commands-view-data.js';
13
- import { BuiltinExplorerView } from './explorer-view.js';
14
- import { isBuiltinExplorerViewRenderData } from './explorer-view-data.js';
15
- import { BuiltinExtensionsView } from './extensions-view.js';
16
- import { isBuiltinExtensionsViewRenderData } from './extensions-view-data.js';
17
- import { SampleJdwLabView } from './jdw-lab-view.js';
18
- import { isSampleJdwLabViewRenderData } from './jdw-lab-view-data.js';
19
- import { SampleFieldRemapView } from './field-remap-view.js';
20
- import { isSampleFieldRemapViewRenderData } from './field-remap-view-data.js';
21
- import { BuiltinSearchView } from './search-view.js';
22
- import { isBuiltinSearchViewRenderData } from './search-view-data.js';
23
-
24
- export function renderDefaultPrimarySidebar(
25
- extensionRegistry: Pick<ExtensionRegistry, 'viewHostFactories' | 'views'>,
26
- activeViewContainerId: string | undefined,
27
- catalogUrl?: string | undefined,
28
- ) {
29
- const views = activeViewContainerId
30
- ? extensionRegistry.views.getViews(activeViewContainerId)
31
- : extensionRegistry.views.getViews();
32
- if (views.length === 0) {
33
- return <aside aria-label="Primary sidebar" />;
34
- }
35
-
36
- return (
37
- <aside
38
- aria-label="Primary sidebar"
39
- className="workbench-primary-side-bar shell-react-primary-sidebar"
40
- >
41
- {views.map((view) => (
42
- <section key={view.id} data-view-id={view.id}>
43
- <WorkbenchViewHost
44
- catalogUrl={catalogUrl}
45
- fallback={view.name}
46
- provider={extensionRegistry.views.getViewProvider(view.id)}
47
- viewHostFactories={extensionRegistry.viewHostFactories}
48
- viewId={view.id}
49
- />
50
- </section>
51
- ))}
52
- </aside>
53
- );
54
- }
55
-
56
- export function WorkbenchViewHost({
57
- catalogUrl,
58
- fallback,
59
- provider,
60
- viewHostFactories,
61
- viewId,
62
- }: {
63
- catalogUrl?: string | undefined;
64
- fallback: ReactNode;
65
- provider: ViewProvider | undefined;
66
- viewHostFactories: ViewHostFactoryRegistry;
67
- viewId: string;
68
- }) {
69
- const hostFrameRef = useRef<HTMLDivElement>(null);
70
- const host = useMemo(() => {
71
- if (!provider) {
72
- return undefined;
73
- }
74
-
75
- return viewHostFactories.createViewHost({ viewId, provider });
76
- }, [provider, viewHostFactories, viewId]);
77
-
78
- useEffect(() => {
79
- if (!host) {
80
- return undefined;
81
- }
82
-
83
- host.onDidShow?.();
84
-
85
- const resizeObserver =
86
- typeof ResizeObserver !== 'undefined' && host.onDidResize
87
- ? new ResizeObserver((entries) => {
88
- const entry = entries[0];
89
- if (!entry) return;
90
-
91
- host.onDidResize?.({
92
- height: entry.contentRect.height,
93
- width: entry.contentRect.width,
94
- });
95
- })
96
- : undefined;
97
-
98
- if (resizeObserver && hostFrameRef.current) {
99
- resizeObserver.observe(hostFrameRef.current);
100
- }
101
-
102
- return () => {
103
- resizeObserver?.disconnect();
104
- host.onDidHide?.();
105
- host.dispose();
106
- };
107
- }, [host]);
108
-
109
- if (!host) {
110
- return <>{fallback}</>;
111
- }
112
-
113
- return (
114
- <div
115
- ref={hostFrameRef}
116
- aria-label={host.title}
117
- data-view-host-id={host.id ?? provider?.viewId}
118
- onBlur={(event) => notifyViewHostBlur(host, event)}
119
- onFocus={(event) => notifyViewHostFocus(host, event)}
120
- >
121
- {toWorkbenchViewHostReactNode(host.render(), fallback, { catalogUrl })}
122
- </div>
123
- );
124
- }
125
-
126
- export function notifyViewHostFocus(host: ViewHost, event: FocusEvent<HTMLDivElement>): void {
127
- if (!event.currentTarget.contains(event.relatedTarget)) {
128
- host.onDidFocus?.();
129
- }
130
- }
131
-
132
- export function notifyViewHostBlur(host: ViewHost, event: FocusEvent<HTMLDivElement>): void {
133
- if (!event.currentTarget.contains(event.relatedTarget)) {
134
- host.onDidBlur?.();
135
- }
136
- }
137
-
138
- export function toReactNode(value: unknown, fallback: ReactNode): ReactNode {
139
- if (
140
- value === undefined ||
141
- value === null ||
142
- typeof value === 'boolean' ||
143
- typeof value === 'symbol' ||
144
- typeof value === 'function'
145
- ) {
146
- return fallback;
147
- }
148
-
149
- if (typeof value === 'string' || typeof value === 'number' || isValidElement(value)) {
150
- return value;
151
- }
152
-
153
- if (Array.isArray(value)) {
154
- return value as ReactNode;
155
- }
156
-
157
- return fallback;
158
- }
159
-
160
- export function toWorkbenchViewHostReactNode(
161
- value: unknown,
162
- fallback: ReactNode,
163
- options: { catalogUrl?: string | undefined } = {},
164
- ): ReactNode {
165
- if (isBuiltinExplorerViewRenderData(value)) {
166
- return <BuiltinExplorerView />;
167
- }
168
-
169
- if (isBuiltinChatViewRenderData(value)) {
170
- return <BuiltinChatView mode={value.mode} />;
171
- }
172
-
173
- if (isBuiltinSearchViewRenderData(value)) {
174
- return <BuiltinSearchView />;
175
- }
176
-
177
- if (isBuiltinCommandsViewRenderData(value)) {
178
- return <BuiltinCommandsView />;
179
- }
180
-
181
- if (isBuiltinExtensionsViewRenderData(value)) {
182
- return <BuiltinExtensionsView catalogUrl={options.catalogUrl} />;
183
- }
184
-
185
- if (isSampleJdwLabViewRenderData(value)) {
186
- return (
187
- <SampleJdwLabView
188
- templateJdwPath={value.templateJdwPath}
189
- widgetTreePath={value.widgetTreePath}
190
- />
191
- );
192
- }
193
-
194
- if (isSampleFieldRemapViewRenderData(value)) {
195
- return <SampleFieldRemapView />;
196
- }
197
-
198
- return toReactNode(value, fallback);
199
- }
1
+ import { isValidElement, useEffect, useMemo, useRef, type FocusEvent, type ReactNode } from 'react';
2
+ import type {
3
+ ExtensionCatalogTrustPolicy,
4
+ ExtensionRegistry,
5
+ ViewHost,
6
+ ViewHostFactoryRegistry,
7
+ ViewProvider,
8
+ } from '@workbench-kit/workbench-core';
9
+
10
+ import { BuiltinChatView } from '../chat/view.js';
11
+ import { isBuiltinChatViewRenderData } from '../chat/view-data.js';
12
+ import { BuiltinCommandsView } from '../commands/view.js';
13
+ import { isBuiltinCommandsViewRenderData } from '../commands/view-data.js';
14
+ import { BuiltinExplorerView } from '../explorer/view.js';
15
+ import { isBuiltinExplorerViewRenderData } from '../explorer/view-data.js';
16
+ import { BuiltinExtensionsView } from '../extensions/view.js';
17
+ import { isBuiltinExtensionsViewRenderData } from '../extensions/view-data.js';
18
+ import { SampleJdwLabView } from '../jdw/lab-view.js';
19
+ import { isSampleJdwLabViewRenderData } from '../jdw/lab-view-data.js';
20
+ import { SampleFieldRemapView } from '../field-remap/view.js';
21
+ import { isSampleFieldRemapViewRenderData } from '../field-remap/view-data.js';
22
+ import { BuiltinSearchView } from '../explorer/search-view.js';
23
+ import { isBuiltinSearchViewRenderData } from '../explorer/search-view-data.js';
24
+
25
+ export function renderDefaultPrimarySidebar(
26
+ extensionRegistry: Pick<ExtensionRegistry, 'viewHostFactories' | 'views'>,
27
+ activeViewContainerId: string | undefined,
28
+ catalogUrl?: string | undefined,
29
+ catalogTrustPolicy?: ExtensionCatalogTrustPolicy | undefined,
30
+ ) {
31
+ const views = activeViewContainerId
32
+ ? extensionRegistry.views.getViews(activeViewContainerId)
33
+ : extensionRegistry.views.getViews();
34
+ if (views.length === 0) {
35
+ return <aside aria-label="Primary sidebar" />;
36
+ }
37
+
38
+ return (
39
+ <aside
40
+ aria-label="Primary sidebar"
41
+ className="workbench-primary-side-bar shell-react-primary-sidebar"
42
+ >
43
+ {views.map((view) => (
44
+ <section key={view.id} data-view-id={view.id}>
45
+ <WorkbenchViewHost
46
+ catalogTrustPolicy={catalogTrustPolicy}
47
+ catalogUrl={catalogUrl}
48
+ fallback={view.name}
49
+ provider={extensionRegistry.views.getViewProvider(view.id)}
50
+ viewHostFactories={extensionRegistry.viewHostFactories}
51
+ viewId={view.id}
52
+ />
53
+ </section>
54
+ ))}
55
+ </aside>
56
+ );
57
+ }
58
+
59
+ export function WorkbenchViewHost({
60
+ catalogTrustPolicy,
61
+ catalogUrl,
62
+ fallback,
63
+ provider,
64
+ viewHostFactories,
65
+ viewId,
66
+ }: {
67
+ catalogTrustPolicy?: ExtensionCatalogTrustPolicy | undefined;
68
+ catalogUrl?: string | undefined;
69
+ fallback: ReactNode;
70
+ provider: ViewProvider | undefined;
71
+ viewHostFactories: ViewHostFactoryRegistry;
72
+ viewId: string;
73
+ }) {
74
+ const hostFrameRef = useRef<HTMLDivElement>(null);
75
+ const host = useMemo(() => {
76
+ if (!provider) {
77
+ return undefined;
78
+ }
79
+
80
+ return viewHostFactories.createViewHost({ viewId, provider });
81
+ }, [provider, viewHostFactories, viewId]);
82
+
83
+ useEffect(() => {
84
+ if (!host) {
85
+ return undefined;
86
+ }
87
+
88
+ host.onDidShow?.();
89
+
90
+ const resizeObserver =
91
+ typeof ResizeObserver !== 'undefined' && host.onDidResize
92
+ ? new ResizeObserver((entries) => {
93
+ const entry = entries[0];
94
+ if (!entry) return;
95
+
96
+ host.onDidResize?.({
97
+ height: entry.contentRect.height,
98
+ width: entry.contentRect.width,
99
+ });
100
+ })
101
+ : undefined;
102
+
103
+ if (resizeObserver && hostFrameRef.current) {
104
+ resizeObserver.observe(hostFrameRef.current);
105
+ }
106
+
107
+ return () => {
108
+ resizeObserver?.disconnect();
109
+ host.onDidHide?.();
110
+ host.dispose();
111
+ };
112
+ }, [host]);
113
+
114
+ if (!host) {
115
+ return <>{fallback}</>;
116
+ }
117
+
118
+ return (
119
+ <div
120
+ ref={hostFrameRef}
121
+ aria-label={host.title}
122
+ data-view-host-id={host.id ?? provider?.viewId}
123
+ onBlur={(event) => notifyViewHostBlur(host, event)}
124
+ onFocus={(event) => notifyViewHostFocus(host, event)}
125
+ >
126
+ {toWorkbenchViewHostReactNode(host.render(), fallback, { catalogTrustPolicy, catalogUrl })}
127
+ </div>
128
+ );
129
+ }
130
+
131
+ export function notifyViewHostFocus(host: ViewHost, event: FocusEvent<HTMLDivElement>): void {
132
+ if (!event.currentTarget.contains(event.relatedTarget)) {
133
+ host.onDidFocus?.();
134
+ }
135
+ }
136
+
137
+ export function notifyViewHostBlur(host: ViewHost, event: FocusEvent<HTMLDivElement>): void {
138
+ if (!event.currentTarget.contains(event.relatedTarget)) {
139
+ host.onDidBlur?.();
140
+ }
141
+ }
142
+
143
+ export function toReactNode(value: unknown, fallback: ReactNode): ReactNode {
144
+ if (
145
+ value === undefined ||
146
+ value === null ||
147
+ typeof value === 'boolean' ||
148
+ typeof value === 'symbol' ||
149
+ typeof value === 'function'
150
+ ) {
151
+ return fallback;
152
+ }
153
+
154
+ if (typeof value === 'string' || typeof value === 'number' || isValidElement(value)) {
155
+ return value;
156
+ }
157
+
158
+ if (Array.isArray(value)) {
159
+ return value as ReactNode;
160
+ }
161
+
162
+ return fallback;
163
+ }
164
+
165
+ export function toWorkbenchViewHostReactNode(
166
+ value: unknown,
167
+ fallback: ReactNode,
168
+ options: {
169
+ catalogTrustPolicy?: ExtensionCatalogTrustPolicy | undefined;
170
+ catalogUrl?: string | undefined;
171
+ } = {},
172
+ ): ReactNode {
173
+ if (isBuiltinExplorerViewRenderData(value)) {
174
+ return <BuiltinExplorerView />;
175
+ }
176
+
177
+ if (isBuiltinChatViewRenderData(value)) {
178
+ return <BuiltinChatView mode={value.mode} />;
179
+ }
180
+
181
+ if (isBuiltinSearchViewRenderData(value)) {
182
+ return <BuiltinSearchView />;
183
+ }
184
+
185
+ if (isBuiltinCommandsViewRenderData(value)) {
186
+ return <BuiltinCommandsView />;
187
+ }
188
+
189
+ if (isBuiltinExtensionsViewRenderData(value)) {
190
+ return (
191
+ <BuiltinExtensionsView
192
+ catalogTrustPolicy={options.catalogTrustPolicy}
193
+ catalogUrl={options.catalogUrl}
194
+ />
195
+ );
196
+ }
197
+
198
+ if (isSampleJdwLabViewRenderData(value)) {
199
+ return (
200
+ <SampleJdwLabView
201
+ templateJdwPath={value.templateJdwPath}
202
+ widgetTreePath={value.widgetTreePath}
203
+ />
204
+ );
205
+ }
206
+
207
+ if (isSampleFieldRemapViewRenderData(value)) {
208
+ return <SampleFieldRemapView />;
209
+ }
210
+
211
+ return toReactNode(value, fallback);
212
+ }
@@ -1,115 +1,107 @@
1
- import {
2
- DEFAULT_DARK_THEME_PRESET,
3
- DEFAULT_LIGHT_THEME_PRESET,
4
- DEFAULT_SHELL_PRESET,
5
- isShellPresetId,
6
- type WorkbenchAppearanceSettings,
7
- type WorkbenchColorSchemePreference,
8
- } from '@workbench-kit/react/workbench';
9
- import type { WorkbenchStorageReader, WorkbenchStorageWriter } from '@workbench-kit/workbench-core';
10
-
11
- export type { WorkbenchAppearanceSettings } from '@workbench-kit/react/workbench/themePresets';
12
-
13
- export const DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY = 'workbench-kit/.workbench/appearance';
14
-
15
- export const DEFAULT_WORKBENCH_APPEARANCE: WorkbenchAppearanceSettings = {
16
- darkPreset: DEFAULT_DARK_THEME_PRESET,
17
- lightPreset: DEFAULT_LIGHT_THEME_PRESET,
18
- shellPreset: DEFAULT_SHELL_PRESET,
19
- themePreference: 'system',
20
- };
21
-
22
- export function isWorkbenchAppearancePersistenceAvailable(): boolean {
23
- try {
24
- return typeof globalThis.localStorage !== 'undefined';
25
- } catch {
26
- return false;
27
- }
28
- }
29
-
30
- export function readPersistedWorkbenchAppearance(
31
- storageKey = DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY,
32
- storage?: WorkbenchStorageReader,
33
- ): WorkbenchAppearanceSettings {
34
- const resolvedStorage = storage ?? getBrowserLocalStorage();
35
- if (!resolvedStorage) {
36
- return DEFAULT_WORKBENCH_APPEARANCE;
37
- }
38
-
39
- try {
40
- const raw = resolvedStorage.getItem(storageKey);
41
- if (!raw) {
42
- return DEFAULT_WORKBENCH_APPEARANCE;
43
- }
44
-
45
- return normalizeWorkbenchAppearance(JSON.parse(raw) as unknown);
46
- } catch {
47
- return DEFAULT_WORKBENCH_APPEARANCE;
48
- }
49
- }
50
-
51
- export function writePersistedWorkbenchAppearance(
52
- settings: WorkbenchAppearanceSettings,
53
- storageKey = DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY,
54
- storage?: WorkbenchStorageWriter,
55
- ): void {
56
- const resolvedStorage = storage ?? getBrowserLocalStorage();
57
- if (!resolvedStorage) {
58
- return;
59
- }
60
-
61
- try {
62
- resolvedStorage.setItem(storageKey, JSON.stringify(settings, null, 2));
63
- } catch {
64
- // Ignore quota and security errors so the shell keeps working offline.
65
- }
66
- }
67
-
68
- function normalizeWorkbenchAppearance(value: unknown): WorkbenchAppearanceSettings {
69
- if (typeof value !== 'object' || value === null) {
70
- return DEFAULT_WORKBENCH_APPEARANCE;
71
- }
72
-
73
- const record = value as Record<string, unknown>;
74
- const themePreference = normalizeThemePreference(record.themePreference);
75
- // Preset ids may belong to a contributed theme, which isn't known until extensions
76
- // register, so we can only validate shape here; the appearance UI falls back to a
77
- // default option if the persisted id no longer resolves to anything.
78
- const lightPreset = normalizePresetId(
79
- record.lightPreset,
80
- DEFAULT_WORKBENCH_APPEARANCE.lightPreset,
81
- );
82
- const darkPreset = normalizePresetId(record.darkPreset, DEFAULT_WORKBENCH_APPEARANCE.darkPreset);
83
- const shellPreset = normalizeShellPresetId(record.shellPreset);
84
-
85
- return {
86
- darkPreset,
87
- lightPreset,
88
- shellPreset,
89
- themePreference,
90
- };
91
- }
92
-
93
- function normalizeShellPresetId(value: unknown): string {
94
- return typeof value === 'string' && isShellPresetId(value) ? value : DEFAULT_SHELL_PRESET;
95
- }
96
-
97
- function normalizePresetId(value: unknown, fallback: string): string {
98
- return typeof value === 'string' && value.length > 0 ? value : fallback;
99
- }
100
-
101
- function normalizeThemePreference(value: unknown): WorkbenchColorSchemePreference {
102
- if (value === 'system' || value === 'light' || value === 'dark') {
103
- return value;
104
- }
105
-
106
- return DEFAULT_WORKBENCH_APPEARANCE.themePreference;
107
- }
108
-
109
- function getBrowserLocalStorage(): (WorkbenchStorageReader & WorkbenchStorageWriter) | undefined {
110
- if (!isWorkbenchAppearancePersistenceAvailable()) {
111
- return undefined;
112
- }
113
-
114
- return globalThis.localStorage;
115
- }
1
+ import {
2
+ DEFAULT_DARK_THEME_PRESET,
3
+ DEFAULT_LIGHT_THEME_PRESET,
4
+ DEFAULT_SHELL_PRESET,
5
+ isShellPresetId,
6
+ type WorkbenchAppearanceSettings,
7
+ type WorkbenchColorSchemePreference,
8
+ } from '@workbench-kit/react/workbench';
9
+ import {
10
+ createBrowserWorkbenchStorage,
11
+ type WorkbenchStorageReader,
12
+ type WorkbenchStorageWriter,
13
+ } from '@workbench-kit/workbench-core';
14
+
15
+ export type { WorkbenchAppearanceSettings } from '@workbench-kit/react/workbench/themePresets';
16
+
17
+ export const DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY = 'workbench-kit/.workbench/appearance';
18
+
19
+ export const DEFAULT_WORKBENCH_APPEARANCE: WorkbenchAppearanceSettings = {
20
+ darkPreset: DEFAULT_DARK_THEME_PRESET,
21
+ lightPreset: DEFAULT_LIGHT_THEME_PRESET,
22
+ shellPreset: DEFAULT_SHELL_PRESET,
23
+ themePreference: 'system',
24
+ };
25
+
26
+ export function isWorkbenchAppearancePersistenceAvailable(): boolean {
27
+ return createBrowserWorkbenchStorage({ kind: 'local' }) !== undefined;
28
+ }
29
+
30
+ export function readPersistedWorkbenchAppearance(
31
+ storageKey = DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY,
32
+ storage?: WorkbenchStorageReader,
33
+ ): WorkbenchAppearanceSettings {
34
+ const resolvedStorage = storage ?? createBrowserWorkbenchStorage({ kind: 'local' });
35
+ if (!resolvedStorage) {
36
+ return DEFAULT_WORKBENCH_APPEARANCE;
37
+ }
38
+
39
+ try {
40
+ const raw = resolvedStorage.getItem(storageKey);
41
+ if (!raw) {
42
+ return DEFAULT_WORKBENCH_APPEARANCE;
43
+ }
44
+
45
+ return normalizeWorkbenchAppearance(JSON.parse(raw) as unknown);
46
+ } catch {
47
+ return DEFAULT_WORKBENCH_APPEARANCE;
48
+ }
49
+ }
50
+
51
+ export function writePersistedWorkbenchAppearance(
52
+ settings: WorkbenchAppearanceSettings,
53
+ storageKey = DEFAULT_WORKBENCH_APPEARANCE_STORAGE_KEY,
54
+ storage?: WorkbenchStorageWriter,
55
+ ): void {
56
+ const resolvedStorage = storage ?? createBrowserWorkbenchStorage({ kind: 'local' });
57
+ if (!resolvedStorage) {
58
+ return;
59
+ }
60
+
61
+ try {
62
+ resolvedStorage.setItem(storageKey, JSON.stringify(settings, null, 2));
63
+ } catch {
64
+ // Ignore quota and security errors so the shell keeps working offline.
65
+ }
66
+ }
67
+
68
+ function normalizeWorkbenchAppearance(value: unknown): WorkbenchAppearanceSettings {
69
+ if (typeof value !== 'object' || value === null) {
70
+ return DEFAULT_WORKBENCH_APPEARANCE;
71
+ }
72
+
73
+ const record = value as Record<string, unknown>;
74
+ const themePreference = normalizeThemePreference(record.themePreference);
75
+ // Preset ids may belong to a contributed theme, which isn't known until extensions
76
+ // register, so we can only validate shape here; the appearance UI falls back to a
77
+ // default option if the persisted id no longer resolves to anything.
78
+ const lightPreset = normalizePresetId(
79
+ record.lightPreset,
80
+ DEFAULT_WORKBENCH_APPEARANCE.lightPreset,
81
+ );
82
+ const darkPreset = normalizePresetId(record.darkPreset, DEFAULT_WORKBENCH_APPEARANCE.darkPreset);
83
+ const shellPreset = normalizeShellPresetId(record.shellPreset);
84
+
85
+ return {
86
+ darkPreset,
87
+ lightPreset,
88
+ shellPreset,
89
+ themePreference,
90
+ };
91
+ }
92
+
93
+ function normalizeShellPresetId(value: unknown): string {
94
+ return typeof value === 'string' && isShellPresetId(value) ? value : DEFAULT_SHELL_PRESET;
95
+ }
96
+
97
+ function normalizePresetId(value: unknown, fallback: string): string {
98
+ return typeof value === 'string' && value.length > 0 ? value : fallback;
99
+ }
100
+
101
+ function normalizeThemePreference(value: unknown): WorkbenchColorSchemePreference {
102
+ if (value === 'system' || value === 'light' || value === 'dark') {
103
+ return value;
104
+ }
105
+
106
+ return DEFAULT_WORKBENCH_APPEARANCE.themePreference;
107
+ }