| 
									
										
										
										
											2020-11-07 15:59:37 +00: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 22:32:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | export const declaration: CommandDeclaration = { | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01:00
										 |  |  | 	name: 'selectTemplate', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | export const runtime = (comp: any): CommandRuntime => { | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01:00
										 |  |  | 	return { | 
					
						
							| 
									
										
										
										
											2020-11-12 19:13:28 +00:00
										 |  |  | 		execute: async (_context: CommandContext, noteType: string) => { | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01: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 19:13:28 +00:00
										 |  |  | 					onClose: async (answer: any) => { | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01:00
										 |  |  | 						if (answer) { | 
					
						
							|  |  |  | 							if (noteType === 'note' || noteType === 'todo') { | 
					
						
							| 
									
										
										
										
											2020-11-25 14:40:25 +00:00
										 |  |  | 								void CommandService.instance().execute('newNote', answer.value, noteType === 'todo'); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01:00
										 |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2020-11-25 14:40:25 +00:00
										 |  |  | 								void CommandService.instance().execute('insertText', TemplateUtils.render(answer.value)); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:32:39 +01:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						comp.setState({ promptOptions: null }); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; |