使用 npm 或 yarn 安装
npm install slate slate-react
# 或者
yarn add slate slate-react
在 React 应用中引入 Slate 并初始化
import React, { useMemo, useState, useCallback } from 'react';
import { createEditor } from 'slate';
import { Slate, Editable, withReact } from 'slate-react';
const MyEditor = () => {
const editor = useMemo(() => withReact(createEditor()), []);
const [value, setValue] = useState([
{
type: 'paragraph',
children: [{ text: 'Hello, Slate!' }],
},
]);
const renderElement = useCallback(props => {
switch (props.element.type) {
case 'code':
return <CodeElement {...props} />;
default:
return <DefaultElement {...props} />;
}
}, []);
return (
<Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
<Editable renderElement={renderElement} />
</Slate>
);
};
const CodeElement = props => {
return (
<pre {...props.attributes}>
<code>{props.children}</code>
</pre>
);
};
const DefaultElement = props => {
return <p {...props.attributes}>{props.children}</p>;
};
export default MyEditor;
1. createEditor()
描述: 创建一个新的 Slate 编辑器实例。
参数: 无
示例:
const editor = createEditor();
2. Slate
组件
描述: Slate 的上下文提供者,用于管理编辑器状态和提供编辑器实例。
参数:
editor
: 编辑器实例。value
: 编辑器内容的初始值。onChange
: 当编辑器内容发生变化时的回调函数。示例:
<Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
<Editable />
</Slate>
3. Editable
组件
描述: 渲染一个可编辑区域,供用户输入内容。
参数:
renderElement
: 一个函数,用于渲染自定义元素。renderLeaf
: 一个函数,用于渲染自定义叶子节点。placeholder
: 一个字符串,用于显示在编辑器为空时的占位符。示例:
<Editable renderElement={renderElement} />
4. Transforms
对象
描述: 提供了一系列方法,用于对编辑器内容进行变换操作。
方法示例:
Transforms.insertText
: 插入文本。Transforms.removeNodes
: 移除节点。Transforms.insertNodes
: 插入节点。示例:
import { Transforms } from 'slate';
// 在当前位置插入文本
Transforms.insertText(editor, 'Hello, world!');
5. Editor
对象
描述: 提供了访问编辑器状态和内容的方法。
方法示例:
Editor.children
: 获取编辑器的子节点。Editor.marks
: 获取当前选区的标记。Editor.addMark
: 为选区添加标记。示例:
import { Editor } from 'slate';
// 获取编辑器内容
const children = Editor.children(editor);
// 为选区添加粗体标记
Editor.addMark(editor, 'bold', true);
6. Element
对象
描述: 用于定义自定义元素的结构和属性。
示例:
import { Element as SlateElement } from 'slate';
const CustomElement = {
type: 'paragraph',
children: [{ text: 'This is a custom element!' }],
};
7. Node
对象
描述: 提供了操作节点的方法。
方法示例:
Node.hasChildren
: 检查节点是否有子节点。Node.get
: 获取节点的值。示例:
import { Node } from 'slate';
// 检查节点是否有子节点
const hasChildren = Node.hasChildren(editor, path);
自定义渲染和操作: Slate 允许开发者自定义渲染逻辑和编辑操作,以满足特定需求。
示例:
const renderElement = useCallback(props => {
switch (props.element.type) {
case 'block-quote':
return <blockquote {...props.attributes}>{props.children}</blockquote>;
default:
return <p {...props.attributes}>{props.children}</p>;
}
}, []);
const withCustomPlugin = editor => {
const { insertText } = editor;
editor.insertText = text => {
if (text === 'magic') {
const { selection } = editor;
if (selection) {
Transforms.insertText(editor, '✨magic✨', { at: selection });
}
} else {
insertText(text);
}
};
return editor;
};
const editor = useMemo(() => withCustomPlugin(withReact(createEditor())), []);