2017-07-31 09:04:01 +00:00
|
|
|
<template>
|
2017-08-04 20:46:06 +00:00
|
|
|
<div class="explorer-node" :class="{'explorer-node--selected': isSelected, 'explorer-node--open': isOpen, 'explorer-node--drag-target': isDragTargetFolder}" @dragover.prevent @dragenter.stop="setDragTarget(node.item.id)" @dragleave.stop="isDragTarget && setDragTargetId()" @drop.prevent.stop="onDrop">
|
2017-08-03 17:08:12 +00:00
|
|
|
<div v-if="isEditing" class="explorer-node__item-editor" :class="['explorer-node__item-editor--' + node.item.type]" :style="{'padding-left': leftPadding}">
|
2017-07-31 09:04:01 +00:00
|
|
|
<input type="text" class="text-input" v-focus @blur="submitEdit()" @keyup.enter="submitEdit()" @keyup.esc="submitEdit(true)" v-model="editingNodeName">
|
|
|
|
</div>
|
2017-08-03 17:08:12 +00:00
|
|
|
<div v-else class="explorer-node__item" :class="['explorer-node__item--' + node.item.type]" :style="{'padding-left': leftPadding}" @click="select(node.item.id)" draggable="true" @dragstart.stop="setDragSourceId" @dragend.stop="setDragTargetId()">
|
2017-07-31 09:04:01 +00:00
|
|
|
{{node.item.name}}
|
|
|
|
</div>
|
2017-08-03 17:08:12 +00:00
|
|
|
<div class="explorer-node__children" v-if="node.isFolder && isOpen">
|
2017-07-31 09:04:01 +00:00
|
|
|
<explorer-node v-for="node in node.folders" :key="node.item.id" :node="node" :depth="depth + 1"></explorer-node>
|
|
|
|
<div v-if="newChild" class="explorer-node__new-child" :class="['explorer-node__new-child--' + newChild.item.type]" :style="{'padding-left': childLeftPadding}">
|
|
|
|
<input type="text" class="text-input" v-focus @blur="submitNewChild()" @keyup.enter="submitNewChild()" @keyup.esc="submitNewChild(true)" v-model.trim="newChildName">
|
|
|
|
</div>
|
|
|
|
<explorer-node v-for="node in node.files" :key="node.item.id" :node="node" :depth="depth + 1"></explorer-node>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2017-08-03 17:08:12 +00:00
|
|
|
import { mapMutations, mapActions } from 'vuex';
|
2017-07-31 09:04:01 +00:00
|
|
|
import utils from '../services/utils';
|
|
|
|
import defaultContent from '../data/defaultContent.md';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'explorer-node',
|
|
|
|
props: ['node', 'depth'],
|
|
|
|
data: () => ({
|
|
|
|
editingValue: '',
|
|
|
|
}),
|
|
|
|
computed: {
|
|
|
|
leftPadding() {
|
|
|
|
return `${this.depth * 15}px`;
|
|
|
|
},
|
|
|
|
childLeftPadding() {
|
|
|
|
return `${(this.depth + 1) * 15}px`;
|
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
isSelected() {
|
2017-07-31 09:04:01 +00:00
|
|
|
return this.$store.getters['explorer/selectedNode'] === this.node;
|
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
isEditing() {
|
2017-07-31 09:04:01 +00:00
|
|
|
return this.$store.getters['explorer/editingNode'] === this.node;
|
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
isDragTarget() {
|
|
|
|
return this.$store.getters['explorer/dragTargetNode'] === this.node;
|
|
|
|
},
|
|
|
|
isDragTargetFolder() {
|
|
|
|
return this.$store.getters['explorer/dragTargetNodeFolder'] === this.node;
|
|
|
|
},
|
|
|
|
isOpen() {
|
2017-07-31 09:04:01 +00:00
|
|
|
return this.$store.state.explorer.openNodes[this.node.item.id] || this.node.isRoot;
|
|
|
|
},
|
|
|
|
newChild() {
|
|
|
|
return this.$store.getters['explorer/newChildNodeParent'] === this.node
|
|
|
|
&& this.$store.state.explorer.newChildNode;
|
|
|
|
},
|
|
|
|
newChildName: {
|
|
|
|
get() {
|
|
|
|
return this.$store.state.explorer.newChildNode.item.name;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.$store.commit('explorer/setNewItemName', value && value.slice(0, 250));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
editingNodeName: {
|
|
|
|
get() {
|
|
|
|
return this.$store.getters['explorer/editingNode'].item.name;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.editingValue = value.trim();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2017-08-03 17:08:12 +00:00
|
|
|
...mapMutations('explorer', [
|
|
|
|
'setDragTargetId',
|
|
|
|
]),
|
|
|
|
...mapActions('explorer', [
|
|
|
|
'setDragTarget',
|
|
|
|
]),
|
2017-07-31 09:04:01 +00:00
|
|
|
select(id) {
|
2017-08-03 17:08:12 +00:00
|
|
|
const node = this.$store.getters['explorer/nodeMap'][id];
|
|
|
|
if (node) {
|
|
|
|
this.$store.commit('explorer/setSelectedId', id);
|
|
|
|
if (node.isFolder) {
|
|
|
|
this.$store.commit('explorer/toggleOpenNode', id);
|
|
|
|
} else {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('file/setCurrentId', id);
|
2017-08-03 17:08:12 +00:00
|
|
|
}
|
2017-07-31 09:04:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
submitNewChild(cancel) {
|
|
|
|
const newChildNode = this.$store.state.explorer.newChildNode;
|
2017-08-03 17:08:12 +00:00
|
|
|
if (!cancel && !newChildNode.isNil && newChildNode.item.name) {
|
2017-07-31 09:04:01 +00:00
|
|
|
const id = utils.uid();
|
|
|
|
if (newChildNode.isFolder) {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('folder/setItem', {
|
2017-07-31 09:04:01 +00:00
|
|
|
...newChildNode.item,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
} else {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('content/setItem', {
|
|
|
|
id: `${id}/content`,
|
2017-07-31 09:04:01 +00:00
|
|
|
text: defaultContent,
|
|
|
|
});
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('file/setItem', {
|
2017-07-31 09:04:01 +00:00
|
|
|
...newChildNode.item,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.select(id);
|
|
|
|
}
|
|
|
|
this.$store.commit('explorer/setNewItem', null);
|
|
|
|
},
|
|
|
|
submitEdit(cancel) {
|
|
|
|
const id = this.$store.getters['explorer/editingNode'].item.id;
|
|
|
|
const value = this.editingValue;
|
|
|
|
if (!cancel && id && value) {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('file/patchItem', {
|
2017-07-31 09:04:01 +00:00
|
|
|
id,
|
|
|
|
name: value.slice(0, 250),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.$store.commit('explorer/setEditingId', null);
|
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
setDragSourceId(evt) {
|
|
|
|
const id = this.node.item.id;
|
|
|
|
if (id === 'fake') {
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.$store.commit('explorer/setDragSourceId', id);
|
|
|
|
},
|
|
|
|
onDrop() {
|
|
|
|
const sourceNode = this.$store.getters['explorer/dragSourceNode'];
|
|
|
|
const targetNode = this.$store.getters['explorer/dragTargetNodeFolder'];
|
|
|
|
this.setDragTargetId();
|
|
|
|
if (!sourceNode.isNil
|
|
|
|
&& !targetNode.isNil
|
|
|
|
&& sourceNode.item.id !== targetNode.item.id
|
|
|
|
) {
|
|
|
|
const patch = {
|
|
|
|
id: sourceNode.item.id,
|
|
|
|
parentId: targetNode.item.id,
|
|
|
|
};
|
|
|
|
if (sourceNode.isFolder) {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('folder/patchItem', patch);
|
2017-08-03 17:08:12 +00:00
|
|
|
} else {
|
2017-08-17 23:10:35 +00:00
|
|
|
this.$store.commit('file/patchItem', patch);
|
2017-08-03 17:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-07-31 09:04:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
$item-font-size: 14px;
|
|
|
|
|
2017-08-03 17:08:12 +00:00
|
|
|
.explorer-node--drag-target {
|
|
|
|
background-color: rgba(0, 128, 255, 0.2);
|
|
|
|
}
|
|
|
|
|
2017-07-31 09:04:01 +00:00
|
|
|
.explorer-node__item {
|
|
|
|
cursor: pointer;
|
|
|
|
font-size: $item-font-size;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
|
|
|
.explorer-node--selected > & {
|
|
|
|
background-color: rgba(0, 0, 0, 0.2);
|
|
|
|
|
|
|
|
.explorer__tree:focus & {
|
|
|
|
background-color: #39f;
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.explorer__tree--new-item & {
|
|
|
|
opacity: 0.33;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.explorer-node__item--folder,
|
|
|
|
.explorer-node__item-editor--folder,
|
|
|
|
.explorer-node__new-child--folder {
|
|
|
|
&::before {
|
|
|
|
content: '▸';
|
|
|
|
position: absolute;
|
|
|
|
margin-left: -13px;
|
|
|
|
|
|
|
|
.explorer-node--open > & {
|
|
|
|
content: '▾';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$new-child-height: 25px;
|
|
|
|
|
|
|
|
.explorer-node__item-editor,
|
|
|
|
.explorer-node__new-child {
|
|
|
|
padding: 1px 10px;
|
|
|
|
|
|
|
|
.text-input {
|
|
|
|
font-size: $item-font-size;
|
|
|
|
padding: 2px;
|
|
|
|
height: $new-child-height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|