Add plain text command

This commit is contained in:
Ajay Bura 2022-09-11 12:18:32 +05:30
parent 5848c02d50
commit bca00f46a9
3 changed files with 33 additions and 17 deletions

View file

@ -181,7 +181,10 @@ function RoomViewInput({
}; };
}, [roomId]); }, [roomId]);
const sendBody = async (body, msgType = 'm.text') => { const sendBody = async (body, options) => {
const opt = options ?? {};
if (!opt.msgType) opt.msgType = 'm.text';
if (typeof opt.autoMarkdown !== 'boolean') opt.autoMarkdown = true;
if (roomsInput.isSending(roomId)) return; if (roomsInput.isSending(roomId)) return;
sendIsTyping(false); sendIsTyping(false);
@ -191,7 +194,7 @@ function RoomViewInput({
} }
textAreaRef.current.disabled = true; textAreaRef.current.disabled = true;
textAreaRef.current.style.cursor = 'not-allowed'; textAreaRef.current.style.cursor = 'not-allowed';
await roomsInput.sendInput(roomId, msgType); await roomsInput.sendInput(roomId, opt);
textAreaRef.current.disabled = false; textAreaRef.current.disabled = false;
textAreaRef.current.style.cursor = 'unset'; textAreaRef.current.style.cursor = 'unset';
focusInput(); focusInput();
@ -209,8 +212,8 @@ function RoomViewInput({
confirmDialog('Invalid Command', `"${cmdName}" is not a valid command.`, 'Alright'); confirmDialog('Invalid Command', `"${cmdName}" is not a valid command.`, 'Alright');
return; return;
} }
if (['me', 'shrug'].includes(cmdName)) { if (['me', 'shrug', 'plain'].includes(cmdName)) {
commands[cmdName].exe(roomId, cmdData, (message, msgType) => sendBody(message, msgType)); commands[cmdName].exe(roomId, cmdData, sendBody);
return; return;
} }
commands[cmdName].exe(roomId, cmdData); commands[cmdName].exe(roomId, cmdData);
@ -226,7 +229,7 @@ function RoomViewInput({
return; return;
} }
if (msgBody === '' && attachment === null) return; if (msgBody === '' && attachment === null) return;
sendBody(msgBody, 'm.text'); sendBody(msgBody);
}; };
const handleSendSticker = async (data) => { const handleSendSticker = async (data) => {

View file

@ -38,7 +38,7 @@ const commands = {
exe: (roomId, data, onSuccess) => { exe: (roomId, data, onSuccess) => {
const body = data.trim(); const body = data.trim();
if (body === '') return; if (body === '') return;
onSuccess(body, 'm.emote'); onSuccess(body, { msgType: 'm.emote' });
}, },
}, },
shrug: { shrug: {
@ -46,9 +46,18 @@ const commands = {
description: 'Send ¯\\_(ツ)_/¯ as message', description: 'Send ¯\\_(ツ)_/¯ as message',
exe: (roomId, data, onSuccess) => onSuccess( exe: (roomId, data, onSuccess) => onSuccess(
`¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}`, `¯\\_(ツ)_/¯${data.trim() !== '' ? ` ${data}` : ''}`,
'm.text', { msgType: 'm.text' },
), ),
}, },
plain: {
name: 'plain',
description: 'Send plain text message',
exe: (roomId, data, onSuccess) => {
const body = data.trim();
if (body === '') return;
onSuccess(body, { msgType: 'm.text', autoMarkdown: false });
},
},
help: { help: {
name: 'help', name: 'help',
description: 'View all commands', description: 'View all commands',

View file

@ -274,7 +274,8 @@ class RoomsInput extends EventEmitter {
return this.roomIdToInput.get(roomId)?.isSending || false; return this.roomIdToInput.get(roomId)?.isSending || false;
} }
async sendInput(roomId, msgType) { async sendInput(roomId, options) {
const { msgType, autoMarkdown } = options;
const room = this.matrixClient.getRoom(roomId); const room = this.matrixClient.getRoom(roomId);
const input = this.getInput(roomId); const input = this.getInput(roomId);
input.isSending = true; input.isSending = true;
@ -292,10 +293,11 @@ class RoomsInput extends EventEmitter {
}; };
// Apply formatting if relevant // Apply formatting if relevant
let formattedBody = settings.isMarkdown let formattedBody = settings.isMarkdown && autoMarkdown
? getFormattedBody(rawMessage) ? getFormattedBody(rawMessage)
: sanitizeText(rawMessage); : sanitizeText(rawMessage);
if (autoMarkdown) {
formattedBody = formatUserPill(room, formattedBody); formattedBody = formatUserPill(room, formattedBody);
formattedBody = formatEmoji(this.matrixClient, room, this.roomList, formattedBody); formattedBody = formatEmoji(this.matrixClient, room, this.roomList, formattedBody);
@ -305,6 +307,8 @@ class RoomsInput extends EventEmitter {
(match) => room.currentState.userIdsToDisplayNames[match[0]], (match) => room.currentState.userIdsToDisplayNames[match[0]],
(match) => `@${room.currentState.userIdsToDisplayNames[match[0]]}`, (match) => `@${room.currentState.userIdsToDisplayNames[match[0]]}`,
); );
}
if (formattedBody !== sanitizeText(rawMessage)) { if (formattedBody !== sanitizeText(rawMessage)) {
// Formatting was applied, and we need to switch to custom HTML // Formatting was applied, and we need to switch to custom HTML
content.format = 'org.matrix.custom.html'; content.format = 'org.matrix.custom.html';