mirror of
https://github.com/danbulant/monaco-yaml
synced 2026-05-19 04:08:48 +00:00
For both the project and all exmaples the prerequisites, setup steps, and running instructions have been added.
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
import { editor, Uri } from 'monaco-editor';
|
|
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
|
|
import { setDiagnosticsOptions } from 'monaco-yaml';
|
|
import YamlWorker from 'monaco-yaml/lib/esm/yaml.worker?worker';
|
|
|
|
window.MonacoEnvironment = {
|
|
getWorker(moduleId, label) {
|
|
switch (label) {
|
|
case 'editorWorkerService':
|
|
return new EditorWorker();
|
|
case 'yaml':
|
|
return new YamlWorker();
|
|
default:
|
|
throw new Error(`Unknown label ${label}`);
|
|
}
|
|
},
|
|
};
|
|
|
|
// The uri is used for the schema file match.
|
|
const modelUri = Uri.parse('a://b/foo.yaml');
|
|
|
|
setDiagnosticsOptions({
|
|
enableSchemaRequest: true,
|
|
hover: true,
|
|
completion: true,
|
|
validate: true,
|
|
format: true,
|
|
schemas: [
|
|
{
|
|
// Id of the first schema
|
|
uri: 'http://myserver/foo-schema.json',
|
|
// Associate with our model
|
|
fileMatch: [String(modelUri)],
|
|
schema: {
|
|
type: 'object',
|
|
properties: {
|
|
p1: {
|
|
enum: ['v1', 'v2'],
|
|
},
|
|
p2: {
|
|
// Reference the second schema
|
|
$ref: 'http://myserver/bar-schema.json',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
// Id of the first schema
|
|
uri: 'http://myserver/bar-schema.json',
|
|
schema: {
|
|
type: 'object',
|
|
properties: {
|
|
q1: {
|
|
enum: ['x1', 'x2'],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
const value = 'p1: \np2: \n';
|
|
|
|
editor.create(document.getElementById('editor'), {
|
|
automaticLayout: true,
|
|
model: editor.createModel(value, 'yaml', modelUri),
|
|
});
|