Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions packages/components/timed-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,32 @@ class TimedTextEditor extends React.Component {
// Re-convert updated content to raw to gain access to block keys
const updatedContentBlocks = convertToRaw(updatedContent);

// Build block map, which maps the block keys of the previous content to the block keys of the
// updated content.
var blockMap = {};
for (var blockIdx = 0; blockIdx < currentContent.blocks.length; blockIdx++) {
blockMap[currentContent.blocks[blockIdx].key] = updatedContentBlocks.blocks[blockIdx].key;
}

// Get current selection state and update block keys
const selectionState = this.state.editorState.getSelection();

const selection = selectionState.merge({
anchorOffset: selectionState.getAnchorOffset(),
anchorKey: blockMap[selectionState.getAnchorKey()],
focusOffset: selectionState.getFocusOffset(),
focusKey: blockMap[selectionState.getFocusKey()],
});
// Check if editor has currently the focus. If yes, keep current selection.
if (selectionState.getHasFocus()) {

// Build block map, which maps the block keys of the previous content to the block keys of the
// updated content.
var blockMap = {};
for (var blockIdx = 0; blockIdx < currentContent.blocks.length; blockIdx++) {
blockMap[currentContent.blocks[blockIdx].key] = updatedContentBlocks.blocks[blockIdx].key;
}

// Set the updated selection state on the new editor state
const newEditorStateSelected = EditorState.forceSelection(newEditorState, selection);
this.setState({ editorState: newEditorStateSelected });
const selection = selectionState.merge({
anchorOffset: selectionState.getAnchorOffset(),
anchorKey: blockMap[selectionState.getAnchorKey()],
focusOffset: selectionState.getFocusOffset(),
focusKey: blockMap[selectionState.getFocusKey()],
});

// Set the updated selection state on the new editor state
const newEditorStateSelected = EditorState.forceSelection(newEditorState, selection);
this.setState({ editorState: newEditorStateSelected });
} else {
this.setState({ editorState: newEditorState });
}
}

loadData() {
Expand Down