restore insert mention

for this, we need to store the last caret in the editor,
to know where to insert the user pill.

Because clicking on a member blurs the editor, and the
selection is moved away from the editor.

For this reason, we keep as cache of the last caretOffset object,
invalidated by a selection with different values.

The selection needs to be cloned because apparently the browser
mutates the object instead of returning a new one.
This commit is contained in:
Bruno Windels 2019-08-07 17:44:49 +02:00
parent 71286b5610
commit c135cd60d2
3 changed files with 111 additions and 17 deletions

View file

@ -108,6 +108,15 @@ export default class EditorModel {
this._updateCallback(caret, inputType);
}
insertPartAt(part, caret) {
const position = this.positionForOffset(caret.offset, caret.atNodeEnd);
const insertIndex = this._splitAt(position);
this._insertPart(insertIndex, part);
// want to put caret after new part?
const newPosition = new DocumentPosition(insertIndex, part.text.length);
this._updateCallback(newPosition);
}
update(newValue, inputType, caret) {
const diff = this._diff(newValue, inputType, caret);
const position = this.positionForOffset(diff.at, caret.atNodeEnd);
@ -232,6 +241,23 @@ export default class EditorModel {
}
return removedOffsetDecrease;
}
// return part index where insertion will insert between at offset
_splitAt(pos) {
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);
this._insertPart(pos.index + 1, secondPart);
return pos.index + 1;
}
/**
* inserts `str` into the model at `pos`.