Skip to content

Commit

Permalink
feat(ui): folder explorer: create new folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Jun 17, 2018
1 parent 6c4ebb0 commit ccde77c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/@vue/cli-ui/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
"placeholder": "Enter the full path to a folder",
"empty": "No favorite folders yet.",
"show-hidden": "Show hidden folders"
},
"new-folder": {
"action": "New folder",
"title": "Create new folder",
"field": {
"title": "New folder",
"subtitle": "You can use the folder separator to create multiple nested folders at once."
},
"cancel": "Cancel",
"create": "Create"
}
},
"list-item-info": {
Expand Down
71 changes: 70 additions & 1 deletion packages/@vue/cli-ui/src/components/FolderExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
class="icon-button"
/>

<VueDropdownButton
:label="$t('components.folder-explorer.new-folder.action')"
icon-left="create_new_folder"
@click="showNewFolder = true"
/>

<VueSwitch
icon="visibility"
v-model="showHidden"
Expand All @@ -141,16 +147,55 @@
/>
</template>
</div>

<VueModal
v-if="showNewFolder"
:title="$t('components.folder-explorer.new-folder.title')"
class="small new-folder-modal"
@close="showNewFolder = false"
>
<div class="default-body">
<VueFormField
:title="$t('components.folder-explorer.new-folder.field.title')"
:subtitle="$t('components.folder-explorer.new-folder.field.subtitle')"
>
<VueInput
v-model="newFolderName"
icon-left="folder"
@keyup.enter="createFolder()"
/>
</VueFormField>
</div>

<div slot="footer" class="actions space-between">
<VueButton
:label="$t('components.folder-explorer.new-folder.cancel')"
class="flat close"
@click="showNewFolder = false"
/>

<VueButton
:label="$t('components.folder-explorer.new-folder.create')"
icon-left="create_new_folder"
class="primary save"
:disabled="!newFolderValid"
@click="createFolder()"
/>
</div>
</VueModal>
</div>
</template>

<script>
import { isValidMultiName } from '../util/folders'
import FOLDER_CURRENT from '../graphql/folderCurrent.gql'
import FOLDERS_FAVORITE from '../graphql/foldersFavorite.gql'
import FOLDER_OPEN from '../graphql/folderOpen.gql'
import FOLDER_OPEN_PARENT from '../graphql/folderOpenParent.gql'
import FOLDER_SET_FAVORITE from '../graphql/folderSetFavorite.gql'
import PROJECT_CWD_RESET from '../graphql/projectCwdReset.gql'
import FOLDER_CREATE from '../graphql/folderCreate.gql'
const SHOW_HIDDEN = 'vue-ui.show-hidden-folders'
Expand All @@ -163,7 +208,9 @@ export default {
editedPath: '',
folderCurrent: {},
foldersFavorite: [],
showHidden: localStorage.getItem(SHOW_HIDDEN) === 'true'
showHidden: localStorage.getItem(SHOW_HIDDEN) === 'true',
showNewFolder: false,
newFolderName: ''
}
},
Expand All @@ -181,6 +228,12 @@ export default {
foldersFavorite: FOLDERS_FAVORITE
},
computed: {
newFolderValid () {
return isValidMultiName(this.newFolderName)
}
},
watch: {
showHidden (value) {
if (value) {
Expand Down Expand Up @@ -311,6 +364,22 @@ export default {
if (startIndex < path.length) addPart(path.length)
return parts
},
async createFolder () {
if (!this.newFolderValid) return
const result = await this.$apollo.mutate({
mutation: FOLDER_CREATE,
variables: {
name: this.newFolderName
}
})
this.openFolder(result.data.folderCreate.path)
this.newFolderName = ''
this.showNewFolder = false
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/@vue/cli-ui/src/graphql-api/connectors/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ async function deleteFolder (file) {
await fs.remove(file)
}

function createFolder (name, context) {
const file = path.join(cwd.get(), name)
fs.mkdirpSync(file)
return generateFolder(file, context)
}

module.exports = {
isDirectory,
getCurrent,
Expand All @@ -148,5 +154,6 @@ module.exports = {
isFavorite,
listFavorite,
setFavorite,
delete: deleteFolder
delete: deleteFolder,
create: createFolder
}
4 changes: 3 additions & 1 deletion packages/@vue/cli-ui/src/graphql-api/schema/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extend type Mutation {
folderOpen (path: String!): Folder
folderOpenParent: Folder
folderSetFavorite (path: String!, favorite: Boolean!): Folder
folderCreate(name: String!): Folder
}
type Folder {
Expand Down Expand Up @@ -47,6 +48,7 @@ exports.resolvers = {
folderSetFavorite: (root, args, context) => folders.setFavorite({
file: args.path,
favorite: args.favorite
}, context)
}, context),
folderCreate: (root, { name }, context) => folders.create(name, context)
}
}
7 changes: 7 additions & 0 deletions packages/@vue/cli-ui/src/graphql/folderCreate.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "./folderCurrentFragment.gql"

mutation folderCreate ($name: String!) {
folderCreate (name: $name) {
...folderCurrent
}
}
5 changes: 5 additions & 0 deletions packages/@vue/cli-ui/src/util/folders.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export function isValidName (name) {
return !name.match(/[/@\s+%:]/) && encodeURIComponent(name) === name
}

export function isValidMultiName (name) {
name = name.replace(/\\/g, '/')
return name.split('/').every(isValidName)
}

0 comments on commit ccde77c

Please sign in to comment.