-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathai-text-tool.qml
56 lines (48 loc) · 1.74 KB
/
ai-text-tool.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import QtQml 2.0
import QOwnNotesTypes 1.0
/**
* This script allows some options to use the AI completer to replace the selected text
*/
Script {
/**
* Initializes the custom actions
*/
function init() {
script.registerCustomAction("run-ai-text-tool", "AI Text Tool", "", "network-server-database", true, true, false);
}
/**
* This function is invoked when a custom action is triggered
* in the menu or via button
*
* @param identifier string the identifier defined in registerCustomAction
*/
function customActionInvoked(identifier) {
if (identifier !== "run-ai-text-tool") {
return;
}
const options = ["Translate selection to English", "Summarize selected text to 3 sentences", "Fix typos in selection"];
let dialogResult = script.inputDialogGetItem(
"AI Text Tool", "Please select an action", options, 0, false);
let aiPrompt = "";
const text = script.noteTextEditSelectedText();
switch (dialogResult) {
case options[0]:
aiPrompt = "Translate the text to English";
break;
case options[1]:
aiPrompt = "Summarize text to 3 sentences";
break;
case options[2]:
aiPrompt = "Fix typos and correct grammatical errors, only return the corrected text";
break;
default:
return;
}
const aiResult = script.aiComplete(aiPrompt + ":\n\n" + text);
dialogResult = script.textDiffDialog("AI Text Tool", "Resulting text", text, aiResult);
if (dialogResult === '') {
return;
}
script.noteTextEditWrite(dialogResult);
}
}