Skip to content

Commit

Permalink
Merge pull request #2837 from dimaanj/fix-serializeHtml-error
Browse files Browse the repository at this point in the history
[serializer-html][list-plugin] Investigate into `serializeHtml` error
  • Loading branch information
zbeyens authored Mar 25, 2024
2 parents 424dd4e + c24e4d8 commit d067806
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-pants-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-serializer-html": patch
---

Fixes "The `useSlateStatic` hook must be used inside the <Slate> component's context." error in `serializeHtml`
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import { createTodoListPlugin } from '@udecode/plate-list';
import {
createListPlugin,
createTodoListPlugin,
ELEMENT_LI,
ELEMENT_LIC,
ELEMENT_UL,
} from '@udecode/plate-list';
import { createPlateUIEditor } from 'www/src/lib/plate/create-plate-ui-editor';

import { serializeHtml } from '../../serializeHtml';

it('serialize elements using useSlateStatic', () => {
const plugins = [createTodoListPlugin()];
const plugins = [
createTodoListPlugin(),
createListPlugin({
overrideByKey: {
[ELEMENT_UL]: {
type: 'unordered-list',
},
[ELEMENT_LI]: {
type: 'list-item',
},
[ELEMENT_LIC]: {
type: 'list-item-child',
},
},
}),
];
const editor = createPlateUIEditor({ plugins });
const render = serializeHtml(editor, {
nodes: [
Expand All @@ -13,6 +34,20 @@ it('serialize elements using useSlateStatic', () => {
checked: true,
children: [{ text: 'Slide to the right.' }],
},
{
type: 'unordered-list',
children: [
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Level 3' }],
},
],
},
],
},
],
});

Expand Down
13 changes: 11 additions & 2 deletions packages/serializer-html/src/utils/createElementWithSlate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React from 'react';
import { createPlateEditor, Plate, PlateProps } from '@udecode/plate-common';
import {
createPlateEditor,
Plate,
PlateContent,
PlateProps,
} from '@udecode/plate-common';

/**
* Create a React element wrapped in a Plate provider.
Expand All @@ -16,6 +21,10 @@ export const createElementWithSlate = (
...props
} = plateProps || {};

const plateContent = React.createElement(PlateContent, {
renderEditable: () => children,
});

const plate = React.createElement(
Plate,
{
Expand All @@ -24,7 +33,7 @@ export const createElementWithSlate = (
onChange,
...props,
} as PlateProps,
children
plateContent
);

if (dndWrapper) {
Expand Down

0 comments on commit d067806

Please sign in to comment.