在将文本显示给用户之前尝试在编辑器中截断文本,但出现以下错误,请仔细查看您可能会发现我遗漏的一些内容
main.js:46 TypeError: n.createFromText is not a function
我的代码
const truncate = (editorState, charCount) =>{ // charCount is a number
const contentState = editorState.getCurrentContent();
const blocks = contentState.getBlockMap();
let count = 0;
let isTruncated = false;
const truncatedBlocks = [];
blocks.forEach((block) => {
if (!isTruncated) {
const length = block.getLength();
if (count + length > charCount) {
isTruncated = true;
const truncatedText = block.getText().slice(0, charCount - count);
console.log(contentState)
const state = contentState.createFromText(`${truncatedText}...`); // the error is around here
truncatedBlocks.push(state.getFirstBlock());
} else {
truncatedBlocks.push(block);
}
count += length + 1;
}
});
if (isTruncated) {
const state = contentState.createFromBlockArray(truncatedBlocks);
return EditorState.createWithContent(state);
}
return editorState;
}
const ContentState = convertFromRaw(JSON.parse(this.props.description));
const editorState = EditorState.createWithContent(ContentState);
const TrancatedText = this.props.charCount ? truncate(editorState, this.props.charCount): editorState