element-web/src/editor/model.js

422 lines
15 KiB
JavaScript
Raw Normal View History

2019-05-06 19:21:28 +03:00
/*
Copyright 2019 New Vector Ltd
2019-05-22 17:16:32 +03:00
Copyright 2019 The Matrix.org Foundation C.I.C.
2019-05-06 19:21:28 +03:00
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 {diffAtCaret, diffDeletion} from "./diff";
import DocumentPosition from "./position";
import Range from "./range";
2019-05-06 19:21:28 +03:00
2019-08-26 17:10:02 +03:00
/**
* @callback ModelCallback
* @param {DocumentPosition?} caretPosition the position where the caret should be position
* @param {string?} inputType the inputType of the DOM input event
* @param {object?} diff an object with `removed` and `added` strings
*/
/**
* @callback TransformCallback
* @param {DocumentPosition?} caretPosition the position where the caret should be position
* @param {string?} inputType the inputType of the DOM input event
* @param {object?} diff an object with `removed` and `added` strings
* @return {Number?} addedLen how many characters were added/removed (-) before the caret during the transformation step.
This is used to adjust the caret position.
*/
2019-05-06 19:21:28 +03:00
export default class EditorModel {
constructor(parts, partCreator, updateCallback = null) {
2019-05-06 19:21:28 +03:00
this._parts = parts;
this._partCreator = partCreator;
this._activePartIdx = null;
this._autoComplete = null;
this._autoCompletePartIdx = null;
this._transformCallback = null;
this.setUpdateCallback(updateCallback);
this._updateInProgress = false;
}
/** Set a callback for the transformation step.
* While processing an update, right before calling the update callback,
* a transform callback can be called, which serves to do modifications
* on the model that can span multiple parts. Also see `startRange()`.
* @param {TransformCallback} transformCallback
*/
setTransformCallback(transformCallback) {
this._transformCallback = transformCallback;
}
2019-08-26 17:10:02 +03:00
/** Set a callback for rerendering the model after it has been updated.
* @param {ModelCallback} updateCallback
*/
setUpdateCallback(updateCallback) {
this._updateCallback = updateCallback;
2019-05-06 19:21:28 +03:00
}
get partCreator() {
return this._partCreator;
}
2019-08-06 18:52:47 +03:00
get isEmpty() {
return this._parts.reduce((len, part) => len + part.text.length, 0) === 0;
}
clone() {
return new EditorModel(this._parts, this._partCreator, this._updateCallback);
}
2019-05-06 19:21:28 +03:00
_insertPart(index, part) {
this._parts.splice(index, 0, part);
if (this._activePartIdx >= index) {
++this._activePartIdx;
}
if (this._autoCompletePartIdx >= index) {
++this._autoCompletePartIdx;
}
2019-05-06 19:21:28 +03:00
}
_removePart(index) {
this._parts.splice(index, 1);
if (this._activePartIdx >= index) {
--this._activePartIdx;
}
if (this._autoCompletePartIdx >= index) {
--this._autoCompletePartIdx;
}
2019-05-06 19:21:28 +03:00
}
_replacePart(index, part) {
this._parts.splice(index, 1, part);
}
get parts() {
return this._parts;
}
get autoComplete() {
if (this._activePartIdx === this._autoCompletePartIdx) {
return this._autoComplete;
}
return null;
}
getPositionAtEnd() {
if (this._parts.length) {
const index = this._parts.length - 1;
const part = this._parts[index];
return new DocumentPosition(index, part.text.length);
} else {
// part index -1, as there are no parts to point at
return new DocumentPosition(-1, 0);
}
}
serializeParts() {
return this._parts.map(p => p.serialize());
}
2019-05-06 19:21:28 +03:00
_diff(newValue, inputType, caret) {
const previousValue = this.parts.reduce((text, p) => text + p.text, "");
2019-05-08 12:13:36 +03:00
// can't use caret position with drag and drop
2019-05-06 19:21:28 +03:00
if (inputType === "deleteByDrag") {
return diffDeletion(previousValue, newValue);
2019-05-06 19:21:28 +03:00
} else {
return diffAtCaret(previousValue, newValue, caret.offset);
2019-05-06 19:21:28 +03:00
}
}
reset(serializedParts, caret, inputType) {
this._parts = serializedParts.map(p => this._partCreator.deserializePart(p));
// close auto complete if open
// this would happen when clearing the composer after sending
// a message with the autocomplete still open
if (this._autoComplete) {
this._autoComplete = null;
this._autoCompletePartIdx = null;
}
this._updateCallback(caret, inputType);
}
insertPartsAt(parts, caret) {
const position = this.positionForOffset(caret.offset, caret.atNodeEnd);
const insertIndex = this._splitAt(position);
let newTextLength = 0;
for (let i = 0; i < parts.length; ++i) {
const part = parts[i];
newTextLength += part.text.length;
this._insertPart(insertIndex + i, part);
}
// put caret after new part
const lastPartIndex = insertIndex + parts.length - 1;
const newPosition = new DocumentPosition(lastPartIndex, newTextLength);
this._updateCallback(newPosition);
}
2019-05-06 19:21:28 +03:00
update(newValue, inputType, caret) {
this._updateInProgress = true;
2019-05-06 19:21:28 +03:00
const diff = this._diff(newValue, inputType, caret);
const position = this.positionForOffset(diff.at, caret.atNodeEnd);
let removedOffsetDecrease = 0;
2019-05-06 19:21:28 +03:00
if (diff.removed) {
removedOffsetDecrease = this.removeText(position, diff.removed.length);
2019-05-06 19:21:28 +03:00
}
const canOpenAutoComplete = inputType !== "insertFromPaste" && inputType !== "insertFromDrop";
let addedLen = 0;
2019-05-06 19:21:28 +03:00
if (diff.added) {
// these shouldn't trigger auto-complete, you just want to append a piece of text
addedLen = this._addText(position, diff.added, {validate: canOpenAutoComplete});
2019-05-06 19:21:28 +03:00
}
this._mergeAdjacentParts();
const caretOffset = diff.at - removedOffsetDecrease + addedLen;
let newPosition = this.positionForOffset(caretOffset, true);
this._setActivePart(newPosition, canOpenAutoComplete);
if (this._transformCallback) {
const transformAddedLen = this._transform(newPosition, inputType, diff);
newPosition = this.positionForOffset(caretOffset + transformAddedLen, true);
}
this._updateInProgress = false;
2019-08-01 12:26:20 +03:00
this._updateCallback(newPosition, inputType, diff);
}
_transform(newPosition, inputType, diff) {
const result = this._transformCallback(newPosition, inputType, diff);
return Number.isFinite(result) ? result : 0;
}
_setActivePart(pos, canOpenAutoComplete) {
const {index} = pos;
const part = this._parts[index];
if (part) {
if (index !== this._activePartIdx) {
this._activePartIdx = index;
if (canOpenAutoComplete && this._activePartIdx !== this._autoCompletePartIdx) {
// else try to create one
const ac = part.createAutoComplete(this._onAutoComplete);
if (ac) {
// make sure that react picks up the difference between both acs
this._autoComplete = ac;
this._autoCompletePartIdx = index;
}
}
}
// not _autoComplete, only there if active part is autocomplete part
if (this.autoComplete) {
this.autoComplete.onPartUpdate(part, pos.offset);
}
} else {
this._activePartIdx = null;
this._autoComplete = null;
this._autoCompletePartIdx = null;
}
}
_onAutoComplete = ({replacePart, caretOffset, close}) => {
let pos;
2019-05-09 17:07:00 +03:00
if (replacePart) {
this._replacePart(this._autoCompletePartIdx, replacePart);
const index = this._autoCompletePartIdx;
if (caretOffset === undefined) {
caretOffset = replacePart.text.length;
}
pos = new DocumentPosition(index, caretOffset);
2019-05-09 17:07:00 +03:00
}
if (close) {
this._autoComplete = null;
this._autoCompletePartIdx = null;
}
// rerender even if editor contents didn't change
// to make sure the MessageEditor checks
// model.autoComplete being empty and closes it
this._updateCallback(pos);
}
_mergeAdjacentParts() {
let prevPart;
for (let i = 0; i < this._parts.length; ++i) {
2019-05-06 19:21:28 +03:00
let part = this._parts[i];
const isEmpty = !part.text.length;
const isMerged = !isEmpty && prevPart && prevPart.merge(part);
2019-05-06 19:21:28 +03:00
if (isEmpty || isMerged) {
// remove empty or merged part
part = prevPart;
this._removePart(i);
//repeat this index, as it's removed now
--i;
}
prevPart = part;
}
}
/**
* removes `len` amount of characters at `pos`.
2019-05-14 17:49:53 +03:00
* @param {Object} pos
* @param {Number} len
* @return {Number} how many characters before pos were also removed,
* usually because of non-editable parts that can only be removed in their entirety.
*/
removeText(pos, len) {
2019-05-06 19:21:28 +03:00
let {index, offset} = pos;
let removedOffsetDecrease = 0;
while (len > 0) {
2019-05-06 19:21:28 +03:00
// part might be undefined here
let part = this._parts[index];
const amount = Math.min(len, part.text.length - offset);
// don't allow 0 amount deletions
if (amount) {
if (part.canEdit) {
const replaceWith = part.remove(offset, amount);
if (typeof replaceWith === "string") {
this._replacePart(index, this._partCreator.createDefaultPart(replaceWith));
}
part = this._parts[index];
// remove empty part
if (!part.text.length) {
this._removePart(index);
} else {
index += 1;
}
} else {
removedOffsetDecrease += offset;
this._removePart(index);
}
2019-05-06 19:21:28 +03:00
} else {
index += 1;
2019-05-06 19:21:28 +03:00
}
len -= amount;
offset = 0;
2019-05-06 19:21:28 +03:00
}
return removedOffsetDecrease;
2019-05-06 19:21:28 +03:00
}
// return part index where insertion will insert between at offset
_splitAt(pos) {
if (pos.index === -1) {
return 0;
}
if (pos.offset === 0) {
return pos.index;
}
const part = this._parts[pos.index];
if (pos.offset >= part.text.length) {
return pos.index + 1;
}
const secondPart = part.split(pos.offset);
this._insertPart(pos.index + 1, secondPart);
return pos.index + 1;
}
2019-05-06 19:21:28 +03:00
/**
* inserts `str` into the model at `pos`.
2019-05-14 17:49:53 +03:00
* @param {Object} pos
* @param {string} str
2019-07-12 11:06:38 +03:00
* @param {Object} options
* @param {bool} options.validate Whether characters will be validated by the part.
* Validating allows the inserted text to be parsed according to the part rules.
* @return {Number} how far from position (in characters) the insertion ended.
* This can be more than the length of `str` when crossing non-editable parts, which are skipped.
*/
_addText(pos, str, {validate=true}) {
let {index} = pos;
const {offset} = pos;
let addLen = str.length;
2019-05-06 19:21:28 +03:00
const part = this._parts[index];
if (part) {
if (part.canEdit) {
if (validate && part.validateAndInsert(offset, str)) {
str = null;
} else if (!validate && part.insert(offset, str)) {
str = null;
} else {
const splitPart = part.split(offset);
index += 1;
this._insertPart(index, splitPart);
}
} else if (offset !== 0) {
// not-editable part, caret is not at start,
// so insert str after this part
addLen += part.text.length - offset;
2019-05-06 19:21:28 +03:00
index += 1;
}
} else if (index < 0) {
// if position was not found (index: -1, as happens for empty editor)
// reset it to insert as first part
index = 0;
2019-05-06 19:21:28 +03:00
}
while (str) {
2019-08-21 16:27:50 +03:00
const newPart = this._partCreator.createPartForInput(str, index);
if (validate) {
str = newPart.appendUntilRejected(str);
} else {
newPart.insert(0, str);
str = null;
}
2019-05-06 19:21:28 +03:00
this._insertPart(index, newPart);
index += 1;
}
return addLen;
2019-05-06 19:21:28 +03:00
}
positionForOffset(totalOffset, atPartEnd) {
2019-05-06 19:21:28 +03:00
let currentOffset = 0;
const index = this._parts.findIndex(part => {
const partLen = part.text.length;
if (
(atPartEnd && (currentOffset + partLen) >= totalOffset) ||
(!atPartEnd && (currentOffset + partLen) > totalOffset)
) {
return true;
}
currentOffset += partLen;
return false;
});
return new DocumentPosition(index, totalOffset - currentOffset);
}
2019-08-26 17:10:02 +03:00
/**
* Starts a range, which can span across multiple parts, to find and replace text.
* @param {DocumentPosition} position where to start the range
* @return {Range}
*/
startRange(position) {
return new Range(this, position);
2019-05-06 19:21:28 +03:00
}
// called from Range.replace
replaceRange(startPosition, endPosition, parts) {
const newStartPartIndex = this._splitAt(startPosition);
const idxDiff = newStartPartIndex - startPosition.index;
// if both position are in the same part, and we split it at start position,
// the offset of the end position needs to be decreased by the offset of the start position
const removedOffset = startPosition.index === endPosition.index ? startPosition.offset : 0;
const adjustedEndPosition = new DocumentPosition(
endPosition.index + idxDiff,
endPosition.offset - removedOffset,
);
const newEndPartIndex = this._splitAt(adjustedEndPosition);
for (let i = newEndPartIndex - 1; i >= newStartPartIndex; --i) {
this._removePart(i);
}
let insertIdx = newStartPartIndex;
for (const part of parts) {
this._insertPart(insertIdx, part);
insertIdx += 1;
}
this._mergeAdjacentParts();
if (!this._updateInProgress) {
this._updateCallback();
}
2019-05-06 19:21:28 +03:00
}
}