element-web/src/editor/history.ts

156 lines
5.3 KiB
TypeScript
Raw Normal View History

2019-08-01 09:25:04 +00:00
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import EditorModel from "./model";
import {IDiff} from "./diff";
import {SerializedPart} from "./parts";
import {Caret} from "./caret";
interface IHistory {
parts: SerializedPart[];
caret: Caret;
}
2019-08-01 14:10:21 +00:00
export const MAX_STEP_LENGTH = 10;
2019-08-01 09:25:04 +00:00
export default class HistoryManager {
private stack: IHistory[] = [];
private newlyTypedCharCount = 0;
private currentIndex = -1;
private changedSinceLastPush = false;
private lastCaret: Caret = null;
private nonWordBoundarySinceLastPush = false;
private addedSinceLastPush = false;
private removedSinceLastPush = false;
clear() {
this.stack = [];
this.newlyTypedCharCount = 0;
this.currentIndex = -1;
this.changedSinceLastPush = false;
this.lastCaret = null;
this.nonWordBoundarySinceLastPush = false;
this.addedSinceLastPush = false;
this.removedSinceLastPush = false;
2019-08-01 09:25:04 +00:00
}
private shouldPush(inputType, diff) {
// right now we can only push a step after
// the input has been applied to the model,
// so we can't push the state before something happened.
// not ideal but changing this would be harder to fit cleanly into
// the editor model.
const isNonBulkInput = inputType === "insertText" ||
inputType === "deleteContentForward" ||
inputType === "deleteContentBackward";
if (diff && isNonBulkInput) {
if (diff.added) {
this.addedSinceLastPush = true;
}
2019-08-01 09:25:04 +00:00
if (diff.removed) {
this.removedSinceLastPush = true;
2019-08-01 09:25:04 +00:00
}
// as long as you've only been adding or removing since the last push
if (this.addedSinceLastPush !== this.removedSinceLastPush) {
// add steps by word boundary, up to MAX_STEP_LENGTH characters
const str = diff.added ? diff.added : diff.removed;
const isWordBoundary = str === " " || str === "\t" || str === "\n";
if (this.nonWordBoundarySinceLastPush && isWordBoundary) {
return true;
}
if (!isWordBoundary) {
this.nonWordBoundarySinceLastPush = true;
}
this.newlyTypedCharCount += str.length;
return this.newlyTypedCharCount > MAX_STEP_LENGTH;
} else {
// if starting to remove while adding before, or the opposite, push
return true;
2019-08-01 09:25:04 +00:00
}
} else {
// bulk input (paste, ...) should be pushed every time
2019-08-01 09:25:04 +00:00
return true;
}
}
private pushState(model: EditorModel, caret: Caret) {
2019-08-01 09:25:04 +00:00
// remove all steps after current step
while (this.currentIndex < (this.stack.length - 1)) {
this.stack.pop();
2019-08-01 09:25:04 +00:00
}
const parts = model.serializeParts();
this.stack.push({parts, caret});
this.currentIndex = this.stack.length - 1;
this.lastCaret = null;
this.changedSinceLastPush = false;
this.newlyTypedCharCount = 0;
this.nonWordBoundarySinceLastPush = false;
this.addedSinceLastPush = false;
this.removedSinceLastPush = false;
2019-08-01 09:25:04 +00:00
}
// needs to persist parts and caret position
tryPush(model: EditorModel, caret: Caret, inputType: string, diff: IDiff) {
2019-08-01 09:25:04 +00:00
// ignore state restoration echos.
// these respect the inputType values of the input event,
// but are actually passed in from MessageEditor calling model.reset()
// in the keydown event handler.
if (inputType === "historyUndo" || inputType === "historyRedo") {
return false;
}
const shouldPush = this.shouldPush(inputType, diff);
2019-08-01 09:25:04 +00:00
if (shouldPush) {
this.pushState(model, caret);
2019-08-01 09:25:04 +00:00
} else {
this.lastCaret = caret;
this.changedSinceLastPush = true;
2019-08-01 09:25:04 +00:00
}
return shouldPush;
}
ensureLastChangesPushed(model: EditorModel) {
if (this.changedSinceLastPush) {
this.pushState(model, this.lastCaret);
}
}
2019-08-01 09:25:04 +00:00
canUndo() {
return this.currentIndex >= 1 || this.changedSinceLastPush;
2019-08-01 09:25:04 +00:00
}
canRedo() {
return this.currentIndex < (this.stack.length - 1);
2019-08-01 09:25:04 +00:00
}
// returns state that should be applied to model
undo(model: EditorModel) {
2019-08-01 09:25:04 +00:00
if (this.canUndo()) {
this.ensureLastChangesPushed(model);
this.currentIndex -= 1;
return this.stack[this.currentIndex];
2019-08-01 09:25:04 +00:00
}
}
// returns state that should be applied to model
redo() {
if (this.canRedo()) {
this.changedSinceLastPush = false;
this.currentIndex += 1;
return this.stack[this.currentIndex];
2019-08-01 09:25:04 +00:00
}
}
}