Add plain text command
authorAjay Bura <32841439+ajbura@users.noreply.github.com>
Sun, 11 Sep 2022 06:48:32 +0000 (12:18 +0530)
committerAjay Bura <32841439+ajbura@users.noreply.github.com>
Sun, 11 Sep 2022 06:48:32 +0000 (12:18 +0530)
src/app/organisms/room/RoomViewInput.jsx
src/app/organisms/room/commands.jsx
src/client/state/RoomsInput.js

index 8f2780cd0e5a0a737ba2d3edb037df3c2974522e..de72e2bb1876f18ad6d07e6b47d03cc6762d366f 100644 (file)
@@ -181,7 +181,10 @@ function RoomViewInput({
     };
   }, [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;
     sendIsTyping(false);
 
@@ -191,7 +194,7 @@ function RoomViewInput({
     }
     textAreaRef.current.disabled = true;
     textAreaRef.current.style.cursor = 'not-allowed';
-    await roomsInput.sendInput(roomId, msgType);
+    await roomsInput.sendInput(roomId, opt);
     textAreaRef.current.disabled = false;
     textAreaRef.current.style.cursor = 'unset';
     focusInput();
@@ -209,8 +212,8 @@ function RoomViewInput({
       confirmDialog('Invalid Command', `"${cmdName}" is not a valid command.`, 'Alright');
       return;
     }
-    if (['me', 'shrug'].includes(cmdName)) {
-      commands[cmdName].exe(roomId, cmdData, (message, msgType) => sendBody(message, msgType));
+    if (['me', 'shrug', 'plain'].includes(cmdName)) {
+      commands[cmdName].exe(roomId, cmdData, sendBody);
       return;
     }
     commands[cmdName].exe(roomId, cmdData);
@@ -226,7 +229,7 @@ function RoomViewInput({
       return;
     }
     if (msgBody === '' && attachment === null) return;
-    sendBody(msgBody, 'm.text');
+    sendBody(msgBody);
   };
 
   const handleSendSticker = async (data) => {
index cceaf045acfcbe83b3235048f67f57203e84230a..463f9d94512b074520d2b207212d4529a064020e 100644 (file)
@@ -38,7 +38,7 @@ const commands = {
     exe: (roomId, data, onSuccess) => {
       const body = data.trim();
       if (body === '') return;
-      onSuccess(body, 'm.emote');
+      onSuccess(body, { msgType: 'm.emote' });
     },
   },
   shrug: {
@@ -46,9 +46,18 @@ const commands = {
     description: 'Send ¯\\_(ツ)_/¯ as message',
     exe: (roomId, data, onSuccess) => onSuccess(
       `¯\\_(ツ)_/¯${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: {
     name: 'help',
     description: 'View all commands',
index 4277b2f0c3730df875acc2f101b989fe7450c1e1..e6778711c3370ebffcdf2ffbfdd75d1e1d2da2a4 100644 (file)
@@ -274,7 +274,8 @@ class RoomsInput extends EventEmitter {
     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 input = this.getInput(roomId);
     input.isSending = true;
@@ -292,19 +293,22 @@ class RoomsInput extends EventEmitter {
       };
 
       // Apply formatting if relevant
-      let formattedBody = settings.isMarkdown
+      let formattedBody = settings.isMarkdown && autoMarkdown
         ? getFormattedBody(rawMessage)
         : sanitizeText(rawMessage);
 
-      formattedBody = formatUserPill(room, formattedBody);
-      formattedBody = formatEmoji(this.matrixClient, room, this.roomList, formattedBody);
+      if (autoMarkdown) {
+        formattedBody = formatUserPill(room, formattedBody);
+        formattedBody = formatEmoji(this.matrixClient, room, this.roomList, formattedBody);
+
+        content.body = findAndReplace(
+          content.body,
+          MXID_REGEX,
+          (match) => room.currentState.userIdsToDisplayNames[match[0]],
+          (match) => `@${room.currentState.userIdsToDisplayNames[match[0]]}`,
+        );
+      }
 
-      content.body = findAndReplace(
-        content.body,
-        MXID_REGEX,
-        (match) => room.currentState.userIdsToDisplayNames[match[0]],
-        (match) => `@${room.currentState.userIdsToDisplayNames[match[0]]}`,
-      );
       if (formattedBody !== sanitizeText(rawMessage)) {
         // Formatting was applied, and we need to switch to custom HTML
         content.format = 'org.matrix.custom.html';