1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00

fix: slash command does not work (#207)

This commit is contained in:
lencx 2023-01-15 16:14:37 +08:00
parent 6d950c09e6
commit f0c635bd3b
7 changed files with 184 additions and 166 deletions

View File

@ -1,5 +1,9 @@
# UPDATE LOG # UPDATE LOG
## v0.9.1
fix: slash command does not work
## v0.9.0 ## v0.9.0
fix: fix:

View File

@ -87,29 +87,59 @@ $(function() {
clearInterval(window.formInterval); clearInterval(window.formInterval);
} }
window.formInterval = setInterval(() => { window.formInterval = setInterval(() => {
const form = document.querySelector("form"); const form = document.querySelector("form textarea");
if (!form) return; if (!form) return;
clearInterval(window.formInterval); clearInterval(window.formInterval);
cmdTip(); cmdTip();
}, 200); new MutationObserver(function (mutationsList) {
for (const mutation of mutationsList) {
if (mutation.target.getAttribute('id') === '__next') {
cmdTip();
}
if (mutation.target.getAttribute('class') === 'chat-model-cmd-list') {
// The `chatgpt prompt` fill can be done by clicking on the event.
const searchDom = document.querySelector("form .chat-model-cmd-list>div");
const searchInput = document.querySelector('form textarea');
if (!searchDom) return;
searchDom.addEventListener('click', (event) => {
const item = event.target.closest("div");
if (item) {
const val = decodeURIComponent(item.getAttribute('data-prompt'));
searchInput.value = val;
document.querySelector('form textarea').focus();
initDom();
}
});
}
}
}).observe(document.body, {
childList: true,
subtree: true,
});
}, 300);
}); });
async function cmdTip() { async function cmdTip() {
initDom();
const chatModelJson = await invoke('get_chat_model_cmd') || {}; const chatModelJson = await invoke('get_chat_model_cmd') || {};
const data = chatModelJson.data; const data = chatModelJson.data;
if (data.length <= 0) return; if (data.length <= 0) return;
const modelDom = document.createElement('div'); let modelDom = document.querySelector('.chat-model-cmd-list');
modelDom.classList.add('chat-model-cmd-list'); if (!modelDom) {
const dom = document.createElement('div');
dom.classList.add('chat-model-cmd-list');
document.querySelector('form').appendChild(dom);
modelDom = document.querySelector('.chat-model-cmd-list');
// fix: tray window // fix: tray window
if (__TAURI_METADATA__.__currentWindow.label === 'tray') { if (__TAURI_METADATA__.__currentWindow.label === 'tray') {
modelDom.style.bottom = '54px'; modelDom.style.bottom = '54px';
} }
document.querySelector('form').appendChild(modelDom);
const itemDom = (v) => `<div class="cmd-item" title="${v.prompt}" data-cmd="${v.cmd}" data-prompt="${encodeURIComponent(v.prompt)}"><b title="${v.cmd}">/${v.cmd}</b><i>${v.act}</i></div>`; const itemDom = (v) => `<div class="cmd-item" title="${v.prompt}" data-cmd="${v.cmd}" data-prompt="${encodeURIComponent(v.prompt)}"><b title="${v.cmd}">/${v.cmd}</b><i>${v.act}</i></div>`;
const renderList = (v) => { const renderList = (v) => {
initDom();
modelDom.innerHTML = `<div>${v.map(itemDom).join('')}</div>`; modelDom.innerHTML = `<div>${v.map(itemDom).join('')}</div>`;
window.__CHAT_MODEL_CMD_PROMPT__ = v[0]?.prompt.trim(); window.__CHAT_MODEL_CMD_PROMPT__ = v[0]?.prompt.trim();
window.__CHAT_MODEL_CMD__ = v[0]?.cmd.trim(); window.__CHAT_MODEL_CMD__ = v[0]?.cmd.trim();
@ -126,7 +156,7 @@ async function cmdTip() {
// Enter a command starting with `/` and press a space to automatically fill `chatgpt prompt`. // Enter a command starting with `/` and press a space to automatically fill `chatgpt prompt`.
// If more than one command appears in the search results, the first one will be used by default. // If more than one command appears in the search results, the first one will be used by default.
searchInput.addEventListener('keydown', (event) => { function cmdKeydown(event) {
if (!window.__CHAT_MODEL_CMD_PROMPT__) { if (!window.__CHAT_MODEL_CMD_PROMPT__) {
return; return;
} }
@ -167,6 +197,9 @@ async function cmdTip() {
if (strGroup[1]) { if (strGroup[1]) {
searchInput.value = `/${window.__CHAT_MODEL_CMD__}` + ` {${strGroup[1]}}` + ' |-> '; searchInput.value = `/${window.__CHAT_MODEL_CMD__}` + ` {${strGroup[1]}}` + ' |-> ';
window.__CHAT_MODEL_STATUS__ = 1; window.__CHAT_MODEL_STATUS__ = 1;
} else {
searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__;
initDom();
} }
event.preventDefault(); event.preventDefault();
} }
@ -201,26 +234,24 @@ async function cmdTip() {
setPrompt(data[1]); setPrompt(data[1]);
searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__; searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__;
modelDom.innerHTML = '';
delete window.__CHAT_MODEL_CMD_PROMPT__; initDom();
delete window.__CHAT_MODEL_CMD__;
delete window.__CHAT_MODEL_STATUS__;
event.preventDefault(); event.preventDefault();
} }
}); }
searchInput.removeEventListener('keydown', cmdKeydown);
searchInput.addEventListener('keydown', cmdKeydown);
searchInput.addEventListener('input', () => { function cmdInput() {
if (searchInput.value === '') { if (searchInput.value === '') {
delete window.__CHAT_MODEL_CMD_PROMPT__; initDom();
delete window.__CHAT_MODEL_CMD__;
delete window.__CHAT_MODEL_STATUS__;
} }
if (window.__CHAT_MODEL_STATUS__) return; if (window.__CHAT_MODEL_STATUS__) return;
const query = searchInput.value; const query = searchInput.value;
if (!query || !/^\//.test(query)) { if (!query || !/^\//.test(query)) {
modelDom.innerHTML = ''; initDom();
return; return;
} }
@ -234,38 +265,20 @@ async function cmdTip() {
if (result.length > 0) { if (result.length > 0) {
renderList(result); renderList(result);
} else { } else {
initDom();
}
}
searchInput.removeEventListener('input', cmdInput);
searchInput.addEventListener('input', cmdInput);
}
}
function initDom() {
const modelDom = document.querySelector('.chat-model-cmd-list');
if (modelDom) {
modelDom.innerHTML = ''; modelDom.innerHTML = '';
delete window.__CHAT_MODEL_CMD_PROMPT__; delete window.__CHAT_MODEL_CMD_PROMPT__;
delete window.__CHAT_MODEL_CMD__; delete window.__CHAT_MODEL_CMD__;
delete window.__CHAT_MODEL_STATUS__; delete window.__CHAT_MODEL_STATUS__;
} }
}, {
capture: false,
passive: true,
once: false
});
if (window.searchInterval) {
clearInterval(window.searchInterval);
}
window.searchInterval = setInterval(() => {
// The `chatgpt prompt` fill can be done by clicking on the event.
const searchDom = document.querySelector("form .chat-model-cmd-list>div");
if (!searchDom) return;
searchDom.addEventListener('click', (event) => {
// .cmd-item
const item = event.target.closest("div");
if (item) {
const val = decodeURIComponent(item.getAttribute('data-prompt'));
searchInput.value = val;
document.querySelector('form textarea').focus();
window.__CHAT_MODEL_CMD_PROMPT__ = val;
modelDom.innerHTML = '';
}
}, {
capture: false,
passive: true,
once: false
});
}, 200);
} }

View File

@ -1,8 +1,7 @@
// *** Core Script - Export *** // *** Core Script - Export ***
const buttonOuterHTMLFallback = `<button class="btn flex justify-center gap-2 btn-neutral" id="download-png-button">Try Again</button>`;
$(async function () { $(async function () {
const buttonOuterHTMLFallback = `<button class="btn flex justify-center gap-2 btn-neutral" id="download-png-button">Try Again</button>`;
if (window.innerWidth < 767) return; if (window.innerWidth < 767) return;
const chatConf = await invoke('get_chat_conf') || {}; const chatConf = await invoke('get_chat_conf') || {};
if (window.buttonsInterval) { if (window.buttonsInterval) {
@ -190,7 +189,7 @@ async function handlePdf(imgData, canvas, pixelRatio) {
} }
function getName() { function getName() {
const id = uid().toString(36); const id = window.crypto.getRandomValues(new Uint32Array(1))[0].toString(36);
const name = document.querySelector('nav .overflow-y-auto a.hover\\:bg-gray-800')?.innerText?.trim() || ''; const name = document.querySelector('nav .overflow-y-auto a.hover\\:bg-gray-800')?.innerText?.trim() || '';
return { filename: name ? name : id, id, pathname: 'chat.download.json' }; return { filename: name ? name : id, id, pathname: 'chat.download.json' };
} }

View File

@ -16,9 +16,9 @@ $(async function () {
background: #4a4a4a; background: #4a4a4a;
color: white; color: white;
font-weight: bold; font-weight: bold;
padding: 5px 8px; padding: 3px 5px;
border-radius: 4px; border-radius: 2px;
font-size: 12px; font-size: 10px;
cursor: pointer; cursor: pointer;
} }
`; `;
@ -31,6 +31,7 @@ $(async function () {
const { computePosition, flip, offset, shift } = window.FloatingUIDOM; const { computePosition, flip, offset, shift } = window.FloatingUIDOM;
document.body.addEventListener('mousedown', async (e) => { document.body.addEventListener('mousedown', async (e) => {
selectionMenu.style.display = 'none';
if (e.target.id === 'chagpt-selection-menu') { if (e.target.id === 'chagpt-selection-menu') {
await invoke('dalle2_window', { query: encodeURIComponent(window.__DALLE2_CONTENT__) }); await invoke('dalle2_window', { query: encodeURIComponent(window.__DALLE2_CONTENT__) });
} else { } else {

View File

@ -53,25 +53,25 @@ interface EditRowProps {
} }
export const EditRow: FC<EditRowProps> = ({ rowKey, row, actions }) => { export const EditRow: FC<EditRowProps> = ({ rowKey, row, actions }) => {
const [isEdit, setEdit] = useState(false); const [isEdit, setEdit] = useState(false);
const [val, setVal] = useState(row[rowKey]); const [val, setVal] = useState(row[rowKey] || '');
const handleEdit = () => { const handleEdit = () => {
setEdit(true); setEdit(true);
}; };
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setVal(e.target.value) setVal(e.target.value)
}; };
const handleSave = () => { const handleSave = () => {
setEdit(false); setEdit(false);
row[rowKey] = val; row[rowKey] = val?.trim();
actions?.setRecord(row, 'rowedit') actions?.setRecord(row, 'rowedit')
}; };
return isEdit return isEdit
? ( ? (
<Input.TextArea <Input
value={val} value={val}
rows={1} autoFocus
onChange={handleChange} onChange={handleChange}
{...DISABLE_AUTO_COMPLETE} {...DISABLE_AUTO_COMPLETE}
onPressEnter={handleSave} onPressEnter={handleSave}

View File

@ -69,7 +69,6 @@ export default function Download() {
const downloadDir = await path.join(await chatRoot(), 'download'); const downloadDir = await path.join(await chatRoot(), 'download');
await fs.removeDir(downloadDir, { recursive: true }); await fs.removeDir(downloadDir, { recursive: true });
await handleRefresh(); await handleRefresh();
rowReset();
message.success('All files have been cleared!'); message.success('All files have been cleared!');
return; return;
} }
@ -88,6 +87,7 @@ export default function Download() {
const handleRefresh = async () => { const handleRefresh = async () => {
await invoke('download_list', { pathname: CHAT_DOWNLOAD_JSON, dir: 'download' }); await invoke('download_list', { pathname: CHAT_DOWNLOAD_JSON, dir: 'download' });
rowReset();
const data = await refreshJson(); const data = await refreshJson();
opInit(data); opInit(data);
}; };

View File

@ -65,7 +65,6 @@ export default function Notes() {
const notesDir = await path.join(await chatRoot(), 'notes'); const notesDir = await path.join(await chatRoot(), 'notes');
await fs.removeDir(notesDir, { recursive: true }); await fs.removeDir(notesDir, { recursive: true });
await handleRefresh(); await handleRefresh();
rowReset();
message.success('All files have been cleared!'); message.success('All files have been cleared!');
return; return;
} }
@ -83,6 +82,7 @@ export default function Notes() {
const handleRefresh = async () => { const handleRefresh = async () => {
await invoke('download_list', { pathname: CHAT_NOTES_JSON, dir: 'notes' }); await invoke('download_list', { pathname: CHAT_NOTES_JSON, dir: 'notes' });
rowReset();
const data = await refreshJson(); const data = await refreshJson();
opInit(data); opInit(data);
}; };
@ -135,6 +135,7 @@ export default function Notes() {
> >
<ReactMarkdown <ReactMarkdown
children={source} children={source}
linkTarget="_blank"
components={{ components={{
code({node, inline, className, children, ...props}) { code({node, inline, className, children, ...props}) {
const match = /language-(\w+)/.exec(className || '') const match = /language-(\w+)/.exec(className || '')