|
| 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