Fix a bug of text insertion with selected text

This commit is contained in:
Simon Yang 2023-03-15 17:10:17 +11:00
parent 9a62867e62
commit 592f819a47

18
main.ts
View file

@ -69,8 +69,6 @@ export default class AICommanderPlugin extends Plugin {
} }
const messages = []; const messages = [];
console.log(contextPrompt);
if (contextPrompt) { if (contextPrompt) {
messages.push({ messages.push({
@ -340,7 +338,7 @@ export default class AICommanderPlugin extends Plugin {
} }
getNextEmptyLine(editor: Editor) { getNextEmptyLine(editor: Editor) {
let line = editor.getCursor().line; let line = editor.getCursor('to').line;
while (editor.getLine(line).trim() !== '') line++; while (editor.getLine(line).trim() !== '') line++;
if (line == editor.lastLine()) { if (line == editor.lastLine()) {
@ -351,22 +349,23 @@ export default class AICommanderPlugin extends Plugin {
return line; return line;
} }
processGeneratedText(editor: Editor, data: any) { processGeneratedText(editor: Editor, data: any, lineToInsert: number) {
new Notice('Text Generated.'); new Notice('Text Generated.');
const nextEmptyLine = this.getNextEmptyLine(editor); editor.setLine(lineToInsert, '\n\n' + data.text.trim() + '\n\n');
editor.setLine(nextEmptyLine, '\n\n' + data.text.trim() + '\n\n');
} }
commandGenerateText(editor: Editor, prompt: string) { commandGenerateText(editor: Editor, prompt: string) {
const lineToInsert = this.getNextEmptyLine(editor);
new Notice("Generating text..."); new Notice("Generating text...");
this.generateText(prompt).then((data) => { this.generateText(prompt).then((data) => {
this.processGeneratedText(editor, data); this.processGeneratedText(editor, data, lineToInsert);
}).catch(error => { }).catch(error => {
new Notice(error.message); new Notice(error.message);
}); });
} }
commandGenerateTextWithPdf(editor: Editor, prompt: string) { commandGenerateTextWithPdf(editor: Editor, prompt: string) {
const lineToInsert = this.getNextEmptyLine(editor);
const position = editor.getCursor(); const position = editor.getCursor();
const text = editor.getRange({line: 0, ch: 0}, position); const text = editor.getRange({line: 0, ch: 0}, position);
const regex = [/(?<=\[(.*)]\()(([^[\]])+)\.pdf(?=\))/g, const regex = [/(?<=\[(.*)]\()(([^[\]])+)\.pdf(?=\))/g,
@ -375,7 +374,7 @@ export default class AICommanderPlugin extends Plugin {
console.log('path', path); console.log('path', path);
new Notice(`Generating text in context of ${path}...`); new Notice(`Generating text in context of ${path}...`);
this.generateTextWithPdf(prompt, path).then((data) => { this.generateTextWithPdf(prompt, path).then((data) => {
this.processGeneratedText(editor, data); this.processGeneratedText(editor, data, lineToInsert);
}).catch(error => { }).catch(error => {
new Notice(error.message); new Notice(error.message);
}); });
@ -385,10 +384,11 @@ export default class AICommanderPlugin extends Plugin {
} }
commandGenerateImage(editor: Editor, prompt: string) { commandGenerateImage(editor: Editor, prompt: string) {
const lineToInsert = this.getNextEmptyLine(editor);
new Notice("Generating image..."); new Notice("Generating image...");
this.generateImage(prompt).then((data) => { this.generateImage(prompt).then((data) => {
new Notice('Image Generated.'); new Notice('Image Generated.');
this.processGeneratedText(editor, data); this.processGeneratedText(editor, data, lineToInsert);
}).catch(error => { }).catch(error => {
new Notice(error.message); new Notice(error.message);
}); });