2323import { getLoggerBuilder } from '@nextcloud/logger'
2424import { loadState } from '@nextcloud/initial-state'
2525import { translate as t , translatePlural as n } from '@nextcloud/l10n'
26+ import { generateOcsUrl } from '@nextcloud/router'
27+ import { getCurrentDirectory } from './utils/davUtils'
28+ import axios from '@nextcloud/axios'
2629import Vue from 'vue'
2730
2831import TemplatePickerView from './views/TemplatePicker'
32+ import { getCurrentUser } from '@nextcloud/auth'
33+ import { showError } from '@nextcloud/dialogs'
2934
3035// Set up logger
3136const logger = getLoggerBuilder ( )
@@ -47,8 +52,10 @@ TemplatePickerRoot.id = 'template-picker'
4752document . body . appendChild ( TemplatePickerRoot )
4853
4954// Retrieve and init templates
50- const templates = loadState ( 'files' , 'templates' , [ ] )
55+ let templates = loadState ( 'files' , 'templates' , [ ] )
56+ let templatesPath = loadState ( 'files' , 'templates_path' , false )
5157logger . debug ( 'Templates providers' , templates )
58+ logger . debug ( 'Templates folder' , { templatesPath } )
5259
5360// Init vue app
5461const View = Vue . extend ( TemplatePickerView )
@@ -60,33 +67,77 @@ const TemplatePicker = new View({
6067} )
6168TemplatePicker . $mount ( '#template-picker' )
6269
63- // Init template engine after load
70+ // Init template engine after load to make sure it's the last injected entry
6471window . addEventListener ( 'DOMContentLoaded' , function ( ) {
65- // Init template files menu
66- templates . forEach ( ( provider , index ) => {
67-
68- const newTemplatePlugin = {
72+ if ( ! templatesPath ) {
73+ logger . debug ( 'Templates folder not initialized' )
74+ const initTemplatesPlugin = {
6975 attach ( menu ) {
70- const fileList = menu . fileList
71-
72- // only attach to main file list, public view is not supported yet
73- if ( fileList . id !== 'files' && fileList . id !== 'files.public' ) {
74- return
75- }
76-
7776 // register the new menu entry
7877 menu . addMenuEntry ( {
79- id : ` template-new- ${ provider . app } - ${ index } ` ,
80- displayName : provider . label ,
81- templateName : provider . label + provider . extension ,
82- iconClass : provider . iconClass || 'icon-file ' ,
78+ id : ' template-init' ,
79+ displayName : t ( 'files' , 'Set up templates folder' ) ,
80+ templateName : t ( 'files' , 'Templates' ) ,
81+ iconClass : 'icon-template-add ' ,
8382 fileType : 'file' ,
8483 actionHandler ( name ) {
85- TemplatePicker . open ( name , provider )
84+ initTemplatesFolder ( name )
8685 } ,
8786 } )
8887 } ,
8988 }
90- OC . Plugins . register ( 'OCA.Files.NewFileMenu' , newTemplatePlugin )
91- } )
89+ OC . Plugins . register ( 'OCA.Files.NewFileMenu' , initTemplatesPlugin )
90+ }
91+ } )
92+
93+ // Init template files menu
94+ templates . forEach ( ( provider , index ) => {
95+ const newTemplatePlugin = {
96+ attach ( menu ) {
97+ const fileList = menu . fileList
98+
99+ // only attach to main file list, public view is not supported yet
100+ if ( fileList . id !== 'files' && fileList . id !== 'files.public' ) {
101+ return
102+ }
103+
104+ // register the new menu entry
105+ menu . addMenuEntry ( {
106+ id : `template-new-${ provider . app } -${ index } ` ,
107+ displayName : provider . label ,
108+ templateName : provider . label + provider . extension ,
109+ iconClass : provider . iconClass || 'icon-file' ,
110+ fileType : 'file' ,
111+ actionHandler ( name ) {
112+ TemplatePicker . open ( name , provider )
113+ } ,
114+ } )
115+ } ,
116+ }
117+ OC . Plugins . register ( 'OCA.Files.NewFileMenu' , newTemplatePlugin )
92118} )
119+
120+ /**
121+ * Init the template directory
122+ *
123+ * @param {string } name the templates folder name
124+ */
125+ const initTemplatesFolder = async function ( name ) {
126+ const templatePath = ( getCurrentDirectory ( ) + `/${ name } ` ) . replace ( '//' , '/' )
127+ try {
128+ logger . debug ( 'Initializing the templates directory' , { templatePath } )
129+ const response = await axios . post ( generateOcsUrl ( 'apps/files/api/v1/templates' , 2 ) + 'path' , {
130+ templatePath,
131+ copySystemTemplates : true ,
132+ } )
133+
134+ // Go to template directory
135+ OCA . Files . App . currentFileList . changeDirectory ( templatePath , true , true )
136+
137+ templates = response . data . ocs . data . templates
138+ templatesPath = response . data . ocs . data . template_path
139+ } catch ( error ) {
140+ logger . error ( 'Unable to initialize the templates directory' )
141+ showError ( t ( 'files' , 'Unable to initialize the templates directory' ) )
142+ }
143+ }
0 commit comments