mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-21 11:30:42 +08:00
This commit is contained in:
parent
28adbc59b6
commit
1c4718fcb9
@ -1,6 +1,5 @@
|
|||||||
import {MenuItem} from "../menus/Menu";
|
|
||||||
import {fetchPost} from "../util/fetch";
|
import {fetchPost} from "../util/fetch";
|
||||||
import {setLastNodeRange} from "../protyle/util/selection";
|
import {focusByRange, setLastNodeRange} from "../protyle/util/selection";
|
||||||
import {insertHTML} from "../protyle/util/insertHTML";
|
import {insertHTML} from "../protyle/util/insertHTML";
|
||||||
import {Dialog} from "../dialog";
|
import {Dialog} from "../dialog";
|
||||||
import {isMobile} from "../util/functions";
|
import {isMobile} from "../util/functions";
|
||||||
@ -10,9 +9,10 @@ import {processRender} from "../protyle/util/processCode";
|
|||||||
import {highlightRender} from "../protyle/render/highlightRender";
|
import {highlightRender} from "../protyle/render/highlightRender";
|
||||||
import {Constants} from "../constants";
|
import {Constants} from "../constants";
|
||||||
import {setStorageVal} from "../protyle/util/compatibility";
|
import {setStorageVal} from "../protyle/util/compatibility";
|
||||||
import {hasClosestByClassName} from "../protyle/util/hasClosest";
|
|
||||||
import {escapeAriaLabel, escapeHtml} from "../util/escape";
|
import {escapeAriaLabel, escapeHtml} from "../util/escape";
|
||||||
import {showMessage} from "../dialog/message";
|
import {showMessage} from "../dialog/message";
|
||||||
|
import {Menu} from "../plugin/Menu";
|
||||||
|
import {upDownHint} from "../util/upDownHint";
|
||||||
|
|
||||||
export const fillContent = (protyle: IProtyle, data: string, elements: Element[]) => {
|
export const fillContent = (protyle: IProtyle, data: string, elements: Element[]) => {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@ -26,81 +26,10 @@ export const fillContent = (protyle: IProtyle, data: string, elements: Element[]
|
|||||||
highlightRender(protyle.wysiwyg.element);
|
highlightRender(protyle.wysiwyg.element);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AIActions = (elements: Element[], protyle: IProtyle) => {
|
const editDialog = (customName: string, customMemo: string) => {
|
||||||
const ids: string[] = [];
|
const dialog = new Dialog({
|
||||||
elements.forEach(item => {
|
title: window.siyuan.languages.update,
|
||||||
ids.push(item.getAttribute("data-node-id"));
|
content: `<div class="b3-dialog__content">
|
||||||
});
|
|
||||||
const customMenu: IMenu[] = [{
|
|
||||||
iconHTML: "",
|
|
||||||
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="" placeholder="${window.siyuan.languages.memo}">
|
|
||||||
<div class="fn__hr"></div>
|
|
||||||
<textarea class="b3-text-field fn__block" placeholder="${window.siyuan.languages.aiCustomAction}"></textarea>
|
|
||||||
</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.use}</button><div class="fn__space"></div>
|
|
||||||
<button class="b3-button b3-button--text">${window.siyuan.languages.save}</button>
|
|
||||||
</div>`,
|
|
||||||
width: isMobile() ? "92vw" : "520px",
|
|
||||||
});
|
|
||||||
dialog.element.setAttribute("data-key", Constants.DIALOG_AICUSTOMACTION);
|
|
||||||
const nameElement = dialog.element.querySelector("input");
|
|
||||||
const customElement = dialog.element.querySelector("textarea");
|
|
||||||
const btnsElement = dialog.element.querySelectorAll(".b3-button");
|
|
||||||
dialog.bindInput(customElement, () => {
|
|
||||||
(btnsElement[1] as HTMLButtonElement).click();
|
|
||||||
});
|
|
||||||
btnsElement[0].addEventListener("click", () => {
|
|
||||||
dialog.destroy();
|
|
||||||
});
|
|
||||||
btnsElement[1].addEventListener("click", () => {
|
|
||||||
if (!customElement.value) {
|
|
||||||
showMessage(window.siyuan.languages["_kernel"][142]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: customElement.value,
|
|
||||||
}, (response) => {
|
|
||||||
dialog.destroy();
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
btnsElement[2].addEventListener("click", () => {
|
|
||||||
if (!nameElement.value && !customElement.value) {
|
|
||||||
showMessage(window.siyuan.languages["_kernel"][142]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
window.siyuan.storage[Constants.LOCAL_AI].push({
|
|
||||||
name: nameElement.value,
|
|
||||||
memo: customElement.value
|
|
||||||
});
|
|
||||||
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
|
||||||
dialog.destroy();
|
|
||||||
});
|
|
||||||
nameElement.focus();
|
|
||||||
}
|
|
||||||
}];
|
|
||||||
if (window.siyuan.storage[Constants.LOCAL_AI].length > 0) {
|
|
||||||
customMenu.push({type: "separator"});
|
|
||||||
}
|
|
||||||
window.siyuan.storage[Constants.LOCAL_AI].forEach((item: { name: string, memo: string }) => {
|
|
||||||
customMenu.push({
|
|
||||||
iconHTML: "",
|
|
||||||
action: "iconEdit",
|
|
||||||
label: `<div aria-label="${escapeAriaLabel(item.memo)}" class="ariaLabel">${escapeHtml(item.name)}</div>`,
|
|
||||||
bind: (element) => {
|
|
||||||
element.addEventListener("click", (event) => {
|
|
||||||
if (hasClosestByClassName(event.target as Element, "b3-menu__action")) {
|
|
||||||
const dialog = new Dialog({
|
|
||||||
title: window.siyuan.languages.update,
|
|
||||||
content: `<div class="b3-dialog__content">
|
|
||||||
<input class="b3-text-field fn__block" placeholder="${window.siyuan.languages.memo}">
|
<input class="b3-text-field fn__block" placeholder="${window.siyuan.languages.memo}">
|
||||||
<div class="fn__hr"></div>
|
<div class="fn__hr"></div>
|
||||||
<textarea class="b3-text-field fn__block" placeholder="${window.siyuan.languages.aiCustomAction}"></textarea>
|
<textarea class="b3-text-field fn__block" placeholder="${window.siyuan.languages.aiCustomAction}"></textarea>
|
||||||
@ -110,217 +39,264 @@ export const AIActions = (elements: Element[], protyle: IProtyle) => {
|
|||||||
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button><div class="fn__space"></div>
|
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button><div class="fn__space"></div>
|
||||||
<button class="b3-button b3-button--error">${window.siyuan.languages.delete}</button>
|
<button class="b3-button b3-button--error">${window.siyuan.languages.delete}</button>
|
||||||
</div>`,
|
</div>`,
|
||||||
width: isMobile() ? "92vw" : "520px",
|
width: isMobile() ? "92vw" : "520px",
|
||||||
});
|
});
|
||||||
dialog.element.setAttribute("data-key", Constants.DIALOG_AIUPDATECUSTOMACTION);
|
dialog.element.setAttribute("data-key", Constants.DIALOG_AIUPDATECUSTOMACTION);
|
||||||
const nameElement = dialog.element.querySelector("input");
|
const nameElement = dialog.element.querySelector("input");
|
||||||
nameElement.value = item.name;
|
nameElement.value = customName;
|
||||||
const customElement = dialog.element.querySelector("textarea");
|
const customElement = dialog.element.querySelector("textarea");
|
||||||
const btnsElement = dialog.element.querySelectorAll(".b3-button");
|
const btnsElement = dialog.element.querySelectorAll(".b3-button");
|
||||||
dialog.bindInput(customElement, () => {
|
dialog.bindInput(customElement, () => {
|
||||||
(btnsElement[1] as HTMLButtonElement).click();
|
(btnsElement[1] as HTMLButtonElement).click();
|
||||||
});
|
});
|
||||||
customElement.value = item.memo;
|
customElement.value = customMemo;
|
||||||
btnsElement[0].addEventListener("click", () => {
|
btnsElement[0].addEventListener("click", () => {
|
||||||
dialog.destroy();
|
dialog.destroy();
|
||||||
});
|
});
|
||||||
btnsElement[1].addEventListener("click", () => {
|
btnsElement[1].addEventListener("click", () => {
|
||||||
window.siyuan.storage[Constants.LOCAL_AI].find((subItem: {
|
window.siyuan.storage[Constants.LOCAL_AI].find((subItem: {
|
||||||
name: string,
|
name: string,
|
||||||
memo: string
|
memo: string
|
||||||
}) => {
|
}) => {
|
||||||
if (item.name === subItem.name && item.memo === subItem.memo) {
|
if (customName === subItem.name && customMemo === subItem.memo) {
|
||||||
item.name = nameElement.value;
|
subItem.name = nameElement.value;
|
||||||
item.memo = customElement.value;
|
subItem.memo = customElement.value;
|
||||||
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
dialog.destroy();
|
dialog.destroy();
|
||||||
});
|
});
|
||||||
btnsElement[2].addEventListener("click", () => {
|
btnsElement[2].addEventListener("click", () => {
|
||||||
window.siyuan.storage[Constants.LOCAL_AI].find((subItem: {
|
window.siyuan.storage[Constants.LOCAL_AI].find((subItem: {
|
||||||
name: string,
|
name: string,
|
||||||
memo: string
|
memo: string
|
||||||
}, index: number) => {
|
}, index: number) => {
|
||||||
if (item.name === subItem.name && item.memo === subItem.memo) {
|
if (customName === subItem.name && customMemo === subItem.memo) {
|
||||||
window.siyuan.storage[Constants.LOCAL_AI].splice(index, 1);
|
window.siyuan.storage[Constants.LOCAL_AI].splice(index, 1);
|
||||||
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
dialog.destroy();
|
dialog.destroy();
|
||||||
});
|
});
|
||||||
nameElement.focus();
|
nameElement.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
const customDialog = (protyle: IProtyle, ids: string[], elements: Element[]) => {
|
||||||
|
const dialog = new Dialog({
|
||||||
|
title: window.siyuan.languages.aiCustomAction,
|
||||||
|
content: `<div class="b3-dialog__content">
|
||||||
|
<input class="b3-text-field fn__block" value="" placeholder="${window.siyuan.languages.memo}">
|
||||||
|
<div class="fn__hr"></div>
|
||||||
|
<textarea class="b3-text-field fn__block" placeholder="${window.siyuan.languages.aiCustomAction}"></textarea>
|
||||||
|
</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.use}</button><div class="fn__space"></div>
|
||||||
|
<button class="b3-button b3-button--text">${window.siyuan.languages.save}</button>
|
||||||
|
</div>`,
|
||||||
|
width: isMobile() ? "92vw" : "520px",
|
||||||
|
});
|
||||||
|
dialog.element.setAttribute("data-key", Constants.DIALOG_AICUSTOMACTION);
|
||||||
|
const nameElement = dialog.element.querySelector("input");
|
||||||
|
const customElement = dialog.element.querySelector("textarea");
|
||||||
|
const btnsElement = dialog.element.querySelectorAll(".b3-button");
|
||||||
|
dialog.bindInput(customElement, () => {
|
||||||
|
(btnsElement[1] as HTMLButtonElement).click();
|
||||||
|
});
|
||||||
|
btnsElement[0].addEventListener("click", () => {
|
||||||
|
dialog.destroy();
|
||||||
|
});
|
||||||
|
btnsElement[1].addEventListener("click", () => {
|
||||||
|
if (!customElement.value) {
|
||||||
|
showMessage(window.siyuan.languages["_kernel"][142]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fetchPost("/api/ai/chatGPTWithAction", {
|
||||||
|
ids,
|
||||||
|
action: customElement.value,
|
||||||
|
}, (response) => {
|
||||||
|
dialog.destroy();
|
||||||
|
fillContent(protyle, response.data, elements);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
btnsElement[2].addEventListener("click", () => {
|
||||||
|
if (!nameElement.value && !customElement.value) {
|
||||||
|
showMessage(window.siyuan.languages["_kernel"][142]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.siyuan.storage[Constants.LOCAL_AI].push({
|
||||||
|
name: nameElement.value,
|
||||||
|
memo: customElement.value
|
||||||
|
});
|
||||||
|
setStorageVal(Constants.LOCAL_AI, window.siyuan.storage[Constants.LOCAL_AI]);
|
||||||
|
dialog.destroy();
|
||||||
|
});
|
||||||
|
nameElement.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
const filterAI = (element: HTMLElement, inputElement: HTMLInputElement) => {
|
||||||
|
element.querySelectorAll(".b3-list-item").forEach(item => {
|
||||||
|
if (item.textContent.indexOf(inputElement.value) > -1) {
|
||||||
|
item.classList.remove("fn__none");
|
||||||
|
} else {
|
||||||
|
item.classList.add("fn__none");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
element.querySelectorAll('.b3-menu__separator').forEach(item => {
|
||||||
|
if (inputElement.value) {
|
||||||
|
item.classList.add("fn__none");
|
||||||
|
} else {
|
||||||
|
item.classList.remove("fn__none");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
element.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus");
|
||||||
|
element.querySelector(".b3-list-item:not(.fn__none)").classList.add("b3-list-item--focus");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AIActions = (elements: Element[], protyle: IProtyle) => {
|
||||||
|
window.siyuan.menus.menu.remove();
|
||||||
|
const ids: string[] = [];
|
||||||
|
elements.forEach(item => {
|
||||||
|
ids.push(item.getAttribute("data-node-id"));
|
||||||
|
});
|
||||||
|
const menu = new Menu("ai", () => {
|
||||||
|
focusByRange(protyle.toolbar.range);
|
||||||
|
});
|
||||||
|
let customHTML = ""
|
||||||
|
window.siyuan.storage[Constants.LOCAL_AI].forEach((item: { name: string, memo: string }) => {
|
||||||
|
customHTML += `<div data-action="${item.memo || item.name}" class="b3-list-item b3-list-item--narrow ariaLabel" aria-label="${escapeAriaLabel(item.memo)}">
|
||||||
|
<span class="b3-list-item__text">${escapeHtml(item.name)}</span>
|
||||||
|
<span data-type="edit" class="b3-list-item__action"><svg><use xlink:href="#iconEdit"></use></svg></span>
|
||||||
|
</div>`;
|
||||||
|
});
|
||||||
|
if (customHTML) {
|
||||||
|
customHTML = `<div class="b3-menu__separator"></div>${customHTML}`;
|
||||||
|
}
|
||||||
|
menu.addItem({
|
||||||
|
iconHTML: "",
|
||||||
|
type: "empty",
|
||||||
|
label: `<div class="fn__flex-column b3-menu__filter">
|
||||||
|
<input class="b3-text-field fn__flex-shrink" placeholder="${window.siyuan.languages.ai}"/>
|
||||||
|
<div class="fn__hr"></div>
|
||||||
|
<div class="b3-list fn__flex-1 b3-list--background">
|
||||||
|
<div class="b3-list-item b3-list-item--narrow b3-list-item--focus" data-action="Continue writing">
|
||||||
|
${window.siyuan.languages.aiContinueWrite}
|
||||||
|
</div>
|
||||||
|
<div class="b3-menu__separator"></div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [zh-Hans]">
|
||||||
|
${window.siyuan.languages.aiTranslate_zh_Hans}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [zh-Hant]">
|
||||||
|
${window.siyuan.languages.aiTranslate_zh_Hant}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [ja-JP]">
|
||||||
|
${window.siyuan.languages.aiTranslate_ja_JP}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [ko-KR]">
|
||||||
|
${window.siyuan.languages.aiTranslate_ko_KR}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [en-US]">
|
||||||
|
${window.siyuan.languages.aiTranslate_en_US}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [es-ES]">
|
||||||
|
${window.siyuan.languages.aiTranslate_es_ES}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [fr-FR]">
|
||||||
|
${window.siyuan.languages.aiTranslate_fr_FR}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Translate as follows to [de-DE]">
|
||||||
|
${window.siyuan.languages.aiTranslate_de_DE}
|
||||||
|
</div>
|
||||||
|
<div class="b3-menu__separator"></div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="${window.siyuan.languages.aiExtractSummary}">
|
||||||
|
${window.siyuan.languages.aiExtractSummary}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="${window.siyuan.languages.aiBrainStorm}">
|
||||||
|
${window.siyuan.languages.aiBrainStorm}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="${window.siyuan.languages.aiFixGrammarSpell}">
|
||||||
|
${window.siyuan.languages.aiFixGrammarSpell}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-action="Clear context">
|
||||||
|
${window.siyuan.languages.clearContext}
|
||||||
|
</div>
|
||||||
|
<div class="b3-list-item b3-list-item--narrow" data-type="custom">
|
||||||
|
${window.siyuan.languages.aiCustomAction}
|
||||||
|
</div>
|
||||||
|
${customHTML}
|
||||||
|
</div>
|
||||||
|
</div>`,
|
||||||
|
bind(element) {
|
||||||
|
const listElement = element.querySelector(".b3-list");
|
||||||
|
const inputElement = element.querySelector("input");
|
||||||
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
||||||
|
if (event.isComposing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const currentElement = upDownHint(listElement, event);
|
||||||
|
if (currentElement) {
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
const currentElement = listElement.querySelector(".b3-list-item--focus") as HTMLElement;
|
||||||
|
if (currentElement.dataset.type === "custom") {
|
||||||
|
customDialog(protyle, ids, elements);
|
||||||
} else {
|
} else {
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
fetchPost("/api/ai/chatGPTWithAction", {
|
||||||
ids,
|
ids,
|
||||||
action: item.memo,
|
action: currentElement.dataset.action
|
||||||
}, (response) => {
|
}, (response) => {
|
||||||
fillContent(protyle, response.data, elements);
|
fillContent(protyle, response.data, elements);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.siyuan.menus.menu.remove();
|
menu.close();
|
||||||
event.preventDefault();
|
}
|
||||||
event.stopPropagation();
|
});
|
||||||
});
|
inputElement.addEventListener("compositionend", () => {
|
||||||
}
|
filterAI(element, inputElement);
|
||||||
});
|
})
|
||||||
|
inputElement.addEventListener("input", (event: KeyboardEvent) => {
|
||||||
|
if (event.isComposing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
filterAI(element, inputElement);
|
||||||
|
});
|
||||||
|
element.addEventListener("click", (event) => {
|
||||||
|
let target = event.target as HTMLElement;
|
||||||
|
while (target && !target.isSameNode(element)) {
|
||||||
|
if (target.classList.contains("b3-list-item__action")) {
|
||||||
|
editDialog(target.previousElementSibling.textContent, target.parentElement.getAttribute("aria-label"));
|
||||||
|
event.stopPropagation();
|
||||||
|
event.preventDefault();
|
||||||
|
break;
|
||||||
|
} else if (target.classList.contains("b3-list-item")) {
|
||||||
|
if (target.dataset.type === "custom") {
|
||||||
|
customDialog(protyle, ids, elements);
|
||||||
|
} else {
|
||||||
|
fetchPost("/api/ai/chatGPTWithAction", {ids, action: target.dataset.action}, (response) => {
|
||||||
|
fillContent(protyle, response.data, elements);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
event.stopPropagation();
|
||||||
|
event.preventDefault();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
target = target.parentElement;
|
||||||
|
}
|
||||||
|
menu.close();
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
window.siyuan.menus.menu.append(new MenuItem({
|
menu.element.querySelector(".b3-menu__items").setAttribute("style", "overflow: initial");
|
||||||
icon: "iconSparkles",
|
const rect = elements[elements.length - 1].getBoundingClientRect();
|
||||||
label: window.siyuan.languages.ai,
|
menu.open({
|
||||||
type: "submenu",
|
x: rect.left,
|
||||||
submenu: [{
|
y: rect.bottom,
|
||||||
iconHTML: "",
|
h: rect.height,
|
||||||
label: window.siyuan.languages.aiContinueWrite,
|
});
|
||||||
click() {
|
menu.element.querySelector("input").focus();
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {ids, action: "Continue writing"}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate,
|
|
||||||
type: "submenu",
|
|
||||||
submenu: [{
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_zh_Hans,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [zh-Hans]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_zh_Hant,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [zh-Hant]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_ja_JP,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [ja-JP]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_ko_KR,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [ko-KR]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_en_US,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [en-US]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_es_ES,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [es-ES]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_fr_FR,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [fr-FR]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiTranslate_de_DE,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Translate as follows to [de-DE]"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiExtractSummary,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: window.siyuan.languages.aiExtractSummary
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiBrainStorm,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: window.siyuan.languages.aiBrainStorm
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.aiFixGrammarSpell,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: window.siyuan.languages.aiFixGrammarSpell
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.clearContext,
|
|
||||||
click() {
|
|
||||||
fetchPost("/api/ai/chatGPTWithAction", {
|
|
||||||
ids,
|
|
||||||
action: "Clear context"
|
|
||||||
}, (response) => {
|
|
||||||
fillContent(protyle, response.data, elements);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
iconHTML: "",
|
|
||||||
label: window.siyuan.languages.custom,
|
|
||||||
type: "submenu",
|
|
||||||
submenu: customMenu
|
|
||||||
}]
|
|
||||||
}).element);
|
|
||||||
};
|
};
|
||||||
|
@ -714,7 +714,14 @@ export class Gutter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!protyle.disabled) {
|
if (!protyle.disabled) {
|
||||||
AIActions(selectsElement, protyle);
|
window.siyuan.menus.menu.append(new MenuItem({
|
||||||
|
icon: "iconSparkles",
|
||||||
|
label: window.siyuan.languages.ai,
|
||||||
|
accelerator: window.siyuan.config.keymap.editor.general.ai.custom,
|
||||||
|
click() {
|
||||||
|
AIActions(selectsElement, protyle);
|
||||||
|
}
|
||||||
|
}).element);
|
||||||
}
|
}
|
||||||
const copyMenu: IMenu[] = [{
|
const copyMenu: IMenu[] = [{
|
||||||
label: window.siyuan.languages.copy,
|
label: window.siyuan.languages.copy,
|
||||||
@ -1175,7 +1182,14 @@ export class Gutter {
|
|||||||
}).element);
|
}).element);
|
||||||
}
|
}
|
||||||
if (!protyle.disabled && !nodeElement.classList.contains("hr")) {
|
if (!protyle.disabled && !nodeElement.classList.contains("hr")) {
|
||||||
AIActions([nodeElement], protyle);
|
window.siyuan.menus.menu.append(new MenuItem({
|
||||||
|
icon: "iconSparkles",
|
||||||
|
label: window.siyuan.languages.ai,
|
||||||
|
accelerator: window.siyuan.config.keymap.editor.general.ai.custom,
|
||||||
|
click() {
|
||||||
|
AIActions([nodeElement], protyle);
|
||||||
|
}
|
||||||
|
}).element);
|
||||||
}
|
}
|
||||||
const copyMenu = (copySubMenu(id, true, nodeElement) as IMenu[]).concat([{
|
const copyMenu = (copySubMenu(id, true, nodeElement) as IMenu[]).concat([{
|
||||||
label: window.siyuan.languages.copy,
|
label: window.siyuan.languages.copy,
|
||||||
|
@ -67,6 +67,7 @@ import {getSavePath, newFileBySelect} from "../../util/newFile";
|
|||||||
import {removeSearchMark} from "../toolbar/util";
|
import {removeSearchMark} from "../toolbar/util";
|
||||||
import {avKeydown} from "../render/av/keydown";
|
import {avKeydown} from "../render/av/keydown";
|
||||||
import {checkFold} from "../../util/noRelyPCFunction";
|
import {checkFold} from "../../util/noRelyPCFunction";
|
||||||
|
import {AIActions} from "../../ai/actions";
|
||||||
|
|
||||||
export const getContentByInlineHTML = (range: Range, cb: (content: string) => void) => {
|
export const getContentByInlineHTML = (range: Range, cb: (content: string) => void) => {
|
||||||
let html = "";
|
let html = "";
|
||||||
@ -1404,6 +1405,17 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!event.repeat && matchHotKey(window.siyuan.config.keymap.editor.general.ai.custom, event)) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
let selectsElement: HTMLElement[] = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
|
||||||
|
if (selectsElement.length === 0) {
|
||||||
|
selectsElement = [nodeElement];
|
||||||
|
}
|
||||||
|
AIActions(selectsElement, protyle)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// tab 需等待 list 和 table 处理完成
|
// tab 需等待 list 和 table 处理完成
|
||||||
if (event.key === "Tab" && isNotCtrl(event) && !event.altKey) {
|
if (event.key === "Tab" && isNotCtrl(event) && !event.altKey) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
Loading…
Reference in New Issue
Block a user