add mod+z/y shortcuts, set editor state to what history manager returns

pull/21833/head
Bruno Windels 2019-08-01 11:27:09 +02:00
parent 98bc0d24f4
commit 234404e598
2 changed files with 28 additions and 0 deletions

View File

@ -34,6 +34,8 @@ import {MatrixClient} from 'matrix-js-sdk';
import classNames from 'classnames'; import classNames from 'classnames';
import {EventStatus} from 'matrix-js-sdk'; import {EventStatus} from 'matrix-js-sdk';
const IS_MAC = navigator.platform.indexOf("Mac") !== -1;
function _isReply(mxEvent) { function _isReply(mxEvent) {
const relatesTo = mxEvent.getContent()["m.relates_to"]; const relatesTo = mxEvent.getContent()["m.relates_to"];
const isReply = !!(relatesTo && relatesTo["m.in_reply_to"]); const isReply = !!(relatesTo && relatesTo["m.in_reply_to"]);
@ -174,6 +176,27 @@ export default class MessageEditor extends React.Component {
} }
_onKeyDown = (event) => { _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 // insert newline on Shift+Enter
if (event.shiftKey && event.key === "Enter") { if (event.shiftKey && event.key === "Enter") {
event.preventDefault(); // just in case the browser does support this event.preventDefault(); // just in case the browser does support this

View File

@ -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) { update(newValue, inputType, caret) {
const diff = this._diff(newValue, inputType, caret); const diff = this._diff(newValue, inputType, caret);
const position = this.positionForOffset(diff.at, caret.atNodeEnd); const position = this.positionForOffset(diff.at, caret.atNodeEnd);