detect emote when sending (and trim "/me " for content)

This commit is contained in:
Bruno Windels 2019-06-14 11:02:20 +02:00
parent aecfbce55c
commit 3cfdd518ee
2 changed files with 21 additions and 5 deletions

View file

@ -150,16 +150,28 @@ export default class MessageEditor extends React.Component {
dis.dispatch({action: 'focus_composer'}); dis.dispatch({action: 'focus_composer'});
} }
_isEmote() {
const firstPart = this.model.parts[0];
return firstPart && firstPart.type === "plain" && firstPart.text.startsWith("/me ");
}
_sendEdit = () => { _sendEdit = () => {
const isEmote = this._isEmote();
let model = this.model;
if (isEmote) {
// trim "/me "
model = model.clone();
model.removeText({index: 0, offset: 0}, 4);
}
const newContent = { const newContent = {
"msgtype": "m.text", "msgtype": isEmote ? "m.emote" : "m.text",
"body": textSerialize(this.model), "body": textSerialize(model),
}; };
const contentBody = { const contentBody = {
msgtype: newContent.msgtype, msgtype: newContent.msgtype,
body: ` * ${newContent.body}`, body: ` * ${newContent.body}`,
}; };
const formattedBody = htmlSerializeIfNeeded(this.model); const formattedBody = htmlSerializeIfNeeded(model);
if (formattedBody) { if (formattedBody) {
newContent.format = "org.matrix.custom.html"; newContent.format = "org.matrix.custom.html";
newContent.formatted_body = formattedBody; newContent.formatted_body = formattedBody;

View file

@ -27,6 +27,10 @@ export default class EditorModel {
this._updateCallback = updateCallback; this._updateCallback = updateCallback;
} }
clone() {
return new EditorModel(this._parts, this._partCreator, this._updateCallback);
}
_insertPart(index, part) { _insertPart(index, part) {
this._parts.splice(index, 0, part); this._parts.splice(index, 0, part);
if (this._activePartIdx >= index) { if (this._activePartIdx >= index) {
@ -91,7 +95,7 @@ export default class EditorModel {
const position = this.positionForOffset(diff.at, caret.atNodeEnd); const position = this.positionForOffset(diff.at, caret.atNodeEnd);
let removedOffsetDecrease = 0; let removedOffsetDecrease = 0;
if (diff.removed) { if (diff.removed) {
removedOffsetDecrease = this._removeText(position, diff.removed.length); removedOffsetDecrease = this.removeText(position, diff.removed.length);
} }
let addedLen = 0; let addedLen = 0;
if (diff.added) { if (diff.added) {
@ -177,7 +181,7 @@ export default class EditorModel {
* @return {Number} how many characters before pos were also removed, * @return {Number} how many characters before pos were also removed,
* usually because of non-editable parts that can only be removed in their entirety. * usually because of non-editable parts that can only be removed in their entirety.
*/ */
_removeText(pos, len) { removeText(pos, len) {
let {index, offset} = pos; let {index, offset} = pos;
let removedOffsetDecrease = 0; let removedOffsetDecrease = 0;
while (len > 0) { while (len > 0) {