Commit ed6669f1 authored by Phil Hughes's avatar Phil Hughes

fixed up adding/creating temp files

this now all goes through the 1 mutationn & the web worker. This keeps all the logic the same for multiple directions
parent 40ea8633
<script>
import { mapActions } from 'vuex';
import icon from '~/vue_shared/components/icon.vue';
import newModal from './modal.vue';
import upload from './upload.vue';
......@@ -18,10 +19,6 @@
type: String,
required: true,
},
parent: {
type: Object,
default: null,
},
},
data() {
return {
......@@ -31,6 +28,9 @@
};
},
methods: {
...mapActions([
'createTempEntry',
]),
createNewItem(type) {
this.modalType = type;
this.openModal = true;
......@@ -85,7 +85,7 @@
<upload
:branch-id="branch"
:path="path"
:parent="parent"
@create="createTempEntry"
/>
</li>
<li>
......@@ -104,8 +104,8 @@
:type="modalType"
:branch-id="branch"
:path="path"
:parent="parent"
@hide="hideModal"
@create="createTempEntry"
/>
</div>
</template>
<script>
import { mapActions, mapState } from 'vuex';
import { __ } from '~/locale';
import modal from '~/vue_shared/components/modal.vue';
......@@ -12,10 +11,6 @@
type: String,
required: true,
},
parent: {
type: Object,
default: null,
},
type: {
type: String,
required: true,
......@@ -31,9 +26,6 @@
};
},
computed: {
...mapState([
'currentProjectId',
]),
modalTitle() {
if (this.type === 'tree') {
return __('Create new directory');
......@@ -60,15 +52,10 @@
this.$refs.fieldName.focus();
},
methods: {
...mapActions([
'createTempEntry',
]),
createEntryInStore() {
this.createTempEntry({
projectId: this.currentProjectId,
this.$emit('create', {
branchId: this.branchId,
parent: this.parent,
name: this.entryName.replace(new RegExp(`^${this.path}/`), ''),
name: this.entryName,
type: this.type,
});
......
<script>
import { mapActions, mapState } from 'vuex';
export default {
props: {
branchId: {
type: String,
required: true,
},
parent: {
type: Object,
default: null,
path: {
type: String,
default: '',
},
},
computed: {
...mapState([
'trees',
'currentProjectId',
]),
},
mounted() {
this.$refs.fileUpload.addEventListener('change', this.openFile);
},
......@@ -25,9 +17,6 @@
this.$refs.fileUpload.removeEventListener('change', this.openFile);
},
methods: {
...mapActions([
'createTempEntry',
]),
createFile(target, file, isText) {
const { name } = file;
let { result } = target;
......@@ -36,11 +25,9 @@
result = result.split('base64,')[1];
}
this.createTempEntry({
name,
projectId: this.currentProjectId,
this.$emit('create', {
name: `${(this.path ? `${this.path}/` : '')}${name}`,
branchId: this.branchId,
parent: this.parent,
type: 'blob',
content: result,
base64: !isText,
......
......@@ -101,7 +101,6 @@
:project-id="file.projectId"
:branch="file.branchId"
:path="file.path"
:parent="file"
/>
<changed-file-icon
:file="file"
......
......@@ -37,20 +37,16 @@ export const setResizingStatus = ({ commit }, resizing) => {
export const createTempEntry = (
{ state, dispatch },
{ projectId, branchId, parent, name, type, content = '', base64 = false },
{ branchId, name, type, content = '', base64 = false },
) => {
const selectedParent = parent || state.trees[`${projectId}/${branchId}`];
if (type === 'tree') {
dispatch('createTempTree', {
projectId,
branchId,
name,
});
} else if (type === 'blob') {
dispatch('createTempFile', {
projectId,
branchId,
parent: selectedParent,
name,
base64,
content,
......
......@@ -105,14 +105,12 @@ export const setEditorPosition = ({ getters, commit }, { editorRow, editorColumn
}
};
export const createTempFile = ({ state, commit, dispatch }, { projectId, branchId, parent, name, content = '', base64 = '' }) =>
export const createTempFile = ({ state, commit, dispatch }, { branchId, name, content = '', base64 = '' }) =>
new Promise((resolve) => {
const worker = new FilesDecoratorWorker();
const path = parent.path !== undefined ? `${parent.path}/` : '';
const entryPath = path ? `${path}/${name}` : name;
if (state.entries[entryPath]) {
return flash(`The name "${name}" is already taken in this directory.`, 'alert', document, null, false, true);
if (state.entries[name]) {
return flash(`The name "${name.split('/').pop()}" is already taken in this directory.`, 'alert', document, null, false, true);
}
worker.addEventListener('message', ({ data }) => {
......@@ -120,9 +118,9 @@ export const createTempFile = ({ state, commit, dispatch }, { projectId, branchI
worker.terminate();
commit(types.CREATE_TMP_FILE, {
commit(types.CREATE_TMP_ENTRY, {
data,
projectId,
projectId: state.currentProjectId,
branchId,
});
commit(types.TOGGLE_FILE_OPEN, file.path);
......@@ -133,8 +131,8 @@ export const createTempFile = ({ state, commit, dispatch }, { projectId, branchI
});
worker.postMessage({
data: [entryPath],
projectId,
data: [name],
projectId: state.currentProjectId,
branchId,
tempFile: true,
content,
......
......@@ -31,7 +31,7 @@ export const handleTreeEntryAction = ({ commit, dispatch }, row) => {
export const createTempTree = (
{ state, commit, dispatch },
{ projectId, branchId, name },
{ branchId, name },
) => new Promise((resolve) => {
const dirName = name.replace(new RegExp(`^${state.path}/`), '');
const worker = new FilesDecoratorWorker();
......@@ -39,9 +39,9 @@ export const createTempTree = (
worker.addEventListener('message', ({ data }) => {
worker.terminate();
commit(types.CREATE_TMP_TREE, {
commit(types.CREATE_TMP_ENTRY, {
data,
projectId,
projectId: state.currentProjectId,
branchId,
});
......@@ -50,7 +50,7 @@ export const createTempTree = (
worker.postMessage({
data: [`${dirName}/`],
projectId,
projectId: state.currentProjectId,
branchId,
tempFile: true,
});
......
......@@ -19,7 +19,6 @@ export const TOGGLE_BRANCH_OPEN = 'TOGGLE_BRANCH_OPEN';
// Tree mutation types
export const SET_DIRECTORY_DATA = 'SET_DIRECTORY_DATA';
export const TOGGLE_TREE_OPEN = 'TOGGLE_TREE_OPEN';
export const CREATE_TMP_TREE = 'CREATE_TMP_TREE';
export const SET_LAST_COMMIT_URL = 'SET_LAST_COMMIT_URL';
export const CREATE_TREE = 'CREATE_TREE';
export const REMOVE_ALL_CHANGES_FILES = 'REMOVE_ALL_CHANGES_FILES';
......@@ -34,7 +33,6 @@ export const SET_FILE_LANGUAGE = 'SET_FILE_LANGUAGE';
export const SET_FILE_POSITION = 'SET_FILE_POSITION';
export const SET_FILE_EOL = 'SET_FILE_EOL';
export const DISCARD_FILE_CHANGES = 'DISCARD_FILE_CHANGES';
export const CREATE_TMP_FILE = 'CREATE_TMP_FILE';
export const ADD_FILE_TO_CHANGED = 'ADD_FILE_TO_CHANGED';
export const REMOVE_FILE_FROM_CHANGED = 'REMOVE_FILE_FROM_CHANGED';
export const TOGGLE_FILE_CHANGED = 'TOGGLE_FILE_CHANGED';
......@@ -42,3 +40,5 @@ export const TOGGLE_FILE_CHANGED = 'TOGGLE_FILE_CHANGED';
export const SET_CURRENT_BRANCH = 'SET_CURRENT_BRANCH';
export const SET_ENTRIES = 'SET_ENTRIES';
export const CREATE_TMP_ENTRY = 'CREATE_TMP_ENTRY';
......@@ -53,6 +53,35 @@ export default {
entries,
});
},
[types.CREATE_TMP_ENTRY](state, { data, projectId, branchId }) {
Object.keys(data.entries).reduce((acc, key) => {
const entry = data.entries[key];
const foundEntry = state.entries[key];
if (!foundEntry) {
Object.assign(state.entries, {
[key]: entry,
});
} else {
const tree = entry.tree.filter(f =>
foundEntry.tree.find(e => e.path === f.path) === undefined,
);
Object.assign(foundEntry, {
tree: foundEntry.tree.concat(tree),
});
}
return acc.concat(key);
}, []);
const foundEntry = state.trees[`${projectId}/${branchId}`].tree.find(e => e.path === data.treeList[0].path);
if (!foundEntry) {
Object.assign(state.trees[`${projectId}/${branchId}`], {
tree: state.trees[`${projectId}/${branchId}`].tree.concat(data.treeList),
});
}
},
...projectMutations,
...fileMutations,
...treeMutations,
......
......@@ -65,34 +65,6 @@ export default {
changed: false,
});
},
[types.CREATE_TMP_FILE](state, { data, projectId, branchId }) {
Object.keys(data.entries).forEach((key) => {
const entry = data.entries[key];
Object.assign(state.entries, {
[key]: entry,
});
});
Object.assign(state.trees[`${projectId}/${branchId}`], {
tree: state.trees[`${projectId}/${branchId}`].tree.concat(data.treeList),
});
// Object.assign(state.entries, {
// [file.path]: file,
// });
// if (parent.path) {
// // Add it as a child of the parent
// Object.assign(state.entries[parent.path], {
// tree: parent.tree.concat(file),
// });
// } else {
// // Add it the root
// Object.assign(parent, {
// tree: parent.tree.concat(file),
// });
// }
},
[types.ADD_FILE_TO_CHANGED](state, path) {
Object.assign(state, {
changedFiles: state.changedFiles.concat(state.entries[path]),
......
......@@ -30,19 +30,6 @@ export default {
lastCommitPath: url,
});
},
[types.CREATE_TMP_TREE](state, { data, projectId, branchId }) {
Object.keys(data.entries).forEach((key) => {
const entry = data.entries[key];
Object.assign(state.entries, {
[key]: entry,
});
});
Object.assign(state.trees[`${projectId}/${branchId}`], {
tree: state.trees[`${projectId}/${branchId}`].tree.concat(data.treeList),
});
},
[types.REMOVE_ALL_CHANGES_FILES](state) {
Object.assign(state, {
changedFiles: [],
......
......@@ -4,7 +4,7 @@ import {
} from '../utils';
self.addEventListener('message', (e) => {
const { data, projectId, branchId, tempFile = false } = e.data;
const { data, projectId, branchId, tempFile = false, content = '', base64 = false } = e.data;
const treeList = [];
let file;
......@@ -65,6 +65,8 @@ self.addEventListener('message', (e) => {
parentTreeUrl: fileFolder ? fileFolder.url : `/${projectId}/blob/${branchId}`,
tempFile,
changed: tempFile,
content,
base64,
});
Object.assign(acc, {
......
......@@ -124,20 +124,6 @@ describe('Multi-file store file mutations', () => {
});
});
describe('CREATE_TMP_FILE', () => {
it('adds file into parent tree', () => {
const f = file('tmpFile');
mutations.CREATE_TMP_FILE(localState, {
file: f,
parent: localFile,
});
expect(localFile.tree.length).toBe(1);
expect(localFile.tree[0].name).toBe(f.name);
});
});
describe('ADD_FILE_TO_CHANGED', () => {
it('adds file into changed files array', () => {
const f = file();
......
......@@ -47,20 +47,6 @@ describe('Multi-file store tree mutations', () => {
});
});
describe('CREATE_TMP_TREE', () => {
it('adds tree into parent tree', () => {
const tmpEntry = file('tmpTree');
mutations.CREATE_TMP_TREE(localState, {
tmpEntry,
parent: localTree,
});
expect(localTree.tree.length).toBe(1);
expect(localTree.tree[0].name).toBe(tmpEntry.name);
});
});
describe('REMOVE_ALL_CHANGES_FILES', () => {
it('removes all files from changedFiles state', () => {
localState.changedFiles.push(file('REMOVE_ALL_CHANGES_FILES'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment