From 69e1c013b33406e66ffb73780dc4a1b8927f20a3 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 27 Jul 2023 11:07:36 +0800 Subject: [PATCH] press esc to exit edit message --- src/message.tsx | 106 +++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/src/message.tsx b/src/message.tsx index fc4e961..65e94a3 100644 --- a/src/message.tsx +++ b/src/message.tsx @@ -1,14 +1,74 @@ -import { useState } from "preact/hooks"; -import { ChatStore } from "./app"; +import { useState, useEffect, StateUpdater } from "preact/hooks"; +import { ChatStore, ChatStoreMessage } from "./app"; import { calculate_token_length } from "./chatgpt"; import Markdown from "preact-markdown"; +interface EditMessageProps { + chat: ChatStoreMessage; + chatStore: ChatStore; + setShowEdit: StateUpdater; + setChatStore: (cs: ChatStore) => void; +} + +function EditMessage(props: EditMessageProps) { + const { setShowEdit, chat, setChatStore, chatStore } = props; + useEffect(() => { + const handleKeyPress = (event: any) => { + if (event.keyCode === 27) { + // keyCode for ESC key is 27 + setShowEdit(false); + } + }; + + document.addEventListener("keydown", handleKeyPress); + + return () => { + document.removeEventListener("keydown", handleKeyPress); + }; + }, []); // The empty dependency array ensures that the effect runs only once + + return ( +
setShowEdit(false)} + > +
+ +
+ +
+
+
+ ); +} + interface Props { messageIndex: number; chatStore: ChatStore; setChatStore: (cs: ChatStore) => void; update_total_tokens: () => void; } + export default function Message(props: Props) { const { chatStore, messageIndex, setChatStore } = props; const chat = chatStore.history[messageIndex]; @@ -106,42 +166,12 @@ export default function Message(props: Props) { {showEdit && ( -
setShowEdit(false)} - onKeyDown={(e: any) => { - if (e.keyCode === 27) { - setShowEdit(false); - } - }} - > -
- -
- -
-
-
+ )} {showCopiedHint && } {chatStore.develop_mode && (