2016-08-03 2 views
2

editorState에서 까다로운 상태 변이를하고 있고 선택을 잃어 버리고 있습니다.돌연변이 후 선택을 잃는

필자는 currentText()를 가져와 일부 마법 라이브러리가있는 HTML로 변환하고이를 editorState로 다시 변환해야합니다. 그게 잘 작동, 그것은 너무 열심히 휴식 단지 선택입니다.

지금 당장은 첫 번째 시작에서 선택을 시도한 다음 forceSelection을 수행하지만 selection.hasFocus()과 관련된 일부 오류는 발생하지 않습니다.

저는 앵커와 오프셋을 기반으로 "새로운"선택을 계산해야하지만 실제로 그렇게 할 생각은 없다고 생각합니다.

// onChangeHandler: 

const currentContentState = editorState.getCurrentContent() 
const selectionState = editorState.getSelection() 

const plainHtml = magicOperation(currentContentState.getPlainText()) 

const currentContentBlocks = convertFromHTML(plainHtml) 
const contentState = ContentState.createFromBlockArray(currentContentBlocks) 

const newEditorState = EditorState.createWithContent(contentState) 

this.setState({ 
    editorState: EditorState.forceSelection(
    newEditorState, 
    selectionState 
) 
}) 

가 해킹되어 나는 그것을 달성 할 수 있다면 난 그냥 내가 확실히 사용하는 것이 원활하게 작동 내가 할 경우, 주변 DraftJS을 연주 해요 알고 : 같은

는 지금 내 코드 보인다 editorState 내에 HTML을 추가하기위한 Decorator.

시간 내 주셔서 감사합니다.

답변

3

selectionState에는 블록 키 (anchorKey & focusKey)가 있습니다. 전체 블록을 바꾸기 때문에 키가 변경되었습니다. 당신이해야 할 일은 새로운 editorState에 적용하기 전에 오프셋에서 키를 찾아서 새로운 selectionState로 설정하는 것입니다.

나는 일반 텍스트를 html로 변환하고 다시 설정해야하는 이유가 흥미 롭습니다.

+0

그래, convertinfFromHTML 대신 엔티티를 구현하고 있는데 여전히 선택 항목을 계산해야합니다. 권리? – davesnx

+0

번호. selectionState의 anchorKey 및 focusKey 속성을 설정해야합니다. –

+0

을 선택하고 병합 하시겠습니까? – davesnx