操作(Operations)

操作是在当调用命令和转换时候执行的细粒度 (granular) 低级操作。单个命令可能导致许多更低级的操作被应用到编辑器。

和命令不同的地方是,操作是不可扩展的。Slate 核心定义了所有在富文本文档上可能用到的操作。比如说:

  1. editor.apply({
  2. type: 'insert_text',
  3. path: [0, 0],
  4. offset: 15,
  5. text: 'A new string of text to be inserted.',
  6. })
  7. editor.apply({
  8. type: 'remove_node',
  9. path: [0, 0],
  10. node: {
  11. text: 'A line of text!',
  12. },
  13. })
  14. editor.apply({
  15. type: 'set_selection',
  16. properties: {
  17. anchor: { path: [0, 0], offset: 0 },
  18. },
  19. newProperties: {
  20. anchor: { path: [0, 0], offset: 15 },
  21. },
  22. })

Slate 会自动将复杂的命令转换为低级操作,然后把它们应用到文档上,所以你基本上不用考虑它们。

🤖 Slate 的编辑行为被定义为操作 (operations),这使得协同编辑成为可能,因为每个变化都是更容易被定义、应用、组合的,以及更容易实现撤销事件的!