From 4c19f3a3d364f65aeb66726f7cbcad86bcd7ac2a Mon Sep 17 00:00:00 2001 From: Vanessa Date: Wed, 8 Mar 2023 23:35:36 +0800 Subject: [PATCH] :sparkles: fix https://github.com/siyuan-note/siyuan/issues/7566 --- app/src/ai/actions.ts | 40 +++++++++++++++-------------------- app/src/ai/chat.ts | 8 +++---- app/src/protyle/hint/index.ts | 2 +- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/app/src/ai/actions.ts b/app/src/ai/actions.ts index 8d2ced11d..85e5b3866 100644 --- a/app/src/ai/actions.ts +++ b/app/src/ai/actions.ts @@ -1,10 +1,15 @@ import {MenuItem} from "../menus/Menu"; import {fetchPost} from "../util/fetch"; -import {focusByRange} from "../protyle/util/selection"; +import {focusByRange, setLastNodeRange} from "../protyle/util/selection"; import {insertHTML} from "../protyle/util/insertHTML"; import {Dialog} from "../dialog"; import {isMobile} from "../util/functions"; +export const fillContent = (protyle:IProtyle, data:string, elements:Element[]) => { + setLastNodeRange(elements[elements.length - 1], protyle.toolbar.range); + insertHTML(data, protyle, true, true); +} + export const AIActions = (elements: Element[], protyle: IProtyle) => { const ids: string[] = []; elements.forEach(item => { @@ -18,8 +23,7 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => { label: window.siyuan.languages.aiContinueWrite, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Continue writing"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { @@ -29,8 +33,7 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => { label: window.siyuan.languages.aiTranslate_zh_CN, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [zh_CN]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { @@ -45,40 +48,35 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => { label: window.siyuan.languages.aiTranslate_ko_KR, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [ko_KR]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiTranslate_en_US, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [en_US]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiTranslate_es_ES, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [es_ES]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiTranslate_fr_FR, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [fr_FR]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiTranslate_de_DE, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [de_DE]"}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }] @@ -86,24 +84,21 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => { label: window.siyuan.languages.aiExtractSummary, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiExtractSummary}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiBrainStorm, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiBrainStorm}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } }, { label: window.siyuan.languages.aiFixGrammarSpell, click() { fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiFixGrammarSpell}, (response) => { - focusByRange(protyle.toolbar.range); - insertHTML(response.data, protyle, true); + fillContent(protyle, response.data, elements); }); } },{ @@ -133,12 +128,11 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => { action: inputElement.value, }, (response) => { dialog.destroy(); - focusByRange(protyle.toolbar.range); let respContent = ""; if (response.data && "" !== response.data) { respContent = "\n\n" + response.data; } - insertHTML(`${inputElement.value}${respContent}`, protyle, true); + fillContent(protyle, `${inputElement.value}${respContent}`, elements); }); }); } diff --git a/app/src/ai/chat.ts b/app/src/ai/chat.ts index a68c5ab3b..d304c90ba 100644 --- a/app/src/ai/chat.ts +++ b/app/src/ai/chat.ts @@ -1,10 +1,9 @@ import {Dialog} from "../dialog"; import {isMobile} from "../util/functions"; import {fetchPost} from "../util/fetch"; -import {focusByRange} from "../protyle/util/selection"; -import {insertHTML} from "../protyle/util/insertHTML"; +import {fillContent} from "./actions"; -export const AIChat = (protyle:IProtyle) => { +export const AIChat = (protyle: IProtyle, element: Element) => { const dialog = new Dialog({ title: "AI Chat", content: `
@@ -28,12 +27,11 @@ export const AIChat = (protyle:IProtyle) => { msg: inputElement.value, }, (response) => { dialog.destroy(); - focusByRange(protyle.toolbar.range); let respContent = ""; if (response.data && "" !== response.data) { respContent = "\n\n" + response.data; } - insertHTML(`${inputElement.value}${respContent}`, protyle, true); + fillContent(protyle, `${inputElement.value}${respContent}`, [element]); }); }); }; diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts index 8be3f82ed..83c997ce4 100644 --- a/app/src/protyle/hint/index.ts +++ b/app/src/protyle/hint/index.ts @@ -552,7 +552,7 @@ ${unicode2Emoji(emoji.unicode, true)}`; }); return; } else if (value === Constants.ZWSP + 5) { - AIChat(protyle); + AIChat(protyle, nodeElement); return; } else if (Constants.INLINE_TYPE.includes(value)) { range.deleteContents();