Skip to content

Commit 7a8e187

Browse files
Kyzyl-oolkkmch
andauthored
feat: yaml tab in editor (read-only) (#449)
* feat: added yaml tab in editor --------- Co-authored-by: kkmch <[email protected]>
1 parent 4b66248 commit 7a8e187

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

package-lock.json

Lines changed: 27 additions & 0 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
@@ -87,7 +87,9 @@
8787
"final-form": "^4.20.9",
8888
"github-buttons": "2.23.0",
8989
"lodash": "^4.17.21",
90+
"monaco-editor": "^0.38.0",
9091
"react-final-form": "^6.5.9",
92+
"react-monaco-editor": "^0.53.0",
9193
"react-player": "^2.9.0",
9294
"react-slick": "^0.29.0",
9395
"react-spring": "^9.3.0",
@@ -128,6 +130,7 @@
128130
"@testing-library/react": "^13.4.0",
129131
"@testing-library/user-event": "^14.4.3",
130132
"@types/jest": "^29.2.4",
133+
"@types/js-yaml": "^4.0.5",
131134
"@types/json-schema": "^7.0.12",
132135
"@types/lodash": "^4.14.176",
133136
"@types/react": "^18.0.27",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@import '../../../../styles/variables.scss';
2+
3+
$block: '.#{$ns}yaml-editor';
4+
5+
#{$block} {
6+
height: 100%;
7+
position: relative;
8+
9+
&__copy-button {
10+
position: absolute;
11+
top: 12px;
12+
right: 12px;
13+
14+
&_hidden {
15+
visibility: hidden;
16+
}
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, {useMemo} from 'react';
2+
3+
import {ClipboardButton} from '@gravity-ui/uikit';
4+
import yaml from 'js-yaml';
5+
import MonacoEditor, {monaco} from 'react-monaco-editor';
6+
7+
import {PageContent} from '../../../models';
8+
import {block} from '../../../utils';
9+
10+
import './YamlEditor.scss';
11+
12+
const b = block('yaml-editor');
13+
14+
interface YamlEditorProps {
15+
content: PageContent;
16+
}
17+
18+
export const YamlEditor = ({content}: YamlEditorProps) => {
19+
const value = useMemo(() => {
20+
return yaml.dump(content);
21+
}, [content]);
22+
23+
const options: monaco.editor.IStandaloneEditorConstructionOptions = useMemo(() => {
24+
return {
25+
minimap: {
26+
enabled: false,
27+
},
28+
renderWhitespace: 'all',
29+
overviewRulerLanes: 0,
30+
hideCursorInOverviewRuler: true,
31+
scrollbar: {
32+
vertical: 'hidden',
33+
},
34+
overviewRulerBorder: false,
35+
readOnly: true,
36+
};
37+
}, []);
38+
39+
return (
40+
<div className={b()}>
41+
<MonacoEditor value={value} language="yaml" options={options} />
42+
<ClipboardButton className={b('copy-button')} text={value} />
43+
</div>
44+
);
45+
};

src/editor/containers/Form/Form.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ $block: '.#{$ns}editor-form';
1919
&__block-form {
2020
margin-bottom: $indentXS;
2121
}
22+
23+
&_yaml-editor-enabled {
24+
height: 100%;
25+
}
2226
}

src/editor/containers/Form/Form.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {Block, PageContent} from '../../../models';
77
import {block, getBlockKey} from '../../../utils';
88
import {BlockForm} from '../../components/BlockForm/BlockForm';
99
import {PagePropsForm, PagePropsFormData} from '../../components/PagePropsForm/PagePropsForm';
10+
import {YamlEditor} from '../../components/YamlEditor/YamlEditor';
1011
import {FormSpecs} from '../../dynamic-forms-custom/parser/types';
1112

1213
import './Form.scss';
1314

1415
enum FormTab {
1516
Blocks = 'blocks',
1617
Page = 'page',
18+
Yaml = 'yaml',
1719
}
1820

1921
const b = block('editor-form');
@@ -83,10 +85,14 @@ export const Form = memo(({content, onChange, activeBlockIndex, onSelect, spec}:
8385
);
8486
break;
8587
}
88+
case FormTab.Yaml: {
89+
form = <YamlEditor content={content} />;
90+
break;
91+
}
8692
}
8793

8894
return (
89-
<div className={b()}>
95+
<div className={b({'yaml-editor-enabled': activeTab === FormTab.Yaml})}>
9096
<Tabs
9197
activeTab={activeTab}
9298
className={b('tabs')}

0 commit comments

Comments
 (0)