From be3ef2b50e2f0b754f7effd67acd2ebc89baf55f Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 15 Jan 2020 19:08:14 -0700 Subject: [PATCH 1/3] Remove all text when cutting in the composer The previous function did in fact remove the elements, but left the model thinking there was a zero-length string. This approach deletes the text from the model and the DOM, resulting in the placeholder being shown when cutting all the text. Part of https://github.com/vector-im/riot-web/issues/11378 --- src/components/views/rooms/BasicMessageComposer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/rooms/BasicMessageComposer.js b/src/components/views/rooms/BasicMessageComposer.js index d9604cf030..c605953473 100644 --- a/src/components/views/rooms/BasicMessageComposer.js +++ b/src/components/views/rooms/BasicMessageComposer.js @@ -209,8 +209,9 @@ export default class BasicMessageEditor extends React.Component { const selectedParts = range.parts.map(p => p.serialize()); event.clipboardData.setData("application/x-riot-composer", JSON.stringify(selectedParts)); if (type === "cut") { - selection.deleteFromDocument(); - range.replace([]); + // Remove the text from the composer + const {caret} = getCaretOffsetAndText(this._editorRef, selection); + this.props.model.update("", event.inputType, caret); } event.preventDefault(); } From b137cd21d3832213ba55ad0c8d74c466f67e96b3 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 15 Jan 2020 21:04:18 -0700 Subject: [PATCH 2/3] Replace the selected range instead of force-setting it This gives people the option of cutting parts or all of their message. --- src/components/views/rooms/BasicMessageComposer.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/BasicMessageComposer.js b/src/components/views/rooms/BasicMessageComposer.js index c605953473..0dd9d16e51 100644 --- a/src/components/views/rooms/BasicMessageComposer.js +++ b/src/components/views/rooms/BasicMessageComposer.js @@ -209,9 +209,8 @@ export default class BasicMessageEditor extends React.Component { const selectedParts = range.parts.map(p => p.serialize()); event.clipboardData.setData("application/x-riot-composer", JSON.stringify(selectedParts)); if (type === "cut") { - // Remove the text from the composer - const {caret} = getCaretOffsetAndText(this._editorRef, selection); - this.props.model.update("", event.inputType, caret); + // Remove the text, updating the model as appropriate + replaceRangeAndMoveCaret(range, []); } event.preventDefault(); } From 1b4ab856c9b9fdfe79a2e97685fe6816db68bf9a Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 15 Jan 2020 21:05:00 -0700 Subject: [PATCH 3/3] Assume the position is at the end when the offset has no last part We get an NPE when the user cuts their entire message, and this fixes it. --- src/editor/position.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/editor/position.js b/src/editor/position.js index 4693f62999..726377ef48 100644 --- a/src/editor/position.js +++ b/src/editor/position.js @@ -117,7 +117,7 @@ export default class DocumentPosition { } offset += this.offset; const lastPart = model.parts[this.index]; - const atEnd = offset >= lastPart.text.length; + const atEnd = !lastPart || offset >= lastPart.text.length; // if no last part, we're at the end return new DocumentOffset(offset, atEnd); }