From 905aa81bf828ca01780380526611f37a92919837 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 10 Oct 2019 16:39:41 +0200 Subject: [PATCH 1/2] safeguard if the offsetnode is null when determining caret position --- src/editor/dom.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/editor/dom.js b/src/editor/dom.js index 9073eb37a3..e82c3f70ca 100644 --- a/src/editor/dom.js +++ b/src/editor/dom.js @@ -92,6 +92,10 @@ function getSelectionOffsetAndText(editor, selectionNode, selectionOffset) { // gets the caret position details, ignoring and adjusting to // the ZWS if you're typing in a caret node function getCaret(node, offsetToNode, offsetWithinNode) { + // if no node is selected, return an offset at the start + if (!node) { + return new DocumentOffset(0, false); + } let atNodeEnd = offsetWithinNode === node.textContent.length; if (node.nodeType === Node.TEXT_NODE && isCaretNode(node.parentElement)) { const zwsIdx = node.nodeValue.indexOf(CARET_NODE_CHAR); From f8a610687f93508edea6b65e113b1d49bbe46f67 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 10 Oct 2019 16:40:03 +0200 Subject: [PATCH 2/2] don't persist caret when selection is missing so caret will be put back at end of editor when remounting --- src/components/views/rooms/EditMessageComposer.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/EditMessageComposer.js b/src/components/views/rooms/EditMessageComposer.js index fce30e6081..00d2e447af 100644 --- a/src/components/views/rooms/EditMessageComposer.js +++ b/src/components/views/rooms/EditMessageComposer.js @@ -210,9 +210,18 @@ export default class EditMessageComposer extends React.Component { } componentWillUnmount() { + // store caret and serialized parts in the + // editorstate so it can be restored when the remote echo event tile gets rendered + // in case we're currently editing a pending event const sel = document.getSelection(); - const {caret} = getCaretOffsetAndText(this._editorRef, sel); + let caret; + if (sel.focusNode) { + caret = getCaretOffsetAndText(this._editorRef, sel).caret; + } const parts = this.model.serializeParts(); + // if caret is undefined because for some reason there isn't a valid selection, + // then when mounting the editor again with the same editor state, + // it will set the cursor at the end. this.props.editState.setEditorState(caret, parts); } @@ -239,7 +248,7 @@ export default class EditMessageComposer extends React.Component { _getInitialCaretPosition() { const {editState} = this.props; let caretPosition; - if (editState.hasEditorState()) { + if (editState.hasEditorState() && editState.getCaret()) { // if restoring state from a previous editor, // restore caret position from the state const caret = editState.getCaret();