2019-08-23 19:37:58 +03: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.
|
|
|
|
*/
|
|
|
|
|
2019-08-28 16:52:39 +03:00
|
|
|
import DocumentOffset from "./offset";
|
2020-07-15 11:45:45 +03:00
|
|
|
import EditorModel from "./model";
|
2020-07-20 18:33:53 +03:00
|
|
|
import {Part} from "./parts";
|
2019-08-28 16:52:39 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
export interface IPosition {
|
|
|
|
index: number;
|
|
|
|
offset: number;
|
|
|
|
}
|
2019-08-23 19:37:58 +03:00
|
|
|
|
2020-07-20 18:33:53 +03:00
|
|
|
type Callback = (part: Part, startIdx: number, endIdx: number) => void;
|
2020-07-20 19:10:08 +03:00
|
|
|
export type Predicate = (index: number, offset: number, part: Part) => boolean;
|
2019-08-23 19:37:58 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
export default class DocumentPosition implements IPosition {
|
|
|
|
constructor(public readonly index: number, public readonly offset: number) {
|
2019-08-23 19:37:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
compare(otherPos: DocumentPosition) {
|
|
|
|
if (this.index === otherPos.index) {
|
|
|
|
return this.offset - otherPos.offset;
|
2019-08-23 19:37:58 +03:00
|
|
|
} else {
|
2020-07-15 11:45:45 +03:00
|
|
|
return this.index - otherPos.index;
|
2019-08-23 19:37:58 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-26 17:00:56 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
iteratePartsBetween(other: DocumentPosition, model: EditorModel, callback: Callback) {
|
2019-08-26 17:00:56 +03:00
|
|
|
if (this.index === -1 || other.index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [startPos, endPos] = this.compare(other) < 0 ? [this, other] : [other, this];
|
|
|
|
if (startPos.index === endPos.index) {
|
|
|
|
callback(model.parts[this.index], startPos.offset, endPos.offset);
|
|
|
|
} else {
|
|
|
|
const firstPart = model.parts[startPos.index];
|
|
|
|
callback(firstPart, startPos.offset, firstPart.text.length);
|
|
|
|
for (let i = startPos.index + 1; i < endPos.index; ++i) {
|
|
|
|
const part = model.parts[i];
|
|
|
|
callback(part, 0, part.text.length);
|
|
|
|
}
|
|
|
|
const lastPart = model.parts[endPos.index];
|
|
|
|
callback(lastPart, 0, endPos.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
forwardsWhile(model: EditorModel, predicate: Predicate) {
|
2019-08-26 17:00:56 +03:00
|
|
|
if (this.index === -1) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
let {index, offset} = this;
|
|
|
|
const {parts} = model;
|
|
|
|
while (index < parts.length) {
|
|
|
|
const part = parts[index];
|
|
|
|
while (offset < part.text.length) {
|
|
|
|
if (!predicate(index, offset, part)) {
|
|
|
|
return new DocumentPosition(index, offset);
|
|
|
|
}
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
// end reached
|
|
|
|
if (index === (parts.length - 1)) {
|
|
|
|
return new DocumentPosition(index, offset);
|
|
|
|
} else {
|
|
|
|
index += 1;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
backwardsWhile(model: EditorModel, predicate: Predicate) {
|
2019-08-26 17:00:56 +03:00
|
|
|
if (this.index === -1) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
let {index, offset} = this;
|
|
|
|
const parts = model.parts;
|
|
|
|
while (index >= 0) {
|
|
|
|
const part = parts[index];
|
|
|
|
while (offset > 0) {
|
|
|
|
if (!predicate(index, offset - 1, part)) {
|
|
|
|
return new DocumentPosition(index, offset);
|
|
|
|
}
|
|
|
|
offset -= 1;
|
|
|
|
}
|
|
|
|
// start reached
|
|
|
|
if (index === 0) {
|
|
|
|
return new DocumentPosition(index, offset);
|
|
|
|
} else {
|
|
|
|
index -= 1;
|
|
|
|
offset = parts[index].text.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 16:52:39 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
asOffset(model: EditorModel) {
|
2019-08-28 16:52:39 +03:00
|
|
|
if (this.index === -1) {
|
|
|
|
return new DocumentOffset(0, true);
|
|
|
|
}
|
|
|
|
let offset = 0;
|
|
|
|
for (let i = 0; i < this.index; ++i) {
|
|
|
|
offset += model.parts[i].text.length;
|
|
|
|
}
|
|
|
|
offset += this.offset;
|
|
|
|
const lastPart = model.parts[this.index];
|
2020-01-16 07:05:00 +03:00
|
|
|
const atEnd = !lastPart || offset >= lastPart.text.length; // if no last part, we're at the end
|
2019-08-28 16:52:39 +03:00
|
|
|
return new DocumentOffset(offset, atEnd);
|
|
|
|
}
|
2019-09-06 12:09:01 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
isAtEnd(model: EditorModel) {
|
2019-09-06 12:09:01 +03:00
|
|
|
if (model.parts.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const lastPartIdx = model.parts.length - 1;
|
|
|
|
const lastPart = model.parts[lastPartIdx];
|
|
|
|
return this.index === lastPartIdx && this.offset === lastPart.text.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
isAtStart() {
|
|
|
|
return this.index === 0 && this.offset === 0;
|
|
|
|
}
|
2019-08-23 19:37:58 +03:00
|
|
|
}
|