2017-04-13 55 views
0

當我輸入space欄時,我試圖將一個單詞分成兩個單詞。我的文本中的每個單詞都是一個實體,因此當我將單詞分成兩部分時,我需要更新文本並創建一個新實體。立即更新編輯器狀態,同時執行兩個操作

我正在使用Modifier模塊進行更新。

const editorStateAfterText = 
    EditorState.push(
    editorState, 
    Modifier.insertText(
     contentState, 
     selectionState, 
     ' ', 
    ), 
    command, 
); 
const editorStateAfterEntity = 
    EditorState.push(
    editorStateAfterText, 
    Modifier.applyEntity(
     contentState, 
     newSelectionState, 
     newEntityKey 
    ), 
    command, 
); 
this.setState({editorState: editorStateAfterEntity}) 

我試圖用兩個操作一次更新編輯器狀態。如果另一個不存在,他們都會工作。當兩者存在時,它只更新最後一個。

有什麼方法可以更新文本(拆分詞)並將新實體添加到entityMap

回答

1

如在文檔https://draftjs.org/docs/api-reference-editor-state.html#push定義,push是要求3個PARAMS:editorStatecontentStatecommand

我是做這行的editorStateAfterEntity經過更新的editorState參數與editorStateAfterText,但我忽略了更新contentState

因此,這裏又是如何呢終於摸索:

const contentAfterText = Modifier.insertText(
    contentState, 
    selectionState, 
    ' ', 
); 
    const editorStateAfterText = EditorState.push(
    editorState, 
    contentAfterText, 
    command, 
); 
    const contentStateAfterTextAndEntity = Modifier.applyEntity(
    contentAfterText, 
    newSelectionState, 
    newEntityKey 
); 
    const editorStateAfterTextAndEntity = EditorState.push(
    editorStateAfterText, 
    contentStateAfterTextAndEntity, 
    command, 
); 
    this.setState({editorState: editorStateAfterTextAndEntity});