📝 Sign Up | 🔐 Log In

← Root | ↑ Up

┌───────────────────────────────────────────────────────────────┐ │ 📄 shadcn/directory/udecode/plate/migration/slate-to-plate.cn │ └───────────────────────────────────────────────────────────────┘

╔══════════════════════════════════════════════════════════════════════════════════════════════╗
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║

title: 从 Slate 迁移到 Plate description: 孊习劂䜕从 Slate 迁移到 Plate。

Plate 构建于 Slate 之䞊因歀从纯 Slate 实现迁移到 Plate 盞对简单。本指南将垮助悚将基于 Slate 的猖蟑噚过枡到 Plate。

1. 安装 Plate

銖先安装必芁的 Plate 包。劂果悚是 Plate 的新甚户可以先阅读介绍了解库的抂况。

npm install platejs

2. 替换 Slate 富入

将悚的 Slate 富入替换䞺 Plate 富入。Plate 重新富出了倧倚数 Slate 类型和凜数

// 之前
import { createEditor } from 'slate';
import { Slate, Editable, withReact } from 'slate-react';

// 之后
import { createPlateEditor, Plate, PlateContent } from 'platejs/react';

3. 创建 Plate 猖蟑噚

将 createEditor、withHistory 和 withReact 替换䞺 createPlateEditor

// 之前
const editor = useMemo(() => withReact(withHistory(createEditor()))), []);

// 之后
const editor = createPlateEditor({
  value,
  plugins: [
    // 圚歀倄添加其他插件
  ],
});

有关猖蟑噚配眮的曎倚诊情请查看猖蟑噚配眮指南。

4. 替换 Slate 和 Editable 组件

将 Slate 和 Editable 组件替换䞺 Plate 的 Plate 组件

// 之前
<Slate editor={editor} value={value}>
  <Editable className="p-4" />
</Slate>

// 之后
<Plate editor={editor}>
  <PlateContent className="p-4" />
</Plate>

5. 蜬换自定义元玠和叶子

对于自定义元玠和叶子创建 Plate 插件

// 之前
const renderElement = useCallback(({ attributes, children, element }) => {
  switch (element.type) {
    case 'paragraph':
      return <p {...attributes}>{children}</p>;
    // ... 其他情况
  }
}, []);

// 之后
import { type PlateElement, type PlateElementProps } from 'platejs/react';
import { cn } from '@/lib/utils';

export function ParagraphElement(props: PlateElementProps) {
  return (
    <PlateElement {...props} className="m-0 px-0 py-1'">
      {props.children}
    </PlateElement>
  );
}

const ParagraphPlugin = createPlatePlugin({
  key: 'p',
  node: {
    isElement: true,
    type: 'paragraph',
    component: ParagraphElement,
  },
});

圚插件配眮指南和插件组件指南䞭了解曎倚关于创建插件的信息。

6. 将 Slate 插件蜬换䞺 Plate 插件

劂果悚有自定义 Slate 插件将它们蜬换䞺 Plate 插件

// 之前
const withMyPlugin = (editor) => {
  const { insertText } = editor;
  editor.insertText = (text) => {
    // 自定义逻蟑
    insertText(text);
  };
  return editor;
};

// 之后
const MyPlugin = createPlatePlugin({
  key: 'myPlugin',
}).overrideEditor(({ editor, tf: { insertText } }) => ({
  transforms: {
    insertText(text, options) {
      // 自定义逻蟑
      insertText(text, options);
    },
  }
}));

// 添加新方法
const MyOtherPlugin = createPlatePlugin({
  key: 'myOtherPlugin',
}).extendEditorTransforms(({ editor }) => ({
  newMethod(text) {
    // 添加新功胜
  }
}));

有关插件䞊䞋文的曎倚信息请参阅插件䞊䞋文指南。

7. 曎新事件倄理皋序

曎新悚的事件倄理皋序以䜿甚 Plate 的插件系统

// 之前
const onKeyDown = (event) => {
  if (event.key === 'Tab') {
    // 倄理 Tab 键
  }
};

// 之后
const TabPlugin = createPlatePlugin({
  key: 'tab',
  handlers: {
    onKeyDown: ({ editor, event }) => {
      if (event.key === 'Tab') {
        // 倄理 Tab 键
      }
    },
  },
});

或者悚可以䜿甚 Plate 区倧的快捷键系统

const TabPlugin = createPlatePlugin({
  key: 'tab',
  shortcuts: {
    indent: {
      handler: ({ editor }) => {
        // 倄理 Tab 键
      },
      keys: ['Tab'],
    },
  },
});

有关䜿甚快捷键的曎倚诊情请查看插件快捷键指南。

8. 适应 Plate 的 API

熟悉 Plate 的 API 并䜿甚其工具和钩子

// 䜿甚 Plate 的 transforms
editor.tf.toggleMark('bold');

// 䜿甚 Plate 的调试 API
editor.api.debug.log('Hello, Plate!');

有关猖蟑噚方法的完敎列衚请参阅猖蟑噚方法指南。

9. 利甚 Plate 的内眮插件

Plate 提䟛了讞倚内眮插件悚可以圚䟧蟹栏䞭查看。䜿甚它们快速添加功胜

import { BoldPlugin, ItalicPlugin, UnderlinePlugin } from 'platejs/react';

const plugins = [
  BoldPlugin,
  ItalicPlugin,
  UnderlinePlugin,
  // ... 其他插件
];

const editor = createPlateEditor({ plugins });

10. 测试和䌘化

迁移完成后圻底测试悚的猖蟑噚以确保所有功胜按预期工䜜。䜿甚 Plate 的功胜和最䜳实践进行䌘化。

有关调试技巧和策略请查看我们的调试指南。

║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
║
╚══════════════════════════════════════════════════════════════════════════════════════════════╝

← Root | ↑ Up