1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
<!--
- @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<Modal v-if="opened"
:clear-view-delay="-1"
class="templates-picker"
size="large"
@close="close">
<form class="templates-picker__form"
:style="style"
@submit.prevent.stop="onSubmit">
<h2>{{ t('files', 'Pick a template for {name}', { name: nameWithoutExt }) }}</h2>
<!-- Templates list -->
<ul class="templates-picker__list">
<TemplatePreview v-bind="emptyTemplate"
:checked="checked === emptyTemplate.fileid"
@check="onCheck" />
<TemplatePreview v-for="template in provider.templates"
:key="template.fileid"
v-bind="template"
:checked="checked === template.fileid"
:ratio="provider.ratio"
@check="onCheck" />
</ul>
<!-- Cancel and submit -->
<div class="templates-picker__buttons">
<button @click="close">
{{ t('files', 'Cancel') }}
</button>
<input type="submit"
class="primary"
:value="t('files', 'Create')"
:aria-label="t('files', 'Create a new file with the selected template')">
</div>
</form>
<EmptyContent v-if="loading" class="templates-picker__loading" icon="icon-loading">
{{ t('files', 'Creating file') }}
</EmptyContent>
</Modal>
</template>
<script>
import { normalize } from 'path'
import { showError } from '@nextcloud/dialogs'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Modal from '@nextcloud/vue/dist/Components/Modal'
import { getCurrentDirectory } from '../utils/davUtils'
import { createFromTemplate, getTemplates } from '../services/Templates'
import TemplatePreview from '../components/TemplatePreview'
const border = 2
const margin = 8
const width = margin * 20
export default {
name: 'TemplatePicker',
components: {
EmptyContent,
Modal,
TemplatePreview,
},
props: {
logger: {
type: Object,
required: true,
},
},
data() {
return {
// Check empty template by default
checked: -1,
loading: false,
name: null,
opened: false,
provider: null,
}
},
computed: {
/**
* Strip away extension from name
*
* @return {string}
*/
nameWithoutExt() {
return this.name.indexOf('.') > -1
? this.name.split('.').slice(0, -1).join('.')
: this.name
},
emptyTemplate() {
return {
basename: t('files', 'Blank'),
fileid: -1,
filename: this.t('files', 'Blank'),
hasPreview: false,
mime: this.provider?.mimetypes[0] || this.provider?.mimetypes,
}
},
selectedTemplate() {
return this.provider.templates.find(template => template.fileid === this.checked)
},
/**
* Style css vars bin,d
*
* @return {object}
*/
style() {
return {
'--margin': margin + 'px',
'--width': width + 'px',
'--border': border + 'px',
'--fullwidth': width + 2 * margin + 2 * border + 'px',
'--height': this.provider.ratio ? Math.round(width / this.provider.ratio) + 'px' : null,
}
},
},
methods: {
/**
* Open the picker
*
* @param {string} name the file name to create
* @param {object} provider the template provider picked
*/
async open(name, provider) {
this.checked = this.emptyTemplate.fileid
this.name = name
this.provider = provider
const templates = await getTemplates()
const fetchedProvider = templates.find((fetchedProvider) => fetchedProvider.app === provider.app && fetchedProvider.label === provider.label)
if (fetchedProvider === null) {
throw new Error('Failed to match provider in results')
}
this.provider = fetchedProvider
// If there is no templates available, just create an empty file
if (fetchedProvider.templates.length === 0) {
this.onSubmit()
return
}
// Else, open the picker
this.opened = true
},
/**
* Close the picker and reset variables
*/
close() {
this.checked = this.emptyTemplate.fileid
this.loading = false
this.name = null
this.opened = false
this.provider = null
},
/**
* Manages the radio template picker change
*
* @param {number} fileid the selected template file id
*/
onCheck(fileid) {
this.checked = fileid
},
async onSubmit() {
this.loading = true
const currentDirectory = getCurrentDirectory()
const fileList = OCA?.Files?.App?.currentFileList
// If the file doesn't have an extension, add the default one
if (this.nameWithoutExt === this.name) {
this.logger.debug('Fixed invalid filename', { name: this.name, extension: this.provider?.extension })
this.name = this.name + this.provider?.extension
}
try {
const fileInfo = await createFromTemplate(
normalize(`${currentDirectory}/${this.name}`),
this.selectedTemplate?.filename,
this.selectedTemplate?.templateType,
)
this.logger.debug('Created new file', fileInfo)
const data = await fileList?.addAndFetchFileInfo(this.name).then((status, data) => data)
const model = new OCA.Files.FileInfoModel(data, {
filesClient: fileList?.filesClient,
})
// Run default action
const fileAction = OCA.Files.fileActions.getDefaultFileAction(fileInfo.mime, 'file', OC.PERMISSION_ALL)
fileAction.action(fileInfo.basename, {
$file: fileList?.findFileEl(this.name),
dir: currentDirectory,
fileList,
fileActions: fileList?.fileActions,
fileInfoModel: model,
})
this.close()
} catch (error) {
this.logger.error('Error while creating the new file from template')
console.error(error)
showError(this.t('files', 'Unable to create new file from template'))
} finally {
this.loading = false
}
},
},
}
</script>
<style lang="scss" scoped>
.templates-picker {
&__form {
padding: calc(var(--margin) * 2);
// Will be handled by the buttons
padding-bottom: 0;
h2 {
text-align: center;
font-weight: bold;
margin: var(--margin) 0 calc(var(--margin) * 2);
}
}
&__list {
display: grid;
grid-gap: calc(var(--margin) * 2);
grid-auto-columns: 1fr;
// We want maximum 5 columns. Putting 6 as we don't count the grid gap. So it will always be lower than 6
max-width: calc(var(--fullwidth) * 6);
grid-template-columns: repeat(auto-fit, var(--fullwidth));
// Make sure all rows are the same height
grid-auto-rows: 1fr;
// Center the columns set
justify-content: center;
}
&__buttons {
display: flex;
justify-content: space-between;
padding: calc(var(--margin) * 2) var(--margin);
position: sticky;
bottom: 0;
background-image: linear-gradient(0, var(--gradient-main-background));
button, input[type='submit'] {
height: 44px;
}
}
// Make sure we're relative for the loading emptycontent on top
::v-deep .modal-container {
position: relative;
height: fit-content;
}
&__loading {
position: absolute;
top: 0;
left: 0;
justify-content: center;
width: 100%;
height: 100%;
margin: 0;
background-color: var(--color-main-background-translucent);
}
}
</style>
|