【问题标题】:How to set selection after Transforms.insertNodes() in Slate JS如何在 Slate JS 中的 Transforms.insertNodes() 之后设置选择
【发布时间】:2021-05-12 18:12:03
【问题描述】:

我正在尝试在当前选择的正下方添加一个新行,然后将选择放入新行。

let current_path = props.selection.anchor.path[0]
Transforms.insertNodes(editor, {type:'line', children:[{ text:'' }]},{at: [current_path+1]});
const point = { anchor: { path: [current_path+1, 0], offset: 0 }, focus: { path: [current_path+1, 0], offset: 0 }}
// set focus
ReactEditor.focus(editor);
// set selection
Transforms.select(editor, point);

但这出现了一个错误:错误:无法从 Slate 节点解析 DOM 节点:{"text":""}。有谁知道如何解决它或有其他方法来实现它?谢谢!

【问题讨论】:

    标签: slatejs


    【解决方案1】:

    我偶然发现了这个问题并找到了答案。您需要保存selection,然后使用Transform.select 来恢复它。

    例如

    import { Transforms } from "slate";
    
    
    // clone to store selection
    const previousSelection = Object.assign({}, editor.selection);
    
    // restore selection
    Transform.select(editor, previousSelection)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多