Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: fix https://github.com/siyuan-note/siyuan/issues/7566

Vanessa4c19f3a3d364
diff --git a/app/src/ai/actions.ts b/app/src/ai/actions.ts
index 8d2ced11d2..85e5b38667 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 a68c5ab3b2..d304c90ba0 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: `<div class="b3-dialog__content"><input class="b3-text-field fn__block" value=""></div>
@@ -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 8be3f82ed6..83c997ce46 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -552,7 +552,7 @@ ${unicode2Emoji(emoji.unicode, true)}</button>`;
});
return;
} else if (value === Constants.ZWSP + 5) {
- AIChat(protyle);
+ AIChat(protyle, nodeElement);
return;
} else if (Constants.INLINE_TYPE.includes(value)) {
range.deleteContents();