From f8fd55f1c21f5df800ce94c71302d4edf1c252d5 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Fri, 22 Dec 2023 16:13:03 +0000 Subject: [PATCH 1/2] Fix: `readOnly` on Plate store defaults to false --- .changeset/chilled-ligers-jam.md | 5 +++++ packages/core/src/stores/plate/createPlateStore.ts | 2 +- .../core/src/stores/plate/selectors/useEditorReadOnly.ts | 4 ++-- packages/core/src/types/PlateStore.ts | 6 +++--- 4 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .changeset/chilled-ligers-jam.md diff --git a/.changeset/chilled-ligers-jam.md b/.changeset/chilled-ligers-jam.md new file mode 100644 index 0000000000..84c381ffcc --- /dev/null +++ b/.changeset/chilled-ligers-jam.md @@ -0,0 +1,5 @@ +--- +'@udecode/plate-core': patch +--- + +Fix: `readOnly` on Plate store defaults to false and overrides `readOnly` on PlateContent diff --git a/packages/core/src/stores/plate/createPlateStore.ts b/packages/core/src/stores/plate/createPlateStore.ts index 501acc4357..b9683c0d75 100644 --- a/packages/core/src/stores/plate/createPlateStore.ts +++ b/packages/core/src/stores/plate/createPlateStore.ts @@ -30,7 +30,7 @@ export const createPlateStore = < editorRef = null, plugins = [], rawPlugins = [], - readOnly = false, + readOnly = null, renderElement = null, renderLeaf = null, value = null as any, diff --git a/packages/core/src/stores/plate/selectors/useEditorReadOnly.ts b/packages/core/src/stores/plate/selectors/useEditorReadOnly.ts index 56055b9026..c7fc3e0759 100644 --- a/packages/core/src/stores/plate/selectors/useEditorReadOnly.ts +++ b/packages/core/src/stores/plate/selectors/useEditorReadOnly.ts @@ -4,6 +4,6 @@ import { PlateId, usePlateSelectors } from '../createPlateStore'; * Whether the editor is read-only. * You can also use `useReadOnly` from `slate-react` in node components. */ -export const useEditorReadOnly = (id?: PlateId) => { - return usePlateSelectors(id).readOnly(); +export const useEditorReadOnly = (id?: PlateId): boolean => { + return !!usePlateSelectors(id).readOnly(); }; diff --git a/packages/core/src/types/PlateStore.ts b/packages/core/src/types/PlateStore.ts index d29492f974..ea07c0bf85 100644 --- a/packages/core/src/types/PlateStore.ts +++ b/packages/core/src/types/PlateStore.ts @@ -38,9 +38,6 @@ export type PlateStoreState< */ rawPlugins: PlatePlugin[]; - // Whether the editor is read-only. - readOnly: boolean; - /** * Flattened plugins. */ @@ -57,6 +54,9 @@ export type PlateStoreState< */ isMounted: boolean; + // Whether the editor is read-only. + readOnly: boolean; + /** * Version incremented on each editor change. */ From c0f7c5960f21fba94b4810e0be0a3a526793f53e Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Fri, 22 Dec 2023 16:21:42 +0000 Subject: [PATCH 2/2] Fix: Plate ignores plugins passed via `editor` --- .changeset/chilled-ligers-jam.md | 3 ++- packages/core/src/components/Plate.spec.tsx | 22 +++++++++++++++++++++ packages/core/src/hooks/usePlateEffects.ts | 3 ++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.changeset/chilled-ligers-jam.md b/.changeset/chilled-ligers-jam.md index 84c381ffcc..e0a35cf229 100644 --- a/.changeset/chilled-ligers-jam.md +++ b/.changeset/chilled-ligers-jam.md @@ -2,4 +2,5 @@ '@udecode/plate-core': patch --- -Fix: `readOnly` on Plate store defaults to false and overrides `readOnly` on PlateContent +- Fix: `readOnly` on Plate store defaults to false and overrides `readOnly` on PlateContent +- Fix: Plate ignores plugins passed via `editor` diff --git a/packages/core/src/components/Plate.spec.tsx b/packages/core/src/components/Plate.spec.tsx index a1a7b058ee..cde740713b 100644 --- a/packages/core/src/components/Plate.spec.tsx +++ b/packages/core/src/components/Plate.spec.tsx @@ -132,6 +132,28 @@ describe('Plate', () => { expect(result.current.at(-1)!.key).toBe('test2'); }); }); + + it('should use plugins from editor', () => { + const _plugins = [{ key: 'test' }]; + const editor = createPlateEditor({ plugins: _plugins }); + + const wrapper = ({ children }: any) => ( + {children} + ); + + const { result, rerender } = renderHook( + () => usePlateSelectors().plugins(), + { + wrapper, + } + ); + + expect(result.current.some((p: any) => p.key === 'test')).toBe(true); + + rerender(); + + expect(result.current.some((p: any) => p.key === 'test')).toBe(true); + }); }); describe('when id updates', () => { diff --git a/packages/core/src/hooks/usePlateEffects.ts b/packages/core/src/hooks/usePlateEffects.ts index 36506dd36e..6b67a1fe95 100644 --- a/packages/core/src/hooks/usePlateEffects.ts +++ b/packages/core/src/hooks/usePlateEffects.ts @@ -1,5 +1,6 @@ import { useEffect } from 'react'; import { Value } from '@udecode/slate'; +import { isDefined } from '@udecode/utils'; import { PlateProps } from '../components'; import { useEditorRef, usePlateStates } from '../stores'; @@ -26,7 +27,7 @@ export const usePlateEffects = < const [, setPlugins] = states.plugins(); useEffect(() => { - if (pluginsProp !== rawPlugins) { + if (isDefined(pluginsProp) && pluginsProp !== rawPlugins) { setRawPlugins(rawPlugins); setPlatePlugins(editor, {