add getter for intersecting parts of range, and total length

we'll need this when replacing selection, to preserve
newlines, etc ... in the selected range (e.g. we can't just
use range.text).
This commit is contained in:
Bruno Windels 2019-09-03 16:03:29 +02:00
parent 48f1bf1816
commit 77b14beb1f

View File

@ -57,4 +57,27 @@ export default class Range {
this._model.replaceRange(this._start, this._end, parts); this._model.replaceRange(this._start, this._end, parts);
return newLength - oldLength; return newLength - oldLength;
} }
/**
* Returns a copy of the (partial) parts within the range.
* For partial parts, only the text is adjusted to the part that intersects with the range.
*/
get parts() {
const parts = [];
this._start.iteratePartsBetween(this._end, this._model, (part, startIdx, endIdx) => {
const serializedPart = part.serialize();
serializedPart.text = part.text.substring(startIdx, endIdx);
const newPart = this._model.partCreator.deserializePart(serializedPart);
parts.push(newPart);
});
return parts;
}
get length() {
let len = 0;
this._start.iteratePartsBetween(this._end, this._model, (part, startIdx, endIdx) => {
len += endIdx - startIdx;
});
return len;
}
} }