-
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 `preset` prop to `useMarkdownEditor` hook.\ The preset specifies a set of plug-in extensions.\ Preset value can be `'zero' | 'commonmark' | 'default' | 'yfm' | 'full'`. `'zero' | 'commonmark' | 'default'` – includes functionality similar to the markdown-it presets.\ `'yfm'` – YFM functionality `'full'` – previously default functionality Default value – `'full'`
- Loading branch information
Showing
13 changed files
with
719 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React, {CSSProperties, useCallback, useEffect} from 'react'; | ||
|
||
import {toaster} from '@gravity-ui/uikit/toaster-singleton-react-18'; | ||
|
||
import {MarkupString, logger} from '../src'; | ||
import { | ||
MarkdownEditorMode, | ||
MarkdownEditorPreset, | ||
MarkdownEditorView, | ||
useMarkdownEditor, | ||
} from '../src/bundle'; | ||
import type {RenderPreview} from '../src/bundle/Editor'; | ||
import type {FileUploadHandler} from '../src/utils/upload'; | ||
import {VERSION} from '../src/version'; | ||
|
||
import {WysiwygSelection} from './PMSelection'; | ||
import {WysiwygDevTools} from './ProseMirrorDevTools'; | ||
import {SplitModePreview} from './SplitModePreview'; | ||
import {block} from './cn'; | ||
import {randomDelay} from './delay'; | ||
import {plugins} from './md-plugins'; | ||
|
||
import './Playground.scss'; | ||
|
||
const b = block('playground'); | ||
const fileUploadHandler: FileUploadHandler = async (file) => { | ||
console.info('[PresetDemo] Uploading file: ' + file.name); | ||
await randomDelay(1000, 3000); | ||
return {url: URL.createObjectURL(file)}; | ||
}; | ||
|
||
export type PresetDemoProps = { | ||
preset: MarkdownEditorPreset; | ||
allowHTML?: boolean; | ||
settingsVisible?: boolean; | ||
breaks?: boolean; | ||
linkify?: boolean; | ||
linkifyTlds?: string | string[]; | ||
splitModeOrientation?: 'horizontal' | 'vertical' | false; | ||
stickyToolbar?: boolean; | ||
height?: CSSProperties['height']; | ||
}; | ||
|
||
logger.setLogger({ | ||
metrics: console.info, | ||
action: (data) => console.info(`Action: ${data.action}`, data), | ||
...console, | ||
}); | ||
|
||
export const PresetDemo = React.memo<PresetDemoProps>((props) => { | ||
const { | ||
preset, | ||
settingsVisible, | ||
allowHTML, | ||
breaks, | ||
linkify, | ||
linkifyTlds, | ||
splitModeOrientation, | ||
stickyToolbar, | ||
height, | ||
} = props; | ||
const [editorMode, setEditorMode] = React.useState<MarkdownEditorMode>('wysiwyg'); | ||
const [mdRaw, setMdRaw] = React.useState<MarkupString>(''); | ||
|
||
const renderPreview = useCallback<RenderPreview>( | ||
({getValue}) => ( | ||
<SplitModePreview | ||
getValue={getValue} | ||
allowHTML={allowHTML} | ||
linkify={linkify} | ||
linkifyTlds={linkifyTlds} | ||
breaks={breaks} | ||
needToSanitizeHtml | ||
plugins={plugins} | ||
/> | ||
), | ||
[allowHTML, breaks, linkify, linkifyTlds], | ||
); | ||
|
||
const mdEditor = useMarkdownEditor({ | ||
preset, | ||
allowHTML, | ||
linkify, | ||
linkifyTlds, | ||
breaks: breaks ?? true, | ||
initialSplitModeEnabled: true, | ||
initialToolbarVisible: true, | ||
splitMode: splitModeOrientation, | ||
renderPreview, | ||
fileUploadHandler, | ||
}); | ||
|
||
useEffect(() => { | ||
function onChange() { | ||
setMdRaw(mdEditor.getValue()); | ||
} | ||
function onChangeEditorType({mode}: {mode: MarkdownEditorMode}) { | ||
setEditorMode(mode); | ||
} | ||
|
||
mdEditor.on('change', onChange); | ||
mdEditor.on('change-editor-mode', onChangeEditorType); | ||
|
||
return () => { | ||
mdEditor.off('change', onChange); | ||
mdEditor.off('change-editor-mode', onChangeEditorType); | ||
}; | ||
}, [mdEditor]); | ||
|
||
return ( | ||
<div className={b()}> | ||
<div className={b('header')}> | ||
Markdown Editor ({preset} preset) | ||
<span className={b('version')}>{VERSION}</span> | ||
</div> | ||
<hr /> | ||
<React.StrictMode> | ||
<div className={b('editor')} style={{height: height ?? 'initial'}}> | ||
<MarkdownEditorView | ||
autofocus | ||
toaster={toaster} | ||
className={b('editor-view')} | ||
stickyToolbar={Boolean(stickyToolbar)} | ||
settingsVisible={settingsVisible} | ||
editor={mdEditor} | ||
/> | ||
<WysiwygDevTools editor={mdEditor} /> | ||
<WysiwygSelection editor={mdEditor} className={b('pm-selection')} /> | ||
</div> | ||
</React.StrictMode> | ||
|
||
<hr /> | ||
|
||
<div className={b('preview')}> | ||
{editorMode === 'wysiwyg' && <pre className={b('markup')}>{mdRaw}</pre>} | ||
</div> | ||
</div> | ||
); | ||
}); | ||
|
||
PresetDemo.displayName = 'PresetDemo'; |
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,58 @@ | ||
import React from 'react'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import type {Meta, StoryFn} from '@storybook/react'; | ||
|
||
import {PresetDemo, PresetDemoProps} from './PresetDemo'; | ||
|
||
export default { | ||
title: 'Markdown Editor / Presets', | ||
} as Meta; | ||
|
||
type PlaygroundStoryProps = Pick< | ||
PresetDemoProps, | ||
| 'settingsVisible' | ||
| 'breaks' | ||
| 'allowHTML' | ||
| 'linkify' | ||
| 'linkifyTlds' | ||
| 'splitModeOrientation' | ||
| 'stickyToolbar' | ||
| 'height' | ||
>; | ||
|
||
const args: Partial<PlaygroundStoryProps> = { | ||
settingsVisible: true, | ||
allowHTML: true, | ||
breaks: true, | ||
linkify: true, | ||
linkifyTlds: [], | ||
splitModeOrientation: 'horizontal', | ||
stickyToolbar: true, | ||
height: 'initial', | ||
}; | ||
|
||
export const Zero: StoryFn<PlaygroundStoryProps> = (props) => ( | ||
<PresetDemo {...props} preset="zero" /> | ||
); | ||
|
||
export const CommonMark: StoryFn<PlaygroundStoryProps> = (props) => ( | ||
<PresetDemo {...props} preset="commonmark" /> | ||
); | ||
|
||
export const Default: StoryFn<PlaygroundStoryProps> = (props) => ( | ||
<PresetDemo {...props} preset="default" /> | ||
); | ||
|
||
export const YFM: StoryFn<PlaygroundStoryProps> = (props) => <PresetDemo {...props} preset="yfm" />; | ||
|
||
export const Full: StoryFn<PlaygroundStoryProps> = (props) => ( | ||
<PresetDemo {...props} preset="full" /> | ||
); | ||
|
||
Zero.args = args; | ||
CommonMark.args = args; | ||
CommonMark.storyName = 'CommonMark'; | ||
Default.args = args; | ||
YFM.args = args; | ||
Full.args = args; |
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
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
Oops, something went wrong.