Skip to content

Commit

Permalink
Merge pull request #4002 from udecode/fix/parsers
Browse files Browse the repository at this point in the history
Fix/parsers
  • Loading branch information
zbeyens authored Jan 17, 2025
2 parents 493086a + 7ca08fd commit 83f58ec
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 244 deletions.
12 changes: 12 additions & 0 deletions .changeset/fresh-coins-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@udecode/plate-code-block': patch
'@udecode/plate-heading': patch
'@udecode/plate-media': patch
'@udecode/plate-table': patch
'@udecode/plate-docx': patch
'@udecode/plate-font': patch
'@udecode/plate-link': patch
'@udecode/plate-list': patch
---

Fix parsers to use custom node type
5 changes: 5 additions & 0 deletions .changeset/unlucky-months-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-core': patch
---

Pass plugin context to `plugin.node.props`
4 changes: 2 additions & 2 deletions packages/code-block/src/react/CodeBlockPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const CodeBlockPlugin = toPlatePlugin(BaseCodeBlockPlugin, {
handlers: {
onKeyDown: onKeyDownCodeBlock,
},
}).extend(({ editor, type }) => ({
}).extend(({ editor, plugin }) => ({
shortcuts: {
toggleCodeBlock: {
keys: [[Key.Mod, Key.Alt, '8']],
preventDefault: true,
handler: () => {
editor.tf.toggleBlock(type);
editor.tf.toggleBlock(editor.getType(plugin));
},
},
},
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/lib/static/utils/getRenderNodeStaticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ export const getRenderNodeStaticProps = ({
node?: TElement | TText;
plugin?: AnyEditorPlugin;
}): SlateRenderNodeProps => {
props = getPluginNodeProps({
attributes,
node,
plugin,
props,
});

const { className } = props;

let nodeProps = {
...props,
...(plugin ? (getEditorPlugin(editor, plugin) as any) : {}),
};

const { className } = props;

nodeProps = {
...getPluginNodeProps({
attributes,
node,
plugin,
props: nodeProps,
}),
className: clsx(getSlateClass(plugin?.node.type), className),
};

Expand Down
18 changes: 10 additions & 8 deletions packages/core/src/react/utils/getRenderNodeProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ export const getRenderNodeProps = ({
attributes?: AnyObject;
plugin?: AnyEditorPlatePlugin;
}): PlateRenderNodeProps => {
props = getPluginNodeProps({
attributes,
plugin: plugin as any,
props: props as any,
});

const { className } = props;

let nodeProps = {
...props,
...(plugin ? (getEditorPlugin(editor, plugin) as any) : {}),
};

const { className } = props;

nodeProps = {
...getPluginNodeProps({
attributes,
plugin: plugin as any,
props: nodeProps as any,
}),
className: clsx(getSlateClass(plugin?.node.type), className),
};

Expand Down
86 changes: 39 additions & 47 deletions packages/docx/src/lib/DocxPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@ import { getTextListStyleType } from './docx-cleaner/utils/getTextListStyleType'
import { isDocxContent } from './docx-cleaner/utils/isDocxContent';
import { isDocxList } from './docx-cleaner/utils/isDocxList';

const getParse =
(type: string): HtmlDeserializer['parse'] =>
({ element }) => {
const node: any = { type };
const parse: HtmlDeserializer['parse'] = ({ element, type }) => {
const node: any = { type };

if (isDocxList(element)) {
node.indent = getDocxListIndent(element);
if (isDocxList(element)) {
node.indent = getDocxListIndent(element);

const text = element.textContent ?? '';
const text = element.textContent ?? '';

node.listStyleType = getTextListStyleType(text) ?? 'disc';
node.listStyleType = getTextListStyleType(text) ?? 'disc';

element.innerHTML = getDocxListContentHtml(element);
} else {
const indent = getDocxIndent(element);
element.innerHTML = getDocxListContentHtml(element);
} else {
const indent = getDocxIndent(element);

if (indent) {
node.indent = indent;
}
if (indent) {
node.indent = indent;
}

const textIndent = getDocxTextIndent(element);
const textIndent = getDocxTextIndent(element);

if (textIndent) {
node.textIndent = textIndent;
}
if (textIndent) {
node.textIndent = textIndent;
}
}

return node;
};
return node;
};

export const DocxPlugin = createSlatePlugin({
key: 'docx',
Expand All @@ -61,38 +59,32 @@ export const DocxPlugin = createSlatePlugin({
},
},
},
}).extend(({ editor }) => {
return {
override: {
plugins: {
...Object.fromEntries(
['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'].map((key) => [
key,
{
parsers: {
html: {
deserializer: {
parse: getParse(editor.getType({ key })),
},
override: {
plugins: {
...Object.fromEntries(
['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'].map((key) => [
key,
{
parsers: {
html: {
deserializer: {
parse,
},
},
} satisfies Partial<SlatePlugin>,
])
),
img: {
parser: {
query: ({ dataTransfer }) => {
const data = dataTransfer.getData('text/html');
const { body } = new DOMParser().parseFromString(
data,
'text/html'
);

return !isDocxContent(body);
},
} satisfies Partial<SlatePlugin>,
])
),
img: {
parser: {
query: ({ dataTransfer }) => {
const data = dataTransfer.getData('text/html');
const { body } = new DOMParser().parseFromString(data, 'text/html');

return !isDocxContent(body);
},
},
},
},
};
},
});
7 changes: 4 additions & 3 deletions packages/font/src/lib/BaseFontBackgroundColorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export const BaseFontBackgroundColorPlugin = createSlatePlugin({
nodeKey: 'backgroundColor',
},
},
}).extend(({ type }) => ({
parsers: {
html: {
deserializer: {
isLeaf: true,
parse: ({ element }) => ({ [type]: element.style.backgroundColor }),
parse: ({ element, type }) => ({
[type]: element.style.backgroundColor,
}),
rules: [
{
validStyle: {
Expand All @@ -23,4 +24,4 @@ export const BaseFontBackgroundColorPlugin = createSlatePlugin({
},
},
},
}));
});
5 changes: 2 additions & 3 deletions packages/font/src/lib/BaseFontColorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export const BaseFontColorPlugin = createSlatePlugin({
nodeKey: 'color',
},
},
}).extend(({ type }) => ({
parsers: {
html: {
deserializer: {
isLeaf: true,
parse({ element }) {
parse({ element, type }) {
if (element.style.color) {
return { [type]: element.style.color };
}
Expand All @@ -28,4 +27,4 @@ export const BaseFontColorPlugin = createSlatePlugin({
},
},
},
}));
});
5 changes: 2 additions & 3 deletions packages/font/src/lib/BaseFontFamilyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export const BaseFontFamilyPlugin = createSlatePlugin({
nodeKey: 'fontFamily',
},
},
}).extend(({ type }) => ({
parsers: {
html: {
deserializer: {
isLeaf: true,
parse: ({ element }) => ({ [type]: element.style.fontFamily }),
parse: ({ element, type }) => ({ [type]: element.style.fontFamily }),
rules: [
{
validStyle: {
Expand All @@ -23,4 +22,4 @@ export const BaseFontFamilyPlugin = createSlatePlugin({
},
},
},
}));
});
33 changes: 15 additions & 18 deletions packages/font/src/lib/BaseFontSizePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,21 @@ export const BaseFontSizePlugin = createTSlatePlugin({
nodeKey: 'fontSize',
},
},
})
.extend(({ type }) => ({
parsers: {
html: {
deserializer: {
isLeaf: true,
parse: ({ element }) => ({ [type]: element.style.fontSize }),
rules: [
{
validStyle: {
fontSize: '*',
},
parsers: {
html: {
deserializer: {
isLeaf: true,
parse: ({ element, type }) => ({ [type]: element.style.fontSize }),
rules: [
{
validStyle: {
fontSize: '*',
},
],
},
},
],
},
},
}))
.extendApi(({ editor }) => ({
setMark: bindFirst(setFontSize, editor),
}));
},
}).extendApi(({ editor }) => ({
setMark: bindFirst(setFontSize, editor),
}));
5 changes: 2 additions & 3 deletions packages/font/src/lib/BaseFontWeightPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export const BaseFontWeightPlugin = createSlatePlugin({
nodeKey: 'fontWeight',
},
},
}).extend(({ type }) => ({
parsers: {
html: {
deserializer: {
isLeaf: true,
parse: ({ element }) => ({ [type]: element.style.fontWeight }),
parse: ({ element, type }) => ({ [type]: element.style.fontWeight }),
rules: [
{
validStyle: {
Expand All @@ -23,4 +22,4 @@ export const BaseFontWeightPlugin = createSlatePlugin({
},
},
},
}));
});
6 changes: 3 additions & 3 deletions packages/heading/src/react/HeadingPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseHeadingPlugin } from '../lib/BaseHeadingPlugin';

export const HeadingPlugin = toPlatePlugin(BaseHeadingPlugin, ({ plugin }) => ({
plugins: (plugin as unknown as PlatePlugin).plugins.map((p) =>
(p as PlatePlugin).extend(({ editor, type }) => {
(p as PlatePlugin).extend(({ plugin }) => {
const level = p.key.at(-1);

if (level > 3) return {};
Expand All @@ -17,8 +17,8 @@ export const HeadingPlugin = toPlatePlugin(BaseHeadingPlugin, ({ plugin }) => ({
[Key.Mod, Key.Shift, level],
],
preventDefault: true,
handler: () => {
editor.tf.toggleBlock(type);
handler: ({ editor }) => {
editor.tf.toggleBlock(editor.getType(plugin));
},
},
},
Expand Down
Loading

0 comments on commit 83f58ec

Please sign in to comment.