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.
|
|
|
|
*/
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
import {diffAtCaret, diffDeletion, IDiff} from "./diff";
|
|
|
|
import DocumentPosition, {IPosition} from "./position";
|
2019-08-23 19:37:58 +03:00
|
|
|
import Range from "./range";
|
2020-07-15 11:45:45 +03:00
|
|
|
import {BasePart, ISerializedPart, PartCreator} from "./parts";
|
|
|
|
import AutocompleteWrapperModel, {ICallback} from "./autocomplete";
|
|
|
|
import DocumentOffset from "./offset";
|
2020-07-15 11:49:02 +03:00
|
|
|
import {Caret} from "./caret";
|
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
|
|
|
|
*/
|
|
|
|
|
2019-08-26 17:09:46 +03:00
|
|
|
/**
|
|
|
|
* @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.
|
2019-08-27 10:54:13 +03:00
|
|
|
* This is used to adjust the caret position.
|
2019-08-26 17:09:46 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-27 17:15:10 +03:00
|
|
|
/**
|
|
|
|
* @callback ManualTransformCallback
|
|
|
|
* @return the caret position
|
|
|
|
*/
|
|
|
|
|
2020-07-15 11:49:02 +03:00
|
|
|
type TransformCallback = (caretPosition: DocumentPosition, inputType: string, diff: IDiff) => number | void;
|
|
|
|
type UpdateCallback = (caret: Caret, inputType?: string, diff?: IDiff) => void;
|
|
|
|
type ManualTransformCallback = () => Caret;
|
2020-07-15 11:45:45 +03:00
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
export default class EditorModel {
|
2020-07-15 11:45:45 +03:00
|
|
|
private _parts: BasePart[];
|
|
|
|
private readonly _partCreator: PartCreator;
|
|
|
|
private activePartIdx: number = null;
|
|
|
|
private _autoComplete: AutocompleteWrapperModel = null;
|
|
|
|
private autoCompletePartIdx: number = null;
|
|
|
|
private autoCompletePartCount = 0;
|
|
|
|
private transformCallback: TransformCallback = null;
|
|
|
|
|
|
|
|
constructor(parts: BasePart[], partCreator: PartCreator, private updateCallback: UpdateCallback = null) {
|
2019-05-06 19:21:28 +03:00
|
|
|
this._parts = parts;
|
2019-05-09 15:59:52 +03:00
|
|
|
this._partCreator = partCreator;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.transformCallback = null;
|
2019-08-26 17:09:46 +03:00
|
|
|
}
|
|
|
|
|
2019-08-27 10:54:13 +03:00
|
|
|
/**
|
|
|
|
* Set a callback for the transformation step.
|
2019-08-26 17:09:46 +03:00
|
|
|
* 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
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
setTransformCallback(transformCallback: TransformCallback) {
|
|
|
|
this.transformCallback = transformCallback;
|
2019-08-05 16:27:40 +03:00
|
|
|
}
|
|
|
|
|
2019-08-27 10:54:13 +03:00
|
|
|
/**
|
|
|
|
* Set a callback for rerendering the model after it has been updated.
|
2019-08-26 17:10:02 +03:00
|
|
|
* @param {ModelCallback} updateCallback
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
setUpdateCallback(updateCallback: UpdateCallback) {
|
|
|
|
this.updateCallback = updateCallback;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 16:27:40 +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;
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:02:20 +03:00
|
|
|
clone() {
|
2020-07-15 11:45:45 +03:00
|
|
|
return new EditorModel(this._parts, this._partCreator, this.updateCallback);
|
2019-06-14 12:02:20 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private insertPart(index: number, part: BasePart) {
|
2019-05-06 19:21:28 +03:00
|
|
|
this._parts.splice(index, 0, part);
|
2020-07-15 11:45:45 +03:00
|
|
|
if (this.activePartIdx >= index) {
|
|
|
|
++this.activePartIdx;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
if (this.autoCompletePartIdx >= index) {
|
|
|
|
++this.autoCompletePartIdx;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private removePart(index: number) {
|
2019-05-06 19:21:28 +03:00
|
|
|
this._parts.splice(index, 1);
|
2020-07-15 11:45:45 +03:00
|
|
|
if (index === this.activePartIdx) {
|
|
|
|
this.activePartIdx = null;
|
|
|
|
} else if (this.activePartIdx > index) {
|
|
|
|
--this.activePartIdx;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
if (index === this.autoCompletePartIdx) {
|
|
|
|
this.autoCompletePartIdx = null;
|
|
|
|
} else if (this.autoCompletePartIdx > index) {
|
|
|
|
--this.autoCompletePartIdx;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private replacePart(index: number, part: BasePart) {
|
2019-05-06 19:21:28 +03:00
|
|
|
this._parts.splice(index, 1, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
get parts() {
|
|
|
|
return this._parts;
|
|
|
|
}
|
|
|
|
|
2019-05-09 15:59:52 +03:00
|
|
|
get autoComplete() {
|
2020-07-15 11:45:45 +03:00
|
|
|
if (this.activePartIdx === this.autoCompletePartIdx) {
|
2019-05-09 15:59:52 +03:00
|
|
|
return this._autoComplete;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-16 20:39:20 +03: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 17:32:01 +03:00
|
|
|
// part index -1, as there are no parts to point at
|
|
|
|
return new DocumentPosition(-1, 0);
|
2019-05-16 20:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:27:09 +03:00
|
|
|
serializeParts() {
|
2019-06-12 19:32:32 +03:00
|
|
|
return this._parts.map(p => p.serialize());
|
2019-05-07 17:27:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private diff(newValue: string, inputType: string, caret: DocumentOffset) {
|
2019-05-09 16:42:10 +03:00
|
|
|
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") {
|
2019-05-09 16:42:10 +03:00
|
|
|
return diffDeletion(previousValue, newValue);
|
2019-05-06 19:21:28 +03:00
|
|
|
} else {
|
2019-05-09 16:42:10 +03:00
|
|
|
return diffAtCaret(previousValue, newValue, caret.offset);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:49:02 +03:00
|
|
|
reset(serializedParts: ISerializedPart[], caret: Caret, inputType: string) {
|
2019-08-01 12:27:09 +03:00
|
|
|
this._parts = serializedParts.map(p => this._partCreator.deserializePart(p));
|
2019-09-05 18:54:08 +03:00
|
|
|
if (!caret) {
|
|
|
|
caret = this.getPositionAtEnd();
|
|
|
|
}
|
2019-08-21 17:22:04 +03:00
|
|
|
// 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;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.autoCompletePartIdx = null;
|
2019-08-21 17:22:04 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
this.updateCallback(caret, inputType);
|
2019-08-01 12:27:09 +03:00
|
|
|
}
|
|
|
|
|
2019-08-27 17:37:04 +03:00
|
|
|
/**
|
|
|
|
* Inserts the given parts at the given position.
|
|
|
|
* Should be run inside a `model.transform()` callback.
|
|
|
|
* @param {Part[]} parts the parts to replace the range with
|
|
|
|
* @param {DocumentPosition} position the position to start inserting at
|
|
|
|
* @return {Number} the amount of characters added
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
insert(parts: BasePart[], position: IPosition) {
|
|
|
|
const insertIndex = this.splitAt(position);
|
2019-08-20 13:36:05 +03:00
|
|
|
let newTextLength = 0;
|
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
|
|
|
const part = parts[i];
|
|
|
|
newTextLength += part.text.length;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.insertPart(insertIndex + i, part);
|
2019-08-20 13:36:05 +03:00
|
|
|
}
|
2019-08-27 17:37:04 +03:00
|
|
|
return newTextLength;
|
2019-08-07 18:44:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
update(newValue: string, inputType: string, caret: DocumentOffset) {
|
|
|
|
const diff = this.diff(newValue, inputType, caret);
|
2019-06-12 19:52:34 +03:00
|
|
|
const position = this.positionForOffset(diff.at, caret.atNodeEnd);
|
2019-05-09 20:55:24 +03:00
|
|
|
let removedOffsetDecrease = 0;
|
2019-05-06 19:21:28 +03:00
|
|
|
if (diff.removed) {
|
2019-06-14 12:02:20 +03:00
|
|
|
removedOffsetDecrease = this.removeText(position, diff.removed.length);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-09 20:55:24 +03:00
|
|
|
let addedLen = 0;
|
2019-05-06 19:21:28 +03:00
|
|
|
if (diff.added) {
|
2020-07-15 11:45:45 +03:00
|
|
|
addedLen = this.addText(position, diff.added, inputType);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
this.mergeAdjacentParts();
|
2019-05-09 20:55:24 +03:00
|
|
|
const caretOffset = diff.at - removedOffsetDecrease + addedLen;
|
2019-08-26 17:09:46 +03:00
|
|
|
let newPosition = this.positionForOffset(caretOffset, true);
|
2019-08-29 13:51:33 +03:00
|
|
|
const canOpenAutoComplete = inputType !== "insertFromPaste" && inputType !== "insertFromDrop";
|
2020-07-15 11:45:45 +03:00
|
|
|
const acPromise = this.setActivePart(newPosition, canOpenAutoComplete);
|
|
|
|
if (this.transformCallback) {
|
|
|
|
const transformAddedLen = this.getTransformAddedLen(newPosition, inputType, diff);
|
2019-08-27 10:49:22 +03:00
|
|
|
newPosition = this.positionForOffset(caretOffset + transformAddedLen, true);
|
2019-08-26 17:09:46 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
this.updateCallback(newPosition, inputType, diff);
|
2019-08-27 17:12:44 +03:00
|
|
|
return acPromise;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:49:02 +03:00
|
|
|
private getTransformAddedLen(newPosition: DocumentPosition, inputType: string, diff: IDiff): number {
|
2020-07-15 11:45:45 +03:00
|
|
|
const result = this.transformCallback(newPosition, inputType, diff);
|
|
|
|
return Number.isFinite(result) ? result as number : 0;
|
2019-08-26 17:09:46 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private setActivePart(pos: DocumentPosition, canOpenAutoComplete: boolean) {
|
2019-05-09 15:59:52 +03:00
|
|
|
const {index} = pos;
|
|
|
|
const part = this._parts[index];
|
2019-05-09 17:54:58 +03:00
|
|
|
if (part) {
|
2020-07-15 11:45:45 +03:00
|
|
|
if (index !== this.activePartIdx) {
|
|
|
|
this.activePartIdx = index;
|
|
|
|
if (canOpenAutoComplete && this.activePartIdx !== this.autoCompletePartIdx) {
|
2019-05-09 17:54:58 +03:00
|
|
|
// else try to create one
|
2020-07-15 11:45:45 +03:00
|
|
|
const ac = part.createAutoComplete(this.onAutoComplete);
|
2019-05-09 17:54:58 +03:00
|
|
|
if (ac) {
|
|
|
|
// make sure that react picks up the difference between both acs
|
|
|
|
this._autoComplete = ac;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.autoCompletePartIdx = index;
|
|
|
|
this.autoCompletePartCount = 1;
|
2019-05-09 17:54:58 +03:00
|
|
|
}
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 17:54:58 +03:00
|
|
|
// not _autoComplete, only there if active part is autocomplete part
|
|
|
|
if (this.autoComplete) {
|
2019-08-29 18:47:14 +03:00
|
|
|
return this.autoComplete.onPartUpdate(part, pos);
|
2019-05-09 17:54:58 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-15 11:45:45 +03:00
|
|
|
this.activePartIdx = null;
|
2019-05-09 17:54:58 +03:00
|
|
|
this._autoComplete = null;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.autoCompletePartIdx = null;
|
|
|
|
this.autoCompletePartCount = 0;
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
2019-08-27 17:12:44 +03:00
|
|
|
return Promise.resolve();
|
2019-05-09 15:59:52 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private onAutoComplete = ({replaceParts, close}: ICallback) => {
|
2019-05-10 15:24:50 +03:00
|
|
|
let pos;
|
2019-08-29 18:43:18 +03:00
|
|
|
if (replaceParts) {
|
2020-07-15 11:45:45 +03:00
|
|
|
this._parts.splice(this.autoCompletePartIdx, this.autoCompletePartCount, ...replaceParts);
|
|
|
|
this.autoCompletePartCount = replaceParts.length;
|
2019-08-29 18:43:18 +03:00
|
|
|
const lastPart = replaceParts[replaceParts.length - 1];
|
2020-07-15 11:45:45 +03:00
|
|
|
const lastPartIndex = this.autoCompletePartIdx + replaceParts.length - 1;
|
2019-08-29 18:43:18 +03:00
|
|
|
pos = new DocumentPosition(lastPartIndex, lastPart.text.length);
|
2019-05-09 17:07:00 +03:00
|
|
|
}
|
2019-05-09 16:43:10 +03:00
|
|
|
if (close) {
|
|
|
|
this._autoComplete = null;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.autoCompletePartIdx = null;
|
|
|
|
this.autoCompletePartCount = 0;
|
2019-05-09 16:43:10 +03:00
|
|
|
}
|
2019-05-10 15:24:50 +03:00
|
|
|
// rerender even if editor contents didn't change
|
|
|
|
// to make sure the MessageEditor checks
|
|
|
|
// model.autoComplete being empty and closes it
|
2020-07-15 11:45:45 +03:00
|
|
|
this.updateCallback(pos);
|
|
|
|
};
|
2019-05-09 16:43:10 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
private mergeAdjacentParts() {
|
2019-06-19 11:57:29 +03:00
|
|
|
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;
|
2019-06-19 11:57:29 +03:00
|
|
|
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;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.removePart(i);
|
2019-05-06 19:21:28 +03:00
|
|
|
//repeat this index, as it's removed now
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
prevPart = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 20:55:24 +03:00
|
|
|
/**
|
|
|
|
* removes `len` amount of characters at `pos`.
|
2019-05-14 17:49:53 +03:00
|
|
|
* @param {Object} pos
|
|
|
|
* @param {Number} len
|
2019-05-09 20:55:24 +03: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.
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
removeText(pos: IPosition, len: number) {
|
2019-05-06 19:21:28 +03:00
|
|
|
let {index, offset} = pos;
|
2019-05-09 20:55:24 +03:00
|
|
|
let removedOffsetDecrease = 0;
|
2019-05-09 16:42:10 +03:00
|
|
|
while (len > 0) {
|
2019-05-06 19:21:28 +03:00
|
|
|
// part might be undefined here
|
|
|
|
let part = this._parts[index];
|
2019-05-09 20:55:24 +03:00
|
|
|
const amount = Math.min(len, part.text.length - offset);
|
2019-05-16 21:14:24 +03:00
|
|
|
// don't allow 0 amount deletions
|
|
|
|
if (amount) {
|
|
|
|
if (part.canEdit) {
|
|
|
|
const replaceWith = part.remove(offset, amount);
|
|
|
|
if (typeof replaceWith === "string") {
|
2020-07-15 11:45:45 +03:00
|
|
|
this.replacePart(index, this._partCreator.createDefaultPart(replaceWith));
|
2019-05-16 21:14:24 +03:00
|
|
|
}
|
|
|
|
part = this._parts[index];
|
|
|
|
// remove empty part
|
|
|
|
if (!part.text.length) {
|
2020-07-15 11:45:45 +03:00
|
|
|
this.removePart(index);
|
2019-05-16 21:14:24 +03:00
|
|
|
} else {
|
|
|
|
index += 1;
|
|
|
|
}
|
2019-05-09 16:42:10 +03:00
|
|
|
} else {
|
2019-05-16 21:14:24 +03:00
|
|
|
removedOffsetDecrease += offset;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.removePart(index);
|
2019-05-09 16:42:10 +03:00
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
} else {
|
2019-05-16 21:14:24 +03:00
|
|
|
index += 1;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-09 20:55:24 +03:00
|
|
|
len -= amount;
|
|
|
|
offset = 0;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-09 20:55:24 +03:00
|
|
|
return removedOffsetDecrease;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
|
2019-08-07 18:44:49 +03:00
|
|
|
// return part index where insertion will insert between at offset
|
2020-07-15 11:45:45 +03:00
|
|
|
private splitAt(pos: IPosition) {
|
2019-08-07 18:44:49 +03:00
|
|
|
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);
|
2020-07-15 11:45:45 +03:00
|
|
|
this.insertPart(pos.index + 1, secondPart);
|
2019-08-07 18:44:49 +03:00
|
|
|
return pos.index + 1;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
|
2019-05-09 20:55:24 +03:00
|
|
|
/**
|
|
|
|
* inserts `str` into the model at `pos`.
|
2019-05-14 17:49:53 +03:00
|
|
|
* @param {Object} pos
|
|
|
|
* @param {string} str
|
2019-08-29 13:51:33 +03:00
|
|
|
* @param {string} inputType the source of the input, see html InputEvent.inputType
|
2019-07-12 11:06:38 +03:00
|
|
|
* @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 20:55:24 +03: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.
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
private addText(pos: IPosition, str: string, inputType: string) {
|
2019-05-09 20:55:24 +03:00
|
|
|
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) {
|
2019-05-09 17:54:58 +03:00
|
|
|
if (part.canEdit) {
|
2019-08-29 13:51:33 +03:00
|
|
|
if (part.validateAndInsert(offset, str, inputType)) {
|
2019-05-09 17:54:58 +03:00
|
|
|
str = null;
|
|
|
|
} else {
|
|
|
|
const splitPart = part.split(offset);
|
|
|
|
index += 1;
|
2020-07-15 11:45:45 +03:00
|
|
|
this.insertPart(index, splitPart);
|
2019-05-09 17:54:58 +03:00
|
|
|
}
|
2019-05-23 11:11:46 +03:00
|
|
|
} else if (offset !== 0) {
|
|
|
|
// not-editable part, caret is not at start,
|
|
|
|
// so insert str after this part
|
2019-05-09 20:55:24 +03:00
|
|
|
addLen += part.text.length - offset;
|
2019-05-06 19:21:28 +03:00
|
|
|
index += 1;
|
|
|
|
}
|
2019-07-11 19:33:22 +03: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 19:21:28 +03:00
|
|
|
}
|
|
|
|
while (str) {
|
2019-08-29 13:51:33 +03:00
|
|
|
const newPart = this._partCreator.createPartForInput(str, index, inputType);
|
|
|
|
str = newPart.appendUntilRejected(str, inputType);
|
2020-07-15 11:45:45 +03:00
|
|
|
this.insertPart(index, newPart);
|
2019-05-06 19:21:28 +03:00
|
|
|
index += 1;
|
|
|
|
}
|
2019-05-09 20:55:24 +03:00
|
|
|
return addLen;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
positionForOffset(totalOffset: number, atPartEnd: boolean) {
|
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;
|
|
|
|
});
|
2019-09-04 17:02:07 +03:00
|
|
|
if (index === -1) {
|
|
|
|
return this.getPositionAtEnd();
|
|
|
|
} else {
|
|
|
|
return new DocumentPosition(index, totalOffset - currentOffset);
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 17:10:02 +03:00
|
|
|
/**
|
|
|
|
* Starts a range, which can span across multiple parts, to find and replace text.
|
2019-09-03 17:00:50 +03:00
|
|
|
* @param {DocumentPosition} positionA a boundary of the range
|
|
|
|
* @param {DocumentPosition?} positionB the other boundary of the range, optional
|
2019-08-26 17:10:02 +03:00
|
|
|
* @return {Range}
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
startRange(positionA: DocumentPosition, positionB = positionA) {
|
2019-09-03 17:00:50 +03:00
|
|
|
return new Range(this, positionA, positionB);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
replaceRange(startPosition: DocumentPosition, endPosition: DocumentPosition, parts: BasePart[]) {
|
2019-08-28 16:52:39 +03:00
|
|
|
// convert end position to offset, so it is independent of how the document is split into parts
|
|
|
|
// which we'll change when splitting up at the start position
|
|
|
|
const endOffset = endPosition.asOffset(this);
|
2020-07-15 11:45:45 +03:00
|
|
|
const newStartPartIndex = this.splitAt(startPosition);
|
2019-08-28 16:52:39 +03:00
|
|
|
// convert it back to position once split at start
|
|
|
|
endPosition = endOffset.asPosition(this);
|
2020-07-15 11:45:45 +03:00
|
|
|
const newEndPartIndex = this.splitAt(endPosition);
|
2019-08-23 19:37:58 +03:00
|
|
|
for (let i = newEndPartIndex - 1; i >= newStartPartIndex; --i) {
|
2020-07-15 11:45:45 +03:00
|
|
|
this.removePart(i);
|
2019-08-23 19:37:58 +03:00
|
|
|
}
|
|
|
|
let insertIdx = newStartPartIndex;
|
|
|
|
for (const part of parts) {
|
2020-07-15 11:45:45 +03:00
|
|
|
this.insertPart(insertIdx, part);
|
2019-08-23 19:37:58 +03:00
|
|
|
insertIdx += 1;
|
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
this.mergeAdjacentParts();
|
2019-08-27 17:15:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a transformation not part of an update cycle.
|
|
|
|
* Modifying the model should only happen inside a transform call if not part of an update call.
|
|
|
|
* @param {ManualTransformCallback} callback to run the transformations in
|
|
|
|
* @return {Promise} a promise when auto-complete (if applicable) is done updating
|
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
transform(callback: ManualTransformCallback) {
|
2019-08-27 17:15:10 +03:00
|
|
|
const pos = callback();
|
2019-09-04 17:04:06 +03:00
|
|
|
let acPromise = null;
|
|
|
|
if (!(pos instanceof Range)) {
|
2020-07-15 11:45:45 +03:00
|
|
|
acPromise = this.setActivePart(pos, true);
|
2019-09-04 17:04:06 +03:00
|
|
|
} else {
|
|
|
|
acPromise = Promise.resolve();
|
|
|
|
}
|
2020-07-15 11:45:45 +03:00
|
|
|
this.updateCallback(pos);
|
2019-08-27 17:15:10 +03:00
|
|
|
return acPromise;
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
}
|