From 77b14beb1f770ce973fd45b30d68c490b5bcd216 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 3 Sep 2019 16:03:29 +0200 Subject: [PATCH] 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). --- src/editor/range.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/editor/range.js b/src/editor/range.js index 1eebfeeb91..d04bbbbfb1 100644 --- a/src/editor/range.js +++ b/src/editor/range.js @@ -57,4 +57,27 @@ export default class Range { this._model.replaceRange(this._start, this._end, parts); 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; + } }