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

Add option to change the default plugin to use when deserializing html #4001

Merged
merged 4 commits into from
Jan 17, 2025
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/cool-sloths-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-core': patch
---

Add new param to HTML deserializer for changing the default element
4 changes: 4 additions & 0 deletions packages/core/src/lib/plugins/html/utils/deserializeHtml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Descendant } from '@udecode/slate';

import type { SlateEditor } from '../../../editor';
import type { WithRequiredKey } from '../../../plugin';

import { normalizeDescendantsToDocumentFragment } from '../../../utils/normalizeDescendantsToDocumentFragment';
import { collapseWhiteSpace } from './collapse-white-space';
Expand All @@ -12,10 +13,12 @@ export const deserializeHtml = (
editor: SlateEditor,
{
collapseWhiteSpace: shouldCollapseWhiteSpace = true,
defaultElementPlugin,
element,
}: {
element: HTMLElement | string;
collapseWhiteSpace?: boolean;
defaultElementPlugin?: WithRequiredKey;
}
): Descendant[] => {
// for serializer
Expand All @@ -29,6 +32,7 @@ export const deserializeHtml = (
const fragment = deserializeHtmlElement(editor, element) as Descendant[];

return normalizeDescendantsToDocumentFragment(editor, {
defaultElementPlugin,
descendants: fragment,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { LinkPlugin } from '@udecode/plate-link/react';
import { jsxt } from '@udecode/plate-test-utils';

import { createPlateEditor } from '../../react';
import { createPlateEditor, createPlatePlugin } from '../../react';
import { normalizeDescendantsToDocumentFragment } from './index';

jsxt;
Expand Down Expand Up @@ -112,4 +112,84 @@ describe('normalizeDescendantsToDocumentFragment()', () => {
expect(result).toEqual(output);
}
);

it.each([
{
input: [<htext>text node</htext>, <htext>another text node</htext>],
output: [<htext>text node</htext>, <htext>another text node</htext>],
},
{
input: [<ha>inline element</ha>, <htext>text node</htext>],
output: [<ha>inline element</ha>, <htext>text node</htext>],
},
{
input: [<hp>block</hp>, <hp>another block</hp>, <htext>text node</htext>],
output: [
<hp>block</hp>,
<hp>another block</hp>,
<hblockquote>text node</hblockquote>,
],
},
{
input: [<ha>inline element</ha>, <hp>block</hp>],
output: [
<hblockquote>
<ha>inline element</ha>
</hblockquote>,
<hp>block</hp>,
],
},
{
input: [
<htext>text 1</htext>,
<htext>text 2</htext>,
<hp>block</hp>,
<htext>text 3</htext>,
<htext>text 4</htext>,
],
output: [
<hblockquote>
<htext>text 1</htext>
<htext>text 2</htext>
</hblockquote>,
<hp>block</hp>,
<hblockquote>
<htext>text 3</htext>
<htext>text 4</htext>
</hblockquote>,
],
},
{
input: [
<hp>
<htext>text node</htext>
<hp>block</hp>
</hp>,
],
output: [
<hp>
<hblockquote>text node</hblockquote>
<hp>block</hp>
</hp>,
],
},
])(
'should wrap inline blocks and text nodes in case they have a sibling block',
({ input, output }: any) => {
const BaseBlockquotePlugin = createPlatePlugin({
key: 'blockquote',
node: { isElement: true },
});

const editor = createPlateEditor({
plugins: [LinkPlugin],
});

const result = normalizeDescendantsToDocumentFragment(editor, {
defaultElementPlugin: BaseBlockquotePlugin,
descendants: input,
});
expect(result).toEqual(output);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@udecode/slate';

import type { SlateEditor } from '../editor';
import type { WithRequiredKey } from '../plugin';

import { BaseParagraphPlugin } from '../plugins';

Expand Down Expand Up @@ -117,10 +118,13 @@ const normalize = (
/** Normalize the descendants to a valid document fragment. */
export const normalizeDescendantsToDocumentFragment = (
editor: SlateEditor,
{ descendants }: { descendants: Descendant[] }
{
defaultElementPlugin = BaseParagraphPlugin,
descendants,
}: { descendants: Descendant[]; defaultElementPlugin?: WithRequiredKey }
): Descendant[] => {
const isInline = isInlineNode(editor);
const defaultType = editor.getType(BaseParagraphPlugin);
const defaultType = editor.getType(defaultElementPlugin);
const makeDefaultBlock = makeBlockLazy(defaultType);

return normalize(descendants, isInline, makeDefaultBlock as any);
Expand Down
Loading
Loading