Skip to content

Commit 9594170

Browse files
author
k.golikov
committed
HTML editor: add 2 view modes
1 parent 8bbeb83 commit 9594170

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Dispatch, SetStateAction, useCallback } from 'react';
2+
3+
const useChangeValueStateHandler = <S>(setState: Dispatch<SetStateAction<S>>) => {
4+
return useCallback(
5+
(value: S) => {
6+
return () => {
7+
setState(value);
8+
};
9+
},
10+
[setState]
11+
);
12+
};
13+
14+
export default useChangeValueStateHandler;

src/pages/htmlEditorPage/HtmlEditorPage.module.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
.col {
1010
display: flex;
1111
flex-direction: column;
12+
13+
transition-property: flex-basis;
14+
transition-duration: 0.2s;
1215
}
1316

1417
.leftCol {

src/pages/htmlEditorPage/HtmlEditorPage.tsx

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { CSSProperties, FunctionComponent, useCallback } from 'react';
22
import PageContainer from '../../layouts/pages/pageContainer/PageContainer';
33
import styles from './HtmlEditorPage.module.scss';
4-
import { Col, Row, Tabs } from 'antd';
4+
import { Button, Col, Row, Tabs } from 'antd';
55
import { useLocalstorageState } from 'rooks';
66
import getLocalStorageKey from '../../utils/getLocalStorageKey';
77
import useChangeStateHandler from '../../hooks/useChangeStateHandler';
@@ -14,6 +14,9 @@ import Javascript from '@mui/icons-material/Javascript';
1414
import { OnMount } from '@monaco-editor/react';
1515
import { emmetCSS, emmetHTML, emmetJSX } from 'emmet-monaco-es';
1616
import { editor } from 'monaco-editor';
17+
import ButtonGroup from 'antd/lib/button/button-group';
18+
import { VerticalSplit, ViewHeadline } from '@mui/icons-material';
19+
import useChangeValueStateHandler from '../../hooks/useChangeValueStateHandler';
1720

1821
enum EditorTab {
1922
HTML = 'html',
@@ -58,6 +61,23 @@ const editorOptions: editor.IStandaloneEditorConstructionOptions = {
5861
minimap: { enabled: false }
5962
};
6063

64+
enum ViewMode {
65+
EDITOR = 'EDITOR',
66+
SPLIT = 'SPLIT',
67+
VIEW = 'VIEW'
68+
}
69+
70+
// const viewModes: CheckboxOptionType[] = [
71+
// {
72+
// value: ViewMode.EDITOR,
73+
// label: <ViewHeadline/>
74+
// },
75+
// {
76+
// value: ViewMode.SPLIT,
77+
// label: <VerticalSplit/>
78+
// }
79+
// ];
80+
6181
const HtmlEditorPage: FunctionComponent = () => {
6282
const [sources, setSources] = useLocalstorageState<EditorSources>(
6383
getLocalStorageKey('html-editor', 'sources'),
@@ -69,12 +89,19 @@ const HtmlEditorPage: FunctionComponent = () => {
6989
EditorTab.HTML
7090
);
7191

92+
const [viewMode, setViewMode] = useLocalstorageState<ViewMode>(
93+
getLocalStorageKey('html-editor', 'viewMode'),
94+
ViewMode.SPLIT
95+
);
96+
7297
const handleSourceChange = useChangeStateHandler(setSources);
7398

7499
const handleEditorTabChange = useCallback((tab: string) => {
75100
setEditorTab(tab as EditorTab);
76101
}, []);
77102

103+
const handleViewModeChange = useChangeValueStateHandler(setViewMode);
104+
78105
const resultSource = useDebouncedMemo(
79106
{ sources },
80107
({ sources }) => {
@@ -99,16 +126,47 @@ ${sources.js}
99126
emmetJSX(monaco);
100127
}, []);
101128

129+
const leftColSpan = {
130+
[ViewMode.EDITOR]: 24,
131+
[ViewMode.SPLIT]: 12,
132+
[ViewMode.VIEW]: 0
133+
}[viewMode];
134+
135+
const rightColSpan = {
136+
[ViewMode.EDITOR]: 0,
137+
[ViewMode.SPLIT]: 12,
138+
[ViewMode.VIEW]: 24
139+
}[viewMode];
140+
102141
return (
103142
<PageContainer noPadding className={styles.container}>
104143
<Row className={styles.containerRow}>
105-
<Col span={12} className={classNames(styles.col, styles.leftCol)}>
144+
<Col
145+
span={leftColSpan}
146+
className={classNames(styles.col, styles.leftCol, { 'd-none': viewMode === ViewMode.VIEW })}
147+
>
106148
<Tabs
107149
activeKey={editorTab}
108150
onChange={handleEditorTabChange}
109151
tabBarStyle={tabBarStyle}
110152
className={styles.editorTabs}
111153
tabBarGutter={10}
154+
tabBarExtraContent={
155+
<ButtonGroup>
156+
<Button
157+
type="text"
158+
icon={<ViewHeadline />}
159+
className={classNames({ 'antd-text-primary': viewMode === ViewMode.EDITOR })}
160+
onClick={handleViewModeChange(ViewMode.EDITOR)}
161+
/>
162+
<Button
163+
type="text"
164+
icon={<VerticalSplit />}
165+
className={classNames({ 'antd-text-primary': viewMode === ViewMode.SPLIT })}
166+
onClick={handleViewModeChange(ViewMode.SPLIT)}
167+
/>
168+
</ButtonGroup>
169+
}
112170
>
113171
<Tabs.TabPane
114172
tab={
@@ -166,7 +224,12 @@ ${sources.js}
166224
</Tabs.TabPane>
167225
</Tabs>
168226
</Col>
169-
<Col span={12} className={classNames('yui3-cssreset', styles.col, styles.rightCol)}>
227+
<Col
228+
span={rightColSpan}
229+
className={classNames('yui3-cssreset', styles.col, styles.rightCol, {
230+
'd-none': viewMode === ViewMode.EDITOR
231+
})}
232+
>
170233
<iframe srcDoc={resultSource} className={styles.resultFrame} />
171234
</Col>
172235
</Row>

src/styles/main.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,18 @@ $monacoBackgroundDark: rgb(30, 30, 30);
7474
}
7575

7676
$contentHeight: calc(100vh - 64px - 76px);
77+
78+
$primaryColorLight: #1890ff;
79+
$primaryColorDark: rgb(23, 125, 220);
80+
81+
@mixin primaryColor {
82+
color: $primaryColorLight;
83+
84+
@include themeDark {
85+
color: $primaryColorDark;
86+
}
87+
}
88+
89+
.antd-text-primary {
90+
@include primaryColor;
91+
}

0 commit comments

Comments
 (0)