这就是我过去在 ^0.42.2 版的 slate 文本编辑器中获取块的方式。
import { Value, Block } from 'slate';
const value = loadInitialData ? Value.fromJSON(initialValue) : Value.create();
getTitle = (value) => {
const firstBlock = value.document.getBlocks().get(0);
const secondBlock = value.document.getBlocks().get(1);
const title = firstBlock.text ? firstBlock.text : 'No Title';
const subTitle = secondBlock.text ? secondBlock.text : 'No SubTitle';
return {title, subTitle};
}
现在它经历了重大变化,它的 api 完全改变了,我看不到任何例子如何从它的 DOM 中获取节点。
我试过这个,但我得到了错误:无法在节点中的路径 [0] 找到后代:{“children”:[],“operations”:[],“selection”:null,“marks”:null,“history” :{"撤消":[],"重做":[]}}
import { Node } from "slate";
const node = Node.get(editor, [0])
console.log("node",node)
我想选择节点的第一个元素作为标题,然后将博客保存到数据库中。过去 2 天我无法进步,我阅读了整个 slate.js api,阅读了 github 中几乎所有无法弄清楚的问题。
const getTitle = () => {
const [firstBlock, secondBlock] = value;
const title =
firstBlock && firstBlock.type === "heading-one"
? firstBlock.children[0].text
: "No title";
const subtitle =
secondBlock && secondBlock.type === "heading-two"
? secondBlock.children[0].text
: "No subtitle";
return {
title,
subtitle
};
};
当我写这篇文章时,我收到一条错误消息“myValue is not iterable”。然后我切换到这个:
const firstBlock = myValue[0];
const secondBlock = myValue[1];
现在两个值都返回未定义。这是我在文本编辑器上的值: