From ade862304808b5f40a97cda4087a6d13014d3923 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 31 Aug 2022 07:54:31 +0200 Subject: [PATCH] Shamelessly copy the good work of Skjnldsv from the files app Signed-off-by: Joas Schilling --- .../NewMessageForm/NewMessageForm.vue | 112 ++++++-- .../NewMessageForm/TemplatePreview.vue | 240 ++++++++++++++++++ src/services/filesSharingServices.js | 8 +- 3 files changed, 342 insertions(+), 18 deletions(-) create mode 100644 src/components/NewMessageForm/TemplatePreview.vue diff --git a/src/components/NewMessageForm/NewMessageForm.vue b/src/components/NewMessageForm/NewMessageForm.vue index 279672c3d..c3691de5e 100644 --- a/src/components/NewMessageForm/NewMessageForm.vue +++ b/src/components/NewMessageForm/NewMessageForm.vue @@ -162,13 +162,15 @@

{{ t('spreed', 'Create and share a new file') }}

-
+ + + +
+ + {{ t('spreed', 'Close') }} + + + {{ t('spreed', 'Create file') }} + +
-
- - {{ t('spreed', 'Close') }} - - - {{ t('spreed', 'Create file') }} - -
@@ -216,6 +234,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js' import Folder from 'vue-material-design-icons/Folder.vue' import Upload from 'vue-material-design-icons/Upload.vue' import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js' +import TemplatePreview from './TemplatePreview.vue' const picker = getFilePickerBuilder(t('spreed', 'File to share')) .setMultiSelect(false) @@ -224,6 +243,10 @@ const picker = getFilePickerBuilder(t('spreed', 'File to share')) .allowDirectories() .build() +const border = 2 +const margin = 8 +const width = margin * 20 + export default { name: 'NewMessageForm', @@ -245,6 +268,7 @@ export default { NcModal, Folder, Upload, + TemplatePreview, NcTextField, }, @@ -266,6 +290,9 @@ export default { showTextFileDialog: false, textFileTitle: t('spreed', 'New file'), newFileError: '', + + // Check empty template by default + checked: -1, } }, @@ -372,6 +399,34 @@ export default { fileTemplateOptions() { return this.$store.getters.getFileTemplates() }, + + fileTemplate() { + return this.fileTemplateOptions[this.showTextFileDialog] + }, + + emptyTemplate() { + return { + basename: t('files', 'Blank'), + fileid: -1, + filename: t('files', 'Blank'), + hasPreview: false, + mime: this.fileTemplate?.mimetypes[0] || this.fileTemplate?.mimetypes, + } + }, + + selectedTemplate() { + return this.fileTemplate.templates.find(template => template.fileid === this.checked) + }, + + style() { + return { + '--margin': margin + 'px', + '--width': width + 'px', + '--border': border + 'px', + '--fullwidth': width + 2 * margin + 2 * border + 'px', + '--height': this.fileTemplate.ratio ? Math.round(width / this.fileTemplate.ratio) + 'px' : null, + } + }, }, watch: { @@ -656,19 +711,31 @@ export default { this.showSimplePollsEditor = value }, + /** + * Manages the radio template picker change + * + * @param {number} fileid the selected template file id + */ + onCheck(fileid) { + this.checked = fileid + }, + // Create text file and share it to a conversation async handleCreateTextFile() { this.newFileError = '' let filePath = this.$store.getters.getAttachmentFolder() + '/' + this.textFileTitle.replace('/', '') - const fileTemplate = this.fileTemplateOptions[this.showTextFileDialog] - if (!filePath.endsWith(fileTemplate.extension)) { - filePath += fileTemplate.extension + if (!filePath.endsWith(this.fileTemplate.extension)) { + filePath += this.fileTemplate.extension } let fileData try { - const response = await createTextFile(filePath) + const response = await createTextFile( + filePath, + this.selectedTemplate?.filename, + this.selectedTemplate?.templateType, + ) fileData = response.data.ocs.data } catch (error) { console.error('Error while creating file', error) @@ -813,6 +880,19 @@ export default { &__form { width: 100%; + + .templates-picker__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; + } } } diff --git a/src/components/NewMessageForm/TemplatePreview.vue b/src/components/NewMessageForm/TemplatePreview.vue new file mode 100644 index 000000000..88a58530c --- /dev/null +++ b/src/components/NewMessageForm/TemplatePreview.vue @@ -0,0 +1,240 @@ + + + + + + + + diff --git a/src/services/filesSharingServices.js b/src/services/filesSharingServices.js index 64f28b2f9..218308ba7 100644 --- a/src/services/filesSharingServices.js +++ b/src/services/filesSharingServices.js @@ -62,12 +62,16 @@ const getFileTemplates = async () => { /** * Share a text file to a conversation * - * @param { string } filePath the file path + * @param {string} filePath The new file destination path + * @param {string} templatePath The template source path + * @param {string} templateType The template type e.g 'user' * @return { object } the file object */ -const createTextFile = async function(filePath) { +const createTextFile = async function(filePath, templatePath, templateType) { return await axios.post(generateOcsUrl('apps/files/api/v1/templates/create'), { filePath, + templatePath, + templateType, }) }