Skip to content

Commit

Permalink
refactor: rename api
Browse files Browse the repository at this point in the history
  • Loading branch information
yf-yang committed Jan 27, 2025
1 parent 4c4d990 commit 2740b76
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 64 deletions.
4 changes: 2 additions & 2 deletions apps/www/content/docs/cn/api/core/plate-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ HTML React 序列化器配置。
<APISubListItem parent="render" name="aboveEditable" type="Component" optional>
`Editable` 组件上方但在 `Slate` 包装器内渲染组件。
</APISubListItem>
<APISubListItem parent="render" name="aboveNodes" type="NodeWrapperFunctionCreator<WithAnyKey<C>>" optional>
<APISubListItem parent="render" name="aboveNodes" type="RenderNodeWrapper<WithAnyKey<C>>" optional>
创建一个函数,该函数为所有其他插件的 `node` React节点生成父节点。
</APISubListItem>
<APISubListItem parent="render" name="aboveSlate" type="React.FC<{ children: React.ReactNode }>" optional>
Expand All @@ -183,7 +183,7 @@ HTML React 序列化器配置。
<APISubListItem parent="render" name="beforeEditable" type="EditableSiblingComponent" optional>
`Editable` 组件前渲染组件。
</APISubListItem>
<APISubListItem parent="render" name="belowNodes" type="NodeWrapperFunctionCreator<WithAnyKey<C>>" optional>
<APISubListItem parent="render" name="belowNodes" type="RenderNodeWrapper<WithAnyKey<C>>" optional>
创建一个函数,该函数为所有其他插件的 `node` React节点和其`children`React节点之间生成一个React节点。
</APISubListItem>
<APISubListItem parent="render" name="node" type="NodeComponent" optional>
Expand Down
4 changes: 2 additions & 2 deletions apps/www/content/docs/en/api/core/plate-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Defines how the plugin renders components.
<APISubListItem parent="render" name="aboveEditable" type="Component" optional>
Component rendered above the Editable component but inside the Slate wrapper.
</APISubListItem>
<APISubListItem parent="render" name="aboveNodes" type="NodeWrapperFunctionCreator<WithAnyKey<C>>" optional>
<APISubListItem parent="render" name="aboveNodes" type="RenderNodeWrapper<WithAnyKey<C>>" optional>
Create a function that generates a parent React node for all other plugins' node components.
</APISubListItem>
<APISubListItem parent="render" name="aboveSlate" type="Component" optional>
Expand All @@ -162,7 +162,7 @@ Renders a component after the Editable component.
<APISubListItem parent="render" name="beforeEditable" type="EditableSiblingComponent" optional>
Renders a component before the Editable component.
</APISubListItem>
<APISubListItem parent="render" name="belowNodes" type="NodeWrapperFunctionCreator<WithAnyKey<C>>" optional>
<APISubListItem parent="render" name="belowNodes" type="RenderNodeWrapper<WithAnyKey<C>>" optional>
Create a function that generates a React node below all other plugins' node React node, but above their children.
</APISubListItem>
<APISubListItem parent="render" name="node" type="NodeComponent" optional>
Expand Down
4 changes: 2 additions & 2 deletions apps/www/src/registry/default/plate-ui/draggable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import React, { useMemo } from 'react';
import { cn, withRef } from '@udecode/cn';
import { isType } from '@udecode/plate';
import {
type NodeWrapperFunctionCreator,
type PlateRenderElementProps,
type RenderNodeWrapper,
MemoizedChildren,
ParagraphPlugin,
useEditorPlugin,
Expand Down Expand Up @@ -46,7 +46,7 @@ const UNDRAGGABLE_KEYS = [
TableCellPlugin.key,
];

export const DraggableAboveNodes: NodeWrapperFunctionCreator = (props) => {
export const DraggableAboveNodes: RenderNodeWrapper = (props) => {
const { editor, element, path } = props;
const readOnly = useReadOnly();

Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/react/copilot/renderCopilotBelowNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import React from 'react';

import {
type NodeWrapperFunctionCreatorProps,
type RenderNodeWrapperProps,
getEditorPlugin,
} from '@udecode/plate/react';

import type { CopilotPluginConfig } from './CopilotPlugin';

export const renderCopilotBelowNodes = ({
editor,
}: NodeWrapperFunctionCreatorProps<CopilotPluginConfig>) => {
}: RenderNodeWrapperProps<CopilotPluginConfig>) => {
const copilot = getEditorPlugin<CopilotPluginConfig>(editor, {
key: 'copilot',
});
Expand Down
62 changes: 38 additions & 24 deletions packages/core/src/lib/plugin/SlatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ export type SlatePlugin<C extends AnyPluginConfig = PluginConfig> =
};
render: Nullable<{
/**
* When other plugins' `node` components are rendered, this creator
* function can return an optional wrapper function that turns a
* `node`'s props to a wrapper React node as its parent. Useful for
* wrapping or decorating nodes with additional UI elements.
* When other plugins' `node` components are rendered, this function can
* return an optional wrapper function that turns a `node`'s props to a
* wrapper React node as its parent. Useful for wrapping or decorating
* nodes with additional UI elements.
*
* NOTE: The creator function can run React hooks. NOTE: Do not run
* React hooks in the wrapper function. It is not equivalent to a React
* component.
* NOTE: The function can run React hooks. NOTE: Do not run React hooks
* in the wrapper function. It is not equivalent to a React component.
*/
aboveNodes?: NodeStaticWrapperFunctionCreator<WithAnyKey<C>>;
aboveNodes?: RenderStaticNodeWrapper<WithAnyKey<C>>;

/**
* Renders a component after the `Editable` component. This is the last
Expand All @@ -91,17 +90,16 @@ export type SlatePlugin<C extends AnyPluginConfig = PluginConfig> =
beforeEditable?: () => React.ReactElement | null;

/**
* When other plugins' `node` components are rendered, this creator
* function can return an optional wrapper function that turns a
* `node`'s props to a wrapper React node. The wrapper node is the
* `node`'s child and its original children's parent. Useful for
* wrapping or decorating nodes with additional UI elements.
* When other plugins' `node` components are rendered, this function can
* return an optional wrapper function that turns a `node`'s props to a
* wrapper React node. The wrapper node is the `node`'s child and its
* original children's parent. Useful for wrapping or decorating nodes
* with additional UI elements.
*
* NOTE: The creator function can run React hooks. NOTE: Do not run
* React hooks in the wrapper function. It is not equivalent to a React
* component.
* NOTE: The function can run React hooks. NOTE: Do not run React hooks
* in the wrapper function. It is not equivalent to a React component.
*/
belowNodes?: NodeStaticWrapperFunctionCreator<WithAnyKey<C>>;
belowNodes?: RenderStaticNodeWrapper<WithAnyKey<C>>;

node?: React.FC;
}>;
Expand Down Expand Up @@ -526,17 +524,33 @@ export type NodeStaticProps<C extends AnyPluginConfig = PluginConfig> =
) => AnyObject | undefined)
| AnyObject;

export type NodeStaticWrapperFunctionCreator<
C extends AnyPluginConfig = PluginConfig,
> = (
props: NodeStaticWrapperFunctionCreatorProps<C>
) => NodeStaticWrapperFunction;
export type RenderStaticNodeWrapper<C extends AnyPluginConfig = PluginConfig> =
(props: RenderStaticNodeWrapperProps<C>) => RenderStaticNodeWrapperFunction;

export type NodeStaticWrapperFunction =
export type RenderStaticNodeWrapperFunction =
| ((hocProps: SlateRenderElementProps) => React.ReactNode)
| undefined;

export interface NodeStaticWrapperFunctionCreatorProps<
export interface RenderStaticNodeWrapperProps<
C extends AnyPluginConfig = PluginConfig,
> extends SlateRenderElementProps<TElement, C> {
key: string;
}

/** @deprecated Use {@link RenderStaticNodeWrapper} instead. */
export type NodeStaticWrapperComponent<
C extends AnyPluginConfig = PluginConfig,
> = (
props: NodeStaticWrapperComponentProps<C>
) => NodeStaticWrapperComponentReturnType<C>;

/** @deprecated Use {@link RenderStaticNodeWrapperFunction} instead. */
export type NodeStaticWrapperComponentReturnType<
C extends AnyPluginConfig = PluginConfig,
> = React.FC<SlateRenderElementProps<TElement, C>> | undefined;

/** @deprecated Use {@link RenderStaticNodeWrapperProps} instead. */
export interface NodeStaticWrapperComponentProps<
C extends AnyPluginConfig = PluginConfig,
> extends SlateRenderElementProps<TElement, C> {
key: string;
Expand Down
57 changes: 36 additions & 21 deletions packages/core/src/react/plugin/PlatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,15 @@ export type PlatePlugin<C extends AnyPluginConfig = PluginConfig> =

render: Nullable<{
/**
* When other plugins' `node` components are rendered, this creator
* function can return an optional wrapper function that turns a
* `node`'s props to a wrapper React node as its parent. Useful for
* wrapping or decorating nodes with additional UI elements.
* When other plugins' `node` components are rendered, this function can
* return an optional wrapper function that turns a `node`'s props to a
* wrapper React node as its parent. Useful for wrapping or decorating
* nodes with additional UI elements.
*
* NOTE: The creator function can run React hooks. NOTE: Do not run
* React hooks in the wrapper function. It is not equivalent to a React
* component.
* NOTE: The function can run React hooks. NOTE: Do not run React hooks
* in the wrapper function. It is not equivalent to a React component.
*/
aboveNodes?: NodeWrapperFunctionCreator<WithAnyKey<C>>;
aboveNodes?: RenderNodeWrapper<WithAnyKey<C>>;

/**
* Renders a component after the `Editable` component. This is the last
Expand All @@ -148,17 +147,16 @@ export type PlatePlugin<C extends AnyPluginConfig = PluginConfig> =
beforeEditable?: EditableSiblingComponent;

/**
* When other plugins' `node` components are rendered, this creator
* function can return an optional wrapper function that turns a
* `node`'s props to a wrapper React node. The wrapper node is the
* `node`'s child and its original children's parent. Useful for
* wrapping or decorating nodes with additional UI elements.
* When other plugins' `node` components are rendered, this function can
* return an optional wrapper function that turns a `node`'s props to a
* wrapper React node. The wrapper node is the `node`'s child and its
* original children's parent. Useful for wrapping or decorating nodes
* with additional UI elements.
*
* NOTE: The creator function can run React hooks. NOTE: Do not run
* React hooks in the wrapper function. It is not equivalent to a React
* component.
* NOTE: The function can run React hooks. NOTE: Do not run React hooks
* in the wrapper function. It is not equivalent to a React component.
*/
belowNodes?: NodeWrapperFunctionCreator<WithAnyKey<C>>;
belowNodes?: RenderNodeWrapper<WithAnyKey<C>>;

/** @see {@link NodeComponent} */
node?: NodeComponent;
Expand Down Expand Up @@ -739,19 +737,36 @@ export type EditableSiblingComponent = (
editableProps: EditableProps
) => React.ReactElement | null;

export interface NodeWrapperFunctionCreatorProps<
export interface RenderNodeWrapperProps<
C extends AnyPluginConfig = PluginConfig,
> extends PlateRenderElementProps<TElement, C> {
key: string;
}

export type NodeWrapperFunction =
export type RenderNodeWrapperFunction =
| ((elementProps: PlateRenderElementProps) => React.ReactNode)
| undefined;

export type NodeWrapperFunctionCreator<
export type RenderNodeWrapper<C extends AnyPluginConfig = PluginConfig> = (
props: RenderNodeWrapperProps<C>
) => RenderNodeWrapperFunction;

/** @deprecated Use {@link RenderNodeWrapperProps} instead. */
export interface NodeWrapperComponentProps<
C extends AnyPluginConfig = PluginConfig,
> = (props: NodeWrapperFunctionCreatorProps<C>) => NodeWrapperFunction;
> extends PlateRenderElementProps<TElement, C> {
key: string;
}

/** @deprecated Use {@link RenderNodeWrapperFunction} instead. */
export type NodeWrapperComponentReturnType<
C extends AnyPluginConfig = PluginConfig,
> = React.FC<PlateRenderElementProps<TElement, C>> | undefined;

/** @deprecated Use {@link RenderNodeWrapper} instead. */
export type NodeWrapperComponent<C extends AnyPluginConfig = PluginConfig> = (
props: NodeWrapperComponentProps<C>
) => NodeWrapperComponentReturnType<C>;

/**
* Function called whenever a change occurs in the editor. Return `false` to
Expand Down
12 changes: 6 additions & 6 deletions packages/indent-list/src/lib/renderIndentListBelowNodes.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';

import type {
NodeStaticWrapperFunction,
NodeStaticWrapperFunctionCreator,
NodeStaticWrapperFunctionCreatorProps,
RenderStaticNodeWrapper,
RenderStaticNodeWrapperFunction,
RenderStaticNodeWrapperProps,
} from '@udecode/plate';

import { clsx } from 'clsx';
Expand All @@ -15,9 +15,9 @@ import {
} from '../lib';
import { ULIST_STYLE_TYPES } from '../lib/types';

export const renderIndentListBelowNodes: NodeStaticWrapperFunctionCreator = (
injectProps: NodeStaticWrapperFunctionCreatorProps<BaseIndentListConfig>
): NodeStaticWrapperFunction => {
export const renderIndentListBelowNodes: RenderStaticNodeWrapper = (
injectProps: RenderStaticNodeWrapperProps<BaseIndentListConfig>
): RenderStaticNodeWrapperFunction => {
const { element } = injectProps;

const listStyleType = element[BaseIndentListPlugin.key] as string;
Expand Down
9 changes: 4 additions & 5 deletions packages/toggle/src/react/renderToggleAboveNodes.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React from 'react';

import type {
NodeWrapperFunction,
NodeWrapperFunctionCreator,
RenderNodeWrapper,
RenderNodeWrapperFunction,
} from '@udecode/plate/react';

import { useIsVisible } from './toggleIndexAtom';

export const renderToggleAboveNodes: NodeWrapperFunctionCreator = () =>
ToggleAboveNodes;
export const renderToggleAboveNodes: RenderNodeWrapper = () => ToggleAboveNodes;

const ToggleAboveNodes: NodeWrapperFunction = ({ children, element }) => {
const ToggleAboveNodes: RenderNodeWrapperFunction = ({ children, element }) => {
const isVisible = useIsVisible(element.id as string);

if (isVisible) return children;
Expand Down

0 comments on commit 2740b76

Please sign in to comment.