Как перевести состояние содержимого в текущее состояние редактора? мне кажется, метод editorState.push не работает?
Есть некоторый простой текстовый контент, который нужно вставить, чтобы существовать textEditor, я пытаюсь использовать метод EditorState.push, что я думаю, что это работает для ситуации. Я пытаюсь что-то вроде этого:
const { ContentState: { createFromText }, EditorState: { createWithContent, push }} = DraftJS;
export const pushTextToCurrentEditorState = (text, editorState) => {
const textContentState = createFromText(text);
const newEditorState = push(editorState, textContentState, 'insert-characters');
// debugger;
console.log(editorStateToJSON(editorState))
console.log(editorStateToJSON(newEditorState))
return JSON.parse(editorStateToJSON(newEditorState));
}
результат newEditorState
это не состояние слияния, а замена одного, более старого значения editorState, newEditorState
стать совершенно новой вещью, как создать из text
, Здесь что-то не так? или есть другие способы решения проблемы?
1 ответ
Я устал сложным способом, но решил эту проблему. код здесь:
export const pushTextToCurrentEditorState = (text, editorState) => {
const textContentState = createFromText(text);
const textContentBlocksArr = textContentState.getBlocksAsArray();
const currentBlocksArr = editorState.getCurrentContent().getBlocksAsArray();
const newBlocksArr = currentBlocksArr.concat(textContentBlocksArr);
const newContentState = createFromBlockArray(newBlocksArr);
const newEditorState = createWithContent(newContentState);
return JSON.parse(editorStateToJSON(newEditorState));
}
способ: 1. преобразовать состояние содержимого, текста в массив блоков; 2. объединить два блока вместе; 3. создать новое состояние содержимого и состояние редактора с объединенным массивом;