Commit dc6af970 authored by Himanshu Kapoor's avatar Himanshu Kapoor Committed by Phil Hughes

Fix error renaming files using web IDE

When renaming files in the Web IDE in the rename modal, pressing
backspace all the way brought back the original filename back, thus
making the backspace redundant.
parent 26e53150
...@@ -11,32 +11,20 @@ export default { ...@@ -11,32 +11,20 @@ export default {
}, },
data() { data() {
return { return {
name: '', entryName: '',
type: modalTypes.blob, modalType: modalTypes.blob,
path: '', path: '',
}; };
}, },
computed: { computed: {
...mapState(['entries']), ...mapState(['entries']),
...mapGetters('fileTemplates', ['templateTypes']), ...mapGetters('fileTemplates', ['templateTypes']),
entryName: {
get() {
if (this.type === modalTypes.rename) {
return this.name || this.path;
}
return this.name || (this.path ? `${this.path}/` : '');
},
set(val) {
this.name = val.trim();
},
},
modalTitle() { modalTitle() {
const entry = this.entries[this.path]; const entry = this.entries[this.path];
if (this.type === modalTypes.tree) { if (this.modalType === modalTypes.tree) {
return __('Create new directory'); return __('Create new directory');
} else if (this.type === modalTypes.rename) { } else if (this.modalType === modalTypes.rename) {
return entry.type === modalTypes.tree ? __('Rename folder') : __('Rename file'); return entry.type === modalTypes.tree ? __('Rename folder') : __('Rename file');
} }
...@@ -45,16 +33,16 @@ export default { ...@@ -45,16 +33,16 @@ export default {
buttonLabel() { buttonLabel() {
const entry = this.entries[this.path]; const entry = this.entries[this.path];
if (this.type === modalTypes.tree) { if (this.modalType === modalTypes.tree) {
return __('Create directory'); return __('Create directory');
} else if (this.type === modalTypes.rename) { } else if (this.modalType === modalTypes.rename) {
return entry.type === modalTypes.tree ? __('Rename folder') : __('Rename file'); return entry.type === modalTypes.tree ? __('Rename folder') : __('Rename file');
} }
return __('Create file'); return __('Create file');
}, },
isCreatingNewFile() { isCreatingNewFile() {
return this.type === modalTypes.blob; return this.modalType === modalTypes.blob;
}, },
placeholder() { placeholder() {
return this.isCreatingNewFile ? 'dir/file_name' : 'dir/'; return this.isCreatingNewFile ? 'dir/file_name' : 'dir/';
...@@ -63,8 +51,8 @@ export default { ...@@ -63,8 +51,8 @@ export default {
methods: { methods: {
...mapActions(['createTempEntry', 'renameEntry']), ...mapActions(['createTempEntry', 'renameEntry']),
submitForm() { submitForm() {
if (this.type === modalTypes.rename) { if (this.modalType === modalTypes.rename) {
if (this.entries[this.entryName] && !this.entries[this.entryName].deleted) { if (!this.entries[this.entryName]?.deleted) {
flash( flash(
sprintf(s__('The name "%{name}" is already taken in this directory.'), { sprintf(s__('The name "%{name}" is already taken in this directory.'), {
name: this.entryName, name: this.entryName,
...@@ -77,32 +65,32 @@ export default { ...@@ -77,32 +65,32 @@ export default {
); );
} else { } else {
let parentPath = this.entryName.split('/'); let parentPath = this.entryName.split('/');
const entryName = parentPath.pop(); const name = parentPath.pop();
parentPath = parentPath.join('/'); parentPath = parentPath.join('/');
this.renameEntry({ this.renameEntry({
path: this.path, path: this.path,
name: entryName, name,
parentPath, parentPath,
}); });
} }
} else { } else {
this.createTempEntry({ this.createTempEntry({
name: this.name, name: this.entryName,
type: this.type, type: this.modalType,
}); });
} }
}, },
createFromTemplate(template) { createFromTemplate(template) {
this.createTempEntry({ this.createTempEntry({
name: template.name, name: template.name,
type: this.type, type: this.modalType,
}); });
this.$refs.modal.toggle(); this.$refs.modal.toggle();
}, },
focusInput() { focusInput() {
const name = this.entries[this.entryName] ? this.entries[this.entryName].name : null; const name = this.entries[this.entryName]?.name;
const inputValue = this.$refs.fieldName.value; const inputValue = this.$refs.fieldName.value;
this.$refs.fieldName.focus(); this.$refs.fieldName.focus();
...@@ -112,19 +100,24 @@ export default { ...@@ -112,19 +100,24 @@ export default {
} }
}, },
resetData() { resetData() {
this.name = ''; this.entryName = '';
this.path = ''; this.path = '';
this.type = modalTypes.blob; this.modalType = modalTypes.blob;
}, },
open(type = modalTypes.blob, path = '') { open(type = modalTypes.blob, path = '') {
this.type = type; this.modalType = type;
this.path = path; this.path = path;
if (this.modalType === modalTypes.rename) {
this.entryName = path;
} else {
this.entryName = path ? `${path}/` : '';
}
this.$refs.modal.show(); this.$refs.modal.show();
// wait for modal to show first // wait for modal to show first
this.$nextTick(() => { this.$nextTick(() => this.focusInput());
this.focusInput();
});
}, },
close() { close() {
this.$refs.modal.hide(); this.$refs.modal.hide();
...@@ -150,7 +143,7 @@ export default { ...@@ -150,7 +143,7 @@ export default {
<div class="col-sm-10"> <div class="col-sm-10">
<input <input
ref="fieldName" ref="fieldName"
v-model="entryName" v-model.trim="entryName"
type="text" type="text"
class="form-control qa-full-file-path" class="form-control qa-full-file-path"
:placeholder="placeholder" :placeholder="placeholder"
......
---
title: Fix error renaming files using web IDE
merge_request: 30969
author:
type: fixed
...@@ -91,22 +91,31 @@ describe('new file modal component', () => { ...@@ -91,22 +91,31 @@ describe('new file modal component', () => {
expect(vm.entryName).toBe('test-path'); expect(vm.entryName).toBe('test-path');
}); });
it('updated name', () => { it('does not reset entryName to its old value if empty', () => {
vm.name = 'index.js'; vm.entryName = 'hello';
vm.entryName = '';
expect(vm.entryName).toBe('index.js'); expect(vm.entryName).toBe('');
});
});
describe('open', () => {
it('sets entryName to path provided if modalType is rename', () => {
vm.open('rename', 'test-path');
expect(vm.entryName).toBe('test-path');
}); });
it('removes leading/trailing spaces when found in the new name', () => { it("appends '/' to the path if modalType isn't rename", () => {
vm.entryName = ' index.js '; vm.open('blob', 'test-path');
expect(vm.entryName).toBe('index.js'); expect(vm.entryName).toBe('test-path/');
}); });
it('does not remove internal spaces in the file name', () => { it('leaves entryName blank if no path is provided', () => {
vm.entryName = ' In Praise of Idleness.txt '; vm.open('blob');
expect(vm.entryName).toBe('In Praise of Idleness.txt'); expect(vm.entryName).toBe('');
}); });
}); });
}); });
......
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