-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreateEditor.ts
83 lines (76 loc) · 2.36 KB
/
createEditor.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
import {
withBreaksOnExpandedSelection,
withBreaksOnVoidNodes,
withInlineVoid,
withNormalization,
withUserFriendlyDeleteBehavior,
} from '@prezly/slate-commons';
import type { Extension, WithOverrides } from '@prezly/slate-commons';
import { isNotUndefined } from '@technically/is-not-undefined';
import { flow } from '@technically/lodash';
import { type SlateEditor } from '@udecode/plate';
import { createPlateEditor } from '@udecode/plate/react';
import { type PlatePlugin } from '@udecode/plate/react';
import { createParagraph } from '#extensions/paragraphs';
import { withNodesHierarchy, hierarchySchema } from '#modules/nodes-hierarchy';
import {
DefaultTextBlockPlugin,
withDeserializeHtml,
RichBlocksPlugin,
ElementsEqualityCheckPlugin,
} from './plugins';
import { type Value } from './types';
type Params = {
initialValue?: Value;
plugins?: PlatePlugin[];
baseEditor?: SlateEditor;
/**
* @deprecated It is planned to migrate extensions to become Plate Plugins
*/
getExtensions?: () => Extension[];
/**
* @deprecated It is recommended to migrate these overrides to become Plate Plugins
*/
withOverrides?: WithOverrides[];
};
export function createEditor({
initialValue,
plugins = [],
baseEditor,
getExtensions = noExtensions,
withOverrides = [],
}: Params) {
const editor = createPlateEditor({
editor: baseEditor,
plugins: [
...plugins,
DefaultTextBlockPlugin.configure({
options: {
createDefaultTextBlock: createParagraph,
},
}),
ElementsEqualityCheckPlugin,
RichBlocksPlugin.configure({
options: { getExtensions },
}),
],
value: initialValue,
});
const extensionsOverrides = getExtensions()
.map((extension) => extension.withOverrides)
.filter(isNotUndefined);
return flow([
...withOverrides,
withNodesHierarchy(hierarchySchema),
withBreaksOnExpandedSelection,
withBreaksOnVoidNodes,
withInlineVoid(getExtensions),
withNormalization(getExtensions),
withUserFriendlyDeleteBehavior,
withDeserializeHtml(getExtensions),
...extensionsOverrides,
])(editor);
}
function noExtensions(): Extension[] {
return [];
}