@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,552 +1,552 @@
1
- import type { CSSProperties, ReactNode } from 'react';
2
- import {
3
- JDW_SCHEMA_DOCUMENT_MIME,
4
- JDW_WIDGET_DOCUMENT_MIME,
5
- isJdwDocumentPath,
6
- isJdwSchemaDocument,
7
- } from '@workbench-kit/react/jdw/document';
8
- import { parseJsonWidgetData } from '@workbench-kit/react/jdw/parse';
9
- import { ScrollArea, Select, TextInput } from '@workbench-kit/react/primitives';
10
- import { WorkbenchMarkdownPreview } from '@workbench-kit/react/workbench/markdown-preview';
11
- import {
12
- createEditorDocumentViewProviderRegistry as createCoreEditorDocumentViewProviderRegistry,
13
- EditorDocumentViewProviderRegistry,
14
- type CreateEditorDocumentViewProviderRegistryOptions as CoreCreateEditorDocumentViewProviderRegistryOptions,
15
- } from '@workbench-kit/workbench-core';
16
- import type {
17
- EditorDocumentContext,
18
- EditorDocumentViewKind,
19
- EditorDocumentViewProvider,
20
- EditorDocumentViewRenderContext,
21
- } from '@workbench-kit/workbench-core';
22
-
23
- import { JdwWidgetFormView } from './jdw-widget-form-view.js';
24
- import { JdwWidgetPreviewView } from './jdw-widget-preview-view.js';
25
- import { replaceJsonValueAtPath, type JsonFormPath } from './json-form-source-patch.js';
26
-
27
- type JsonPath = JsonFormPath;
28
-
29
- export type {
30
- EditorDocumentContext,
31
- EditorDocumentViewKind,
32
- EditorDocumentViewProvider,
33
- EditorDocumentViewRenderContext,
34
- };
35
- export { EditorDocumentViewProviderRegistry };
36
-
37
- export interface ResolvedEditorDocumentViews {
38
- readonly formProvider: EditorDocumentViewProvider | undefined;
39
- readonly previewProvider: EditorDocumentViewProvider | undefined;
40
- }
41
-
42
- export const JSON_FORM_PROVIDER_ID = 'workbench-kit.editor.form.json' as const;
43
- export const JDW_WIDGET_FORM_PROVIDER_ID = 'workbench-kit.editor.form.jdw-widget' as const;
44
- export const JDW_PREVIEW_PROVIDER_ID = 'workbench-kit.editor.preview.jdw' as const;
45
- export const MARKDOWN_PREVIEW_PROVIDER_ID = 'workbench-kit.editor.preview.markdown' as const;
46
-
47
- /**
48
- * Opt-in shallow top-level JSON object form.
49
- * Not included in {@link DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS} — plain JSON is
50
- * Code-only unless a host registers a dedicated form provider (or this one).
51
- */
52
- export const JSON_FORM_PROVIDER: EditorDocumentViewProvider = {
53
- id: JSON_FORM_PROVIDER_ID,
54
- kind: 'form',
55
- label: 'Form',
56
- priority: 0,
57
- matches: (document) =>
58
- !isJdwSchemaDocument(document) &&
59
- !isJdwDocument(document) &&
60
- !isJdwWidgetJson(document.content) &&
61
- (isJsonLikeDocument(document) || parseJsonObject(document.content) !== null),
62
- render: ({ document, onContentChange }) => (
63
- <JsonObjectFormView content={document.content} onContentChange={onContentChange} />
64
- ),
65
- };
66
-
67
- const JDW_WIDGET_FORM_PROVIDER: EditorDocumentViewProvider = {
68
- id: JDW_WIDGET_FORM_PROVIDER_ID,
69
- kind: 'form',
70
- label: 'Form',
71
- priority: 20,
72
- matches: (document) =>
73
- !isJdwSchemaDocument(document) &&
74
- (isJdwDocument(document) || isJdwWidgetJson(document.content)),
75
- render: ({ document, onContentChange }) => (
76
- <JdwWidgetFormView
77
- content={document.content}
78
- path={document.path}
79
- onContentChange={onContentChange}
80
- />
81
- ),
82
- };
83
-
84
- const JDW_PREVIEW_PROVIDER: EditorDocumentViewProvider = {
85
- id: JDW_PREVIEW_PROVIDER_ID,
86
- kind: 'preview',
87
- label: 'Preview',
88
- priority: 10,
89
- matches: (document) =>
90
- !isJdwSchemaDocument(document) &&
91
- (isJdwDocument(document) || isJdwWidgetJson(document.content)),
92
- render: ({ document }) => (
93
- <JdwWidgetPreviewView
94
- className="workbench-editor-area__jdw-preview-viewport"
95
- content={document.content}
96
- path={document.path}
97
- />
98
- ),
99
- };
100
-
101
- const MARKDOWN_PREVIEW_PROVIDER: EditorDocumentViewProvider = {
102
- id: MARKDOWN_PREVIEW_PROVIDER_ID,
103
- kind: 'preview',
104
- label: 'Preview',
105
- priority: 5,
106
- matches: isMarkdownDocument,
107
- render: ({ document }) => <WorkbenchMarkdownPreview source={document.content} />,
108
- };
109
-
110
- export const DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS: readonly EditorDocumentViewProvider[] = [
111
- JDW_WIDGET_FORM_PROVIDER,
112
- JDW_PREVIEW_PROVIDER,
113
- MARKDOWN_PREVIEW_PROVIDER,
114
- ];
115
-
116
- export interface CreateEditorDocumentViewProviderRegistryOptions extends CoreCreateEditorDocumentViewProviderRegistryOptions {
117
- readonly includeDefaultProviders?: boolean | undefined;
118
- }
119
-
120
- export function createEditorDocumentViewProviderRegistry(
121
- options: CreateEditorDocumentViewProviderRegistryOptions = {},
122
- ): EditorDocumentViewProviderRegistry {
123
- const providers = [
124
- ...(options.includeDefaultProviders === false ? [] : DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS),
125
- ...(options.providers ?? []),
126
- ];
127
- return createCoreEditorDocumentViewProviderRegistry({ providers });
128
- }
129
-
130
- export function resolveEditorDocumentViews(
131
- document: EditorDocumentContext,
132
- providers: readonly EditorDocumentViewProvider[] = DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS,
133
- ): ResolvedEditorDocumentViews {
134
- return {
135
- formProvider: resolveEditorDocumentViewProvider(document, 'form', providers),
136
- previewProvider: resolveEditorDocumentViewProvider(document, 'preview', providers),
137
- };
138
- }
139
-
140
- export function resolveEditorDocumentViewProvider(
141
- document: EditorDocumentContext,
142
- kind: EditorDocumentViewKind,
143
- providers: readonly EditorDocumentViewProvider[] = DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS,
144
- ): EditorDocumentViewProvider | undefined {
145
- return providers
146
- .filter(
147
- (provider) => provider.kind === kind && matchesEditorDocumentViewProvider(provider, document),
148
- )
149
- .sort((left, right) => (right.priority ?? 0) - (left.priority ?? 0))[0];
150
- }
151
-
152
- export function matchesEditorDocumentViewProvider(
153
- provider: EditorDocumentViewProvider,
154
- document: EditorDocumentContext,
155
- ): boolean {
156
- if (provider.matches) {
157
- return provider.matches(document);
158
- }
159
-
160
- const mimeType = document.mimeType?.toLowerCase();
161
- const path = normalizePath(document.path);
162
- const hasMimeTypes = Boolean(provider.mimeTypes?.length);
163
- const hasFilenamePatterns = Boolean(provider.filenamePatterns?.length);
164
-
165
- if (!hasMimeTypes && !hasFilenamePatterns) {
166
- return false;
167
- }
168
-
169
- const mimeMatches =
170
- !hasMimeTypes ||
171
- Boolean(provider.mimeTypes?.some((candidate) => candidate.toLowerCase() === mimeType));
172
- const pathMatches =
173
- !hasFilenamePatterns ||
174
- Boolean(provider.filenamePatterns?.some((pattern) => matchesFilenamePattern(pattern, path)));
175
-
176
- return mimeMatches && pathMatches;
177
- }
178
-
179
- function matchesFilenamePattern(pattern: string, path: string): boolean {
180
- const normalizedPattern = normalizePath(pattern);
181
- const segments = path.split('/');
182
- const target = normalizedPattern.includes('/') ? path : (segments[segments.length - 1] ?? path);
183
- const regex = new RegExp(`^${globToRegExpSource(normalizedPattern)}$`, 'i');
184
- return regex.test(target);
185
- }
186
-
187
- function globToRegExpSource(pattern: string): string {
188
- let source = '';
189
-
190
- for (let index = 0; index < pattern.length; index += 1) {
191
- const char = pattern[index];
192
- const nextChar = pattern[index + 1];
193
-
194
- if (char === '*' && nextChar === '*') {
195
- source += '.*';
196
- index += 1;
197
- continue;
198
- }
199
-
200
- if (char === '*') {
201
- source += '[^/]*';
202
- continue;
203
- }
204
-
205
- if (char === '?') {
206
- source += '[^/]';
207
- continue;
208
- }
209
-
210
- source += escapeRegExp(char ?? '');
211
- }
212
-
213
- return source;
214
- }
215
-
216
- function normalizePath(path: string): string {
217
- return path.split('\\').join('/');
218
- }
219
-
220
- function escapeRegExp(value: string): string {
221
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
222
- }
223
-
224
- function JsonObjectFormView({
225
- content,
226
- onContentChange,
227
- }: {
228
- content: string;
229
- onContentChange: (content: string) => void;
230
- }) {
231
- const parsed = parseJsonObject(content);
232
-
233
- const handleFieldChange = (path: JsonPath, nextValue: string) => {
234
- if (!parsed) {
235
- return;
236
- }
237
-
238
- const previousValue = getJsonPathValue(parsed, path);
239
- const coerced = coerceFormFieldValue(previousValue, nextValue);
240
- onContentChange(replaceJsonValueAtPath(content, path, coerced));
241
- };
242
-
243
- if (!parsed) {
244
- return (
245
- <ScrollArea className="workbench-editor-area__form-placeholder" orientation="vertical">
246
- <p>Form view is unavailable while the document is not valid JSON.</p>
247
- <p>Switch to Source to fix parse errors.</p>
248
- </ScrollArea>
249
- );
250
- }
251
-
252
- const entries = Object.entries(parsed);
253
-
254
- if (entries.length === 0) {
255
- return (
256
- <ScrollArea className="workbench-editor-area__form-placeholder" orientation="vertical">
257
- <p>Form view</p>
258
- <p>This JSON object has no top-level fields yet.</p>
259
- </ScrollArea>
260
- );
261
- }
262
-
263
- return (
264
- <ScrollArea
265
- as="form"
266
- className="workbench-editor-area__form"
267
- orientation="vertical"
268
- onSubmit={(event) => {
269
- event.preventDefault();
270
- }}
271
- >
272
- <div className="workbench-editor-area__form-grid">
273
- {entries.map(([key, value]) => (
274
- <JsonValueFormField
275
- key={key}
276
- depth={0}
277
- label={key}
278
- path={[key]}
279
- value={value}
280
- onFieldChange={handleFieldChange}
281
- />
282
- ))}
283
- </div>
284
- </ScrollArea>
285
- );
286
- }
287
-
288
- function JsonValueFormField({
289
- depth = 0,
290
- label,
291
- path,
292
- value,
293
- onFieldChange,
294
- }: {
295
- depth?: number;
296
- label: string;
297
- path: JsonPath;
298
- value: unknown;
299
- onFieldChange: (path: JsonPath, value: string) => void;
300
- }) {
301
- if (Array.isArray(value)) {
302
- return (
303
- <JsonFormGroup depth={depth} label={label} meta={`array (${value.length})`} path={path}>
304
- {value.length === 0 ? (
305
- <div className="workbench-editor-area__form-empty" style={formDepthStyle(depth + 1)}>
306
- Empty array
307
- </div>
308
- ) : (
309
- value.map((item, index) => (
310
- <JsonValueFormField
311
- key={index}
312
- depth={depth + 1}
313
- label={`[${index}]`}
314
- path={[...path, index]}
315
- value={item}
316
- onFieldChange={onFieldChange}
317
- />
318
- ))
319
- )}
320
- </JsonFormGroup>
321
- );
322
- }
323
-
324
- if (isJsonRecord(value)) {
325
- const entries = Object.entries(value);
326
-
327
- return (
328
- <JsonFormGroup depth={depth} label={label} meta={`object (${entries.length})`} path={path}>
329
- {entries.length === 0 ? (
330
- <div className="workbench-editor-area__form-empty" style={formDepthStyle(depth + 1)}>
331
- Empty object
332
- </div>
333
- ) : (
334
- entries.map(([key, childValue]) => (
335
- <JsonValueFormField
336
- key={key}
337
- depth={depth + 1}
338
- label={key}
339
- path={[...path, key]}
340
- value={childValue}
341
- onFieldChange={onFieldChange}
342
- />
343
- ))
344
- )}
345
- </JsonFormGroup>
346
- );
347
- }
348
-
349
- const fieldLabel = getJsonPathLabel(path);
350
- const labelStyle = formDepthStyle(depth);
351
-
352
- if (typeof value === 'boolean') {
353
- return (
354
- <label className="workbench-editor-area__form-field">
355
- <span className="workbench-editor-area__form-label" style={labelStyle}>
356
- {label}
357
- </span>
358
- <Select
359
- aria-label={fieldLabel}
360
- className="workbench-editor-area__form-control"
361
- controlWidth="full"
362
- value={String(value)}
363
- onValueChange={(nextValue) => {
364
- onFieldChange(path, nextValue);
365
- }}
366
- >
367
- <option value="true">true</option>
368
- <option value="false">false</option>
369
- </Select>
370
- </label>
371
- );
372
- }
373
-
374
- if (typeof value === 'string' || typeof value === 'number') {
375
- return (
376
- <label className="workbench-editor-area__form-field">
377
- <span className="workbench-editor-area__form-label" style={labelStyle}>
378
- {label}
379
- </span>
380
- <TextInput
381
- aria-label={fieldLabel}
382
- className="workbench-editor-area__form-control"
383
- controlWidth="full"
384
- onValueChange={(nextValue) => {
385
- onFieldChange(path, nextValue);
386
- }}
387
- step={typeof value === 'number' ? 'any' : undefined}
388
- type={typeof value === 'number' ? 'number' : 'text'}
389
- value={formatFormFieldValue(value)}
390
- />
391
- </label>
392
- );
393
- }
394
-
395
- return (
396
- <div className="workbench-editor-area__form-field">
397
- <span className="workbench-editor-area__form-label" style={labelStyle}>
398
- {label}
399
- </span>
400
- <output aria-label={fieldLabel} className="workbench-editor-area__form-readonly">
401
- {formatFormFieldValue(value)}
402
- </output>
403
- </div>
404
- );
405
- }
406
-
407
- function JsonFormGroup({
408
- children,
409
- depth,
410
- label,
411
- meta,
412
- path,
413
- }: {
414
- children: ReactNode;
415
- depth: number;
416
- label: string;
417
- meta: string;
418
- path: JsonPath;
419
- }) {
420
- return (
421
- <>
422
- <div
423
- aria-label={getJsonPathLabel(path)}
424
- className="workbench-editor-area__form-group-header"
425
- role="group"
426
- style={formDepthStyle(depth)}
427
- >
428
- <span className="workbench-editor-area__form-label">{label}</span>
429
- <span className="workbench-editor-area__form-badge">{meta}</span>
430
- </div>
431
- {children}
432
- </>
433
- );
434
- }
435
-
436
- function formDepthStyle(depth: number): CSSProperties {
437
- return {
438
- '--workbench-editor-form-depth': String(depth),
439
- } as CSSProperties;
440
- }
441
-
442
- function isJsonLikeDocument(document: EditorDocumentContext): boolean {
443
- const mimeType = document.mimeType?.toLowerCase();
444
- const path = document.path.toLowerCase();
445
-
446
- return (
447
- path.endsWith('.json') ||
448
- mimeType === 'application/json' ||
449
- mimeType === 'application/schema+json' ||
450
- mimeType === JDW_SCHEMA_DOCUMENT_MIME ||
451
- Boolean(mimeType?.endsWith('+json'))
452
- );
453
- }
454
-
455
- function isJdwDocument(document: EditorDocumentContext): boolean {
456
- return document.mimeType === JDW_WIDGET_DOCUMENT_MIME || isJdwDocumentPath(document.path);
457
- }
458
-
459
- function isMarkdownDocument(document: EditorDocumentContext): boolean {
460
- const mimeType = document.mimeType?.toLowerCase();
461
- const path = document.path.toLowerCase();
462
-
463
- return path.endsWith('.md') || path.endsWith('.mdx') || mimeType === 'text/markdown';
464
- }
465
-
466
- function parseJsonObject(content: string): Record<string, unknown> | null {
467
- const trimmed = content.trim();
468
- if (!trimmed) {
469
- return {};
470
- }
471
-
472
- try {
473
- const parsed: unknown = JSON.parse(trimmed);
474
- if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
475
- return null;
476
- }
477
-
478
- return parsed as Record<string, unknown>;
479
- } catch {
480
- return null;
481
- }
482
- }
483
-
484
- function isJsonRecord(value: unknown): value is Record<string, unknown> {
485
- return typeof value === 'object' && value !== null && !Array.isArray(value);
486
- }
487
-
488
- function isJdwWidgetJson(content: string): boolean {
489
- const parsed = parseJsonWidgetData(content);
490
- return parsed.value !== null;
491
- }
492
-
493
- function formatFormFieldValue(value: unknown): string {
494
- if (typeof value === 'string') {
495
- return value;
496
- }
497
-
498
- if (typeof value === 'number' || typeof value === 'boolean') {
499
- return String(value);
500
- }
501
-
502
- return JSON.stringify(value) ?? String(value);
503
- }
504
-
505
- function coerceFormFieldValue(previousValue: unknown, nextValue: string): unknown {
506
- if (typeof previousValue === 'number') {
507
- const parsedNumber = Number(nextValue);
508
- return Number.isNaN(parsedNumber) ? previousValue : parsedNumber;
509
- }
510
-
511
- if (typeof previousValue === 'boolean') {
512
- if (nextValue === 'true') {
513
- return true;
514
- }
515
-
516
- if (nextValue === 'false') {
517
- return false;
518
- }
519
- }
520
-
521
- return nextValue;
522
- }
523
-
524
- function getJsonPathValue(value: unknown, path: JsonPath): unknown {
525
- let cursor = value;
526
-
527
- for (const segment of path) {
528
- if (Array.isArray(cursor) && typeof segment === 'number') {
529
- cursor = cursor[segment];
530
- continue;
531
- }
532
-
533
- if (isJsonRecord(cursor) && typeof segment === 'string') {
534
- cursor = cursor[segment];
535
- continue;
536
- }
537
-
538
- return undefined;
539
- }
540
-
541
- return cursor;
542
- }
543
-
544
- function getJsonPathLabel(path: JsonPath): string {
545
- return path.reduce<string>((label, segment) => {
546
- if (typeof segment === 'number') {
547
- return `${label}[${segment}]`;
548
- }
549
-
550
- return label ? `${label}.${segment}` : segment;
551
- }, '');
552
- }
1
+ import type { CSSProperties, ReactNode } from 'react';
2
+ import {
3
+ JDW_SCHEMA_DOCUMENT_MIME,
4
+ JDW_WIDGET_DOCUMENT_MIME,
5
+ isJdwDocumentPath,
6
+ isJdwSchemaDocument,
7
+ } from '@workbench-kit/react/jdw/document';
8
+ import { parseJsonWidgetData } from '@workbench-kit/react/jdw/parse';
9
+ import { ScrollArea, Select, TextInput } from '@workbench-kit/react/primitives';
10
+ import { WorkbenchMarkdownPreview } from '@workbench-kit/react/workbench/markdown-preview';
11
+ import {
12
+ createEditorDocumentViewProviderRegistry as createCoreEditorDocumentViewProviderRegistry,
13
+ EditorDocumentViewProviderRegistry,
14
+ type CreateEditorDocumentViewProviderRegistryOptions as CoreCreateEditorDocumentViewProviderRegistryOptions,
15
+ } from '@workbench-kit/workbench-core';
16
+ import type {
17
+ EditorDocumentContext,
18
+ EditorDocumentViewKind,
19
+ EditorDocumentViewProvider,
20
+ EditorDocumentViewRenderContext,
21
+ } from '@workbench-kit/workbench-core';
22
+
23
+ import { JdwWidgetFormView } from '../jdw/widget-form-view.js';
24
+ import { JdwWidgetPreviewView } from '../jdw/widget-preview-view.js';
25
+ import { replaceJsonValueAtPath, type JsonFormPath } from '../jdw/json-form-source-patch.js';
26
+
27
+ type JsonPath = JsonFormPath;
28
+
29
+ export type {
30
+ EditorDocumentContext,
31
+ EditorDocumentViewKind,
32
+ EditorDocumentViewProvider,
33
+ EditorDocumentViewRenderContext,
34
+ };
35
+ export { EditorDocumentViewProviderRegistry };
36
+
37
+ export interface ResolvedEditorDocumentViews {
38
+ readonly formProvider: EditorDocumentViewProvider | undefined;
39
+ readonly previewProvider: EditorDocumentViewProvider | undefined;
40
+ }
41
+
42
+ export const JSON_FORM_PROVIDER_ID = 'workbench-kit.editor.form.json' as const;
43
+ export const JDW_WIDGET_FORM_PROVIDER_ID = 'workbench-kit.editor.form.jdw-widget' as const;
44
+ export const JDW_PREVIEW_PROVIDER_ID = 'workbench-kit.editor.preview.jdw' as const;
45
+ export const MARKDOWN_PREVIEW_PROVIDER_ID = 'workbench-kit.editor.preview.markdown' as const;
46
+
47
+ /**
48
+ * Opt-in shallow top-level JSON object form.
49
+ * Not included in {@link DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS} — plain JSON is
50
+ * Code-only unless a host registers a dedicated form provider (or this one).
51
+ */
52
+ export const JSON_FORM_PROVIDER: EditorDocumentViewProvider = {
53
+ id: JSON_FORM_PROVIDER_ID,
54
+ kind: 'form',
55
+ label: 'Form',
56
+ priority: 0,
57
+ matches: (document) =>
58
+ !isJdwSchemaDocument(document) &&
59
+ !isJdwDocument(document) &&
60
+ !isJdwWidgetJson(document.content) &&
61
+ (isJsonLikeDocument(document) || parseJsonObject(document.content) !== null),
62
+ render: ({ document, onContentChange }) => (
63
+ <JsonObjectFormView content={document.content} onContentChange={onContentChange} />
64
+ ),
65
+ };
66
+
67
+ const JDW_WIDGET_FORM_PROVIDER: EditorDocumentViewProvider = {
68
+ id: JDW_WIDGET_FORM_PROVIDER_ID,
69
+ kind: 'form',
70
+ label: 'Form',
71
+ priority: 20,
72
+ matches: (document) =>
73
+ !isJdwSchemaDocument(document) &&
74
+ (isJdwDocument(document) || isJdwWidgetJson(document.content)),
75
+ render: ({ document, onContentChange }) => (
76
+ <JdwWidgetFormView
77
+ content={document.content}
78
+ path={document.path}
79
+ onContentChange={onContentChange}
80
+ />
81
+ ),
82
+ };
83
+
84
+ const JDW_PREVIEW_PROVIDER: EditorDocumentViewProvider = {
85
+ id: JDW_PREVIEW_PROVIDER_ID,
86
+ kind: 'preview',
87
+ label: 'Preview',
88
+ priority: 10,
89
+ matches: (document) =>
90
+ !isJdwSchemaDocument(document) &&
91
+ (isJdwDocument(document) || isJdwWidgetJson(document.content)),
92
+ render: ({ document }) => (
93
+ <JdwWidgetPreviewView
94
+ className="workbench-editor-area__jdw-preview-viewport"
95
+ content={document.content}
96
+ path={document.path}
97
+ />
98
+ ),
99
+ };
100
+
101
+ const MARKDOWN_PREVIEW_PROVIDER: EditorDocumentViewProvider = {
102
+ id: MARKDOWN_PREVIEW_PROVIDER_ID,
103
+ kind: 'preview',
104
+ label: 'Preview',
105
+ priority: 5,
106
+ matches: isMarkdownDocument,
107
+ render: ({ document }) => <WorkbenchMarkdownPreview source={document.content} />,
108
+ };
109
+
110
+ export const DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS: readonly EditorDocumentViewProvider[] = [
111
+ JDW_WIDGET_FORM_PROVIDER,
112
+ JDW_PREVIEW_PROVIDER,
113
+ MARKDOWN_PREVIEW_PROVIDER,
114
+ ];
115
+
116
+ export interface CreateEditorDocumentViewProviderRegistryOptions extends CoreCreateEditorDocumentViewProviderRegistryOptions {
117
+ readonly includeDefaultProviders?: boolean | undefined;
118
+ }
119
+
120
+ export function createEditorDocumentViewProviderRegistry(
121
+ options: CreateEditorDocumentViewProviderRegistryOptions = {},
122
+ ): EditorDocumentViewProviderRegistry {
123
+ const providers = [
124
+ ...(options.includeDefaultProviders === false ? [] : DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS),
125
+ ...(options.providers ?? []),
126
+ ];
127
+ return createCoreEditorDocumentViewProviderRegistry({ providers });
128
+ }
129
+
130
+ export function resolveEditorDocumentViews(
131
+ document: EditorDocumentContext,
132
+ providers: readonly EditorDocumentViewProvider[] = DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS,
133
+ ): ResolvedEditorDocumentViews {
134
+ return {
135
+ formProvider: resolveEditorDocumentViewProvider(document, 'form', providers),
136
+ previewProvider: resolveEditorDocumentViewProvider(document, 'preview', providers),
137
+ };
138
+ }
139
+
140
+ export function resolveEditorDocumentViewProvider(
141
+ document: EditorDocumentContext,
142
+ kind: EditorDocumentViewKind,
143
+ providers: readonly EditorDocumentViewProvider[] = DEFAULT_EDITOR_DOCUMENT_VIEW_PROVIDERS,
144
+ ): EditorDocumentViewProvider | undefined {
145
+ return providers
146
+ .filter(
147
+ (provider) => provider.kind === kind && matchesEditorDocumentViewProvider(provider, document),
148
+ )
149
+ .sort((left, right) => (right.priority ?? 0) - (left.priority ?? 0))[0];
150
+ }
151
+
152
+ export function matchesEditorDocumentViewProvider(
153
+ provider: EditorDocumentViewProvider,
154
+ document: EditorDocumentContext,
155
+ ): boolean {
156
+ if (provider.matches) {
157
+ return provider.matches(document);
158
+ }
159
+
160
+ const mimeType = document.mimeType?.toLowerCase();
161
+ const path = normalizePath(document.path);
162
+ const hasMimeTypes = Boolean(provider.mimeTypes?.length);
163
+ const hasFilenamePatterns = Boolean(provider.filenamePatterns?.length);
164
+
165
+ if (!hasMimeTypes && !hasFilenamePatterns) {
166
+ return false;
167
+ }
168
+
169
+ const mimeMatches =
170
+ !hasMimeTypes ||
171
+ Boolean(provider.mimeTypes?.some((candidate) => candidate.toLowerCase() === mimeType));
172
+ const pathMatches =
173
+ !hasFilenamePatterns ||
174
+ Boolean(provider.filenamePatterns?.some((pattern) => matchesFilenamePattern(pattern, path)));
175
+
176
+ return mimeMatches && pathMatches;
177
+ }
178
+
179
+ function matchesFilenamePattern(pattern: string, path: string): boolean {
180
+ const normalizedPattern = normalizePath(pattern);
181
+ const segments = path.split('/');
182
+ const target = normalizedPattern.includes('/') ? path : (segments[segments.length - 1] ?? path);
183
+ const regex = new RegExp(`^${globToRegExpSource(normalizedPattern)}$`, 'i');
184
+ return regex.test(target);
185
+ }
186
+
187
+ function globToRegExpSource(pattern: string): string {
188
+ let source = '';
189
+
190
+ for (let index = 0; index < pattern.length; index += 1) {
191
+ const char = pattern[index];
192
+ const nextChar = pattern[index + 1];
193
+
194
+ if (char === '*' && nextChar === '*') {
195
+ source += '.*';
196
+ index += 1;
197
+ continue;
198
+ }
199
+
200
+ if (char === '*') {
201
+ source += '[^/]*';
202
+ continue;
203
+ }
204
+
205
+ if (char === '?') {
206
+ source += '[^/]';
207
+ continue;
208
+ }
209
+
210
+ source += escapeRegExp(char ?? '');
211
+ }
212
+
213
+ return source;
214
+ }
215
+
216
+ function normalizePath(path: string): string {
217
+ return path.split('\\').join('/');
218
+ }
219
+
220
+ function escapeRegExp(value: string): string {
221
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
222
+ }
223
+
224
+ function JsonObjectFormView({
225
+ content,
226
+ onContentChange,
227
+ }: {
228
+ content: string;
229
+ onContentChange: (content: string) => void;
230
+ }) {
231
+ const parsed = parseJsonObject(content);
232
+
233
+ const handleFieldChange = (path: JsonPath, nextValue: string) => {
234
+ if (!parsed) {
235
+ return;
236
+ }
237
+
238
+ const previousValue = getJsonPathValue(parsed, path);
239
+ const coerced = coerceFormFieldValue(previousValue, nextValue);
240
+ onContentChange(replaceJsonValueAtPath(content, path, coerced));
241
+ };
242
+
243
+ if (!parsed) {
244
+ return (
245
+ <ScrollArea className="workbench-editor-area__form-placeholder" orientation="vertical">
246
+ <p>Form view is unavailable while the document is not valid JSON.</p>
247
+ <p>Switch to Source to fix parse errors.</p>
248
+ </ScrollArea>
249
+ );
250
+ }
251
+
252
+ const entries = Object.entries(parsed);
253
+
254
+ if (entries.length === 0) {
255
+ return (
256
+ <ScrollArea className="workbench-editor-area__form-placeholder" orientation="vertical">
257
+ <p>Form view</p>
258
+ <p>This JSON object has no top-level fields yet.</p>
259
+ </ScrollArea>
260
+ );
261
+ }
262
+
263
+ return (
264
+ <ScrollArea
265
+ as="form"
266
+ className="workbench-editor-area__form"
267
+ orientation="vertical"
268
+ onSubmit={(event) => {
269
+ event.preventDefault();
270
+ }}
271
+ >
272
+ <div className="workbench-editor-area__form-grid">
273
+ {entries.map(([key, value]) => (
274
+ <JsonValueFormField
275
+ key={key}
276
+ depth={0}
277
+ label={key}
278
+ path={[key]}
279
+ value={value}
280
+ onFieldChange={handleFieldChange}
281
+ />
282
+ ))}
283
+ </div>
284
+ </ScrollArea>
285
+ );
286
+ }
287
+
288
+ function JsonValueFormField({
289
+ depth = 0,
290
+ label,
291
+ path,
292
+ value,
293
+ onFieldChange,
294
+ }: {
295
+ depth?: number;
296
+ label: string;
297
+ path: JsonPath;
298
+ value: unknown;
299
+ onFieldChange: (path: JsonPath, value: string) => void;
300
+ }) {
301
+ if (Array.isArray(value)) {
302
+ return (
303
+ <JsonFormGroup depth={depth} label={label} meta={`array (${value.length})`} path={path}>
304
+ {value.length === 0 ? (
305
+ <div className="workbench-editor-area__form-empty" style={formDepthStyle(depth + 1)}>
306
+ Empty array
307
+ </div>
308
+ ) : (
309
+ value.map((item, index) => (
310
+ <JsonValueFormField
311
+ key={index}
312
+ depth={depth + 1}
313
+ label={`[${index}]`}
314
+ path={[...path, index]}
315
+ value={item}
316
+ onFieldChange={onFieldChange}
317
+ />
318
+ ))
319
+ )}
320
+ </JsonFormGroup>
321
+ );
322
+ }
323
+
324
+ if (isJsonRecord(value)) {
325
+ const entries = Object.entries(value);
326
+
327
+ return (
328
+ <JsonFormGroup depth={depth} label={label} meta={`object (${entries.length})`} path={path}>
329
+ {entries.length === 0 ? (
330
+ <div className="workbench-editor-area__form-empty" style={formDepthStyle(depth + 1)}>
331
+ Empty object
332
+ </div>
333
+ ) : (
334
+ entries.map(([key, childValue]) => (
335
+ <JsonValueFormField
336
+ key={key}
337
+ depth={depth + 1}
338
+ label={key}
339
+ path={[...path, key]}
340
+ value={childValue}
341
+ onFieldChange={onFieldChange}
342
+ />
343
+ ))
344
+ )}
345
+ </JsonFormGroup>
346
+ );
347
+ }
348
+
349
+ const fieldLabel = getJsonPathLabel(path);
350
+ const labelStyle = formDepthStyle(depth);
351
+
352
+ if (typeof value === 'boolean') {
353
+ return (
354
+ <label className="workbench-editor-area__form-field">
355
+ <span className="workbench-editor-area__form-label" style={labelStyle}>
356
+ {label}
357
+ </span>
358
+ <Select
359
+ aria-label={fieldLabel}
360
+ className="workbench-editor-area__form-control"
361
+ controlWidth="full"
362
+ value={String(value)}
363
+ onValueChange={(nextValue) => {
364
+ onFieldChange(path, nextValue);
365
+ }}
366
+ >
367
+ <option value="true">true</option>
368
+ <option value="false">false</option>
369
+ </Select>
370
+ </label>
371
+ );
372
+ }
373
+
374
+ if (typeof value === 'string' || typeof value === 'number') {
375
+ return (
376
+ <label className="workbench-editor-area__form-field">
377
+ <span className="workbench-editor-area__form-label" style={labelStyle}>
378
+ {label}
379
+ </span>
380
+ <TextInput
381
+ aria-label={fieldLabel}
382
+ className="workbench-editor-area__form-control"
383
+ controlWidth="full"
384
+ onValueChange={(nextValue) => {
385
+ onFieldChange(path, nextValue);
386
+ }}
387
+ step={typeof value === 'number' ? 'any' : undefined}
388
+ type={typeof value === 'number' ? 'number' : 'text'}
389
+ value={formatFormFieldValue(value)}
390
+ />
391
+ </label>
392
+ );
393
+ }
394
+
395
+ return (
396
+ <div className="workbench-editor-area__form-field">
397
+ <span className="workbench-editor-area__form-label" style={labelStyle}>
398
+ {label}
399
+ </span>
400
+ <output aria-label={fieldLabel} className="workbench-editor-area__form-readonly">
401
+ {formatFormFieldValue(value)}
402
+ </output>
403
+ </div>
404
+ );
405
+ }
406
+
407
+ function JsonFormGroup({
408
+ children,
409
+ depth,
410
+ label,
411
+ meta,
412
+ path,
413
+ }: {
414
+ children: ReactNode;
415
+ depth: number;
416
+ label: string;
417
+ meta: string;
418
+ path: JsonPath;
419
+ }) {
420
+ return (
421
+ <>
422
+ <div
423
+ aria-label={getJsonPathLabel(path)}
424
+ className="workbench-editor-area__form-group-header"
425
+ role="group"
426
+ style={formDepthStyle(depth)}
427
+ >
428
+ <span className="workbench-editor-area__form-label">{label}</span>
429
+ <span className="workbench-editor-area__form-badge">{meta}</span>
430
+ </div>
431
+ {children}
432
+ </>
433
+ );
434
+ }
435
+
436
+ function formDepthStyle(depth: number): CSSProperties {
437
+ return {
438
+ '--workbench-editor-form-depth': String(depth),
439
+ } as CSSProperties;
440
+ }
441
+
442
+ function isJsonLikeDocument(document: EditorDocumentContext): boolean {
443
+ const mimeType = document.mimeType?.toLowerCase();
444
+ const path = document.path.toLowerCase();
445
+
446
+ return (
447
+ path.endsWith('.json') ||
448
+ mimeType === 'application/json' ||
449
+ mimeType === 'application/schema+json' ||
450
+ mimeType === JDW_SCHEMA_DOCUMENT_MIME ||
451
+ Boolean(mimeType?.endsWith('+json'))
452
+ );
453
+ }
454
+
455
+ function isJdwDocument(document: EditorDocumentContext): boolean {
456
+ return document.mimeType === JDW_WIDGET_DOCUMENT_MIME || isJdwDocumentPath(document.path);
457
+ }
458
+
459
+ function isMarkdownDocument(document: EditorDocumentContext): boolean {
460
+ const mimeType = document.mimeType?.toLowerCase();
461
+ const path = document.path.toLowerCase();
462
+
463
+ return path.endsWith('.md') || path.endsWith('.mdx') || mimeType === 'text/markdown';
464
+ }
465
+
466
+ function parseJsonObject(content: string): Record<string, unknown> | null {
467
+ const trimmed = content.trim();
468
+ if (!trimmed) {
469
+ return {};
470
+ }
471
+
472
+ try {
473
+ const parsed: unknown = JSON.parse(trimmed);
474
+ if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
475
+ return null;
476
+ }
477
+
478
+ return parsed as Record<string, unknown>;
479
+ } catch {
480
+ return null;
481
+ }
482
+ }
483
+
484
+ function isJsonRecord(value: unknown): value is Record<string, unknown> {
485
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
486
+ }
487
+
488
+ function isJdwWidgetJson(content: string): boolean {
489
+ const parsed = parseJsonWidgetData(content);
490
+ return parsed.value !== null;
491
+ }
492
+
493
+ function formatFormFieldValue(value: unknown): string {
494
+ if (typeof value === 'string') {
495
+ return value;
496
+ }
497
+
498
+ if (typeof value === 'number' || typeof value === 'boolean') {
499
+ return String(value);
500
+ }
501
+
502
+ return JSON.stringify(value) ?? String(value);
503
+ }
504
+
505
+ function coerceFormFieldValue(previousValue: unknown, nextValue: string): unknown {
506
+ if (typeof previousValue === 'number') {
507
+ const parsedNumber = Number(nextValue);
508
+ return Number.isNaN(parsedNumber) ? previousValue : parsedNumber;
509
+ }
510
+
511
+ if (typeof previousValue === 'boolean') {
512
+ if (nextValue === 'true') {
513
+ return true;
514
+ }
515
+
516
+ if (nextValue === 'false') {
517
+ return false;
518
+ }
519
+ }
520
+
521
+ return nextValue;
522
+ }
523
+
524
+ function getJsonPathValue(value: unknown, path: JsonPath): unknown {
525
+ let cursor = value;
526
+
527
+ for (const segment of path) {
528
+ if (Array.isArray(cursor) && typeof segment === 'number') {
529
+ cursor = cursor[segment];
530
+ continue;
531
+ }
532
+
533
+ if (isJsonRecord(cursor) && typeof segment === 'string') {
534
+ cursor = cursor[segment];
535
+ continue;
536
+ }
537
+
538
+ return undefined;
539
+ }
540
+
541
+ return cursor;
542
+ }
543
+
544
+ function getJsonPathLabel(path: JsonPath): string {
545
+ return path.reduce<string>((label, segment) => {
546
+ if (typeof segment === 'number') {
547
+ return `${label}[${segment}]`;
548
+ }
549
+
550
+ return label ? `${label}.${segment}` : segment;
551
+ }, '');
552
+ }