@@ -5,6 +5,7 @@ import { DateAndTimeUtils } from "./utils/dateAndTime";
55import { doesFolderExist } from "./utils/folders" ;
66import { getTemplateFromId , getUserTemplateSelection } from "./utils/templates" ;
77import { setDefaultTemplatesView } from "./views/defaultTemplates" ;
8+ import { JoplinCommand } from "./types" ;
89
910joplin . plugins . register ( {
1011 onStart : async function ( ) {
@@ -48,46 +49,42 @@ joplin.plugins.register({
4849 const parser = new Parser ( dateAndTimeUtils , dialogViewHandle ) ;
4950
5051
52+ // Utility Functions
53+ const executeCommandWithParsedTemplate = async ( command : JoplinCommand , template : string | null ) => {
54+ const parsedTemplate = await parser . parseTemplate ( template ) ;
55+ if ( parsedTemplate ) {
56+ await joplin . commands . execute ( command , parsedTemplate ) ;
57+ }
58+ }
59+
60+ const getTemplateAndExecuteCommand = async ( command : JoplinCommand ) => {
61+ const template = await getUserTemplateSelection ( templatesFolderId ) ;
62+ await executeCommandWithParsedTemplate ( command , template ) ;
63+ }
64+
65+
5166 // Register all commands
5267 await joplin . commands . register ( {
5368 name : "createNoteFromTemplate" ,
5469 label : "Create note from template" ,
5570 execute : async ( ) => {
56- const template = await getUserTemplateSelection ( templatesFolderId ) ;
57- if ( template ) {
58- const parsedTemplate = await parser . parseTemplate ( template ) ;
59- if ( parsedTemplate ) {
60- await joplin . commands . execute ( "newNote" , parsedTemplate ) ;
61- }
62- }
71+ await getTemplateAndExecuteCommand ( JoplinCommand . NewNote ) ;
6372 }
6473 } ) ;
6574
6675 await joplin . commands . register ( {
6776 name : "createTodoFromTemplate" ,
6877 label : "Create to-do from template" ,
6978 execute : async ( ) => {
70- const template = await getUserTemplateSelection ( templatesFolderId ) ;
71- if ( template ) {
72- const parsedTemplate = await parser . parseTemplate ( template ) ;
73- if ( parsedTemplate ) {
74- await joplin . commands . execute ( "newTodo" , parsedTemplate ) ;
75- }
76- }
79+ await getTemplateAndExecuteCommand ( JoplinCommand . NewTodo ) ;
7780 }
7881 } ) ;
7982
8083 await joplin . commands . register ( {
8184 name : "insertTemplate" ,
8285 label : "Insert template" ,
8386 execute : async ( ) => {
84- const template = await getUserTemplateSelection ( templatesFolderId ) ;
85- if ( template ) {
86- const parsedTemplate = await parser . parseTemplate ( template ) ;
87- if ( parsedTemplate ) {
88- await joplin . commands . execute ( "insertText" , parsedTemplate ) ;
89- }
90- }
87+ await getTemplateAndExecuteCommand ( JoplinCommand . InsertText ) ;
9188 }
9289 } ) ;
9390
@@ -136,11 +133,7 @@ joplin.plugins.register({
136133 execute : async ( ) => {
137134 const template = await getTemplateFromId ( await joplin . settings . value ( "defaultNoteTemplateId" ) ) ;
138135 if ( template ) {
139- const parsedTemplate = await parser . parseTemplate ( template . body ) ;
140- if ( parsedTemplate ) {
141- await joplin . commands . execute ( "newNote" , parsedTemplate ) ;
142- }
143- return ;
136+ return await executeCommandWithParsedTemplate ( JoplinCommand . NewNote , template . body ) ;
144137 }
145138 await joplin . views . dialogs . showMessageBox ( "No default note template is set." ) ;
146139 }
@@ -152,11 +145,7 @@ joplin.plugins.register({
152145 execute : async ( ) => {
153146 const template = await getTemplateFromId ( await joplin . settings . value ( "defaultTodoTemplateId" ) ) ;
154147 if ( template ) {
155- const parsedTemplate = await parser . parseTemplate ( template . body ) ;
156- if ( parsedTemplate ) {
157- await joplin . commands . execute ( "newTodo" , parsedTemplate ) ;
158- }
159- return ;
148+ return await executeCommandWithParsedTemplate ( JoplinCommand . NewTodo , template . body ) ;
160149 }
161150 await joplin . views . dialogs . showMessageBox ( "No default to-do template is set." ) ;
162151 }
0 commit comments