Reset Node

Reset the block type using rules.

Loading...
Files
components/demo.tsx
'use client';

import React from 'react';

import { Plate } from '@udecode/plate-common/react';

import { editorPlugins } from '@/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/components/editor/use-create-editor';
import { Editor, EditorContainer } from '@/components/plate-ui/editor';

import { DEMO_VALUES } from './values/demo-values';

export default function Demo({ id }: { id: string }) {
  const editor = useCreateEditor({
    plugins: [...editorPlugins],
    value: DEMO_VALUES[id],
  });

  return (
    <Plate editor={editor}>
      <EditorContainer variant="demo">
        <Editor />
      </EditorContainer>
    </Plate>
  );
}

功能

  • 在按键事件(如 Enter)时快速重置文本块格式为默认值。
  • 要测试,请在空块或块引号末尾按 Enter,或在块引号开头按 Backspace。

安装

npm install @udecode/plate-reset-node

用法

// ...
import {
  isBlockAboveEmpty,
  isSelectionAtBlockStart,
} from '@udecode/plate-common';
import { ResetNodePlugin } from '@udecode/plate-reset-node/react';
 
const resetBlockTypesCommonRule = {
  types: [BlockquotePlugin.key, TodoListPlugin.key],
  defaultType: ParagraphPlugin.key,
};
 
const resetBlockTypesCodeBlockRule = {
  types: [CodeBlockPlugin.key],
  defaultType: ParagraphPlugin.key,
  onReset: unwrapCodeBlock,
};
 
const plugins = [
  // ...otherPlugins,
  ResetNodePlugin.configure({
    options: {
      rules: [
        {
          ...resetBlockTypesCommonRule,
          hotkey: 'Enter',
          predicate: isBlockAboveEmpty,
        },
        {
          ...resetBlockTypesCommonRule,
          hotkey: 'Backspace',
          predicate: isSelectionAtBlockStart,
        },
        {
          ...resetBlockTypesCodeBlockRule,
          hotkey: 'Enter',
          predicate: isCodeBlockEmpty,
        },
        {
          ...resetBlockTypesCodeBlockRule,
          hotkey: 'Backspace',
          predicate: isSelectionAtCodeBlockStart,
        },
      ],
    },
  }),
];

插件

ResetNodePlugin

Options

Collapse all

    一个规则数组,用于控制节点重置的执行方式。每个规则包括:

    是否禁用编辑器中第一个块的重置。

    是否禁用整个编辑器的重置操作。