From f27607a74cb8b6d2d4f7c724cdae5bfeb6346ff9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 16 May 2019 17:58:22 +0100 Subject: [PATCH] don't put cursor position in NewlinePart after adding it You can't append to it anyway, so mark it uneditable and skip uneditable parts if that's where an edit ended up. This has the added advantage that if there is text after a newly insert pill, the cursor will be put just before it rather than in the pill, after the last character. --- src/editor/model.js | 12 +++++++++++- src/editor/parts.js | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/editor/model.js b/src/editor/model.js index 85dd425b0e..c6f0c5529f 100644 --- a/src/editor/model.js +++ b/src/editor/model.js @@ -88,7 +88,8 @@ export default class EditorModel { } this._mergeAdjacentParts(); const caretOffset = diff.at - removedOffsetDecrease + addedLen; - const newPosition = this._positionForOffset(caretOffset, true); + let newPosition = this._positionForOffset(caretOffset, true); + newPosition = newPosition.skipUneditableParts(this._parts); this._setActivePart(newPosition); this._updateCallback(newPosition); } @@ -261,4 +262,13 @@ class DocumentPosition { get offset() { return this._offset; } + + skipUneditableParts(parts) { + const part = parts[this.index]; + if (part && !part.canEdit) { + return new DocumentPosition(this.index + 1, 0); + } else { + return this; + } + } } diff --git a/src/editor/parts.js b/src/editor/parts.js index a20b857fee..8ba18997a9 100644 --- a/src/editor/parts.js +++ b/src/editor/parts.js @@ -205,6 +205,14 @@ export class NewlinePart extends BasePart { get type() { return "newline"; } + + // this makes the cursor skip this part when it is inserted + // rather than trying to append to it, which is what we want. + // As a newline can also be only one character, it makes sense + // as it can only be one character long. This caused #9741. + get canEdit() { + return false; + } } export class RoomPillPart extends PillPart {