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

Fix Plate 27 regressions #2822

Merged
merged 2 commits into from
Dec 22, 2023
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
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