自定义节点

在之前的例子中,我们从实现一个段落开始,但是实际上我们从来没有告诉过 Slate 关于 paragraph block 节点的任何信息。我们仅仅是使用了内置的默认渲染器,它使用的是普通古老的 <div>

但是你能做到的不止如此。Slate 允许我们定义任何类型的自定义 block 节点,比如块引用代码块列表项等。

我们将给你展示如何做到。让我们从之前的应用程序继续吧:

  1. const App = () => {
  2. const editor = useMemo(() => withReact(createEditor()), [])
  3. const [value, setValue] = useState([
  4. {
  5. type: 'paragraph',
  6. children: [{ text: 'A line of text in a paragraph.' }],
  7. },
  8. ])
  9. return (
  10. <Slate editor={editor} value={value} onChange={value => setValue(value)}>
  11. <Editable
  12. onKeyDown={event => {
  13. if (event.key === '&') {
  14. event.preventDefault()
  15. editor.insertText("and")
  16. }
  17. }}
  18. />
  19. </Slate>
  20. )
  21. }

现在让我们添加“代码块”到我们的编辑器中。

问题是,代码块不仅仅要渲染为一个普通的段落,它还需要以不同的方式渲染出来。为了做到这一点,我们需要为 code 元素节点定义一个特定的render

元素render仅仅是一个简单的 React 组件。像是这样:

  1. // 为 code 节点定义一个 React 组件渲染器
  2. const CodeElement = props => {
  3. return (
  4. <pre {...props.attributes}>
  5. <code>{props.children}</code>
  6. </pre>
  7. )
  8. }

非常简单。

看到 props.attributes 参数了吗?Slate 将需要在 block 顶层元素上渲染的属性通过这种方式传入。这样你就不必自己去构建它们了。你必须在你的组件中传入这些属性。

另外,看到 props.children 参数了吗?Slate 会自动为你渲染 block 的所有子元素,并且就像在其他 React 组件中那样,通过 props.children 传递给你。这样你就不必去为如何正确渲染文本节点或其他类似的事情而费神了。你必须将这些子节点作为最终的叶子节点在你的组件中渲染。

下面是一个默认元素的组件:

  1. const DefaultElement = props => {
  2. return <p {...props.attributes}>{props.children}</p>
  3. }

现在,让我们为 Editor 添加一些渲染器:

  1. const App = () => {
  2. const editor = useMemo(() => withReact(createEditor()), [])
  3. const [value, setValue] = useState([
  4. {
  5. type: 'paragraph',
  6. children: [{ text: 'A line of text in a paragraph.' }],
  7. },
  8. ])
  9. // 基于传递的 props 定义一个渲染函数。
  10. // 我们在这里使用 useCallback 在随后的渲染中记住这个函数。
  11. const renderElement = useCallback(props => {
  12. switch (props.element.type) {
  13. case 'code':
  14. return <CodeElement {...props} />
  15. default:
  16. return <DefaultElement {...props} />
  17. }
  18. }, [])
  19. return (
  20. <Slate editor={editor} value={value} onChange={value => setValue(value)}>
  21. <Editable
  22. // Pass in the `renderElement` function.
  23. renderElement={renderElement}
  24. onKeyDown={event => {
  25. if (event.key === '&') {
  26. event.preventDefault()
  27. editor.insertText("and")
  28. }
  29. }}
  30. />
  31. </Slate>
  32. )
  33. }
  34. const CodeElement = props => {
  35. return (
  36. <pre {...props.attributes}>
  37. <code>{props.children}</code>
  38. </pre>
  39. )
  40. }
  41. const DefaultElement = props => {
  42. return <p {...props.attributes}>{props.children}</p>
  43. }

好了,但是我们还需要一个办法让用户实际转换一个 block 为代码块。所以让我们修改 onKeyDown 函数,添加一个 ctrl + -快捷键来做这件事:

  1. // Import the `Editor` and `Transforms` helpers from Slate.
  2. import { Editor, Transforms } from 'slate'
  3. const App = () => {
  4. const editor = useMemo(() => withReact(createEditor()), [])
  5. const [value, setValue] = useState([
  6. {
  7. type: 'paragraph',
  8. children: [{ text: 'A line of text in a paragraph.' }],
  9. },
  10. ])
  11. const renderElement = useCallback(props => {
  12. switch (props.element.type) {
  13. case 'code':
  14. return <CodeElement {...props} />
  15. default:
  16. return <DefaultElement {...props} />
  17. }
  18. }, [])
  19. return (
  20. <Slate editor={editor} value={value} onChange={value => setValue(value)}>
  21. <Editable
  22. renderElement={renderElement}
  23. onKeyDown={event => {
  24. if (event.key === '`' && event.ctrlKey) {
  25. // 阻止插入 "`" 的默认行为。
  26. event.preventDefault()
  27. // 否则,把当前选择的 blocks 的类型设为 "code"
  28. Transforms.setNodes(
  29. editor,
  30. { type: 'code' },
  31. { match: n => Editor.isBlock(editor, n) }
  32. )
  33. }
  34. }}
  35. />
  36. </Slate>
  37. )
  38. }
  39. const CodeElement = props => {
  40. return (
  41. <pre {...props.attributes}>
  42. <code>{props.children}</code>
  43. </pre>
  44. )
  45. }
  46. const DefaultElement = props => {
  47. return <p {...props.attributes}>{props.children}</p>
  48. }

现在,如果你按下 ctrl + - ,你光标所在的块应该会转换为一个代码块!多么神奇!

但是我们忘记了一件事。当我们再次按下 ctrl + - ,它应该从代码块变回普通段落。为了做到这点,我们需要添加一点点逻辑,基于我们当前选择的块是否已经是一个代码块来改变我们设置的类型::

  1. const App = () => {
  2. const editor = useMemo(() => withReact(createEditor()), [])
  3. const [value, setValue] = useState([
  4. {
  5. type: 'paragraph',
  6. children: [{ text: 'A line of text in a paragraph.' }],
  7. },
  8. ])
  9. const renderElement = useCallback(props => {
  10. switch (props.element.type) {
  11. case 'code':
  12. return <CodeElement {...props} />
  13. default:
  14. return <DefaultElement {...props} />
  15. }
  16. }, [])
  17. return (
  18. <Slate editor={editor} value={value} onChange={value => setValue(value)}>
  19. <Editable
  20. renderElement={renderElement}
  21. onKeyDown={event => {
  22. if (event.key === '`' && event.ctrlKey) {
  23. event.preventDefault()
  24. // 确定当前选中的块是否为任意的代码块.
  25. const [match] = Editor.nodes(editor, {
  26. match: n => n.type === 'code',
  27. })
  28. // 根据是否已经存在匹配项来切换 block 的类型.
  29. Transforms.setNodes(
  30. editor,
  31. { type: match ? 'paragraph' : 'code' },
  32. { match: n => Editor.isBlock(editor, n) }
  33. )
  34. }
  35. }}
  36. />
  37. </Slate>
  38. )
  39. }

现在你完成了!如果你在一个代码块中按下 ctrl + -,它将会变回一个段落!