-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added "Remember the mode" story (#269)
- Loading branch information
1 parent
86fcb00
commit ef0d24b
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import type {Meta, StoryFn} from '@storybook/react'; | ||
|
||
import {EditorMode} from '../src/bundle/Editor'; | ||
|
||
import {Playground as PlaygroundComponent, PlaygroundProps} from './Playground'; | ||
|
||
export default { | ||
title: 'Experiments / Remember the mode', | ||
} as Meta; | ||
|
||
const markup = { | ||
RememberMode: ` | ||
## Remember the мode | ||
MarkdownEditor API provides access to flexible configuration, in this demo, when the page is reloaded, the editor's mode of operation does not change. | ||
For this example, the settings are saved in localStorage, but you can use other methods | ||
`.trim(), | ||
}; | ||
|
||
type PlaygroundStoryProps = Pick< | ||
PlaygroundProps, | ||
| 'initialEditor' | ||
| 'settingsVisible' | ||
| 'breaks' | ||
| 'allowHTML' | ||
| 'linkify' | ||
| 'linkifyTlds' | ||
| 'sanitizeHtml' | ||
| 'prepareRawMarkup' | ||
| 'splitModeOrientation' | ||
| 'stickyToolbar' | ||
| 'initialSplitModeEnabled' | ||
| 'renderPreviewDefined' | ||
| 'height' | ||
>; | ||
|
||
const args: Partial<PlaygroundStoryProps> = { | ||
initialEditor: 'wysiwyg', | ||
settingsVisible: true, | ||
allowHTML: true, | ||
breaks: true, | ||
linkify: true, | ||
linkifyTlds: [], | ||
sanitizeHtml: false, | ||
prepareRawMarkup: false, | ||
splitModeOrientation: 'horizontal', | ||
stickyToolbar: true, | ||
initialSplitModeEnabled: false, | ||
renderPreviewDefined: true, | ||
height: 'initial', | ||
}; | ||
|
||
export const RememberMode: StoryFn<PlaygroundStoryProps> = (props) => { | ||
const [mode, setMode] = useState<EditorMode>(); | ||
const [splitModeEnabled, setSplitModeEnabled] = useState<boolean>(); | ||
|
||
const handleChangeEditorType = (mode: EditorMode) => { | ||
localStorage.setItem('markdownEditorMode', mode); | ||
}; | ||
|
||
const handleChangeSplitModeEnabled = (enabled: boolean) => { | ||
localStorage.setItem('markdownEditorSplitModeEnabled', enabled.toString()); | ||
}; | ||
|
||
useEffect(() => { | ||
const storedMode = localStorage.getItem('markdownEditorMode') || 'wysiwyg'; | ||
const storedSplitModeEnabled = localStorage.getItem('markdownEditorSplitModeEnabled'); | ||
|
||
if (storedMode) { | ||
setMode(storedMode as EditorMode); | ||
setSplitModeEnabled(storedSplitModeEnabled === 'true'); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{mode && ( | ||
<PlaygroundComponent | ||
{...props} | ||
onChangeEditorType={handleChangeEditorType} | ||
initialEditor={mode} | ||
initialSplitModeEnabled={splitModeEnabled} | ||
onChangeSplitModeEnabled={handleChangeSplitModeEnabled} | ||
initial={markup.RememberMode} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
RememberMode.args = args; | ||
RememberMode.storyName = 'Playground'; |