@snack-uikit/code-editor 0.2.1-preview-69f7a1cd.0 → 0.2.1-preview-41bee0a4.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 +1 -1
- package/dist/components/CodeEditor.d.ts +7 -5
- package/dist/components/CodeEditor.js +10 -9
- package/dist/components/hooks.d.ts +6 -1
- package/dist/components/hooks.js +7 -8
- package/package.json +2 -2
- package/src/components/CodeEditor.tsx +22 -15
- package/src/components/hooks.ts +12 -13
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
### Props
|
|
13
13
|
| name | type | default value | description |
|
|
14
14
|
|------|------|---------------|-------------|
|
|
15
|
-
|
|
|
15
|
+
| themeName | `string` | - | Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей. |
|
|
16
16
|
| hasBackground | `boolean` | true | Включение/отключение псевдобекграунда |
|
|
17
17
|
| defaultValue | `string` | - | Default value of the current model |
|
|
18
18
|
| defaultLanguage | `string` | - | Default language of the current model |
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { EditorProps } from '@monaco-editor/react';
|
|
2
2
|
import { WithSupportProps } from '@snack-uikit/utils';
|
|
3
3
|
export type CodeEditorProps = WithSupportProps<{
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
4
|
+
/**
|
|
5
|
+
* Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
|
|
6
|
+
*/
|
|
7
|
+
themeName?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Включение/отключение псевдобекграунда
|
|
6
10
|
*/
|
|
7
|
-
themeClassName?: string;
|
|
8
|
-
/** Включение/отключение псевдобекграунда*/
|
|
9
11
|
hasBackground?: boolean;
|
|
10
12
|
}> & EditorProps;
|
|
11
|
-
export declare function CodeEditor({
|
|
13
|
+
export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -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 {
|
|
15
|
+
import { useCallback, useMemo, useRef } 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';
|
|
@@ -21,9 +21,15 @@ import styles from './styles.module.css';
|
|
|
21
21
|
import { initLoaderConfig, isDark } from './utils';
|
|
22
22
|
initLoaderConfig();
|
|
23
23
|
export function CodeEditor(_a) {
|
|
24
|
-
var {
|
|
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
|
|
26
|
+
const wrapperRef = useRef(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: wrapperRef.current });
|
|
27
33
|
const themeDataWithoutBase = useMemo(() => (Object.assign({ inherit: true }, ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys)
|
|
28
34
|
? {
|
|
29
35
|
semanticTokenColors: {
|
|
@@ -80,10 +86,5 @@ export function CodeEditor(_a) {
|
|
|
80
86
|
fontFamily: themeValues.mono.body.s['font-family'],
|
|
81
87
|
}
|
|
82
88
|
: DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
|
|
83
|
-
|
|
84
|
-
// eslint-disable-next-line no-console
|
|
85
|
-
console.log('!_! remeasureFonts');
|
|
86
|
-
setTimeout(() => monaco === null || monaco === void 0 ? void 0 : monaco.editor.remeasureFonts(), 0);
|
|
87
|
-
}, [mergedOptions, monaco === null || monaco === void 0 ? void 0 : monaco.editor]);
|
|
88
|
-
return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { 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 })) })));
|
|
89
|
+
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
90
|
}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
type UseCalculatedThemeValuesProps = {
|
|
2
|
+
stylesOriginNode: HTMLDivElement | null;
|
|
3
|
+
themeName?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function useCalculatedThemeValues({ stylesOriginNode, themeName }: UseCalculatedThemeValuesProps): {
|
|
2
6
|
sys: {
|
|
3
7
|
neutral: {
|
|
4
8
|
decorDefault: string;
|
|
@@ -37,3 +41,4 @@ export declare function useCalculatedThemeValues(themeClassNameProps?: string):
|
|
|
37
41
|
};
|
|
38
42
|
};
|
|
39
43
|
} | undefined;
|
|
44
|
+
export {};
|
package/dist/components/hooks.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
import { useThemeContext } from '@snack-uikit/utils';
|
|
3
3
|
import { THEME_VARS } from './constants';
|
|
4
4
|
function getThemeVars(styles) {
|
|
@@ -14,16 +14,15 @@ function getThemeVars(styles) {
|
|
|
14
14
|
return res;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
export function useCalculatedThemeValues(
|
|
17
|
+
export function useCalculatedThemeValues({ stylesOriginNode, themeName }) {
|
|
18
18
|
const [values, setValues] = useState(undefined);
|
|
19
|
-
const { themeClassName
|
|
20
|
-
const
|
|
19
|
+
const { themeClassName } = useThemeContext();
|
|
20
|
+
const trigger = themeName !== null && themeName !== void 0 ? themeName : themeClassName;
|
|
21
21
|
useEffect(() => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const styles = getComputedStyle(elem);
|
|
22
|
+
if (stylesOriginNode) {
|
|
23
|
+
const styles = getComputedStyle(stylesOriginNode);
|
|
25
24
|
setValues(getThemeVars(styles)(THEME_VARS));
|
|
26
25
|
}
|
|
27
|
-
}, [
|
|
26
|
+
}, [stylesOriginNode, trigger]);
|
|
28
27
|
return values;
|
|
29
28
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.2.1-preview-
|
|
7
|
+
"version": "0.2.1-preview-41bee0a4.0",
|
|
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": "74a281ab089c84e90be8d8a6c363c277b53bd802"
|
|
41
41
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Editor, EditorProps, useMonaco } from '@monaco-editor/react';
|
|
1
|
+
import { Editor, EditorProps, OnMount, useMonaco } from '@monaco-editor/react';
|
|
2
2
|
import cn from 'classnames';
|
|
3
|
-
import {
|
|
3
|
+
import { useCallback, useMemo, useRef } from 'react';
|
|
4
4
|
|
|
5
5
|
import { Spinner } from '@snack-uikit/loaders';
|
|
6
6
|
import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
|
|
@@ -13,27 +13,39 @@ import { initLoaderConfig, isDark } from './utils';
|
|
|
13
13
|
initLoaderConfig();
|
|
14
14
|
|
|
15
15
|
export type CodeEditorProps = WithSupportProps<{
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
16
|
+
/**
|
|
17
|
+
* Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
|
|
18
|
+
*/
|
|
19
|
+
themeName?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Включение/отключение псевдобекграунда
|
|
18
22
|
*/
|
|
19
|
-
themeClassName?: string;
|
|
20
|
-
/** Включение/отключение псевдобекграунда*/
|
|
21
23
|
hasBackground?: boolean;
|
|
22
24
|
}> &
|
|
23
25
|
EditorProps;
|
|
24
26
|
|
|
25
27
|
export function CodeEditor({
|
|
26
|
-
|
|
28
|
+
themeName,
|
|
27
29
|
className,
|
|
28
30
|
theme,
|
|
29
31
|
options,
|
|
30
32
|
loading,
|
|
31
33
|
hasBackground = true,
|
|
34
|
+
onMount,
|
|
32
35
|
...props
|
|
33
36
|
}: CodeEditorProps) {
|
|
34
37
|
const monaco = useMonaco();
|
|
38
|
+
const wrapperRef = useRef<HTMLDivElement>(null);
|
|
39
|
+
|
|
40
|
+
const onEditorMount = useCallback<OnMount>(
|
|
41
|
+
(editor, monaco) => {
|
|
42
|
+
monaco && window?.document?.fonts?.ready.finally(monaco.editor.remeasureFonts);
|
|
43
|
+
onMount?.(editor, monaco);
|
|
44
|
+
},
|
|
45
|
+
[onMount],
|
|
46
|
+
);
|
|
35
47
|
|
|
36
|
-
const themeValues = useCalculatedThemeValues(
|
|
48
|
+
const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperRef.current });
|
|
37
49
|
|
|
38
50
|
const themeDataWithoutBase = useMemo(
|
|
39
51
|
() => ({
|
|
@@ -111,20 +123,15 @@ export function CodeEditor({
|
|
|
111
123
|
[options, themeValues?.mono],
|
|
112
124
|
);
|
|
113
125
|
|
|
114
|
-
useEffect(() => {
|
|
115
|
-
// eslint-disable-next-line no-console
|
|
116
|
-
console.log('!_! remeasureFonts');
|
|
117
|
-
setTimeout(() => monaco?.editor.remeasureFonts(), 0);
|
|
118
|
-
}, [mergedOptions, monaco?.editor]);
|
|
119
|
-
|
|
120
126
|
return (
|
|
121
|
-
<div className={className} {...extractSupportProps(props)}>
|
|
127
|
+
<div className={className} {...extractSupportProps(props)} ref={wrapperRef}>
|
|
122
128
|
<Editor
|
|
123
129
|
{...props}
|
|
124
130
|
theme={theme ?? dark ? 'snackDark' : 'snack'}
|
|
125
131
|
className={cn({ [styles.editor]: hasBackground })}
|
|
126
132
|
loading={loading ?? <Spinner />}
|
|
127
133
|
options={mergedOptions}
|
|
134
|
+
onMount={onEditorMount}
|
|
128
135
|
/>
|
|
129
136
|
</div>
|
|
130
137
|
);
|
package/src/components/hooks.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
2
|
|
|
3
3
|
import { useThemeContext } from '@snack-uikit/utils';
|
|
4
4
|
|
|
@@ -21,25 +21,24 @@ function getThemeVars(styles: CSSStyleDeclaration) {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
type UseCalculatedThemeValuesProps = {
|
|
25
|
+
stylesOriginNode: HTMLDivElement | null;
|
|
26
|
+
themeName?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function useCalculatedThemeValues({ stylesOriginNode, themeName }: UseCalculatedThemeValuesProps) {
|
|
25
30
|
const [values, setValues] = useState<typeof THEME_VARS | undefined>(undefined);
|
|
26
31
|
|
|
27
|
-
const { themeClassName
|
|
32
|
+
const { themeClassName } = useThemeContext();
|
|
28
33
|
|
|
29
|
-
const
|
|
30
|
-
() => themeClassNameProps ?? themeClassNameContext,
|
|
31
|
-
[themeClassNameContext, themeClassNameProps],
|
|
32
|
-
);
|
|
34
|
+
const trigger = themeName ?? themeClassName;
|
|
33
35
|
|
|
34
36
|
useEffect(() => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (elem) {
|
|
38
|
-
const styles = getComputedStyle(elem);
|
|
39
|
-
|
|
37
|
+
if (stylesOriginNode) {
|
|
38
|
+
const styles = getComputedStyle(stylesOriginNode);
|
|
40
39
|
setValues(getThemeVars(styles)(THEME_VARS) as typeof THEME_VARS);
|
|
41
40
|
}
|
|
42
|
-
}, [
|
|
41
|
+
}, [stylesOriginNode, trigger]);
|
|
43
42
|
|
|
44
43
|
return values;
|
|
45
44
|
}
|