@snack-uikit/code-editor 0.4.7-preview-b4050474.0 → 0.4.7-preview-aa6874cd.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 CHANGED
@@ -12,6 +12,36 @@
12
12
  ### Props
13
13
  | name | type | default value | description |
14
14
  |------|------|---------------|-------------|
15
+ | themeName | `string` | - | Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей. |
16
+ | hasBackground | `boolean` | true | Включение/отключение псевдобекграунда |
17
+ | yamlSchema | `SchemasSettings` | - | Схема для yaml валидации |
18
+ | defaultValue | `string` | - | Default value of the current model |
19
+ | defaultLanguage | `string` | - | Default language of the current model |
20
+ | 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))` |
21
+ | value | `string` | - | Value of the current model |
22
+ | language | `string` | - | Language of the current model |
23
+ | path | `string` | - | Path of the current model Will be passed as the third argument to `.createModel` method `monaco.editor.createModel(..., ..., monaco.Uri.parse(defaultPath))` |
24
+ | theme | `string` | "light" | The theme for the monaco Available options "vs-dark" \| "light" Define new themes by `monaco.editor.defineTheme` |
25
+ | line | `number` | - | The line to jump on it |
26
+ | loading | `ReactNode` | "Loading..." | The loading screen before the editor will be mounted |
27
+ | options | `IStandaloneEditorConstructionOptions` | - | IStandaloneEditorConstructionOptions |
28
+ | overrideServices | `IEditorOverrideServices` | - | IEditorOverrideServices |
29
+ | saveViewState | `boolean` | - | Indicator whether to save the models' view states between model changes or not Defaults to true |
30
+ | keepCurrentModel | `boolean` | false | Indicator whether to dispose the current model when the Editor is unmounted or not |
31
+ | width | `string \| number` | "100%" | Width of the editor wrapper |
32
+ | height | `string \| number` | "100%" | Height of the editor wrapper |
33
+ | className | `string` | - | Class name for the editor container |
34
+ | wrapperProps | `object` | - | Props applied to the wrapper element |
35
+ | 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" |
36
+ | 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" |
37
+ | 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 |
38
+ | 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" |
15
39
 
16
40
 
17
41
  [//]: DOCUMENTATION_SECTION_END
42
+
43
+ ### Поддержка YAML
44
+ Для обеспечения полноценной работы YAML Monaco Editor необходимо подключить `yaml.worker`. Это можно сделать следующими способами: с использованием [`window.MonacoEnvironment`](https://www.npmjs.com/package/monaco-yaml#usage) или с помощью плагина [`MonacoWebpackPlugin`](https://www.npmjs.com/package/monaco-yaml#using-monaco-webpack-loader-plugin) . Подробнее с `monaco-yaml` можно ознакомиться [тут]((https://www.npmjs.com/package/monaco-yaml))
45
+
46
+ ### Как редактор загружает worker?
47
+ Для storybook воркеры определены и расширены `yaml` в настройках вебпака сторибука. Плагин создает новые точки входа каждого build-in воркера и для каждого дабавленного отдельно. Плагин создат глобальную переменную, используя которую редкатор будет загружать нужный воркер на основе текущего `language`.
@@ -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);
@@ -110,6 +114,9 @@ function CodeEditor(_a) {
110
114
  fontWeight: themeValues.mono.body.s['font-weight'],
111
115
  fontFamily: themeValues.mono.body.s['font-family']
112
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
+ }
113
120
  return (0, jsx_runtime_1.jsx)("div", Object.assign({
114
121
  className: className
115
122
  }, (0, utils_1.extractSupportProps)(props), {
@@ -5,5 +5,8 @@ export type EditorProps = {
5
5
  } & MonacoEditorProps;
6
6
  export type YamlEditorProps = {
7
7
  language: 'yaml';
8
+ /**
9
+ * Схема для yaml валидации
10
+ */
8
11
  yamlSchema?: SchemasSettings;
9
12
  } & Omit<MonacoEditorProps, 'language'>;
@@ -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);
@@ -88,5 +92,8 @@ export function CodeEditor(_a) {
88
92
  fontFamily: themeValues.mono.body.s['font-family'],
89
93
  }
90
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
+ }
91
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 })) })));
92
99
  }
@@ -5,5 +5,8 @@ export type EditorProps = {
5
5
  } & MonacoEditorProps;
6
6
  export type YamlEditorProps = {
7
7
  language: 'yaml';
8
+ /**
9
+ * Схема для yaml валидации
10
+ */
8
11
  yamlSchema?: SchemasSettings;
9
12
  } & Omit<MonacoEditorProps, 'language'>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.4.7-preview-b4050474.0",
7
+ "version": "0.4.7-preview-aa6874cd.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": "fbfcf57310f12a2d2878de25dacf35e8c2268173"
49
+ "gitHead": "e8e7969f21fb78891213f7bac60d2c87ceb7b8f9"
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);
@@ -134,6 +138,10 @@ export function CodeEditor({
134
138
  [options, language, themeValues?.mono],
135
139
  );
136
140
 
141
+ if (!canRender) {
142
+ return null;
143
+ }
144
+
137
145
  return (
138
146
  <div className={className} {...extractSupportProps(props)} ref={setWrapperElement}>
139
147
  <Editor
@@ -7,5 +7,8 @@ export type EditorProps = {
7
7
 
8
8
  export type YamlEditorProps = {
9
9
  language: 'yaml';
10
+ /**
11
+ * Схема для yaml валидации
12
+ */
10
13
  yamlSchema?: SchemasSettings;
11
14
  } & Omit<MonacoEditorProps, 'language'>;