Skip to content

Commit

Permalink
Merge pull request #116 from prezly/refactor/editor-with-overrides
Browse files Browse the repository at this point in the history
Move `Extension.withOverrides` support into `createEditorV4()`
  • Loading branch information
e1himself authored Feb 23, 2022
2 parents fb18d41 + 3efd5a3 commit b760c3a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/slate-commons/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export type { OnDOMBeforeInput } from './OnDOMBeforeInput';
export type { OnKeyDown } from './OnKeyDown';
export type { RenderElement } from './RenderElement';
export type { RenderLeaf } from './RenderLeaf';
export type { WithOverrides } from './WithOverrides';
10 changes: 8 additions & 2 deletions packages/slate-editor/src/modules/editor-v4/createEditorV4.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Extension } from '@prezly/slate-commons';
import {
withBreaksOnExpandedSelection,
withBreaksOnVoidNodes,
withInlineVoid,
withNormalization,
withUserFriendlyDeleteBehavior,
} from '@prezly/slate-commons';
import type { Extension } from '@prezly/slate-commons';
import type { WithOverrides } from '@prezly/slate-commons';
import type { Editor } from 'slate';
import { withHistory } from 'slate-history';
import { withReact } from 'slate-react';
Expand All @@ -28,8 +29,12 @@ import {
export function createEditorV4(
baseEditor: Editor,
getExtensions: () => Extension[],
plugins: (<T extends Editor>(editor: T) => T)[],
plugins: WithOverrides[],
) {
const overrides = getExtensions()
.map(({ withOverrides }) => withOverrides)
.filter((o): o is WithOverrides => Boolean(o));

return flow([
withReact,
withHistory,
Expand All @@ -49,6 +54,7 @@ export function createEditorV4(
withRichFormatting,
withImages,
withFilePasting(getExtensions),
...overrides,
...plugins,
])(baseEditor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ export function useCreateEditor({
const [userPlugins] = useState(plugins);
const finalPlugins = useMemo(() => [withEvents(events), ...userPlugins], [userPlugins, events]);
const editor = useMemo(() => {
const editor = createEditor();

getExtensions().forEach((ext) => ext.withOverrides?.(editor));

return createEditorV4(editor, getExtensions, finalPlugins);
return createEditorV4(createEditor(), getExtensions, finalPlugins);
}, [getExtensions, finalPlugins]);

useEffect(() => {
Expand Down

0 comments on commit b760c3a

Please sign in to comment.