Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Dec 13, 2024
1 parent 64aadf6 commit 8a00478
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
16 changes: 14 additions & 2 deletions packages/core/src/lib/utils/pipeInjectNodeProps.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import type { TElement, TText } from '@udecode/slate';
import type { Path } from 'slate';

import clsx from 'clsx';

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

import { pluginInjectNodeProps } from './pluginInjectNodeProps';

/** Inject plugin props, editor. */
export const pipeInjectNodeProps = (editor: SlateEditor, nodeProps: any) => {
export const pipeInjectNodeProps = (
editor: SlateEditor,
nodeProps: any,
getElementPath: (node: TElement | TText) => Path
) => {
editor.pluginList.forEach((plugin) => {
if (plugin.inject.nodeProps) {
const newProps = pluginInjectNodeProps(editor, plugin, nodeProps);
const newProps = pluginInjectNodeProps(
editor,
plugin,
nodeProps,
getElementPath
);

if (!newProps) return;

Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/lib/utils/pluginInjectNodeProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { findNode } from '@udecode/slate';
import { findNodePath } from '@udecode/slate-react';
import { IS_SERVER, isDefined } from '@udecode/utils';
import type { TElement, TText } from '@udecode/slate';
import type { Path } from 'slate';

import { isDefined } from '@udecode/utils';

import type { SlateEditor } from '../../lib/editor';
import type {
Expand All @@ -25,7 +26,8 @@ import { getInjectMatch } from '../../lib/utils/getInjectMatch';
export const pluginInjectNodeProps = (
editor: SlateEditor,
plugin: EditorPlugin,
nodeProps: GetInjectNodePropsOptions
nodeProps: GetInjectNodePropsOptions,
getElementPath: (node: TElement | TText) => Path
): GetInjectNodePropsReturnType | undefined => {
const {
key,
Expand Down Expand Up @@ -54,13 +56,7 @@ export const pluginInjectNodeProps = (

const injectMatch = getInjectMatch(editor, plugin);

const elementPath = findNode(editor, { match: (n) => n === node })?.[1];

if (IS_SERVER) {
if (!elementPath || !injectMatch(node, elementPath)) return;
} else {
if (!injectMatch(node, findNodePath(editor, node)!)) return;
}
if (!injectMatch(node, getElementPath(node))) return;

const queryResult = query?.({
...injectNodeProps,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/react/utils/getRenderNodeProps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AnyObject } from '@udecode/utils';

import { findNodePath } from '@udecode/slate-react';
import { clsx } from 'clsx';

import type { PlateEditor } from '../editor';
Expand Down Expand Up @@ -37,7 +38,11 @@ export const getRenderNodeProps = ({
className: clsx(getSlateClass(plugin?.node.type), className),
};

nodeProps = pipeInjectNodeProps(editor, nodeProps) as PlateRenderNodeProps;
nodeProps = pipeInjectNodeProps(
editor,
nodeProps,
(node) => findNodePath(editor, node)!
) as PlateRenderNodeProps;

if (nodeProps.style && Object.keys(nodeProps.style).length === 0) {
delete nodeProps.style;
Expand Down

0 comments on commit 8a00478

Please sign in to comment.