Skip to content

Commit

Permalink
Merge pull request #2822 from udecode/fix/read-only-editor-plugins
Browse files Browse the repository at this point in the history
Fix Plate 27 regressions
  • Loading branch information
12joan authored Dec 22, 2023
2 parents 30a7572 + c0f7c59 commit eef0c33
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilled-ligers-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@udecode/plate-core': patch
---

- Fix: `readOnly` on Plate store defaults to false and overrides `readOnly` on PlateContent
- Fix: Plate ignores plugins passed via `editor`
22 changes: 22 additions & 0 deletions packages/core/src/components/Plate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<Plate editor={editor}>{children}</Plate>
);

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', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/hooks/usePlateEffects.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -26,7 +27,7 @@ export const usePlateEffects = <
const [, setPlugins] = states.plugins();

useEffect(() => {
if (pluginsProp !== rawPlugins) {
if (isDefined(pluginsProp) && pluginsProp !== rawPlugins) {
setRawPlugins(rawPlugins);

setPlatePlugins<V, E>(editor, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/stores/plate/createPlateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const createPlateStore = <
editorRef = null,
plugins = [],
rawPlugins = [],
readOnly = false,
readOnly = null,
renderElement = null,
renderLeaf = null,
value = null as any,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/stores/plate/selectors/useEditorReadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
6 changes: 3 additions & 3 deletions packages/core/src/types/PlateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export type PlateStoreState<
*/
rawPlugins: PlatePlugin<PluginOptions, V, E>[];

// Whether the editor is read-only.
readOnly: boolean;

/**
* Flattened plugins.
*/
Expand All @@ -57,6 +54,9 @@ export type PlateStoreState<
*/
isMounted: boolean;

// Whether the editor is read-only.
readOnly: boolean;

/**
* Version incremented on each editor change.
*/
Expand Down

0 comments on commit eef0c33

Please sign in to comment.