mirror of
https://github.com/talwrii/plugin-repl.git
synced 2026-07-22 11:40:27 +00:00
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { Editor, MarkdownView, Notice, App, Modal, Setting } from 'obsidian';
|
|
|
|
import { History } from './history'
|
|
|
|
export function promptCommand(app: App, history: History, editor: Editor): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
new CommandModal(app, history, (result) => {
|
|
resolve(result)
|
|
}).open()
|
|
})
|
|
}
|
|
|
|
class CommandModal extends Modal {
|
|
constructor(app: App, history: History, onSubmit: (result: string) => void) {
|
|
super(app);
|
|
this.setTitle('Execute javascript (press return)');
|
|
|
|
let enterDown = false;
|
|
let command = '';
|
|
new Setting(this.contentEl)
|
|
.setName('Javascript')
|
|
.addText((text) => {
|
|
text.inputEl.addEventListener("keydown", ({ key }) => {
|
|
if (key === 'Enter') {
|
|
enterDown = true
|
|
}
|
|
})
|
|
text.inputEl.addEventListener("keyup", ({ key }) => {
|
|
if ((key === 'Enter') && enterDown) {
|
|
this.close()
|
|
history.add(command)
|
|
onSubmit(command)
|
|
return true
|
|
} else if (key === 'ArrowUp') {
|
|
command = history.previous()
|
|
text.inputEl.value = command
|
|
} else if (key === 'ArrowDown') {
|
|
command = history.next()
|
|
text.inputEl.value = command
|
|
}
|
|
})
|
|
text.onChange((value) => {
|
|
command = value;
|
|
});
|
|
})
|
|
}
|
|
}
|