2020-11-07 17:59:37 +02:00
|
|
|
import CommandService, { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
const TemplateUtils = require('@joplin/lib/TemplateUtils');
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'selectTemplate',
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (comp: any): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (_context: CommandContext, noteType: string) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
comp.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Template file:'),
|
|
|
|
inputType: 'dropdown',
|
|
|
|
value: comp.props.templates[0], // Need to start with some value
|
|
|
|
autocomplete: comp.props.templates,
|
2020-11-12 21:13:28 +02:00
|
|
|
onClose: async (answer: any) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
if (answer) {
|
|
|
|
if (noteType === 'note' || noteType === 'todo') {
|
2020-11-25 16:40:25 +02:00
|
|
|
void CommandService.instance().execute('newNote', answer.value, noteType === 'todo');
|
2020-07-03 23:32:39 +02:00
|
|
|
} else {
|
2020-11-25 16:40:25 +02:00
|
|
|
void CommandService.instance().execute('insertText', TemplateUtils.render(answer.value));
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.setState({ promptOptions: null });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|