From 234404e5981b5d5195188bab6709ba6eecc32ea5 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 1 Aug 2019 11:27:09 +0200 Subject: [PATCH] add mod+z/y shortcuts, set editor state to what history manager returns --- .../views/elements/MessageEditor.js | 23 +++++++++++++++++++ src/editor/model.js | 5 ++++ 2 files changed, 28 insertions(+) diff --git a/src/components/views/elements/MessageEditor.js b/src/components/views/elements/MessageEditor.js index bdbbcba026..2a2872b2f0 100644 --- a/src/components/views/elements/MessageEditor.js +++ b/src/components/views/elements/MessageEditor.js @@ -34,6 +34,8 @@ import {MatrixClient} from 'matrix-js-sdk'; import classNames from 'classnames'; import {EventStatus} from 'matrix-js-sdk'; +const IS_MAC = navigator.platform.indexOf("Mac") !== -1; + function _isReply(mxEvent) { const relatesTo = mxEvent.getContent()["m.relates_to"]; const isReply = !!(relatesTo && relatesTo["m.in_reply_to"]); @@ -174,6 +176,27 @@ export default class MessageEditor extends React.Component { } _onKeyDown = (event) => { + const modKey = IS_MAC ? event.metaKey : event.ctrlKey; + // undo + if (modKey && event.key === "z") { + if (this.historyManager.canUndo()) { + const {parts, caret} = this.historyManager.undo(this.model); + // pass matching inputType so historyManager doesn't push echo + // when invoked from rerender callback. + this.model.reset(parts, caret, "historyUndo"); + } + event.preventDefault(); + } + // redo + if (modKey && event.key === "y") { + if (this.historyManager.canRedo()) { + const {parts, caret} = this.historyManager.redo(); + // pass matching inputType so historyManager doesn't push echo + // when invoked from rerender callback. + this.model.reset(parts, caret, "historyRedo"); + } + event.preventDefault(); + } // insert newline on Shift+Enter if (event.shiftKey && event.key === "Enter") { event.preventDefault(); // just in case the browser does support this diff --git a/src/editor/model.js b/src/editor/model.js index e1640c31ca..5c0b69bf03 100644 --- a/src/editor/model.js +++ b/src/editor/model.js @@ -90,6 +90,11 @@ export default class EditorModel { } } + reset(serializedParts, caret, inputType) { + this._parts = serializedParts.map(p => this._partCreator.deserializePart(p)); + this._updateCallback(caret, inputType); + } + update(newValue, inputType, caret) { const diff = this._diff(newValue, inputType, caret); const position = this.positionForOffset(diff.at, caret.atNodeEnd);