Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add normalizeInitialValue prop to Plate #1063

Merged
merged 8 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-years-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-core": minor
---

add `normalizeInitialValue` prop to `Plate`. When `true`, it will normalize the initial value passed to the `editor` once it's created. This is useful when adding normalization rules on already existing content. Default is `false`.
6 changes: 6 additions & 0 deletions docs/docs/guides/Plate.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ props if `editor` is defined.

- Initial value of the editor.

### `normalizeInitialValue`

- When `true`, it will normalize the initial value passed to the `editor` once it gets created.
- This is useful when adding normalization rules on already existing content.
- Default is `false`.

### `options`

- Options stored by plugin key.
Expand Down
88 changes: 88 additions & 0 deletions packages/core/src/components/Plate.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';
import { render } from '@testing-library/react';
import { isEqual, memoize } from 'lodash';
import { createEditor, Editor, NodeEntry, Transforms } from 'slate';
import { PlatePlugin, TEditor } from '../types';
import { Plate } from './Plate';

describe('Plate', () => {
Expand All @@ -9,3 +12,88 @@ describe('Plate', () => {
expect(1).toBe(1);
});
});

describe('Plate', () => {
it('should trigger normalize if normalizeInitialValue set', () => {
const fn = jest.fn((e: TEditor, [node, path]: NodeEntry) => {
if (
Editor.isBlock(e, node) &&
path?.length &&
!isEqual((node as any).path, path)
) {
Transforms.setNodes(e, { path } as any, { at: path });
}
});

const plugins: PlatePlugin[] = memoize((): PlatePlugin[] => [
{
withOverrides: (e) => {
const { normalizeNode } = e;
e.normalizeNode = (n: NodeEntry) => {
fn(e, n);
normalizeNode(n);
};
return e;
},
},
])();

const editor = createEditor();

render(
<Plate
editor={editor as any}
plugins={plugins}
normalizeInitialValue
initialValue={[{ children: [{ text: '' }] }]}
/>
);

expect(fn).toBeCalled();

expect(editor.children).toStrictEqual([
{ children: [{ text: '' }], path: [0] },
]);
});

it('should not trigger normalize if normalizeInitialValue is not set to true', () => {
const fn = jest.fn((e: TEditor, [node, path]: NodeEntry) => {
if (
Editor.isBlock(e, node) &&
path?.length &&
!isEqual((node as any).path, path)
) {
Transforms.setNodes(e, { path } as any, { at: path });
}
});

const plugins: PlatePlugin[] = memoize((): PlatePlugin[] => [
{
withOverrides: (e) => {
const { normalizeNode } = e;
e.normalizeNode = (n: NodeEntry) => {
fn(e, n);
normalizeNode(n);
};
return e;
},
},
])();

const editor = createEditor();

render(
<Plate
editor={editor as any}
plugins={plugins}
initialValue={[{ children: [{ text: '' }] }]}
/>
);

expect(fn).not.toBeCalled();

expect(editor.children).not.toStrictEqual([
{ children: [{ text: '' }], path: [0] },
]);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/hooks/usePlate/usePlate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const usePlate = <T extends SPEditor = SPEditor>({
plugins,
onChange,
editableProps,
normalizeInitialValue,
}: UsePlateOptions<T>) => {
usePlateEffects({
id,
Expand All @@ -27,6 +28,7 @@ export const usePlate = <T extends SPEditor = SPEditor>({
editor,
value,
options,
normalizeInitialValue,
});

return {
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/hooks/usePlate/usePlateEffects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { createEditor } from 'slate';
import { createEditor, Editor } from 'slate';
import { createHistoryPlugin } from '../../plugins/createHistoryPlugin';
import { createReactPlugin } from '../../plugins/createReactPlugin';
import { usePlateActions } from '../../stores/plate/plate.actions';
Expand All @@ -24,6 +24,7 @@ export const usePlateEffects = <T extends SPEditor = SPEditor>({
components,
options,
initialValue,
normalizeInitialValue,
plugins,
}: UsePlateEffectsOptions<T>) => {
const {
Expand Down Expand Up @@ -114,4 +115,10 @@ export const usePlateEffects = <T extends SPEditor = SPEditor>({
storePlugins,
setEditor,
]);

useEffect(() => {
if (storeEditor && normalizeInitialValue) {
Editor.normalize(storeEditor, { force: true });
}
}, [storeEditor, normalizeInitialValue]);
};
7 changes: 7 additions & 0 deletions packages/core/src/types/UsePlateEffectsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ export interface UsePlateEffectsOptions<T extends SPEditor = SPEditor>
* @see {@link EditorId}
*/
components?: Record<string, PlatePluginComponent>;

/**
* When `true`, it will normalize the initial value passed to the `editor` once it gets created.
* This is useful when adding normalization rules on already existing content.
* @default false
*/
normalizeInitialValue?: boolean;
}