Skip to content

Commit ef0d24b

Browse files
authored
added "Remember the mode" story (#269)
1 parent 86fcb00 commit ef0d24b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

demo/Playground.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export type PlaygroundProps = {
6767
initialSplitModeEnabled?: boolean;
6868
renderPreviewDefined?: boolean;
6969
height?: CSSProperties['height'];
70+
onChangeEditorType?: (mode: MarkdownEditorMode) => void;
71+
onChangeSplitModeEnabled?: (splitModeEnabled: boolean) => void;
7072
};
7173

7274
logger.setLogger({
@@ -169,12 +171,14 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
169171
setMdRaw(mdEditor.getValue());
170172
}
171173
function onChangeEditorType({mode}: {mode: MarkdownEditorMode}) {
174+
props.onChangeEditorType?.(mode);
172175
setEditorMode(mode);
173176
}
174177
const onToolbarAction = ({id, editorMode: type}: ToolbarActionData) => {
175178
console.info(`The '${id}' action is performed in the ${type}-editor.`);
176179
};
177180
function onChangeSplitModeEnabled({splitModeEnabled}: {splitModeEnabled: boolean}) {
181+
props.onChangeSplitModeEnabled?.(splitModeEnabled);
178182
console.info(`Split mode enabled: ${splitModeEnabled}`);
179183
}
180184
function onChangeToolbarVisibility({visible}: {visible: boolean}) {

demo/RememberMode.stories.tsx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import React, {useEffect, useState} from 'react';
2+
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import type {Meta, StoryFn} from '@storybook/react';
5+
6+
import {EditorMode} from '../src/bundle/Editor';
7+
8+
import {Playground as PlaygroundComponent, PlaygroundProps} from './Playground';
9+
10+
export default {
11+
title: 'Experiments / Remember the mode',
12+
} as Meta;
13+
14+
const markup = {
15+
RememberMode: `
16+
## Remember the мode
17+
18+
MarkdownEditor API provides access to flexible configuration, in this demo, when the page is reloaded, the editor's mode of operation does not change.
19+
20+
For this example, the settings are saved in localStorage, but you can use other methods
21+
22+
`.trim(),
23+
};
24+
25+
type PlaygroundStoryProps = Pick<
26+
PlaygroundProps,
27+
| 'initialEditor'
28+
| 'settingsVisible'
29+
| 'breaks'
30+
| 'allowHTML'
31+
| 'linkify'
32+
| 'linkifyTlds'
33+
| 'sanitizeHtml'
34+
| 'prepareRawMarkup'
35+
| 'splitModeOrientation'
36+
| 'stickyToolbar'
37+
| 'initialSplitModeEnabled'
38+
| 'renderPreviewDefined'
39+
| 'height'
40+
>;
41+
42+
const args: Partial<PlaygroundStoryProps> = {
43+
initialEditor: 'wysiwyg',
44+
settingsVisible: true,
45+
allowHTML: true,
46+
breaks: true,
47+
linkify: true,
48+
linkifyTlds: [],
49+
sanitizeHtml: false,
50+
prepareRawMarkup: false,
51+
splitModeOrientation: 'horizontal',
52+
stickyToolbar: true,
53+
initialSplitModeEnabled: false,
54+
renderPreviewDefined: true,
55+
height: 'initial',
56+
};
57+
58+
export const RememberMode: StoryFn<PlaygroundStoryProps> = (props) => {
59+
const [mode, setMode] = useState<EditorMode>();
60+
const [splitModeEnabled, setSplitModeEnabled] = useState<boolean>();
61+
62+
const handleChangeEditorType = (mode: EditorMode) => {
63+
localStorage.setItem('markdownEditorMode', mode);
64+
};
65+
66+
const handleChangeSplitModeEnabled = (enabled: boolean) => {
67+
localStorage.setItem('markdownEditorSplitModeEnabled', enabled.toString());
68+
};
69+
70+
useEffect(() => {
71+
const storedMode = localStorage.getItem('markdownEditorMode') || 'wysiwyg';
72+
const storedSplitModeEnabled = localStorage.getItem('markdownEditorSplitModeEnabled');
73+
74+
if (storedMode) {
75+
setMode(storedMode as EditorMode);
76+
setSplitModeEnabled(storedSplitModeEnabled === 'true');
77+
}
78+
}, []);
79+
80+
return (
81+
<>
82+
{mode && (
83+
<PlaygroundComponent
84+
{...props}
85+
onChangeEditorType={handleChangeEditorType}
86+
initialEditor={mode}
87+
initialSplitModeEnabled={splitModeEnabled}
88+
onChangeSplitModeEnabled={handleChangeSplitModeEnabled}
89+
initial={markup.RememberMode}
90+
/>
91+
)}
92+
</>
93+
);
94+
};
95+
96+
RememberMode.args = args;
97+
RememberMode.storyName = 'Playground';

0 commit comments

Comments
 (0)