@snack-uikit/code-editor 0.4.7-preview-9b2da27a.0 → 0.4.7-preview-ac85ab7b.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.
@@ -10,4 +10,4 @@ export type CodeEditorProps = WithSupportProps<{
10
10
  */
11
11
  hasBackground?: boolean;
12
12
  }> & (EditorProps | YamlEditorProps);
13
- export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
13
+ export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element | null;
@@ -27,7 +27,6 @@ const constants_1 = require("./constants");
27
27
  const hooks_1 = require("./hooks");
28
28
  const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
29
29
  const utils_2 = require("./utils");
30
- (0, utils_2.initLoaderConfig)();
31
30
  function CodeEditor(_a) {
32
31
  var {
33
32
  themeName,
@@ -42,6 +41,11 @@ function CodeEditor(_a) {
42
41
  const {
43
42
  language
44
43
  } = props;
44
+ const [canRender, setCanRender] = (0, react_2.useState)(false);
45
+ (0, utils_1.useLayoutEffect)(() => {
46
+ (0, utils_2.initLoaderConfig)();
47
+ setCanRender(true);
48
+ }, []);
45
49
  const monaco = (0, react_1.useMonaco)();
46
50
  const [wrapperElement, setWrapperElement] = (0, react_2.useState)(null);
47
51
  const yamlModelPath = (0, hooks_1.useYamlEditor)(props.language === 'yaml' ? props.yamlSchema : undefined);
@@ -49,12 +53,7 @@ function CodeEditor(_a) {
49
53
  var _a;
50
54
  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));
51
55
  onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
52
- if (language === 'yaml') {
53
- monaco.languages.register({
54
- id: 'yaml'
55
- });
56
- }
57
- }, [language, onMount]);
56
+ }, [onMount]);
58
57
  const themeValues = (0, hooks_1.useCalculatedThemeValues)({
59
58
  themeName,
60
59
  stylesOriginNode: wrapperElement
@@ -115,6 +114,9 @@ function CodeEditor(_a) {
115
114
  fontWeight: themeValues.mono.body.s['font-weight'],
116
115
  fontFamily: themeValues.mono.body.s['font-family']
117
116
  } : constants_1.DEFAULT_THEME_OPTIONS.mono.s), options), [options, language, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
117
+ if (!canRender) {
118
+ return null;
119
+ }
118
120
  return (0, jsx_runtime_1.jsx)("div", Object.assign({
119
121
  className: className
120
122
  }, (0, utils_1.extractSupportProps)(props), {
@@ -10,4 +10,4 @@ export type CodeEditorProps = WithSupportProps<{
10
10
  */
11
11
  hasBackground?: boolean;
12
12
  }> & (EditorProps | YamlEditorProps);
13
- export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
13
+ export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element | null;
@@ -14,15 +14,19 @@ import { Editor, useMonaco } from '@monaco-editor/react';
14
14
  import cn from 'classnames';
15
15
  import { useCallback, useMemo, useState } from 'react';
16
16
  import { Spinner } from '@snack-uikit/loaders';
17
- import { extractSupportProps, isBrowser } from '@snack-uikit/utils';
17
+ import { extractSupportProps, isBrowser, useLayoutEffect } from '@snack-uikit/utils';
18
18
  import { CODE_EDITOR_OPTIONS, DEFAULT_THEME_OPTIONS, DEFAULT_THEME_VALUES, YAML_CODE_EDITOR_OPTIONS, } from './constants';
19
19
  import { useCalculatedThemeValues, useYamlEditor } from './hooks';
20
20
  import styles from './styles.module.css';
21
21
  import { initLoaderConfig, isDark } from './utils';
22
- initLoaderConfig();
23
22
  export function CodeEditor(_a) {
24
23
  var { themeName, className, theme, options, loading, hasBackground = true, onMount } = _a, props = __rest(_a, ["themeName", "className", "theme", "options", "loading", "hasBackground", "onMount"]);
25
24
  const { language } = props;
25
+ const [canRender, setCanRender] = useState(false);
26
+ useLayoutEffect(() => {
27
+ initLoaderConfig();
28
+ setCanRender(true);
29
+ }, []);
26
30
  const monaco = useMonaco();
27
31
  const [wrapperElement, setWrapperElement] = useState(null);
28
32
  const yamlModelPath = useYamlEditor(props.language === 'yaml' ? props.yamlSchema : undefined);
@@ -30,10 +34,7 @@ export function CodeEditor(_a) {
30
34
  var _a;
31
35
  monaco && isBrowser() && ((_a = document === null || document === void 0 ? void 0 : document.fonts) === null || _a === void 0 ? void 0 : _a.ready.finally(monaco.editor.remeasureFonts));
32
36
  onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
33
- if (language === 'yaml') {
34
- monaco.languages.register({ id: 'yaml' });
35
- }
36
- }, [language, onMount]);
37
+ }, [onMount]);
37
38
  const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
38
39
  const themeDataWithoutBase = useMemo(() => (Object.assign({ inherit: true }, ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys)
39
40
  ? {
@@ -91,5 +92,8 @@ export function CodeEditor(_a) {
91
92
  fontFamily: themeValues.mono.body.s['font-family'],
92
93
  }
93
94
  : DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, language, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
95
+ if (!canRender) {
96
+ return null;
97
+ }
94
98
  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 })) })));
95
99
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.4.7-preview-9b2da27a.0",
7
+ "version": "0.4.7-preview-ac85ab7b.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -46,5 +46,5 @@
46
46
  "devDependencies": {
47
47
  "@types/chroma-js": "2.4.4"
48
48
  },
49
- "gitHead": "dff6045c82808e07fd469610e3d894ed9d5eb1b9"
49
+ "gitHead": "b48c28c30724091afb0c9c00a9f06a0d8fdaa943"
50
50
  }
@@ -3,7 +3,7 @@ import cn from 'classnames';
3
3
  import { useCallback, useMemo, useState } from 'react';
4
4
 
5
5
  import { Spinner } from '@snack-uikit/loaders';
6
- import { extractSupportProps, isBrowser, WithSupportProps } from '@snack-uikit/utils';
6
+ import { extractSupportProps, isBrowser, useLayoutEffect, WithSupportProps } from '@snack-uikit/utils';
7
7
 
8
8
  import {
9
9
  CODE_EDITOR_OPTIONS,
@@ -16,8 +16,6 @@ import styles from './styles.module.scss';
16
16
  import { EditorProps, YamlEditorProps } from './types';
17
17
  import { initLoaderConfig, isDark } from './utils';
18
18
 
19
- initLoaderConfig();
20
-
21
19
  export type CodeEditorProps = WithSupportProps<{
22
20
  /**
23
21
  * Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
@@ -41,6 +39,12 @@ export function CodeEditor({
41
39
  ...props
42
40
  }: CodeEditorProps) {
43
41
  const { language } = props;
42
+ const [canRender, setCanRender] = useState(false);
43
+
44
+ useLayoutEffect(() => {
45
+ initLoaderConfig();
46
+ setCanRender(true);
47
+ }, []);
44
48
 
45
49
  const monaco = useMonaco();
46
50
  const [wrapperElement, setWrapperElement] = useState<HTMLDivElement | null>(null);
@@ -51,12 +55,8 @@ export function CodeEditor({
51
55
  (editor, monaco) => {
52
56
  monaco && isBrowser() && document?.fonts?.ready.finally(monaco.editor.remeasureFonts);
53
57
  onMount?.(editor, monaco);
54
-
55
- if (language === 'yaml') {
56
- monaco.languages.register({ id: 'yaml' });
57
- }
58
58
  },
59
- [language, onMount],
59
+ [onMount],
60
60
  );
61
61
 
62
62
  const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperElement });
@@ -138,6 +138,10 @@ export function CodeEditor({
138
138
  [options, language, themeValues?.mono],
139
139
  );
140
140
 
141
+ if (!canRender) {
142
+ return null;
143
+ }
144
+
141
145
  return (
142
146
  <div className={className} {...extractSupportProps(props)} ref={setWrapperElement}>
143
147
  <Editor