Skip to content

Commit 6e9ed9d

Browse files
authored
feat: add code editing (#730)
feat: add code editing
1 parent e3351a7 commit 6e9ed9d

File tree

22 files changed

+587
-227
lines changed

22 files changed

+587
-227
lines changed

.storybook/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {resolve} from 'path';
22
import WebpackShellPluginNext from 'webpack-shell-plugin-next';
3+
import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin';
34

45
const customAlias = {
56
widget: resolve(__dirname, '../widget'),
@@ -27,6 +28,7 @@ const config = {
2728
],
2829
webpackFinal: (storybookBaseConfig: any) => {
2930
storybookBaseConfig.plugins.push(
31+
new MonacoWebpackPlugin(),
3032
new WebpackShellPluginNext({
3133
onBuildStart: {
3234
scripts: ['npm run build:widget'],

package-lock.json

Lines changed: 35 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@
8686
"@gravity-ui/i18n": "^1.0.0",
8787
"@react-spring/web": "^9.7.3",
8888
"ajv": "^8.12.0",
89+
"ajv-keywords": "^5.1.0",
8990
"final-form": "^4.20.9",
9091
"github-buttons": "2.23.0",
92+
"js-yaml-source-map": "^0.2.2",
9193
"lodash": "^4.17.21",
9294
"monaco-editor": "^0.38.0",
9395
"react-final-form": "^6.5.9",
@@ -164,6 +166,7 @@
164166
"js-yaml": "^4.1.0",
165167
"lint-staged": "^11.2.6",
166168
"markdown-loader": "^6.0.0",
169+
"monaco-editor-webpack-plugin": "^7.1.0",
167170
"move-file-cli": "^3.0.0",
168171
"npm-run-all": "^4.1.5",
169172
"postcss": "^8.4.16",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
@import '../../../../styles/variables.scss';
2+
3+
$block: '.#{$ns}code-editor';
4+
5+
#{$block} {
6+
height: 100%;
7+
position: relative;
8+
overflow: hidden;
9+
10+
&_fullscreen {
11+
position: fixed;
12+
top: 0;
13+
left: 0;
14+
width: 100%;
15+
height: 100vh;
16+
z-index: 1000;
17+
background: var(--g-color-base-background);
18+
19+
#{$block}__header {
20+
margin-top: var(--pc-editor-header-height);
21+
}
22+
}
23+
24+
&__code {
25+
width: 100%;
26+
height: 100%;
27+
}
28+
29+
&__header,
30+
&__footer {
31+
padding: 0 $indentS;
32+
background: var(--g-color-base-background);
33+
}
34+
35+
&__header {
36+
display: flex;
37+
align-items: center;
38+
justify-content: flex-end;
39+
height: var(--pc-editor-code-header-height);
40+
41+
border-bottom: 1px solid var(--g-color-line-generic);
42+
}
43+
44+
&__footer {
45+
position: absolute;
46+
left: 0;
47+
bottom: 0;
48+
width: 100%;
49+
min-height: var(--pc-editor-code-header-height);
50+
border-top: 1px solid var(--g-color-line-generic);
51+
}
52+
53+
&__message-container {
54+
max-height: 140px;
55+
padding: 12px;
56+
57+
overflow-y: auto;
58+
59+
font-family: Menlo, Monaco, 'Courier New', monospace;
60+
white-space: pre-wrap;
61+
}
62+
63+
&__message {
64+
&_status {
65+
&_success {
66+
color: var(--g-color-text-positive);
67+
}
68+
69+
&_warning {
70+
color: var(--g-color-text-warning-heavy);
71+
}
72+
73+
&_error {
74+
color: var(--g-color-text-danger);
75+
}
76+
}
77+
}
78+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, {useCallback, useMemo, useState} from 'react';
2+
3+
import {ChevronsCollapseUpRight, ChevronsExpandUpRight} from '@gravity-ui/icons';
4+
import {Button, Icon} from '@gravity-ui/uikit';
5+
import yaml from 'js-yaml';
6+
import MonacoEditor from 'react-monaco-editor';
7+
8+
import {PageContent} from '../../../models';
9+
import {block} from '../../../utils';
10+
import {parseCode} from '../../utils/code';
11+
import {CodeEditorMessageProps} from '../../utils/validation';
12+
13+
import {options} from './constants';
14+
15+
import './CodeEditor.scss';
16+
17+
const b = block('code-editor');
18+
19+
interface CodeEditorProps {
20+
content: PageContent;
21+
fullscreenModeOn: boolean;
22+
validator: (code: string) => CodeEditorMessageProps;
23+
onFullscreenModeOnUpdate: (fullscreenModeOn: boolean) => void;
24+
onChange: (content: PageContent) => void;
25+
message?: CodeEditorMessageProps;
26+
}
27+
28+
export const CodeEditor = ({
29+
content,
30+
onChange,
31+
validator,
32+
fullscreenModeOn,
33+
onFullscreenModeOnUpdate,
34+
}: CodeEditorProps) => {
35+
const value = useMemo(() => yaml.dump(content), [content]);
36+
const [message, setMessage] = useState(() => validator(value));
37+
38+
const onChangeWithValidation = useCallback(
39+
(code: string) => {
40+
const validationResult = validator(code);
41+
42+
setMessage(validationResult);
43+
onChange(parseCode(code));
44+
},
45+
[onChange, validator],
46+
);
47+
48+
return (
49+
<div className={b({fullscreen: fullscreenModeOn})}>
50+
<div className={b('header')}>
51+
<Button
52+
view="flat-secondary"
53+
onClick={() => onFullscreenModeOnUpdate(!fullscreenModeOn)}
54+
>
55+
<Icon
56+
data={fullscreenModeOn ? ChevronsCollapseUpRight : ChevronsExpandUpRight}
57+
size={16}
58+
/>
59+
</Button>
60+
</div>
61+
<div className={b('code')}>
62+
<MonacoEditor
63+
key={String(fullscreenModeOn)}
64+
value={value}
65+
language="yaml"
66+
options={options}
67+
onChange={onChangeWithValidation}
68+
theme="vs"
69+
/>
70+
</div>
71+
<div className={b('footer')}>
72+
{message && (
73+
<div className={b('message-container')}>
74+
<div className={b('message', {status: message.status})}>{message.text}</div>
75+
</div>
76+
)}
77+
</div>
78+
</div>
79+
);
80+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {editor} from 'monaco-editor';
2+
import {monaco} from 'react-monaco-editor';
3+
4+
export const options: monaco.editor.IStandaloneEditorConstructionOptions = {
5+
wordWrap: 'on' as editor.IEditorOptions['wordWrap'],
6+
renderLineHighlight: 'none' as editor.IEditorOptions['renderLineHighlight'],
7+
selectOnLineNumbers: true,
8+
renderWhitespace: 'all',
9+
automaticLayout: true,
10+
minimap: {
11+
enabled: false,
12+
},
13+
overviewRulerLanes: 0,
14+
hideCursorInOverviewRuler: true,
15+
scrollbar: {
16+
vertical: 'hidden',
17+
},
18+
overviewRulerBorder: false,
19+
readOnly: false,
20+
};

src/editor/components/YamlEditor/YamlEditor.scss

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/editor/components/YamlEditor/YamlEditor.tsx

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)