From c83a682321b3b96f45b2c1a3c4975d22daaadac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Labruy=C3=A8re?= Date: Mon, 29 Oct 2018 20:29:21 +0100 Subject: [PATCH] Generate file name if needed. (#51) --- .../create-note-modal/CreateNoteModal.html | 3 +- .../create-note-modal/CreateNoteModal.vue | 42 +++++++++---------- .../update-note-modal/UpdateNoteModal.vue | 21 +--------- 3 files changed, 24 insertions(+), 42 deletions(-) diff --git a/src/renderer/components/modals/create-note-modal/CreateNoteModal.html b/src/renderer/components/modals/create-note-modal/CreateNoteModal.html index 02dd134..3194283 100644 --- a/src/renderer/components/modals/create-note-modal/CreateNoteModal.html +++ b/src/renderer/components/modals/create-note-modal/CreateNoteModal.html @@ -11,8 +11,7 @@

Oh sorry, you can't have duplicated type="text" ref="noteName" v-model="note.name" - placeholder="Your note name" - required> + placeholder="Your note name"> diff --git a/src/renderer/components/modals/create-note-modal/CreateNoteModal.vue b/src/renderer/components/modals/create-note-modal/CreateNoteModal.vue index 654f755..97a8432 100644 --- a/src/renderer/components/modals/create-note-modal/CreateNoteModal.vue +++ b/src/renderer/components/modals/create-note-modal/CreateNoteModal.vue @@ -7,6 +7,15 @@ import languages from '@/assets/data/languages.json'; import converter from '@/converter'; + const noteNameCharacters = "abcdef0123456789"; + + const generateNoteName = () => { + let text = ""; + for (let i = 0; i < 32; i += 1) + text += noteNameCharacters.charAt(Math.floor(Math.random() * 16)); + return text; + }; + export default { name: 'cn-create-note-modal', components: { editor }, @@ -44,14 +53,18 @@ createNote() { if (!this.containsDupFiles()) { let separator = '-'; + let prefix = 'note'; if (this.gistsSelected) { separator = '.'; + prefix = 'gist'; } - this.files.forEach(file => { + let name; + this.files.forEach((file, i) => { + name = file.name || `${prefix}file${(i + 1)}`; this.note.files[ - `${file.name}${separator}${converter.languageToExtension( + `${name}${separator}${converter.languageToExtension( file.language )}` ] = file; @@ -59,6 +72,10 @@ this.note.createdAt = new Date(); this.note.updatedAt = new Date(); + if (!this.note.name || this.note.name.trim() === "") { + this.note.name = `${prefix}:${generateNoteName()}`; + } + this.addNote(this.note).then(() => { this.$parent.close(); }); @@ -98,25 +115,8 @@ computed: { ...mapGetters(['gistsSelected']), isDisabled() { - if (this.gistsSelected) { - return this.files.some( - file => - !/^[^.]*$/.test(file.name) || - !/\S/.test(file.name) || - !/\S/.test(file.language) || - !/\S/.test(file.content) - ); - } - - return ( - !/\S/.test(this.note.name) || - this.files.some( - file => - !/^[^.]*$/.test(file.name) || - !/\S/.test(file.name) || - !/\S/.test(file.language) || - !/\S/.test(file.content) - ) + return this.files.some( + file => !/\S/.test(file.content) ); }, }, diff --git a/src/renderer/components/modals/update-note-modal/UpdateNoteModal.vue b/src/renderer/components/modals/update-note-modal/UpdateNoteModal.vue index dec8998..cb5ccca 100644 --- a/src/renderer/components/modals/update-note-modal/UpdateNoteModal.vue +++ b/src/renderer/components/modals/update-note-modal/UpdateNoteModal.vue @@ -137,25 +137,8 @@ computed: { ...mapGetters(['gistsSelected']), isDisabled() { - if (this.gistsSelected) { - return this.files.some( - file => - !/^[^.]*$/.test(file.name) || - !/\S/.test(file.name) || - !/\S/.test(file.language) || - !/\S/.test(file.content) - ); - } - - return ( - !/\S/.test(this.note.name) || - this.files.some( - file => - !/^[^.]*$/.test(file.name) || - !/\S/.test(file.name) || - !/\S/.test(file.language) || - !/\S/.test(file.content) - ) + return this.files.some( + file => !/\S/.test(file.content) ); }, },