-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathuseMarkdownEditor.ts
109 lines (97 loc) · 3.62 KB
/
useMarkdownEditor.ts
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
import {useLayoutEffect, useMemo} from 'react';
import type {Extension} from '../core';
import {getPMTransformers} from '../core/markdown/ProseMirrorTransformer/getTransformers';
import {ReactRenderStorage} from '../extensions';
import {Logger2, globalLogger} from '../logger';
import {DirectiveSyntaxContext} from '../utils/directive';
import {EditorImpl, type EditorInt} from './Editor';
import type {
MarkdownEditorInstance,
MarkdownEditorMode,
MarkdownEditorOptions,
MarkdownEditorPreset,
} from './types';
import {BundlePreset} from './wysiwyg-preset';
export type UseMarkdownEditorProps = MarkdownEditorOptions;
export function useMarkdownEditor(
props: UseMarkdownEditorProps,
deps: React.DependencyList = [],
): MarkdownEditorInstance {
const editor = useMemo<EditorInt>(() => {
const {
md = {},
initial = {},
handlers = {},
experimental = {},
markupConfig = {},
wysiwygConfig = {},
logger = new Logger2(),
} = props;
const preset: MarkdownEditorPreset = props.preset ?? 'full';
const renderStorage = new ReactRenderStorage();
const pmTransformers = getPMTransformers({
emptyRowTransformer: experimental.preserveEmptyRows,
});
const directiveSyntax = new DirectiveSyntaxContext(experimental.directiveSyntax);
const extensions: Extension = (builder) => {
const extensionOptions = wysiwygConfig.extensionOptions;
builder.use(BundlePreset, {
...extensionOptions,
directiveSyntax,
preset,
reactRenderer: renderStorage,
onCancel: () => {
editor.emit('cancel', null);
return true;
},
onSubmit: () => {
editor.emit('submit', null);
return true;
},
disableMdAttrs: wysiwygConfig.disableMarkdownAttrs,
preserveEmptyRows: experimental.preserveEmptyRows,
placeholderOptions: wysiwygConfig.placeholderOptions,
mdBreaks: md.breaks,
fileUploadHandler: handlers.uploadFile,
needToSetDimensionsForUploadedImages:
experimental.needToSetDimensionsForUploadedImages,
enableNewImageSizeCalculation: experimental.enableNewImageSizeCalculation,
});
{
const extraExtensions = wysiwygConfig.extensions;
if (extraExtensions) {
builder.use(extraExtensions, props.wysiwygConfig?.extensionOptions);
}
}
};
return new EditorImpl({
...props,
logger,
preset,
renderStorage,
directiveSyntax,
pmTransformers,
md,
initial,
handlers,
experimental,
markupConfig,
wysiwygConfig: {
...wysiwygConfig,
extensions,
},
});
}, deps);
useLayoutEffect(() => {
function onToolbarAction({editorMode, id}: {editorMode: MarkdownEditorMode; id: string}) {
globalLogger.action({mode: editorMode, source: 'toolbar', action: id});
editor.logger.action({mode: editorMode, source: 'toolbar', action: id});
}
editor.on('toolbar-action', onToolbarAction);
return () => {
editor.off('toolbar-action', onToolbarAction);
editor.destroy();
};
}, [editor]);
return editor;
}