Skip to content

Commit bebb794

Browse files
authored
Update the monaco editor's JSON validator schema construction so that (#123)
* Update the monaco editor's JSON validator schema construction so that primitives are referenced from the local copy of the common_types and basic_catalog instead of the ones on the web. * refactor: remove unnecessary line break in buildValidationSchemas method signature
1 parent dc31ffa commit bebb794

1 file changed

Lines changed: 53 additions & 8 deletions

File tree

shell/src/app/shared/monaco-editor/monaco-editor.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@ import {
3535
AppConfigProvider,
3636
ThemePreference,
3737
} from '../../settings/app-config-provider/app-config-provider';
38+
import {COMMON_TYPES_SCHEMA} from '../../gallery/schema/common-types-schema';
39+
import {BASIC_CATALOG_SCHEMA} from '../../gallery/schema/basic-catalog-schema';
3840

3941
const LAYOUT_MODEL_URI = 'a2ui://layout.json';
4042

43+
/**
44+
* A standalone Angular component that wraps the Monaco Editor.
45+
*
46+
* This component provides an embedded code editor specifically configured for
47+
* editing A2UI layout JSON. It automatically synchronizes theme preferences
48+
* (dark/light mode) and integrates with the active A2UI catalog to provide
49+
* real-time schema validation and autocompletion for component properties.
50+
*/
4151
@Component({
4252
selector: 'a2ui-composer-monaco-editor',
4353
standalone: true,
@@ -145,13 +155,7 @@ export class MonacoEditor {
145155
const jsonContrib = (monacoInstance.languages as unknown as {json: typeof monaco.json}).json;
146156
jsonContrib.jsonDefaults.setDiagnosticsOptions({
147157
validate: true,
148-
schemas: [
149-
{
150-
uri: 'a2ui-catalog-schema',
151-
fileMatch: [LAYOUT_MODEL_URI],
152-
schema: layoutSchema,
153-
},
154-
],
158+
schemas: this.buildValidationSchemas(layoutSchema),
155159
});
156160
});
157161

@@ -192,7 +196,7 @@ export class MonacoEditor {
192196
}
193197

194198
const editor = monacoInstance.editor.create(this.editorContainer().nativeElement, {
195-
model: model,
199+
model,
196200
theme: this.monacoTheme(),
197201
automaticLayout: true,
198202
minimap: {enabled: false},
@@ -215,6 +219,47 @@ export class MonacoEditor {
215219
});
216220
}
217221

222+
/**
223+
* Constructs the array of JSON schemas provided to the Monaco Editor's JSON worker
224+
* for real-time validation and autocompletion.
225+
*
226+
* @param layoutSchema The dynamically generated schema representing the full A2UI layout
227+
* structure, including the active catalog's components.
228+
* @returns An array of schemas configured with URIs that match `$ref` references
229+
* within the component schemas. We map local constants (like COMMON_TYPES_SCHEMA)
230+
* to both `file:///` and HTTP URIs so the Monaco JSON worker can resolve them
231+
* synchronously without needing an external schema request service.
232+
*/
233+
private buildValidationSchemas(layoutSchema: unknown): monaco.json.DiagnosticsOptions['schemas'] {
234+
return [
235+
{
236+
uri: 'a2ui-catalog-schema',
237+
fileMatch: [LAYOUT_MODEL_URI],
238+
schema: layoutSchema,
239+
},
240+
{
241+
uri: 'file:///common_types.json',
242+
schema: COMMON_TYPES_SCHEMA,
243+
},
244+
{
245+
uri: 'file:///catalog.json',
246+
schema: BASIC_CATALOG_SCHEMA,
247+
},
248+
{
249+
uri: 'https://a2ui.org/specification/v0_9/common_types.json',
250+
schema: COMMON_TYPES_SCHEMA,
251+
},
252+
{
253+
uri: 'https://a2ui.org/specification/v0_9/catalog.json',
254+
schema: BASIC_CATALOG_SCHEMA,
255+
},
256+
{
257+
uri: 'https://a2ui.org/specification/v0_9/catalogs/basic/catalog.json',
258+
schema: BASIC_CATALOG_SCHEMA,
259+
},
260+
];
261+
}
262+
218263
private updateEditorContent(value: string): void {
219264
if (!this.editor || this.editor.getValue() === value) {
220265
return;

0 commit comments

Comments
 (0)