Ошибка «n.createFromText not a function» при попытке инициализировать редактор с текстом - PullRequest
1 голос
/ 01 мая 2020

Попытка обрезать текст в редакторе, прежде чем я отобразлю его пользователю, но я получаю следующую ошибку, пожалуйста, взгляните немного, вы можете поймать кое-что, что мне не хватает

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
...