diff --git a/__main__.py b/__main__.py index 7560b97..9ba62ad 100644 --- a/__main__.py +++ b/__main__.py @@ -79,7 +79,7 @@ class ChatBot(ChatBotFrame): with gr.Box(elem_id='chat_box'): with gr.Row(): gr.Button(elem_classes='sm_btn').style(size='sm', full_width=False) - gr.HTML(func_box.get_html("appearance_switcher.html").format(label=""), elem_classes="insert_block") + gr.HTML(func_box.get_html("appearance_switcher.html").format(label=""), elem_id='user_input_tb', elem_classes="insert_block") with gr.Row(): self.txt = gr.Textbox(show_label=False, placeholder="Input question here.", elem_classes='chat_input').style(container=False) self.input_copy = gr.State('') @@ -186,7 +186,7 @@ class ChatBot(ChatBotFrame): self.pro_func_prompt, self.pro_fp_state]) self.pro_reuse_btn.click( fn=func_box.reuse_chat, - inputs=[self.pro_results, self.chatbot, self.history, self.pro_name_txt], + inputs=[self.pro_results, self.chatbot, self.history, self.pro_name_txt, self.txt], outputs=[self.chatbot, self.history, self.txt, self.tabs_chatbot, self.pro_name_txt, self.examples_column] ) @@ -312,7 +312,7 @@ class ChatBot(ChatBotFrame): self.cancel_handles.append(self.txt.submit(**self.clear_agrs).then(**self.predict_args)) self.cancel_handles.append(self.submitBtn.click(**self.clear_agrs).then(**self.predict_args)) # self.cpopyBtn.click(fn=func_box.copy_result, inputs=[self.history], outputs=[self.status]) - self.resetBtn.click(lambda: ([], [], "已重置"), None, [self.chatbot, self.history, self.status]) + self.resetBtn.click(lambda: ([], [], "已重置"), None, [self.chatbot, self.history, self.status], _js='()=>{clearHistoryHtml();}') def signals_function(self): # 基础功能区的回调函数注册 diff --git a/docs/assets/custom.js b/docs/assets/custom.js index 9a07884..0100a6a 100644 --- a/docs/assets/custom.js +++ b/docs/assets/custom.js @@ -9,7 +9,7 @@ var user_input_ta; var gradioContainer = null; var user_input_ta = null; -var chat_txt = null; +var user_input_tb = null; var userInfoDiv = null; var appTitleDiv = null; var chatbot = null; @@ -55,11 +55,11 @@ function gradioLoaded(mutations) { if (mutations[i].addedNodes.length) { loginUserForm = document.querySelector(".gradio-container > .main > .wrap > .panel > .form") gradioContainer = document.querySelector(".gradio-container"); - chat_txt = document.getElementById('chat_txt'); + user_input_tb = document.getElementById('user_input_tb'); userInfoDiv = document.getElementById("user_info"); appTitleDiv = document.getElementById("app_title"); - chatbot = document.querySelector('#废弃'); - chatbotWrap = document.querySelector('#废弃 > .wrap'); + chatbot = document.querySelector('#main_chatbot'); + chatbotWrap = document.querySelector('#main_chatbot > .wrap'); apSwitch = document.querySelector('.apSwitch input[type="checkbox"]'); if (loginUserForm) { @@ -70,21 +70,18 @@ function gradioLoaded(mutations) { if (gradioContainer && apSwitch) { // gradioCainter 加载出来了没? adjustDarkMode(); } - if (chat_txt) { // chat_txt 加载出来了没? - selectHistory(); + if (user_input_tb) { // user_input_tb 加载出来了没? } if (userInfoDiv && appTitleDiv) { // userInfoDiv 和 appTitleDiv 加载出来了没? if (!usernameGotten) { getUserInfo(); } - setTimeout(showOrHideUserInfo(), 2000); } if (chatbot) { // chatbot 加载出来了没? setChatbotHeight(); } if (chatbotWrap) { if (!historyLoaded) { - loadHistoryHtml(); } setChatbotScroll(); } @@ -116,54 +113,6 @@ function showConfirmationDialog(a, file, c) { return [a, "CANCELED", c]; } -function selectHistory() { - user_input_ta = chat_txt.querySelector("textarea"); - if (user_input_ta) { - observer.disconnect(); // 停止监听 - // 在 textarea 上监听 keydown 事件 - user_input_ta.addEventListener("keydown", function (event) { - var value = user_input_ta.value.trim(); - // 判断按下的是否为方向键 - if (event.code === 'ArrowUp' || event.code === 'ArrowDown') { - // 如果按下的是方向键,且输入框中有内容,且历史记录中没有该内容,则不执行操作 - if (value && key_down_history.indexOf(value) === -1) - return; - // 对于需要响应的动作,阻止默认行为。 - event.preventDefault(); - var length = key_down_history.length; - if (length === 0) { - currentIndex = -1; // 如果历史记录为空,直接将当前选中的记录重置 - return; - } - if (currentIndex === -1) { - currentIndex = length; - } - if (event.code === 'ArrowUp' && currentIndex > 0) { - currentIndex--; - user_input_ta.value = key_down_history[currentIndex]; - } else if (event.code === 'ArrowDown' && currentIndex < length - 1) { - currentIndex++; - user_input_ta.value = key_down_history[currentIndex]; - } - user_input_ta.selectionStart = user_input_ta.value.length; - user_input_ta.selectionEnd = user_input_ta.value.length; - const input_event = new InputEvent("input", { bubbles: true, cancelable: true }); - user_input_ta.dispatchEvent(input_event); - } else if (event.code === "Enter") { - if (value) { - currentIndex = -1; - if (key_down_history.indexOf(value) === -1) { - key_down_history.push(value); - if (key_down_history.length > MAX_HISTORY_LENGTH) { - key_down_history.shift(); - } - } - } - } - }); - } -} - var username = null; function getUserInfo() { if (usernameGotten) { @@ -186,7 +135,6 @@ function getUserInfo() { username = username.match(/User:\s*(.*)/)[1] || username; localStorage.setItem("username", username); usernameGotten = true; - clearHistoryHtml(); } } } @@ -413,8 +361,7 @@ var mObserver = new MutationObserver(function (mutationsList) { if (mmutation.type === 'childList') { for (var node of mmutation.addedNodes) { if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') { - saveHistoryHtml(); - document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); + document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); } if (node.tagName === 'INPUT' && node.getAttribute('type') === 'range') { setSlider(); @@ -422,8 +369,7 @@ var mObserver = new MutationObserver(function (mutationsList) { } for (var node of mmutation.removedNodes) { if (node.nodeType === 1 && node.classList.contains('message') && node.getAttribute('data-testid') === 'bot') { - saveHistoryHtml(); - document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); + document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); } } } else if (mmutation.type === 'attributes') { @@ -433,8 +379,7 @@ var mObserver = new MutationObserver(function (mutationsList) { clearTimeout(timeoutId); timeoutId = setTimeout(() => { isThrottled = false; - document.querySelectorAll('#废弃>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); - saveHistoryHtml(); + document.querySelectorAll('#main_chatbot>.wrap>.message-wrap .message.bot').forEach(addChuanhuButton); }, 500); } } @@ -442,59 +387,6 @@ var mObserver = new MutationObserver(function (mutationsList) { }); mObserver.observe(document.documentElement, { attributes: true, childList: true, subtree: true }); -var loadhistorytime = 0; // for debugging -function saveHistoryHtml() { - var historyHtml = document.querySelector('#废弃 > .wrap'); - localStorage.setItem('chatHistory', historyHtml.innerHTML); - // console.log("History Saved") - historyLoaded = false; -} -function loadHistoryHtml() { - var historyHtml = localStorage.getItem('chatHistory'); - if (!historyHtml) { - historyLoaded = true; - return; // no history, do nothing - } - userLogged = localStorage.getItem('userLogged'); - if (userLogged){ - historyLoaded = true; - return; // logged in, do nothing - } - if (!historyLoaded) { - var tempDiv = document.createElement('div'); - tempDiv.innerHTML = historyHtml; - var buttons = tempDiv.querySelectorAll('button.chuanhu-btn'); - var gradioCopyButtons = tempDiv.querySelectorAll('button.copy_code_button'); - for (var i = 0; i < buttons.length; i++) { - buttons[i].parentNode.removeChild(buttons[i]); - } - for (var i = 0; i < gradioCopyButtons.length; i++) { - gradioCopyButtons[i].parentNode.removeChild(gradioCopyButtons[i]); - } - var fakeHistory = document.createElement('div'); - fakeHistory.classList.add('history-message'); - fakeHistory.innerHTML = tempDiv.innerHTML; - webLocale(); - chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild); - // var fakeHistory = document.createElement('div'); - // fakeHistory.classList.add('history-message'); - // fakeHistory.innerHTML = historyHtml; - // chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild); - historyLoaded = true; - console.log("History Loaded"); - loadhistorytime += 1; // for debugging - } else { - historyLoaded = false; - } -} -function clearHistoryHtml() { - localStorage.removeItem("chatHistory"); - historyMessages = chatbotWrap.querySelector('.history-message'); - if (historyMessages) { - chatbotWrap.removeChild(historyMessages); - console.log("History Cleared"); - } -} // 监视页面内部 DOM 变动 var observer = new MutationObserver(function (mutations) { @@ -502,11 +394,6 @@ var observer = new MutationObserver(function (mutations) { }); observer.observe(targetNode, { childList: true, subtree: true }); -// 监视页面变化 -window.addEventListener("DOMContentLoaded", function () { - isInIframe = (window.self !== window.top); - historyLoaded = false; -}); window.addEventListener('resize', setChatbotHeight); window.addEventListener('scroll', setChatbotHeight); window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode); diff --git a/docs/test_markdown_format.py b/docs/test_markdown_format.py index 896f6f1..74ba8f5 100644 --- a/docs/test_markdown_format.py +++ b/docs/test_markdown_format.py @@ -49,7 +49,7 @@ def markdown_convertion(txt): """ 将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。 """ - pre = '
|<\/p><\/div>$') -pattern_markdown_p = re.compile(r'^