2023-03-08 21:57:57 +08:00
|
|
|
import {MenuItem} from "../menus/Menu";
|
|
|
|
|
import {fetchPost} from "../util/fetch";
|
2023-03-08 23:35:36 +08:00
|
|
|
import {focusByRange, setLastNodeRange} from "../protyle/util/selection";
|
2023-03-08 21:57:57 +08:00
|
|
|
import {insertHTML} from "../protyle/util/insertHTML";
|
2023-03-08 22:03:40 +08:00
|
|
|
import {Dialog} from "../dialog";
|
|
|
|
|
import {isMobile} from "../util/functions";
|
2023-03-08 21:57:57 +08:00
|
|
|
|
2023-03-08 23:35:36 +08:00
|
|
|
export const fillContent = (protyle:IProtyle, data:string, elements:Element[]) => {
|
|
|
|
|
setLastNodeRange(elements[elements.length - 1], protyle.toolbar.range);
|
|
|
|
|
insertHTML(data, protyle, true, true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 21:57:57 +08:00
|
|
|
export const AIActions = (elements: Element[], protyle: IProtyle) => {
|
2023-03-08 22:34:46 +08:00
|
|
|
const ids: string[] = [];
|
2023-03-08 21:57:57 +08:00
|
|
|
elements.forEach(item => {
|
2023-03-08 22:34:46 +08:00
|
|
|
ids.push(item.getAttribute("data-node-id"));
|
|
|
|
|
});
|
2023-03-08 21:57:57 +08:00
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
2023-03-08 22:03:40 +08:00
|
|
|
icon: "iconSparkles",
|
2023-03-08 21:57:57 +08:00
|
|
|
label: window.siyuan.languages.ai,
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: [{
|
|
|
|
|
label: window.siyuan.languages.aiContinueWrite,
|
|
|
|
|
click() {
|
|
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Continue writing"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate,
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: [{
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_zh_CN,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [zh_CN]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_ja_JP,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [ja_JP]"}, (response) => {
|
2023-03-08 21:57:57 +08:00
|
|
|
focusByRange(protyle.toolbar.range);
|
|
|
|
|
insertHTML(response.data, protyle, true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_ko_KR,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [ko_KR]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_en_US,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [en_US]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_es_ES,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [es_ES]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_fr_FR,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [fr_FR]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiTranslate_de_DE,
|
|
|
|
|
click() {
|
2023-03-08 22:46:58 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Translate as follows to [de_DE]"}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 21:57:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}]
|
2023-03-08 22:03:40 +08:00
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiExtractSummary,
|
|
|
|
|
click() {
|
2023-03-08 22:50:40 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiExtractSummary}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 22:03:40 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
label: window.siyuan.languages.aiBrainStorm,
|
|
|
|
|
click() {
|
2023-03-08 22:50:40 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiBrainStorm}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 22:03:40 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, {
|
2023-03-08 22:46:58 +08:00
|
|
|
label: window.siyuan.languages.aiFixGrammarSpell,
|
|
|
|
|
click() {
|
2023-03-08 22:50:40 +08:00
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: window.siyuan.languages.aiFixGrammarSpell}, (response) => {
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, response.data, elements);
|
2023-03-08 22:46:58 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},{
|
2023-03-08 22:03:40 +08:00
|
|
|
label: window.siyuan.languages.aiCustomAction,
|
|
|
|
|
click() {
|
|
|
|
|
const dialog = new Dialog({
|
|
|
|
|
title: window.siyuan.languages.aiCustomAction,
|
|
|
|
|
content: `<div class="b3-dialog__content"><input class="b3-text-field fn__block" value=""></div>
|
|
|
|
|
<div class="b3-dialog__action">
|
|
|
|
|
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
|
|
|
|
|
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
|
|
|
|
|
</div>`,
|
|
|
|
|
width: isMobile() ? "80vw" : "520px",
|
|
|
|
|
});
|
|
|
|
|
const inputElement = dialog.element.querySelector("input") as HTMLInputElement;
|
|
|
|
|
const btnsElement = dialog.element.querySelectorAll(".b3-button");
|
|
|
|
|
dialog.bindInput(inputElement, () => {
|
|
|
|
|
(btnsElement[1] as HTMLButtonElement).click();
|
|
|
|
|
});
|
|
|
|
|
inputElement.focus();
|
|
|
|
|
btnsElement[0].addEventListener("click", () => {
|
|
|
|
|
dialog.destroy();
|
|
|
|
|
});
|
|
|
|
|
btnsElement[1].addEventListener("click", () => {
|
|
|
|
|
fetchPost("/api/ai/chatGPTWithAction", {
|
|
|
|
|
ids,
|
|
|
|
|
action: inputElement.value,
|
|
|
|
|
}, (response) => {
|
|
|
|
|
dialog.destroy();
|
|
|
|
|
let respContent = "";
|
|
|
|
|
if (response.data && "" !== response.data) {
|
|
|
|
|
respContent = "\n\n" + response.data;
|
|
|
|
|
}
|
2023-03-08 23:35:36 +08:00
|
|
|
fillContent(protyle, `${inputElement.value}${respContent}`, elements);
|
2023-03-08 22:03:40 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-03-08 21:57:57 +08:00
|
|
|
}]
|
|
|
|
|
}).element);
|
2023-03-08 22:34:46 +08:00
|
|
|
};
|