monaco-yaml/examples/react-webpack/index.jsx
Himanshu Kapoor fa72f5d6be
WIP: Dependency upgrades (#27)
* Update dependencies and remove language server

Dependency upgrades:
- monaco-editor 0.16.2 -> 0.20.0
- monaco-editor-core 0.16.1 -> 0.20.0
- monaco-languages 1.6.0 -> 1.10.0
- monaco-plugin-helpers 1.0.2 -> 1.0.3

Remove dependencies that are also dependencies of yaml-language-server

Remove all code from
- src/languageservice
- src/yaml-ast-parser-custom-tags
- test

* Update paths and deps in react examples

* fix: 🐛 fix package (#29)

Co-authored-by: DMY <147dmy@gmail.com>
2020-08-20 15:12:43 +05:30

75 lines
2.2 KiB
JavaScript

import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import MonacoEditor from 'react-monaco-editor';
import 'monaco-yaml/lib/esm/monaco.contribution';
import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
// NOTE: This will give you all editor featues. If you would prefer to limit to only the editor
// features you want to use, import them each individually. See this example: (https://github.com/microsoft/monaco-editor-samples/blob/master/browser-esm-webpack-small/index.js#L1-L91)
import 'monaco-editor';
window.MonacoEnvironment = {
getWorkerUrl(moduleId, label) {
if (label === 'yaml') {
return './yaml.worker.bundle.js';
}
return './editor.worker.bundle.js';
},
};
const { yaml } = languages || {};
const Editor = () => {
const [value, setValue] = useState('p1: ');
useEffect(() => {
yaml &&
yaml.yamlDefaults.setDiagnosticsOptions({
validate: true,
enableSchemaRequest: true,
hover: true,
completion: true,
schemas: [
{
uri: 'http://myserver/foo-schema.json', // id of the first schema
fileMatch: ['*'], // associate with our model
schema: {
id: 'http://myserver/foo-schema.json', // id of the first schema
type: 'object',
properties: {
p1: {
enum: ['v1', 'v2'],
},
p2: {
$ref: 'http://myserver/bar-schema.json', // reference the second schema
},
},
},
},
{
uri: 'http://myserver/bar-schema.json', // id of the first schema
schema: {
id: 'http://myserver/bar-schema.json', // id of the first schema
type: 'object',
properties: {
q1: {
enum: ['x1', 'x2'],
},
},
},
},
],
});
}, []);
return (
<MonacoEditor
width="800"
height="600"
language="yaml"
value={value}
onChange={setValue}
/>
);
};
ReactDOM.render(<Editor />, document.getElementById('react'));