-
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.
chore(demo): added new story; rewritten some other stories (#536)
For `demo` only: 1. Added PlaygroundLayout component 2. Added `useMarkdownEditorValue()` hook 3. Rewritten `CustomCSSVariables`, `EditorInEditor`, `EscapeConfig`, `Ghost`, `GPT` stories 4. Added new story – `PreserveEmptyRows`
- Loading branch information
Showing
14 changed files
with
400 additions
and
198 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,68 @@ | ||
import React, {useEffect} from 'react'; | ||
|
||
import {useUpdate} from 'react-use'; | ||
|
||
import type {MarkdownEditorInstance} from '../../src'; | ||
import {VERSION} from '../../src/version'; | ||
import {useMarkdownEditorValue} from '../hooks/useMarkdownEditorValue'; | ||
import {block} from '../utils/cn'; | ||
|
||
import {WysiwygSelection} from './PMSelection'; | ||
import {WysiwygDevTools} from './ProseMirrorDevTools'; | ||
|
||
import './Playground.scss'; | ||
|
||
export const b = block('playground'); | ||
|
||
export type RenderFn = (props: {className?: string}) => React.ReactNode; | ||
|
||
export type PlaygroundLayoutProps = { | ||
title?: string; | ||
editor: MarkdownEditorInstance; | ||
view: RenderFn; | ||
viewHeight?: React.CSSProperties['height']; | ||
actions?: RenderFn; | ||
style?: React.CSSProperties; | ||
}; | ||
|
||
export const PlaygroundLayout: React.FC<PlaygroundLayoutProps> = function PlaygroundLayout(props) { | ||
const {editor} = props; | ||
|
||
const forceRender = useUpdate(); | ||
const mdMarkup = useMarkdownEditorValue(editor); | ||
|
||
useEffect(() => { | ||
editor.on('change-editor-mode', forceRender); | ||
return () => { | ||
editor.off('change-editor-mode', forceRender); | ||
}; | ||
}, [editor, forceRender]); | ||
|
||
return ( | ||
<div className={b()} style={props.style}> | ||
<div className={b('header')}> | ||
{props.title ?? 'Markdown Editor Playground'} | ||
<span className={b('version')}>{VERSION}</span> | ||
</div> | ||
|
||
<div className={b('actions')}>{props.actions?.({})}</div> | ||
|
||
<hr /> | ||
|
||
<React.StrictMode> | ||
<div className={b('editor')} style={{height: props.viewHeight ?? 'initial'}}> | ||
{props.view({className: b('editor-view')})} | ||
|
||
<WysiwygDevTools editor={editor} /> | ||
<WysiwygSelection editor={editor} className={b('pm-selection')} /> | ||
</div> | ||
</React.StrictMode> | ||
|
||
<hr /> | ||
|
||
<div className={b('preview')}> | ||
{editor.currentMode === 'wysiwyg' && <pre className={b('markup')}>{mdMarkup}</pre>} | ||
</div> | ||
</div> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
import {type MarkdownEditorInstance, type MarkupString, useDebounce} from '../../src'; | ||
|
||
export function useMarkdownEditorValue(editor: MarkdownEditorInstance, delay = 500): MarkupString { | ||
const [value, setValue] = useState(() => editor.getValue()); | ||
|
||
const fn = useDebounce(() => setValue(editor.getValue()), delay); | ||
|
||
useEffect(() => { | ||
editor.on('change', fn); | ||
return () => { | ||
editor.off('change', fn); | ||
}; | ||
}, [editor, fn]); | ||
|
||
return value; | ||
} |
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.