@snack-uikit/code-editor 0.2.2-preview-6dbb02ba.0 → 0.2.2
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/CHANGELOG.md +11 -0
- package/dist/components/CodeEditor.js +11 -16
- package/package.json +2 -2
- package/src/components/CodeEditor.tsx +11 -21
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## 0.2.2 (2024-07-19)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **FF-0000:** code-editor theme init ([da4f622](https://git.sbercloud.tech/sbercloud-ui/tokens-design-system/snack-uikit/commits/da4f622f5a5b9b8028127d357dbe6fc8e20a81ab))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## 0.2.1 (2024-07-18)
|
|
7
18
|
|
|
8
19
|
|
|
@@ -12,7 +12,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
13
|
import { Editor, useMonaco } from '@monaco-editor/react';
|
|
14
14
|
import cn from 'classnames';
|
|
15
|
-
import { useCallback, useMemo,
|
|
15
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
16
16
|
import { Spinner } from '@snack-uikit/loaders';
|
|
17
17
|
import { extractSupportProps } from '@snack-uikit/utils';
|
|
18
18
|
import { CODE_EDITOR_OPTIONS, DEFAULT_THEME_OPTIONS, DEFAULT_THEME_VALUES } from './constants';
|
|
@@ -23,13 +23,13 @@ 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
25
|
const monaco = useMonaco();
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}, [
|
|
32
|
-
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode:
|
|
26
|
+
const [wrapperElement, setWrapperElement] = useState(null);
|
|
27
|
+
const onEditorMount = useCallback((editor, monaco) => {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
monaco && ((_b = (_a = window === null || window === void 0 ? void 0 : window.document) === null || _a === void 0 ? void 0 : _a.fonts) === null || _b === void 0 ? void 0 : _b.ready.finally(monaco.editor.remeasureFonts));
|
|
30
|
+
onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
|
|
31
|
+
}, [onMount]);
|
|
32
|
+
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
|
|
33
33
|
const themeDataWithoutBase = useMemo(() => (Object.assign({ inherit: true }, ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys)
|
|
34
34
|
? {
|
|
35
35
|
semanticTokenColors: {
|
|
@@ -77,7 +77,8 @@ export function CodeEditor(_a) {
|
|
|
77
77
|
}
|
|
78
78
|
: DEFAULT_THEME_VALUES))), [themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys]);
|
|
79
79
|
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
|
-
|
|
80
|
+
monaco === null || monaco === void 0 ? void 0 : monaco.editor.defineTheme('snackDark', Object.assign(Object.assign({}, themeDataWithoutBase), { base: 'vs-dark' }));
|
|
81
|
+
monaco === null || monaco === void 0 ? void 0 : monaco.editor.defineTheme('snack', Object.assign(Object.assign({}, themeDataWithoutBase), { base: 'vs' }));
|
|
81
82
|
const mergedOptions = useMemo(() => (Object.assign(Object.assign(Object.assign({}, CODE_EDITOR_OPTIONS), ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono)
|
|
82
83
|
? {
|
|
83
84
|
fontSize: Number.parseFloat(themeValues.mono.body.s['font-size']),
|
|
@@ -85,11 +86,5 @@ export function CodeEditor(_a) {
|
|
|
85
86
|
fontFamily: themeValues.mono.body.s['font-family'],
|
|
86
87
|
}
|
|
87
88
|
: DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
88
|
-
|
|
89
|
-
var _a, _b;
|
|
90
|
-
monaco && ((_b = (_a = window === null || window === void 0 ? void 0 : window.document) === null || _a === void 0 ? void 0 : _a.fonts) === null || _b === void 0 ? void 0 : _b.ready.finally(monaco.editor.remeasureFonts));
|
|
91
|
-
applyTheme(themeDataWithoutBase);
|
|
92
|
-
onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
|
|
93
|
-
}, [applyTheme, onMount, themeDataWithoutBase]);
|
|
94
|
-
return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { ref: wrapperRef, 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 })) })));
|
|
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 })) })));
|
|
95
90
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.2.2
|
|
7
|
+
"version": "0.2.2",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"@snack-uikit/utils": "3.3.0",
|
|
38
38
|
"classnames": "2.5.1"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "7bb0d0cace003d77312cb0cc9a567540f3d58bc6"
|
|
41
41
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Editor, EditorProps, OnMount, useMonaco } from '@monaco-editor/react';
|
|
2
2
|
import cn from 'classnames';
|
|
3
|
-
import { useCallback, useMemo,
|
|
3
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
4
4
|
|
|
5
5
|
import { Spinner } from '@snack-uikit/loaders';
|
|
6
6
|
import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
|
|
@@ -35,19 +35,17 @@ export function CodeEditor({
|
|
|
35
35
|
...props
|
|
36
36
|
}: CodeEditorProps) {
|
|
37
37
|
const monaco = useMonaco();
|
|
38
|
-
const
|
|
39
|
-
const defineTheme = monaco?.editor.defineTheme;
|
|
40
|
-
type ThemeData = Parameters<Exclude<typeof defineTheme, undefined>>[1];
|
|
38
|
+
const [wrapperElement, setWrapperElement] = useState<HTMLDivElement | null>(null);
|
|
41
39
|
|
|
42
|
-
const
|
|
43
|
-
(
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
const onEditorMount = useCallback<OnMount>(
|
|
41
|
+
(editor, monaco) => {
|
|
42
|
+
monaco && window?.document?.fonts?.ready.finally(monaco.editor.remeasureFonts);
|
|
43
|
+
onMount?.(editor, monaco);
|
|
46
44
|
},
|
|
47
|
-
[
|
|
45
|
+
[onMount],
|
|
48
46
|
);
|
|
49
47
|
|
|
50
|
-
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode:
|
|
48
|
+
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
|
|
51
49
|
|
|
52
50
|
const themeDataWithoutBase = useMemo(
|
|
53
51
|
() => ({
|
|
@@ -107,7 +105,8 @@ export function CodeEditor({
|
|
|
107
105
|
[themeValues?.sys.available.complementary],
|
|
108
106
|
);
|
|
109
107
|
|
|
110
|
-
|
|
108
|
+
monaco?.editor.defineTheme('snackDark', { ...themeDataWithoutBase, base: 'vs-dark' });
|
|
109
|
+
monaco?.editor.defineTheme('snack', { ...themeDataWithoutBase, base: 'vs' });
|
|
111
110
|
|
|
112
111
|
const mergedOptions = useMemo(
|
|
113
112
|
() => ({
|
|
@@ -124,17 +123,8 @@ export function CodeEditor({
|
|
|
124
123
|
[options, themeValues?.mono],
|
|
125
124
|
);
|
|
126
125
|
|
|
127
|
-
const onEditorMount = useCallback<OnMount>(
|
|
128
|
-
(editor, monaco) => {
|
|
129
|
-
monaco && window?.document?.fonts?.ready.finally(monaco.editor.remeasureFonts);
|
|
130
|
-
applyTheme(themeDataWithoutBase);
|
|
131
|
-
onMount?.(editor, monaco);
|
|
132
|
-
},
|
|
133
|
-
[applyTheme, onMount, themeDataWithoutBase],
|
|
134
|
-
);
|
|
135
|
-
|
|
136
126
|
return (
|
|
137
|
-
<div className={className} {...extractSupportProps(props)} ref={
|
|
127
|
+
<div className={className} {...extractSupportProps(props)} ref={setWrapperElement}>
|
|
138
128
|
<Editor
|
|
139
129
|
{...props}
|
|
140
130
|
theme={theme ?? dark ? 'snackDark' : 'snack'}
|