gpt4 book ai didi

slatejs - 如何在 Slate JS 中的 Transforms.insertNodes() 之后设置选择

转载 作者:行者123 更新时间:2023-12-03 17:24:42 27 4
gpt4 key购买 nike

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

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":""}。有谁知道如何解决它或有其他方法来实现它?谢谢!

最佳答案

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

import { Transforms } from "slate";


// clone to store selection
const previousSelection = Object.assign({}, editor.selection);

// restore selection
Transform.select(editor, previousSelection)

关于slatejs - 如何在 Slate JS 中的 Transforms.insertNodes() 之后设置选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62448817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com