From 884402e62aa4ee14af586ca2ccdf0d8cd7cec704 Mon Sep 17 00:00:00 2001 From: janderedev Date: Thu, 12 Aug 2021 22:21:37 +0200 Subject: [PATCH 1/3] sed replacement in message box. --- .../common/messaging/MessageBox.tsx | 77 +++++++++++++------ 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/src/components/common/messaging/MessageBox.tsx b/src/components/common/messaging/MessageBox.tsx index abb2ecf1..3f0e1e89 100644 --- a/src/components/common/messaging/MessageBox.tsx +++ b/src/components/common/messaging/MessageBox.tsx @@ -106,6 +106,9 @@ const Action = styled.div` } `; +// For sed replacement +const SED_REGEX = new RegExp("^s/([^])+/([^])+$"); + // ! FIXME: add to app config and load from app config export const CAN_UPLOAD_AT_ONCE = 4; @@ -198,37 +201,63 @@ export default observer(({ channel }: Props) => { stopTyping(); setMessage(); setReplies([]); - playSound("outbound"); - const nonce = ulid(); - dispatch({ - type: "QUEUE_ADD", - nonce, - channel: channel._id, - message: { - _id: nonce, - channel: channel._id, - author: client.user!._id, - content, - replies, - }, - }); + // sed style message editing. + // If the user types for example `s/abc/def`, the string "abc" + // will be replaced with "def" in their last sent message. + if (SED_REGEX.test(content)) { + renderer.messages.reverse(); + const msg = renderer.messages.find( + (msg) => msg.author_id === client.user!._id, + ); + renderer.messages.reverse(); - defer(() => renderer.jumpToBottom(SMOOTH_SCROLL_ON_RECEIVE)); + if (msg) { + const [_, toReplace, newText, flags] = + content.split(/(? renderer.jumpToBottom(SMOOTH_SCROLL_ON_RECEIVE)); + + try { + await channel.sendMessage({ + content, + nonce, + replies, + }); + } catch (error) { + dispatch({ + type: "QUEUE_FAIL", + error: takeError(error), + nonce, + }); + } } } From 82d3dd3c7b0b8929312aa2197a0e9acc4a0d3cdd Mon Sep 17 00:00:00 2001 From: janderedev Date: Thu, 12 Aug 2021 22:54:54 +0200 Subject: [PATCH 2/3] Scroll to bottom after sed editing message --- src/components/common/messaging/MessageBox.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components/common/messaging/MessageBox.tsx b/src/components/common/messaging/MessageBox.tsx index 3f0e1e89..69c6bcab 100644 --- a/src/components/common/messaging/MessageBox.tsx +++ b/src/components/common/messaging/MessageBox.tsx @@ -223,7 +223,13 @@ export default observer(({ channel }: Props) => { if (newContent != msg.content) { msg.edit({ content: newContent.substr(0, 2000), - }).catch(console.warn); + }) + .then(() => + defer(() => + renderer.jumpToBottom(SMOOTH_SCROLL_ON_RECEIVE), + ), + ) + .catch(console.warn); } } } else { From dfed9515d186ac127096ab2653d4e0710d3ec199 Mon Sep 17 00:00:00 2001 From: janderedev Date: Thu, 12 Aug 2021 23:17:52 +0200 Subject: [PATCH 3/3] fix regex, add * and empty text matchers --- .../common/messaging/MessageBox.tsx | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/components/common/messaging/MessageBox.tsx b/src/components/common/messaging/MessageBox.tsx index 69c6bcab..d058bcff 100644 --- a/src/components/common/messaging/MessageBox.tsx +++ b/src/components/common/messaging/MessageBox.tsx @@ -107,7 +107,7 @@ const Action = styled.div` `; // For sed replacement -const SED_REGEX = new RegExp("^s/([^])+/([^])+$"); +const SED_REGEX = new RegExp("^s/([^])*/([^])*$"); // ! FIXME: add to app config and load from app config export const CAN_UPLOAD_AT_ONCE = 4; @@ -214,22 +214,34 @@ export default observer(({ channel }: Props) => { renderer.messages.reverse(); if (msg) { - const [_, toReplace, newText, flags] = - content.split(/(? - defer(() => - renderer.jumpToBottom(SMOOTH_SCROLL_ON_RECEIVE), - ), - ) - .catch(console.warn); + if (newContent.length == 0) { + msg.delete().catch(console.error); + } else { + msg.edit({ + content: newContent.substr(0, 2000), + }) + .then(() => + defer(() => + renderer.jumpToBottom( + SMOOTH_SCROLL_ON_RECEIVE, + ), + ), + ) + .catch(console.error); + } } } } else {