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/html #3974

Merged
merged 8 commits into from
Jan 15, 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/funny-mice-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-core': patch
---

fix import html
13 changes: 13 additions & 0 deletions .changeset/strong-lemons-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@udecode/plate-indent-list': patch
'@udecode/plate-line-height': patch
'@udecode/plate-code-block': patch
'@udecode/plate-comments': patch
'@udecode/plate-layout': patch
'@udecode/plate-media': patch
'@udecode/plate-table': patch
'@udecode/plate-core': patch
'@udecode/plate-docx': patch
---

Remove useless html parser.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ import {
BaseCodeBlockPlugin,
BaseCodeLinePlugin,
} from '../BaseCodeBlockPlugin';
import { htmlDeserializerCodeBlockStatic } from './htmlDeserializerCodeBlockStatic';

export const htmlDeserializerCodeBlock: HtmlDeserializer = {
parse: ({ element }) => {
const staticCodeBlock = htmlDeserializerCodeBlockStatic(element);

if (staticCodeBlock) {
return staticCodeBlock;
}

const languageSelectorText =
[...element.childNodes].find(
(node: ChildNode) => node.nodeName === 'SELECT'
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/code-block/src/lib/deserializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
*/

export * from './htmlDeserializerCodeBlock';
export * from './htmlDeserializerCodeBlockStatic';
2 changes: 1 addition & 1 deletion packages/comments/src/lib/BaseCommentsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const BaseCommentsPlugin = createTSlatePlugin<BaseCommentsConfig>({
parsers: {
html: {
deserializer: {
disabledDefaultNodeProps: true,
disableDefaultNodeProps: true,
toNodeProps: ({ element }) => {
const { slateComment } = element.dataset;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/plugin/SlatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export type HtmlDeserializer<C extends AnyPluginConfig = PluginConfig> =
*
* @default false
*/
disabledDefaultNodeProps?: boolean;
disableDefaultNodeProps?: boolean;
};

export type HtmlSerializer<C extends AnyPluginConfig = PluginConfig> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SlateEditor } from '../../../editor';
import type { DeserializeHtmlNodeReturnType } from '../types';

import { isSlateNode } from '../../../static';
import { htmlBodyToFragment } from './htmlBodyToFragment';
import { htmlBrToNewLine } from './htmlBrToNewLine';
import { htmlElementToElement } from './htmlElementToElement';
Expand Down Expand Up @@ -28,7 +29,11 @@ export const deserializeHtmlNode =
if (fragment) return fragment;

// element
const element = htmlElementToElement(editor, node as HTMLElement);
const element = htmlElementToElement(
editor,
node as HTMLElement,
isSlateNode(node as HTMLElement)
);

if (element) return element;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import type { SlateEditor } from '../../../editor';
import type { DeserializeHtmlChildren } from '../types';

import { isSlateNode } from '../../../static';
import { deserializeHtmlNode } from './deserializeHtmlNode';

export const deserializeHtmlNodeChildren = (
editor: SlateEditor,
node: ChildNode | HTMLElement
) =>
Array.from(node.childNodes).flatMap(
deserializeHtmlNode(editor)
) as DeserializeHtmlChildren[];
node: ChildNode | HTMLElement,
isSlateParent = false
): DeserializeHtmlChildren[] => {
return Array.from(node.childNodes).flatMap((child) => {
if (
child.nodeType === 1 &&
!isSlateNode(child as HTMLElement) &&
isSlateParent
) {
return deserializeHtmlNodeChildren(
editor,
child as HTMLElement,
isSlateParent
);
}

return deserializeHtmlNode(editor)(child);
}) as DeserializeHtmlChildren[];
};
10 changes: 5 additions & 5 deletions packages/core/src/lib/plugins/html/utils/getDataNodeProps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SlateEditor } from '../../../editor';

import { type AnyEditorPlugin, getEditorPlugin } from '../../../plugin';
import { isSlatePluginNode } from '../../../static';
import { isSlateLeaf, isSlatePluginNode } from '../../../static';

const getDefaultNodeProps = ({
element,
Expand All @@ -10,7 +10,7 @@ const getDefaultNodeProps = ({
element: HTMLElement;
type: string;
}) => {
if (!isSlatePluginNode(element, type)) return;
if (!isSlatePluginNode(element, type) && !isSlateLeaf(element)) return;

const dataAttributes: Record<string, any> = {};

Expand Down Expand Up @@ -56,10 +56,10 @@ export const getDataNodeProps = ({
}) => {
const toNodeProps = plugin.parsers.html?.deserializer?.toNodeProps;

const disabledDefaultNodeProps =
plugin.parsers.html?.deserializer?.disabledDefaultNodeProps ?? false;
const disableDefaultNodeProps =
plugin.parsers.html?.deserializer?.disableDefaultNodeProps ?? false;

const defaultNodeProps = disabledDefaultNodeProps
const defaultNodeProps = disableDefaultNodeProps
? {}
: getDefaultNodeProps({
...(getEditorPlugin(editor, plugin) as any),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { pipeDeserializeHtmlElement } from './pipeDeserializeHtmlElement';
/** Deserialize HTML to Element. */
export const htmlElementToElement = (
editor: SlateEditor,
element: HTMLElement
element: HTMLElement,
isSlate = false
) => {
const deserialized = pipeDeserializeHtmlElement(editor, element);

Expand All @@ -20,7 +21,7 @@ export const htmlElementToElement = (

let descendants =
node.children ??
(deserializeHtmlNodeChildren(editor, element) as Descendant[]);
(deserializeHtmlNodeChildren(editor, element, isSlate) as Descendant[]);

if (descendants.length === 0 || withoutChildren || isSlateVoid(element)) {
descendants = [{ text: '' }];
Expand Down
22 changes: 15 additions & 7 deletions packages/core/src/lib/plugins/html/utils/pluginDeserializeHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
} from '../../../plugin/SlatePlugin';

import { getEditorPlugin } from '../../../plugin';
import { isSlateNode } from '../../../static';
import { getInjectedPlugins } from '../../../utils/getInjectedPlugins';
import { getDataNodeProps } from './getDataNodeProps';

Expand Down Expand Up @@ -155,12 +156,19 @@ export const pluginDeserializeHtml = (
return;
}

const parsedNode =
parse({
...(getEditorPlugin(editor, plugin) as any),
element: el,
node: {},
}) ?? {};
const parsedNode = (() => {
if (isSlateNode(el)) {
return {};
}

return (
parse({
...(getEditorPlugin(editor, plugin) as any),
element: el,
node: {},
}) ?? {}
);
})();

const dataNodeProps = getDataNodeProps({
editor,
Expand All @@ -184,7 +192,7 @@ export const pluginDeserializeHtml = (
node,
});

if (res) {
if (res && !isSlateNode(el)) {
node = {
...node,
...res,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/lib/static/__tests__/deserialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { basicElementsValue } from 'www/src/registry/default/example/values/basi
import { basicMarksValue } from 'www/src/registry/default/example/values/basic-marks-value';
import { commentsValue } from 'www/src/registry/default/example/values/comments-value';
import { dateValue } from 'www/src/registry/default/example/values/date-value';
import { fontValue } from 'www/src/registry/default/example/values/font-value';
// import { equationValue } from 'www/src/registry/default/example/values/equation-value';
import { highlightValue } from 'www/src/registry/default/example/values/highlight-value';
import { horizontalRuleValue } from 'www/src/registry/default/example/values/horizontal-rule-value';
Expand Down Expand Up @@ -30,7 +31,7 @@ describe('deserializePlateStatic', () => {
// ...columnValue,
...mentionValue,
...dateValue,
// ...fontValue,
...fontValue,
...highlightValue,
...kbdValue,
...commentsValue,
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('deserializePlateStatic', () => {
// ...columnValue,
...mentionValue,
...dateValue,
// ...fontValue,
...fontValue,
...highlightValue,
...kbdValue,
...commentsValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('serializePlateStatic with attributes', () => {
});

expect(html).toEqual(
'<div data-slate-editor="true" data-slate-node="value"><div data-slate-node="element" data-slate-type="p" style="position:relative"><span data-slate-node="text"><span data-slate-leaf="true"><strong data-slate-leaf="true" data-slate-bold="true"><span data-slate-string="true">Right Aligned Heading</span></strong></span></span></div></div>'
'<div data-slate-editor="true" data-slate-node="value"><div data-slate-node="element" data-slate-type="p" style="position:relative"><span data-slate-node="text"><span data-slate-leaf="true" data-slate-bold="true"><strong data-slate-leaf="true" data-slate-bold="true"><span data-slate-string="true">Right Aligned Heading</span></strong></span></span></div></div>'
);
});
});
9 changes: 7 additions & 2 deletions packages/core/src/lib/static/deserialize/checkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ export const isSlateElement = (element: HTMLElement) => {
return element.dataset.slateNode === 'element';
};

export const isSlateString = (element: HTMLElement) => {
export const isSlateText = (element: HTMLElement) => {
return element.dataset.slateNode === 'text';
};

export const isSlateString = (element: HTMLElement) => {
return element.dataset.slateString === 'true';
};

export const isSlateLeaf = (element: HTMLElement) => {
return element.dataset.slateLeaf === 'true';
};
Expand All @@ -19,7 +23,8 @@ export const isSlateNode = (element: HTMLElement) => {
isSlateLeaf(element) ||
isSlateElement(element) ||
isSlateVoid(element) ||
isSlateString(element)
isSlateString(element) ||
isSlateText(element)
);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/static/pluginRenderElementStatic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AnyEditorPlugin, NodeComponents } from '../plugin';
import type { RenderElementProps } from '../types/RenderElementProps';

import { SlateElement } from './components/SlateElement';
import { getNodeDataAttributes } from './utils/getNodeDataAttributes';
import { getPluginDataAttributes } from './utils';
import { getRenderNodeStaticProps } from './utils/getRenderNodeStaticProps';

export type SlateRenderElement = (
Expand Down Expand Up @@ -33,7 +33,7 @@ export const pluginRenderElementStatic = (
(o) => o.render?.belowNodes ?? []
);

const dataAttributes = getNodeDataAttributes(editor, plugin, element);
const dataAttributes = getPluginDataAttributes(editor, plugin, element);

nodeProps = getRenderNodeStaticProps({
attributes: {
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/lib/static/pluginRenderLeafStatic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type { NodeComponents, SlatePlugin } from '../plugin';
import type { RenderLeafProps } from '../types/RenderLeafProps';

import { SlateLeaf } from './components/SlateLeaf';
import { getNodeDataAttributes } from './utils/getNodeDataAttributes';
import {
getLeafDataAttributes,
getPluginDataAttributes,
} from './utils/getNodeDataAttributes';
import { getRenderNodeStaticProps } from './utils/getRenderNodeStaticProps';

export type SlateRenderLeaf = (
Expand All @@ -23,7 +26,7 @@ export const pluginRenderLeafStatic = (
if (leaf[plugin.node.type ?? plugin.key]) {
const Leaf = components?.[plugin.key] ?? SlateLeaf;

const dataAttributes = getNodeDataAttributes(editor, plugin, leaf);
const dataAttributes = getPluginDataAttributes(editor, plugin, leaf);

const ctxProps = getRenderNodeStaticProps({
attributes: {
Expand Down Expand Up @@ -80,6 +83,10 @@ export const pipeRenderLeafStatic = (
props: props as any,
}) as any;

return <SlateLeaf {...ctxProps} />;
const leaf = ctxProps.leaf;

const dataAttributes = getLeafDataAttributes(leaf);

return <SlateLeaf {...ctxProps} {...dataAttributes} />;
};
};
Loading
Loading