allow inserting multiple parts at a position

This commit is contained in:
Bruno Windels 2019-08-20 12:36:05 +02:00
parent ce44c651d0
commit 10c218825b
2 changed files with 11 additions and 5 deletions

View file

@ -134,7 +134,7 @@ export default class SendMessageComposer extends React.Component {
const displayName = member ? const displayName = member ?
member.rawDisplayName : payload.user_id; member.rawDisplayName : payload.user_id;
const userPillPart = this.model.partCreator.userPill(displayName, userId); const userPillPart = this.model.partCreator.userPill(displayName, userId);
this.model.insertPartAt(userPillPart, this._editorRef.getCaret()); this.model.insertPartsAt([userPillPart], this._editorRef.getCaret());
break; break;
} }
} }

View file

@ -108,12 +108,18 @@ export default class EditorModel {
this._updateCallback(caret, inputType); this._updateCallback(caret, inputType);
} }
insertPartAt(part, caret) { insertPartsAt(parts, caret) {
const position = this.positionForOffset(caret.offset, caret.atNodeEnd); const position = this.positionForOffset(caret.offset, caret.atNodeEnd);
const insertIndex = this._splitAt(position); const insertIndex = this._splitAt(position);
this._insertPart(insertIndex, part); let newTextLength = 0;
// want to put caret after new part? for (let i = 0; i < parts.length; ++i) {
const newPosition = new DocumentPosition(insertIndex, part.text.length); const part = parts[i];
newTextLength += part.text.length;
this._insertPart(insertIndex + i, part);
}
// put caret after new part
const lastPartIndex = insertIndex + parts.length - 1;
const newPosition = new DocumentPosition(lastPartIndex, newTextLength);
this._updateCallback(newPosition); this._updateCallback(newPosition);
} }