fixed #56: tab (after last cmd suggestion) and esc will focus back to input
authorunknown <ajbura@gmail.com>
Thu, 26 Aug 2021 09:13:14 +0000 (14:43 +0530)
committerunknown <ajbura@gmail.com>
Thu, 26 Aug 2021 09:13:14 +0000 (14:43 +0530)
src/app/organisms/channel/ChannelViewCmdBar.jsx
src/app/organisms/channel/ChannelViewInput.jsx

index 394357636187defcafcd1c678afb5a6082890136..17fc03d645b7356cfe9ee1a6db6d566d1f262814 100644 (file)
@@ -385,6 +385,22 @@ function ChannelViewCmdBar({ roomId, roomTimeline, viewEvent }) {
     });
   }
 
+  function listenKeyboard(event) {
+    const { activeElement } = document;
+    const lastCmdItem = document.activeElement.parentNode.lastElementChild;
+    if (event.keyCode === 27) {
+      if (activeElement.className !== 'cmd-item') return;
+      viewEvent.emit('focus_msg_input');
+    }
+    if (event.keyCode === 9) {
+      if (lastCmdItem.className !== 'cmd-item') return;
+      if (lastCmdItem !== activeElement) return;
+      if (event.shiftKey) return;
+      viewEvent.emit('focus_msg_input');
+      event.preventDefault();
+    }
+  }
+
   useEffect(() => {
     viewEvent.on('cmd_activate', activateCmd);
     viewEvent.on('cmd_deactivate', deactivateCmd);
@@ -396,10 +412,13 @@ function ChannelViewCmdBar({ roomId, roomTimeline, viewEvent }) {
   }, [roomId]);
 
   useEffect(() => {
+    if (cmd !== null) document.body.addEventListener('keydown', listenKeyboard);
     viewEvent.on('cmd_process', processCmd);
     viewEvent.on('cmd_exe', executeCmd);
     asyncSearch.on(asyncSearch.RESULT_SENT, displaySuggestions);
     return () => {
+      if (cmd !== null) document.body.removeEventListener('keydown', listenKeyboard);
+
       viewEvent.removeListener('cmd_process', processCmd);
       viewEvent.removeListener('cmd_exe', executeCmd);
       asyncSearch.removeListener(asyncSearch.RESULT_SENT, displaySuggestions);
index 773b47d02ccb4888bfaeacec9bddbbaad4e0ddf4..08b32c0ea0b62dcbb0ad6e62cdeda6a4db2da7fb 100644 (file)
@@ -50,10 +50,17 @@ function ChannelViewInput({
   const mx = initMatrix.matrixClient;
   const { roomsInput } = initMatrix;
 
+  function requestFocusInput() {
+    if (textAreaRef === null) return;
+    textAreaRef.current.focus();
+  }
+
   useEffect(() => {
     settings.on(cons.events.settings.MARKDOWN_TOGGLED, setIsMarkdown);
+    viewEvent.on('focus_msg_input', requestFocusInput);
     return () => {
       settings.removeListener(cons.events.settings.MARKDOWN_TOGGLED, setIsMarkdown);
+      viewEvent.removeListener('focus_msg_input', requestFocusInput);
     };
   }, []);