-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPreset.tsx
159 lines (143 loc) · 4.81 KB
/
Preset.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {StrictMode, memo, useCallback, useEffect, useState} from 'react';
import {
type MarkdownEditorMode,
type MarkdownEditorPreset,
MarkdownEditorView,
type MarkupString,
type RenderPreview,
useMarkdownEditor,
} from 'src/index';
import type {ToolbarsPreset} from 'src/modules/toolbars/types';
import type {FileUploadHandler} from 'src/utils/upload';
import {VERSION} from 'src/version';
import {WysiwygSelection} from '../../components/PMSelection';
import {WysiwygDevTools} from '../../components/ProseMirrorDevTools';
import {SplitModePreview} from '../../components/SplitModePreview';
import {plugins} from '../../defaults/md-plugins';
import {useLogs} from '../../hooks/useLogs';
import {block} from '../../utils/cn';
import {randomDelay} from '../../utils/delay';
import {parseInsertedUrlAsImage} from '../../utils/imageUrl';
import '../../components/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?: React.CSSProperties['height'];
toolbarsPreset?: ToolbarsPreset;
};
export const Preset = memo<PresetDemoProps>((props) => {
const {
preset,
settingsVisible,
allowHTML,
breaks,
linkify,
linkifyTlds,
splitModeOrientation,
stickyToolbar,
height,
toolbarsPreset,
} = props;
const [editorMode, setEditorMode] = useState<MarkdownEditorMode>('wysiwyg');
const [mdRaw, setMdRaw] = useState<MarkupString>('');
const renderPreview = useCallback<RenderPreview>(
({getValue, md}) => (
<SplitModePreview
getValue={getValue}
allowHTML={md.html}
linkify={md.linkify}
linkifyTlds={md.linkifyTlds}
breaks={md.breaks}
needToSanitizeHtml
plugins={plugins}
/>
),
[],
);
const mdEditor = useMarkdownEditor({
preset,
md: {
html: allowHTML,
linkify,
linkifyTlds,
breaks: breaks ?? true,
},
handlers: {
uploadFile: fileUploadHandler,
},
initial: {
toolbarVisible: true,
splitModeEnabled: true,
},
wysiwygConfig: {
disableMarkdownAttrs: true,
extensionOptions: {
imgSize: {
parseInsertedUrlAsImage,
},
},
},
markupConfig: {
splitMode: splitModeOrientation,
renderPreview,
parseInsertedUrlAsImage,
},
});
useLogs(mdEditor.logger);
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 />
<div className={b('editor-markup')}>
<StrictMode>
<div className={b('editor')} style={{height: height ?? 'initial'}}>
<MarkdownEditorView
autofocus
toolbarsPreset={toolbarsPreset}
className={b('editor-view')}
stickyToolbar={Boolean(stickyToolbar)}
settingsVisible={settingsVisible}
editor={mdEditor}
/>
<WysiwygDevTools editor={mdEditor} />
<WysiwygSelection editor={mdEditor} className={b('pm-selection')} />
</div>
</StrictMode>
<hr />
<div className={b('preview')}>
{editorMode === 'wysiwyg' && <pre className={b('markup')}>{mdRaw}</pre>}
</div>
</div>
</div>
);
});
Preset.displayName = 'PresetDemo';