-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71a8983
commit 8387bfe
Showing
6 changed files
with
195 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,74 @@ | ||
import * as monaco from 'monaco-editor' | ||
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' | ||
import { configureMonacoYaml } from 'monaco-yaml' | ||
import YamlWorker from 'monaco-yaml/yaml.worker?worker' | ||
|
||
// The uri is used for the schema file match. | ||
const modelUri = monaco.Uri.parse('a://b/foo.yaml') | ||
window.MonacoEnvironment = { | ||
getWorker(moduleId, label) { | ||
switch (label) { | ||
case 'editorWorkerService': | ||
return new EditorWorker() | ||
case 'yaml': | ||
return new YamlWorker() | ||
default: | ||
throw new Error(`Unknown label ${label}`) | ||
} | ||
} | ||
} | ||
|
||
configureMonacoYaml(monaco, { | ||
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' | ||
} | ||
} | ||
} | ||
// If YAML file is opened matching this glob | ||
fileMatch: ['**/.prettierrc.*'], | ||
// Then this schema will be downloaded from the internet and used. | ||
uri: 'https://json.schemastore.org/prettierrc.json' | ||
}, | ||
{ | ||
// Id of the first schema | ||
uri: 'http://myserver/bar-schema.json', | ||
// If YAML file is opened matching this glob | ||
fileMatch: ['**/person.yaml'], | ||
// The following schema will be applied | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
q1: { | ||
enum: ['x1', 'x2'] | ||
name: { | ||
type: 'string', | ||
description: 'The person’s display name' | ||
}, | ||
age: { | ||
type: 'integer', | ||
description: 'How old is the person in years?' | ||
}, | ||
occupation: { | ||
enum: ['Delivery person', 'Software engineer', 'Astronaut'] | ||
} | ||
} | ||
} | ||
}, | ||
// And the following URI will be linked to as the source. | ||
uri: 'https://github.com/remcohaszing/monaco-yaml#usage' | ||
} | ||
] | ||
}) | ||
|
||
const value = 'p1: \np2: \n' | ||
const prettierc = monaco.editor.createModel( | ||
'singleQuote: true\nproseWrap: always\nsemi: yes\n', | ||
undefined, | ||
monaco.Uri.parse('file:///.prettierrc.yaml') | ||
) | ||
|
||
monaco.editor.create(document.getElementById('editor'), { | ||
monaco.editor.createModel( | ||
'name: John Doe\nage: 42\noccupation: Pirate\n', | ||
undefined, | ||
monaco.Uri.parse('file:///person.yaml') | ||
) | ||
|
||
const editor = monaco.editor.create(document.getElementById('editor'), { | ||
automaticLayout: true, | ||
model: monaco.editor.createModel(value, 'yaml', modelUri) | ||
model: prettierc | ||
}) | ||
|
||
const select = document.getElementById('model') | ||
select.addEventListener('change', () => { | ||
editor.setModel(monaco.editor.getModel(monaco.Uri.parse(select.value))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.