2019-05-06 16:21:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 14:16:32 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-06 16:21:28 +00: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";
|
|
|
|
|
|
|
|
export default class EditorModel {
|
2019-05-09 13:58:32 +00:00
|
|
|
constructor(parts, partCreator, updateCallback) {
|
2019-05-06 16:21:28 +00:00
|
|
|
this._parts = parts;
|
2019-05-09 12:59:52 +00:00
|
|
|
this._partCreator = partCreator;
|
|
|
|
this._activePartIdx = null;
|
|
|
|
this._autoComplete = null;
|
|
|
|
this._autoCompletePartIdx = null;
|
2019-08-05 13:27:40 +00:00
|
|
|
this.setUpdateCallback(updateCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdateCallback(updateCallback) {
|
2019-05-09 13:58:32 +00:00
|
|
|
this._updateCallback = updateCallback;
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 13:27:40 +00:00
|
|
|
get partCreator() {
|
|
|
|
return this._partCreator;
|
|
|
|
}
|
|
|
|
|
2019-06-14 09:02:20 +00:00
|
|
|
clone() {
|
|
|
|
return new EditorModel(this._parts, this._partCreator, this._updateCallback);
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:21:28 +00:00
|
|
|
_insertPart(index, part) {
|
|
|
|
this._parts.splice(index, 0, part);
|
2019-05-09 12:59:52 +00:00
|
|
|
if (this._activePartIdx >= index) {
|
|
|
|
++this._activePartIdx;
|
|
|
|
}
|
|
|
|
if (this._autoCompletePartIdx >= index) {
|
|
|
|
++this._autoCompletePartIdx;
|
|
|
|
}
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_removePart(index) {
|
|
|
|
this._parts.splice(index, 1);
|
2019-05-09 12:59:52 +00:00
|
|
|
if (this._activePartIdx >= index) {
|
|
|
|
--this._activePartIdx;
|
|
|
|
}
|
|
|
|
if (this._autoCompletePartIdx >= index) {
|
|
|
|
--this._autoCompletePartIdx;
|
|
|
|
}
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_replacePart(index, part) {
|
|
|
|
this._parts.splice(index, 1, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
get parts() {
|
|
|
|
return this._parts;
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:59:52 +00:00
|
|
|
get autoComplete() {
|
|
|
|
if (this._activePartIdx === this._autoCompletePartIdx) {
|
|
|
|
return this._autoComplete;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-16 17:39:20 +00:00
|
|
|
getPositionAtEnd() {
|
|
|
|
if (this._parts.length) {
|
|
|
|
const index = this._parts.length - 1;
|
|
|
|
const part = this._parts[index];
|
|
|
|
return new DocumentPosition(index, part.text.length);
|
|
|
|
} else {
|
2019-08-06 14:32:01 +00:00
|
|
|
// part index -1, as there are no parts to point at
|
|
|
|
return new DocumentPosition(-1, 0);
|
2019-05-16 17:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:27:09 +00:00
|
|
|
serializeParts() {
|
2019-06-12 16:32:32 +00:00
|
|
|
return this._parts.map(p => p.serialize());
|
2019-05-07 14:27:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 16:21:28 +00:00
|
|
|
_diff(newValue, inputType, caret) {
|
2019-05-09 13:42:10 +00:00
|
|
|
const previousValue = this.parts.reduce((text, p) => text + p.text, "");
|
2019-05-08 09:13:36 +00:00
|
|
|
// can't use caret position with drag and drop
|
2019-05-06 16:21:28 +00:00
|
|
|
if (inputType === "deleteByDrag") {
|
2019-05-09 13:42:10 +00:00
|
|
|
return diffDeletion(previousValue, newValue);
|
2019-05-06 16:21:28 +00:00
|
|
|
} else {
|
2019-05-09 13:42:10 +00:00
|
|
|
return diffAtCaret(previousValue, newValue, caret.offset);
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:27:09 +00:00
|
|
|
reset(serializedParts, caret, inputType) {
|
|
|
|
this._parts = serializedParts.map(p => this._partCreator.deserializePart(p));
|
|
|
|
this._updateCallback(caret, inputType);
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:21:28 +00:00
|
|
|
update(newValue, inputType, caret) {
|
|
|
|
const diff = this._diff(newValue, inputType, caret);
|
2019-06-12 16:52:34 +00:00
|
|
|
const position = this.positionForOffset(diff.at, caret.atNodeEnd);
|
2019-05-09 17:55:24 +00:00
|
|
|
let removedOffsetDecrease = 0;
|
2019-05-06 16:21:28 +00:00
|
|
|
if (diff.removed) {
|
2019-06-14 09:02:20 +00:00
|
|
|
removedOffsetDecrease = this.removeText(position, diff.removed.length);
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
2019-07-11 16:32:58 +00:00
|
|
|
const canOpenAutoComplete = inputType !== "insertFromPaste" && inputType !== "insertFromDrop";
|
2019-05-09 17:55:24 +00:00
|
|
|
let addedLen = 0;
|
2019-05-06 16:21:28 +00:00
|
|
|
if (diff.added) {
|
2019-07-11 16:32:58 +00:00
|
|
|
// 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 16:21:28 +00:00
|
|
|
}
|
|
|
|
this._mergeAdjacentParts();
|
2019-05-09 17:55:24 +00:00
|
|
|
const caretOffset = diff.at - removedOffsetDecrease + addedLen;
|
2019-06-19 15:37:17 +00:00
|
|
|
const newPosition = this.positionForOffset(caretOffset, true);
|
2019-07-11 16:32:58 +00:00
|
|
|
this._setActivePart(newPosition, canOpenAutoComplete);
|
2019-08-01 09:26:20 +00:00
|
|
|
this._updateCallback(newPosition, inputType, diff);
|
2019-05-09 12:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:32:58 +00:00
|
|
|
_setActivePart(pos, canOpenAutoComplete) {
|
2019-05-09 12:59:52 +00:00
|
|
|
const {index} = pos;
|
|
|
|
const part = this._parts[index];
|
2019-05-09 14:54:58 +00:00
|
|
|
if (part) {
|
|
|
|
if (index !== this._activePartIdx) {
|
|
|
|
this._activePartIdx = index;
|
2019-07-11 16:32:58 +00:00
|
|
|
if (canOpenAutoComplete && this._activePartIdx !== this._autoCompletePartIdx) {
|
2019-05-09 14:54:58 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2019-05-09 12:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:54:58 +00:00
|
|
|
// 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;
|
2019-05-09 12:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:37:45 +00:00
|
|
|
_onAutoComplete = ({replacePart, caretOffset, close}) => {
|
2019-05-10 12:24:50 +00:00
|
|
|
let pos;
|
2019-05-09 14:07:00 +00:00
|
|
|
if (replacePart) {
|
|
|
|
this._replacePart(this._autoCompletePartIdx, replacePart);
|
2019-06-19 15:37:17 +00:00
|
|
|
const index = this._autoCompletePartIdx;
|
2019-05-10 12:24:50 +00:00
|
|
|
if (caretOffset === undefined) {
|
2019-06-19 15:37:17 +00:00
|
|
|
caretOffset = replacePart.text.length;
|
2019-05-10 12:24:50 +00:00
|
|
|
}
|
|
|
|
pos = new DocumentPosition(index, caretOffset);
|
2019-05-09 14:07:00 +00:00
|
|
|
}
|
2019-05-09 13:43:10 +00:00
|
|
|
if (close) {
|
|
|
|
this._autoComplete = null;
|
|
|
|
this._autoCompletePartIdx = null;
|
|
|
|
}
|
2019-05-10 12:24:50 +00:00
|
|
|
// rerender even if editor contents didn't change
|
|
|
|
// to make sure the MessageEditor checks
|
|
|
|
// model.autoComplete being empty and closes it
|
|
|
|
this._updateCallback(pos);
|
2019-05-09 13:43:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 16:21:28 +00:00
|
|
|
_mergeAdjacentParts(docPos) {
|
2019-06-19 08:57:29 +00:00
|
|
|
let prevPart;
|
|
|
|
for (let i = 0; i < this._parts.length; ++i) {
|
2019-05-06 16:21:28 +00:00
|
|
|
let part = this._parts[i];
|
|
|
|
const isEmpty = !part.text.length;
|
2019-06-19 08:57:29 +00:00
|
|
|
const isMerged = !isEmpty && prevPart && prevPart.merge(part);
|
2019-05-06 16:21:28 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:55:24 +00:00
|
|
|
/**
|
|
|
|
* removes `len` amount of characters at `pos`.
|
2019-05-14 14:49:53 +00:00
|
|
|
* @param {Object} pos
|
|
|
|
* @param {Number} len
|
2019-05-09 17:55:24 +00:00
|
|
|
* @return {Number} how many characters before pos were also removed,
|
|
|
|
* usually because of non-editable parts that can only be removed in their entirety.
|
|
|
|
*/
|
2019-06-14 09:02:20 +00:00
|
|
|
removeText(pos, len) {
|
2019-05-06 16:21:28 +00:00
|
|
|
let {index, offset} = pos;
|
2019-05-09 17:55:24 +00:00
|
|
|
let removedOffsetDecrease = 0;
|
2019-05-09 13:42:10 +00:00
|
|
|
while (len > 0) {
|
2019-05-06 16:21:28 +00:00
|
|
|
// part might be undefined here
|
|
|
|
let part = this._parts[index];
|
2019-05-09 17:55:24 +00:00
|
|
|
const amount = Math.min(len, part.text.length - offset);
|
2019-05-16 18:14:24 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2019-05-09 13:42:10 +00:00
|
|
|
} else {
|
2019-05-16 18:14:24 +00:00
|
|
|
removedOffsetDecrease += offset;
|
|
|
|
this._removePart(index);
|
2019-05-09 13:42:10 +00:00
|
|
|
}
|
2019-05-06 16:21:28 +00:00
|
|
|
} else {
|
2019-05-16 18:14:24 +00:00
|
|
|
index += 1;
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
2019-05-09 17:55:24 +00:00
|
|
|
len -= amount;
|
|
|
|
offset = 0;
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
2019-05-09 17:55:24 +00:00
|
|
|
return removedOffsetDecrease;
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:55:24 +00:00
|
|
|
/**
|
|
|
|
* inserts `str` into the model at `pos`.
|
2019-05-14 14:49:53 +00:00
|
|
|
* @param {Object} pos
|
|
|
|
* @param {string} str
|
2019-07-12 08:06:38 +00: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.
|
2019-05-09 17:55:24 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2019-07-11 16:32:58 +00:00
|
|
|
_addText(pos, str, {validate=true}) {
|
2019-05-09 17:55:24 +00:00
|
|
|
let {index} = pos;
|
|
|
|
const {offset} = pos;
|
|
|
|
let addLen = str.length;
|
2019-05-06 16:21:28 +00:00
|
|
|
const part = this._parts[index];
|
|
|
|
if (part) {
|
2019-05-09 14:54:58 +00:00
|
|
|
if (part.canEdit) {
|
2019-07-11 16:32:58 +00:00
|
|
|
if (validate && part.validateAndInsert(offset, str)) {
|
|
|
|
str = null;
|
|
|
|
} else if (!validate && part.insert(offset, str)) {
|
2019-05-09 14:54:58 +00:00
|
|
|
str = null;
|
|
|
|
} else {
|
|
|
|
const splitPart = part.split(offset);
|
|
|
|
index += 1;
|
|
|
|
this._insertPart(index, splitPart);
|
|
|
|
}
|
2019-05-23 08:11:46 +00:00
|
|
|
} else if (offset !== 0) {
|
|
|
|
// not-editable part, caret is not at start,
|
|
|
|
// so insert str after this part
|
2019-05-09 17:55:24 +00:00
|
|
|
addLen += part.text.length - offset;
|
2019-05-06 16:21:28 +00:00
|
|
|
index += 1;
|
|
|
|
}
|
2019-07-11 16:33:22 +00:00
|
|
|
} 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 16:21:28 +00:00
|
|
|
}
|
|
|
|
while (str) {
|
2019-05-09 12:59:52 +00:00
|
|
|
const newPart = this._partCreator.createPartForInput(str);
|
2019-07-11 16:32:58 +00:00
|
|
|
if (validate) {
|
|
|
|
str = newPart.appendUntilRejected(str);
|
|
|
|
} else {
|
|
|
|
newPart.insert(0, str);
|
|
|
|
str = null;
|
|
|
|
}
|
2019-05-06 16:21:28 +00:00
|
|
|
this._insertPart(index, newPart);
|
|
|
|
index += 1;
|
|
|
|
}
|
2019-05-09 17:55:24 +00:00
|
|
|
return addLen;
|
2019-05-06 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 16:52:34 +00:00
|
|
|
positionForOffset(totalOffset, atPartEnd) {
|
2019-05-06 16:21:28 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DocumentPosition {
|
|
|
|
constructor(index, offset) {
|
|
|
|
this._index = index;
|
|
|
|
this._offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
get index() {
|
|
|
|
return this._index;
|
|
|
|
}
|
|
|
|
|
|
|
|
get offset() {
|
|
|
|
return this._offset;
|
|
|
|
}
|
|
|
|
}
|