Fix escaping commands using double-slash //, e.g //plain sends /plain

This commit is contained in:
Michael Telatynski 2020-01-21 15:55:21 +00:00
parent adec308529
commit b5e902e1f2
2 changed files with 17 additions and 4 deletions

View file

@ -24,6 +24,8 @@ import {
containsEmote, containsEmote,
stripEmoteCommand, stripEmoteCommand,
unescapeMessage, unescapeMessage,
startsWith,
stripPrefix,
} from '../../../editor/serialize'; } from '../../../editor/serialize';
import {CommandPartCreator} from '../../../editor/parts'; import {CommandPartCreator} from '../../../editor/parts';
import BasicMessageComposer from "./BasicMessageComposer"; import BasicMessageComposer from "./BasicMessageComposer";
@ -61,6 +63,9 @@ function createMessageContent(model, permalinkCreator) {
if (isEmote) { if (isEmote) {
model = stripEmoteCommand(model); model = stripEmoteCommand(model);
} }
if (startsWith(model, "//")) {
model = stripPrefix(model, "/");
}
model = unescapeMessage(model); model = unescapeMessage(model);
const repliedToEvent = RoomViewStore.getQuotingEvent(); const repliedToEvent = RoomViewStore.getQuotingEvent();
@ -175,13 +180,13 @@ export default class SendMessageComposer extends React.Component {
const parts = this.model.parts; const parts = this.model.parts;
const firstPart = parts[0]; const firstPart = parts[0];
if (firstPart) { if (firstPart) {
if (firstPart.type === "command") { if (firstPart.type === "command" && !firstPart.text.startsWith("//")) {
return true; return true;
} }
// be extra resilient when somehow the AutocompleteWrapperModel or // be extra resilient when somehow the AutocompleteWrapperModel or
// CommandPartCreator fails to insert a command part, so we don't send // CommandPartCreator fails to insert a command part, so we don't send
// a command as a message // a command as a message
if (firstPart.text.startsWith("/") && (firstPart.type === "plain" || firstPart.type === "pill-candidate")) { if (firstPart.text.startsWith("/") && !firstPart.text.startsWith("//") && (firstPart.type === "plain" || firstPart.type === "pill-candidate")) {
return true; return true;
} }
} }

View file

@ -61,18 +61,26 @@ export function textSerialize(model) {
} }
export function containsEmote(model) { export function containsEmote(model) {
return startsWith(model, "/me ");
}
export function startsWith(model, prefix) {
const firstPart = model.parts[0]; const firstPart = model.parts[0];
// part type will be "plain" while editing, // part type will be "plain" while editing,
// and "command" while composing a message. // and "command" while composing a message.
return firstPart && return firstPart &&
(firstPart.type === "plain" || firstPart.type === "command") && (firstPart.type === "plain" || firstPart.type === "command") &&
firstPart.text.startsWith("/me "); firstPart.text.startsWith(prefix);
} }
export function stripEmoteCommand(model) { export function stripEmoteCommand(model) {
// trim "/me " // trim "/me "
return stripPrefix(model, "/me ");
}
export function stripPrefix(model, prefix) {
model = model.clone(); model = model.clone();
model.removeText({index: 0, offset: 0}, 4); model.removeText({index: 0, offset: 0}, prefix.length);
return model; return model;
} }