@snack-uikit/code-editor 0.4.6 → 0.4.7-preview-9b2da27a.0
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.
- package/README.md +0 -23
- package/dist/cjs/components/CodeEditor.d.ts +2 -2
- package/dist/cjs/components/CodeEditor.js +14 -4
- package/dist/cjs/components/constants.d.ts +5 -0
- package/dist/cjs/components/constants.js +6 -1
- package/dist/cjs/components/hooks.d.ts +2 -0
- package/dist/cjs/components/hooks.js +21 -3
- package/dist/cjs/components/index.d.ts +1 -0
- package/dist/cjs/components/index.js +2 -1
- package/dist/cjs/components/types.d.ts +9 -0
- package/dist/cjs/components/types.js +5 -0
- package/dist/esm/components/CodeEditor.d.ts +2 -2
- package/dist/esm/components/CodeEditor.js +11 -6
- package/dist/esm/components/constants.d.ts +5 -0
- package/dist/esm/components/constants.js +5 -0
- package/dist/esm/components/hooks.d.ts +2 -0
- package/dist/esm/components/hooks.js +17 -0
- package/dist/esm/components/index.d.ts +1 -0
- package/dist/esm/components/index.js +1 -0
- package/dist/esm/components/types.d.ts +9 -0
- package/dist/esm/components/types.js +1 -0
- package/package.json +4 -3
- package/src/components/CodeEditor.tsx +22 -6
- package/src/components/constants.ts +6 -0
- package/src/components/hooks.ts +20 -0
- package/src/components/index.ts +1 -0
- package/src/components/types.ts +11 -0
package/README.md
CHANGED
|
@@ -12,29 +12,6 @@
|
|
|
12
12
|
### Props
|
|
13
13
|
| name | type | default value | description |
|
|
14
14
|
|------|------|---------------|-------------|
|
|
15
|
-
| themeName | `string` | - | Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей. |
|
|
16
|
-
| hasBackground | `boolean` | true | Включение/отключение псевдобекграунда |
|
|
17
|
-
| defaultValue | `string` | - | Default value of the current model |
|
|
18
|
-
| defaultLanguage | `string` | - | Default language of the current model |
|
|
19
|
-
| defaultPath | `string` | - | Default path of the current model Will be passed as the third argument to `.createModel` method `monaco.editor.createModel(..., ..., monaco.Uri.parse(defaultPath))` |
|
|
20
|
-
| value | `string` | - | Value of the current model |
|
|
21
|
-
| language | `string` | - | Language of the current model |
|
|
22
|
-
| path | `string` | - | Path of the current model Will be passed as the third argument to `.createModel` method `monaco.editor.createModel(..., ..., monaco.Uri.parse(defaultPath))` |
|
|
23
|
-
| theme | `string` | "light" | The theme for the monaco Available options "vs-dark" \| "light" Define new themes by `monaco.editor.defineTheme` |
|
|
24
|
-
| line | `number` | - | The line to jump on it |
|
|
25
|
-
| loading | `ReactNode` | "Loading..." | The loading screen before the editor will be mounted |
|
|
26
|
-
| options | `IStandaloneEditorConstructionOptions` | - | IStandaloneEditorConstructionOptions |
|
|
27
|
-
| overrideServices | `IEditorOverrideServices` | - | IEditorOverrideServices |
|
|
28
|
-
| saveViewState | `boolean` | - | Indicator whether to save the models' view states between model changes or not Defaults to true |
|
|
29
|
-
| keepCurrentModel | `boolean` | false | Indicator whether to dispose the current model when the Editor is unmounted or not |
|
|
30
|
-
| width | `string \| number` | "100%" | Width of the editor wrapper |
|
|
31
|
-
| height | `string \| number` | "100%" | Height of the editor wrapper |
|
|
32
|
-
| className | `string` | - | Class name for the editor container |
|
|
33
|
-
| wrapperProps | `object` | - | Props applied to the wrapper element |
|
|
34
|
-
| beforeMount | `BeforeMount` | - | Signature: function(monaco: Monaco) => void An event is emitted before the editor is mounted It gets the monaco instance as a first argument Defaults to "noop" |
|
|
35
|
-
| onMount | `OnMount` | - | Signature: function(editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => void An event is emitted when the editor is mounted It gets the editor instance as a first argument and the monaco instance as a second Defaults to "noop" |
|
|
36
|
-
| onChange | `OnChange` | - | Signature: function(value: string \| undefined, ev: monaco.editor.IModelContentChangedEvent) => void An event is emitted when the content of the current model is changed |
|
|
37
|
-
| onValidate | `OnValidate` | - | Signature: function(markers: monaco.editor.IMarker[]) => void An event is emitted when the content of the current model is changed and the current model markers are ready Defaults to "noop" |
|
|
38
15
|
|
|
39
16
|
|
|
40
17
|
[//]: DOCUMENTATION_SECTION_END
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { EditorProps } from '@monaco-editor/react';
|
|
2
1
|
import { WithSupportProps } from '@snack-uikit/utils';
|
|
2
|
+
import { EditorProps, YamlEditorProps } from './types';
|
|
3
3
|
export type CodeEditorProps = WithSupportProps<{
|
|
4
4
|
/**
|
|
5
5
|
* Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
|
|
@@ -9,5 +9,5 @@ export type CodeEditorProps = WithSupportProps<{
|
|
|
9
9
|
* Включение/отключение псевдобекграунда
|
|
10
10
|
*/
|
|
11
11
|
hasBackground?: boolean;
|
|
12
|
-
}> & EditorProps;
|
|
12
|
+
}> & (EditorProps | YamlEditorProps);
|
|
13
13
|
export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -39,13 +39,22 @@ function CodeEditor(_a) {
|
|
|
39
39
|
onMount
|
|
40
40
|
} = _a,
|
|
41
41
|
props = __rest(_a, ["themeName", "className", "theme", "options", "loading", "hasBackground", "onMount"]);
|
|
42
|
+
const {
|
|
43
|
+
language
|
|
44
|
+
} = props;
|
|
42
45
|
const monaco = (0, react_1.useMonaco)();
|
|
43
46
|
const [wrapperElement, setWrapperElement] = (0, react_2.useState)(null);
|
|
47
|
+
const yamlModelPath = (0, hooks_1.useYamlEditor)(props.language === 'yaml' ? props.yamlSchema : undefined);
|
|
44
48
|
const onEditorMount = (0, react_2.useCallback)((editor, monaco) => {
|
|
45
49
|
var _a;
|
|
46
50
|
monaco && (0, utils_1.isBrowser)() && ((_a = document === null || document === void 0 ? void 0 : document.fonts) === null || _a === void 0 ? void 0 : _a.ready.finally(monaco.editor.remeasureFonts));
|
|
47
51
|
onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
|
|
48
|
-
|
|
52
|
+
if (language === 'yaml') {
|
|
53
|
+
monaco.languages.register({
|
|
54
|
+
id: 'yaml'
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}, [language, onMount]);
|
|
49
58
|
const themeValues = (0, hooks_1.useCalculatedThemeValues)({
|
|
50
59
|
themeName,
|
|
51
60
|
stylesOriginNode: wrapperElement
|
|
@@ -101,11 +110,11 @@ function CodeEditor(_a) {
|
|
|
101
110
|
monaco === null || monaco === void 0 ? void 0 : monaco.editor.defineTheme('snack', Object.assign(Object.assign({}, themeDataWithoutBase), {
|
|
102
111
|
base: 'vs'
|
|
103
112
|
}));
|
|
104
|
-
const mergedOptions = (0, react_2.useMemo)(() => Object.assign(Object.assign(Object.assign({}, constants_1.CODE_EDITOR_OPTIONS), (themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono) ? {
|
|
113
|
+
const mergedOptions = (0, react_2.useMemo)(() => Object.assign(Object.assign(Object.assign(Object.assign({}, constants_1.CODE_EDITOR_OPTIONS), language === 'yaml' ? constants_1.YAML_CODE_EDITOR_OPTIONS : {}), (themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono) ? {
|
|
105
114
|
fontSize: Number.parseFloat(themeValues.mono.body.s['font-size']),
|
|
106
115
|
fontWeight: themeValues.mono.body.s['font-weight'],
|
|
107
116
|
fontFamily: themeValues.mono.body.s['font-family']
|
|
108
|
-
} : constants_1.DEFAULT_THEME_OPTIONS.mono.s), options), [options, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
117
|
+
} : constants_1.DEFAULT_THEME_OPTIONS.mono.s), options), [options, language, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
109
118
|
return (0, jsx_runtime_1.jsx)("div", Object.assign({
|
|
110
119
|
className: className
|
|
111
120
|
}, (0, utils_1.extractSupportProps)(props), {
|
|
@@ -117,7 +126,8 @@ function CodeEditor(_a) {
|
|
|
117
126
|
}),
|
|
118
127
|
loading: loading !== null && loading !== void 0 ? loading : (0, jsx_runtime_1.jsx)(loaders_1.Spinner, {}),
|
|
119
128
|
options: mergedOptions,
|
|
120
|
-
onMount: onEditorMount
|
|
129
|
+
onMount: onEditorMount,
|
|
130
|
+
path: yamlModelPath
|
|
121
131
|
}))
|
|
122
132
|
}));
|
|
123
133
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.CODE_EDITOR_OPTIONS = exports.THEME_VARS = exports.DEFAULT_THEME_OPTIONS = exports.DEFAULT_THEME_VALUES = void 0;
|
|
6
|
+
exports.YAML_CODE_EDITOR_OPTIONS = exports.CODE_EDITOR_OPTIONS = exports.THEME_VARS = exports.DEFAULT_THEME_OPTIONS = exports.DEFAULT_THEME_VALUES = void 0;
|
|
7
7
|
exports.DEFAULT_THEME_VALUES = {
|
|
8
8
|
semanticTokenColors: {
|
|
9
9
|
enumMember: {
|
|
@@ -113,4 +113,9 @@ exports.CODE_EDITOR_OPTIONS = {
|
|
|
113
113
|
guides: {
|
|
114
114
|
indentation: false
|
|
115
115
|
}
|
|
116
|
+
};
|
|
117
|
+
exports.YAML_CODE_EDITOR_OPTIONS = {
|
|
118
|
+
quickSuggestions: {
|
|
119
|
+
strings: true
|
|
120
|
+
}
|
|
116
121
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SchemasSettings } from 'monaco-yaml';
|
|
1
2
|
type UseCalculatedThemeValuesProps = {
|
|
2
3
|
stylesOriginNode: HTMLDivElement | null;
|
|
3
4
|
themeName?: string;
|
|
@@ -41,4 +42,5 @@ export declare function useCalculatedThemeValues({ stylesOriginNode, themeName }
|
|
|
41
42
|
};
|
|
42
43
|
};
|
|
43
44
|
} | undefined;
|
|
45
|
+
export declare function useYamlEditor(yamlSchema?: SchemasSettings): string | undefined;
|
|
44
46
|
export {};
|
|
@@ -4,7 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.useCalculatedThemeValues = useCalculatedThemeValues;
|
|
7
|
-
|
|
7
|
+
exports.useYamlEditor = useYamlEditor;
|
|
8
|
+
const react_1 = require("@monaco-editor/react");
|
|
9
|
+
const monaco_yaml_1 = require("monaco-yaml");
|
|
10
|
+
const react_2 = require("react");
|
|
8
11
|
const utils_1 = require("@snack-uikit/utils");
|
|
9
12
|
const constants_1 = require("./constants");
|
|
10
13
|
function getThemeVars(styles) {
|
|
@@ -25,16 +28,31 @@ function useCalculatedThemeValues(_ref) {
|
|
|
25
28
|
stylesOriginNode,
|
|
26
29
|
themeName
|
|
27
30
|
} = _ref;
|
|
28
|
-
const [values, setValues] = (0,
|
|
31
|
+
const [values, setValues] = (0, react_2.useState)(undefined);
|
|
29
32
|
const {
|
|
30
33
|
themeClassName
|
|
31
34
|
} = (0, utils_1.useThemeContext)();
|
|
32
35
|
const trigger = themeName !== null && themeName !== void 0 ? themeName : themeClassName;
|
|
33
|
-
(0,
|
|
36
|
+
(0, react_2.useEffect)(() => {
|
|
34
37
|
if (stylesOriginNode) {
|
|
35
38
|
const styles = getComputedStyle(stylesOriginNode);
|
|
36
39
|
setValues(getThemeVars(styles)(constants_1.THEME_VARS));
|
|
37
40
|
}
|
|
38
41
|
}, [stylesOriginNode, trigger]);
|
|
39
42
|
return values;
|
|
43
|
+
}
|
|
44
|
+
function useYamlEditor(yamlSchema) {
|
|
45
|
+
var _a;
|
|
46
|
+
const modelPath = (_a = yamlSchema === null || yamlSchema === void 0 ? void 0 : yamlSchema.fileMatch) === null || _a === void 0 ? void 0 : _a[0];
|
|
47
|
+
const monaco = (0, react_1.useMonaco)();
|
|
48
|
+
(0, react_2.useEffect)(() => {
|
|
49
|
+
if (!yamlSchema || !monaco) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
(0, monaco_yaml_1.configureMonacoYaml)(monaco, {
|
|
53
|
+
enableSchemaRequest: true,
|
|
54
|
+
schemas: [yamlSchema]
|
|
55
|
+
});
|
|
56
|
+
}, [monaco, yamlSchema]);
|
|
57
|
+
return modelPath;
|
|
40
58
|
}
|
|
@@ -22,4 +22,5 @@ var __exportStar = void 0 && (void 0).__exportStar || function (m, exports) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", {
|
|
23
23
|
value: true
|
|
24
24
|
});
|
|
25
|
-
__exportStar(require("./CodeEditor"), exports);
|
|
25
|
+
__exportStar(require("./CodeEditor"), exports);
|
|
26
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EditorProps as MonacoEditorProps } from '@monaco-editor/react';
|
|
2
|
+
import { SchemasSettings } from 'monaco-yaml';
|
|
3
|
+
export type EditorProps = {
|
|
4
|
+
yamlSchema?: never;
|
|
5
|
+
} & MonacoEditorProps;
|
|
6
|
+
export type YamlEditorProps = {
|
|
7
|
+
language: 'yaml';
|
|
8
|
+
yamlSchema?: SchemasSettings;
|
|
9
|
+
} & Omit<MonacoEditorProps, 'language'>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { EditorProps } from '@monaco-editor/react';
|
|
2
1
|
import { WithSupportProps } from '@snack-uikit/utils';
|
|
2
|
+
import { EditorProps, YamlEditorProps } from './types';
|
|
3
3
|
export type CodeEditorProps = WithSupportProps<{
|
|
4
4
|
/**
|
|
5
5
|
* Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
|
|
@@ -9,5 +9,5 @@ export type CodeEditorProps = WithSupportProps<{
|
|
|
9
9
|
* Включение/отключение псевдобекграунда
|
|
10
10
|
*/
|
|
11
11
|
hasBackground?: boolean;
|
|
12
|
-
}> & EditorProps;
|
|
12
|
+
}> & (EditorProps | YamlEditorProps);
|
|
13
13
|
export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -15,20 +15,25 @@ import cn from 'classnames';
|
|
|
15
15
|
import { useCallback, useMemo, useState } from 'react';
|
|
16
16
|
import { Spinner } from '@snack-uikit/loaders';
|
|
17
17
|
import { extractSupportProps, isBrowser } from '@snack-uikit/utils';
|
|
18
|
-
import { CODE_EDITOR_OPTIONS, DEFAULT_THEME_OPTIONS, DEFAULT_THEME_VALUES } from './constants';
|
|
19
|
-
import { useCalculatedThemeValues } from './hooks';
|
|
18
|
+
import { CODE_EDITOR_OPTIONS, DEFAULT_THEME_OPTIONS, DEFAULT_THEME_VALUES, YAML_CODE_EDITOR_OPTIONS, } from './constants';
|
|
19
|
+
import { useCalculatedThemeValues, useYamlEditor } from './hooks';
|
|
20
20
|
import styles from './styles.module.css';
|
|
21
21
|
import { initLoaderConfig, isDark } from './utils';
|
|
22
22
|
initLoaderConfig();
|
|
23
23
|
export function CodeEditor(_a) {
|
|
24
24
|
var { themeName, className, theme, options, loading, hasBackground = true, onMount } = _a, props = __rest(_a, ["themeName", "className", "theme", "options", "loading", "hasBackground", "onMount"]);
|
|
25
|
+
const { language } = props;
|
|
25
26
|
const monaco = useMonaco();
|
|
26
27
|
const [wrapperElement, setWrapperElement] = useState(null);
|
|
28
|
+
const yamlModelPath = useYamlEditor(props.language === 'yaml' ? props.yamlSchema : undefined);
|
|
27
29
|
const onEditorMount = useCallback((editor, monaco) => {
|
|
28
30
|
var _a;
|
|
29
31
|
monaco && isBrowser() && ((_a = document === null || document === void 0 ? void 0 : document.fonts) === null || _a === void 0 ? void 0 : _a.ready.finally(monaco.editor.remeasureFonts));
|
|
30
32
|
onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
|
|
31
|
-
|
|
33
|
+
if (language === 'yaml') {
|
|
34
|
+
monaco.languages.register({ id: 'yaml' });
|
|
35
|
+
}
|
|
36
|
+
}, [language, onMount]);
|
|
32
37
|
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
|
|
33
38
|
const themeDataWithoutBase = useMemo(() => (Object.assign({ inherit: true }, ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys)
|
|
34
39
|
? {
|
|
@@ -79,12 +84,12 @@ export function CodeEditor(_a) {
|
|
|
79
84
|
const dark = useMemo(() => { var _a; return isDark((_a = themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys.available.complementary) !== null && _a !== void 0 ? _a : ''); }, [themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys.available.complementary]);
|
|
80
85
|
monaco === null || monaco === void 0 ? void 0 : monaco.editor.defineTheme('snackDark', Object.assign(Object.assign({}, themeDataWithoutBase), { base: 'vs-dark' }));
|
|
81
86
|
monaco === null || monaco === void 0 ? void 0 : monaco.editor.defineTheme('snack', Object.assign(Object.assign({}, themeDataWithoutBase), { base: 'vs' }));
|
|
82
|
-
const mergedOptions = useMemo(() => (Object.assign(Object.assign(Object.assign({}, CODE_EDITOR_OPTIONS), ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono)
|
|
87
|
+
const mergedOptions = useMemo(() => (Object.assign(Object.assign(Object.assign(Object.assign({}, CODE_EDITOR_OPTIONS), (language === 'yaml' ? YAML_CODE_EDITOR_OPTIONS : {})), ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono)
|
|
83
88
|
? {
|
|
84
89
|
fontSize: Number.parseFloat(themeValues.mono.body.s['font-size']),
|
|
85
90
|
fontWeight: themeValues.mono.body.s['font-weight'],
|
|
86
91
|
fontFamily: themeValues.mono.body.s['font-family'],
|
|
87
92
|
}
|
|
88
|
-
: DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
89
|
-
return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { ref: setWrapperElement, children: _jsx(Editor, Object.assign({}, props, { theme: theme !== null && theme !== void 0 ? theme : (dark ? 'snackDark' : 'snack'), className: cn({ [styles.editor]: hasBackground }), loading: loading !== null && loading !== void 0 ? loading : _jsx(Spinner, {}), options: mergedOptions, onMount: onEditorMount })) })));
|
|
93
|
+
: DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, language, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
94
|
+
return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { ref: setWrapperElement, children: _jsx(Editor, Object.assign({}, props, { theme: theme !== null && theme !== void 0 ? theme : (dark ? 'snackDark' : 'snack'), className: cn({ [styles.editor]: hasBackground }), loading: loading !== null && loading !== void 0 ? loading : _jsx(Spinner, {}), options: mergedOptions, onMount: onEditorMount, path: yamlModelPath })) })));
|
|
90
95
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SchemasSettings } from 'monaco-yaml';
|
|
1
2
|
type UseCalculatedThemeValuesProps = {
|
|
2
3
|
stylesOriginNode: HTMLDivElement | null;
|
|
3
4
|
themeName?: string;
|
|
@@ -41,4 +42,5 @@ export declare function useCalculatedThemeValues({ stylesOriginNode, themeName }
|
|
|
41
42
|
};
|
|
42
43
|
};
|
|
43
44
|
} | undefined;
|
|
45
|
+
export declare function useYamlEditor(yamlSchema?: SchemasSettings): string | undefined;
|
|
44
46
|
export {};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useMonaco } from '@monaco-editor/react';
|
|
2
|
+
import { configureMonacoYaml } from 'monaco-yaml';
|
|
1
3
|
import { useEffect, useState } from 'react';
|
|
2
4
|
import { useThemeContext } from '@snack-uikit/utils';
|
|
3
5
|
import { THEME_VARS } from './constants';
|
|
@@ -26,3 +28,18 @@ export function useCalculatedThemeValues({ stylesOriginNode, themeName }) {
|
|
|
26
28
|
}, [stylesOriginNode, trigger]);
|
|
27
29
|
return values;
|
|
28
30
|
}
|
|
31
|
+
export function useYamlEditor(yamlSchema) {
|
|
32
|
+
var _a;
|
|
33
|
+
const modelPath = (_a = yamlSchema === null || yamlSchema === void 0 ? void 0 : yamlSchema.fileMatch) === null || _a === void 0 ? void 0 : _a[0];
|
|
34
|
+
const monaco = useMonaco();
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!yamlSchema || !monaco) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
configureMonacoYaml(monaco, {
|
|
40
|
+
enableSchemaRequest: true,
|
|
41
|
+
schemas: [yamlSchema],
|
|
42
|
+
});
|
|
43
|
+
}, [monaco, yamlSchema]);
|
|
44
|
+
return modelPath;
|
|
45
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EditorProps as MonacoEditorProps } from '@monaco-editor/react';
|
|
2
|
+
import { SchemasSettings } from 'monaco-yaml';
|
|
3
|
+
export type EditorProps = {
|
|
4
|
+
yamlSchema?: never;
|
|
5
|
+
} & MonacoEditorProps;
|
|
6
|
+
export type YamlEditorProps = {
|
|
7
|
+
language: 'yaml';
|
|
8
|
+
yamlSchema?: SchemasSettings;
|
|
9
|
+
} & Omit<MonacoEditorProps, 'language'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.4.
|
|
7
|
+
"version": "0.4.7-preview-9b2da27a.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -40,10 +40,11 @@
|
|
|
40
40
|
"@snack-uikit/loaders": "0.9.0",
|
|
41
41
|
"@snack-uikit/utils": "3.6.0",
|
|
42
42
|
"chroma-js": "3.1.2",
|
|
43
|
-
"classnames": "2.5.1"
|
|
43
|
+
"classnames": "2.5.1",
|
|
44
|
+
"monaco-yaml": "5.2.3"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@types/chroma-js": "2.4.4"
|
|
47
48
|
},
|
|
48
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "dff6045c82808e07fd469610e3d894ed9d5eb1b9"
|
|
49
50
|
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import { Editor,
|
|
1
|
+
import { Editor, OnMount, useMonaco } from '@monaco-editor/react';
|
|
2
2
|
import cn from 'classnames';
|
|
3
3
|
import { useCallback, useMemo, useState } from 'react';
|
|
4
4
|
|
|
5
5
|
import { Spinner } from '@snack-uikit/loaders';
|
|
6
6
|
import { extractSupportProps, isBrowser, WithSupportProps } from '@snack-uikit/utils';
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
CODE_EDITOR_OPTIONS,
|
|
10
|
+
DEFAULT_THEME_OPTIONS,
|
|
11
|
+
DEFAULT_THEME_VALUES,
|
|
12
|
+
YAML_CODE_EDITOR_OPTIONS,
|
|
13
|
+
} from './constants';
|
|
14
|
+
import { useCalculatedThemeValues, useYamlEditor } from './hooks';
|
|
10
15
|
import styles from './styles.module.scss';
|
|
16
|
+
import { EditorProps, YamlEditorProps } from './types';
|
|
11
17
|
import { initLoaderConfig, isDark } from './utils';
|
|
12
18
|
|
|
13
19
|
initLoaderConfig();
|
|
@@ -22,7 +28,7 @@ export type CodeEditorProps = WithSupportProps<{
|
|
|
22
28
|
*/
|
|
23
29
|
hasBackground?: boolean;
|
|
24
30
|
}> &
|
|
25
|
-
EditorProps;
|
|
31
|
+
(EditorProps | YamlEditorProps);
|
|
26
32
|
|
|
27
33
|
export function CodeEditor({
|
|
28
34
|
themeName,
|
|
@@ -34,15 +40,23 @@ export function CodeEditor({
|
|
|
34
40
|
onMount,
|
|
35
41
|
...props
|
|
36
42
|
}: CodeEditorProps) {
|
|
43
|
+
const { language } = props;
|
|
44
|
+
|
|
37
45
|
const monaco = useMonaco();
|
|
38
46
|
const [wrapperElement, setWrapperElement] = useState<HTMLDivElement | null>(null);
|
|
39
47
|
|
|
48
|
+
const yamlModelPath = useYamlEditor(props.language === 'yaml' ? props.yamlSchema : undefined);
|
|
49
|
+
|
|
40
50
|
const onEditorMount = useCallback<OnMount>(
|
|
41
51
|
(editor, monaco) => {
|
|
42
52
|
monaco && isBrowser() && document?.fonts?.ready.finally(monaco.editor.remeasureFonts);
|
|
43
53
|
onMount?.(editor, monaco);
|
|
54
|
+
|
|
55
|
+
if (language === 'yaml') {
|
|
56
|
+
monaco.languages.register({ id: 'yaml' });
|
|
57
|
+
}
|
|
44
58
|
},
|
|
45
|
-
[onMount],
|
|
59
|
+
[language, onMount],
|
|
46
60
|
);
|
|
47
61
|
|
|
48
62
|
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
|
|
@@ -111,6 +125,7 @@ export function CodeEditor({
|
|
|
111
125
|
const mergedOptions = useMemo(
|
|
112
126
|
() => ({
|
|
113
127
|
...CODE_EDITOR_OPTIONS,
|
|
128
|
+
...(language === 'yaml' ? YAML_CODE_EDITOR_OPTIONS : {}),
|
|
114
129
|
...(themeValues?.mono
|
|
115
130
|
? {
|
|
116
131
|
fontSize: Number.parseFloat(themeValues.mono.body.s['font-size']),
|
|
@@ -120,7 +135,7 @@ export function CodeEditor({
|
|
|
120
135
|
: DEFAULT_THEME_OPTIONS.mono.s),
|
|
121
136
|
...options,
|
|
122
137
|
}),
|
|
123
|
-
[options, themeValues?.mono],
|
|
138
|
+
[options, language, themeValues?.mono],
|
|
124
139
|
);
|
|
125
140
|
|
|
126
141
|
return (
|
|
@@ -132,6 +147,7 @@ export function CodeEditor({
|
|
|
132
147
|
loading={loading ?? <Spinner />}
|
|
133
148
|
options={mergedOptions}
|
|
134
149
|
onMount={onEditorMount}
|
|
150
|
+
path={yamlModelPath}
|
|
135
151
|
/>
|
|
136
152
|
</div>
|
|
137
153
|
);
|
package/src/components/hooks.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useMonaco } from '@monaco-editor/react';
|
|
2
|
+
import { configureMonacoYaml, SchemasSettings } from 'monaco-yaml';
|
|
1
3
|
import { useEffect, useState } from 'react';
|
|
2
4
|
|
|
3
5
|
import { useThemeContext } from '@snack-uikit/utils';
|
|
@@ -42,3 +44,21 @@ export function useCalculatedThemeValues({ stylesOriginNode, themeName }: UseCal
|
|
|
42
44
|
|
|
43
45
|
return values;
|
|
44
46
|
}
|
|
47
|
+
|
|
48
|
+
export function useYamlEditor(yamlSchema?: SchemasSettings) {
|
|
49
|
+
const modelPath = yamlSchema?.fileMatch?.[0];
|
|
50
|
+
const monaco = useMonaco();
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!yamlSchema || !monaco) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
configureMonacoYaml(monaco, {
|
|
58
|
+
enableSchemaRequest: true,
|
|
59
|
+
schemas: [yamlSchema],
|
|
60
|
+
});
|
|
61
|
+
}, [monaco, yamlSchema]);
|
|
62
|
+
|
|
63
|
+
return modelPath;
|
|
64
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EditorProps as MonacoEditorProps } from '@monaco-editor/react';
|
|
2
|
+
import { SchemasSettings } from 'monaco-yaml';
|
|
3
|
+
|
|
4
|
+
export type EditorProps = {
|
|
5
|
+
yamlSchema?: never;
|
|
6
|
+
} & MonacoEditorProps;
|
|
7
|
+
|
|
8
|
+
export type YamlEditorProps = {
|
|
9
|
+
language: 'yaml';
|
|
10
|
+
yamlSchema?: SchemasSettings;
|
|
11
|
+
} & Omit<MonacoEditorProps, 'language'>;
|