Commit e7fe50bf authored by Phil Hughes's avatar Phil Hughes

rename files

not working for folders yet
parent 73473dfc
...@@ -44,12 +44,19 @@ export default { ...@@ -44,12 +44,19 @@ export default {
}, },
}, },
methods: { methods: {
...mapActions(['createTempEntry']), ...mapActions(['createTempEntry', 'renameEntry']),
createEntryInStore() { submitForm() {
if (this.entryModal.type === 'rename') {
this.renameEntry({
path: this.entryModal.entry.path,
name: this.entryName,
});
} else {
this.createTempEntry({ this.createTempEntry({
name: this.name, name: this.name,
type: this.entryModal.type, type: this.entryModal.type,
}); });
}
}, },
focusInput() { focusInput() {
setTimeout(() => { setTimeout(() => {
...@@ -66,7 +73,7 @@ export default { ...@@ -66,7 +73,7 @@ export default {
:header-title-text="modalTitle" :header-title-text="modalTitle"
:footer-primary-button-text="buttonLabel" :footer-primary-button-text="buttonLabel"
footer-primary-button-variant="success" footer-primary-button-variant="success"
@submit="createEntryInStore" @submit="submitForm"
@open="focusInput" @open="focusInput"
> >
<div <div
......
...@@ -193,6 +193,11 @@ export const deleteEntry = ({ commit, dispatch, state }, path) => { ...@@ -193,6 +193,11 @@ export const deleteEntry = ({ commit, dispatch, state }, path) => {
export const resetOpenFiles = ({ commit }) => commit(types.RESET_OPEN_FILES); export const resetOpenFiles = ({ commit }) => commit(types.RESET_OPEN_FILES);
export const renameEntry = ({ dispatch, commit }, { path, name }) => {
commit(types.RENAME_ENTRY, { path, name });
dispatch('deleteEntry', path);
};
export * from './actions/tree'; export * from './actions/tree';
export * from './actions/file'; export * from './actions/file';
export * from './actions/project'; export * from './actions/project';
......
...@@ -77,3 +77,4 @@ export const SET_ERROR_MESSAGE = 'SET_ERROR_MESSAGE'; ...@@ -77,3 +77,4 @@ export const SET_ERROR_MESSAGE = 'SET_ERROR_MESSAGE';
export const OPEN_NEW_ENTRY_MODAL = 'OPEN_NEW_ENTRY_MODAL'; export const OPEN_NEW_ENTRY_MODAL = 'OPEN_NEW_ENTRY_MODAL';
export const DELETE_ENTRY = 'DELETE_ENTRY'; export const DELETE_ENTRY = 'DELETE_ENTRY';
export const RENAME_ENTRY = 'RENAME_ENTRY';
...@@ -131,11 +131,14 @@ export default { ...@@ -131,11 +131,14 @@ export default {
}, },
[types.UPDATE_FILE_AFTER_COMMIT](state, { file, lastCommit }) { [types.UPDATE_FILE_AFTER_COMMIT](state, { file, lastCommit }) {
const changedFile = state.changedFiles.find(f => f.path === file.path); const changedFile = state.changedFiles.find(f => f.path === file.path);
const { prevPath } = file;
Object.assign(state.entries[file.path], { Object.assign(state.entries[file.path], {
raw: file.content, raw: file.content,
changed: !!changedFile, changed: !!changedFile,
staged: false, staged: false,
prevPath: '',
moved: false,
lastCommit: Object.assign(state.entries[file.path].lastCommit, { lastCommit: Object.assign(state.entries[file.path].lastCommit, {
id: lastCommit.commit.id, id: lastCommit.commit.id,
url: lastCommit.commit_path, url: lastCommit.commit_path,
...@@ -144,6 +147,18 @@ export default { ...@@ -144,6 +147,18 @@ export default {
updatedAt: lastCommit.commit.authored_date, updatedAt: lastCommit.commit.authored_date,
}), }),
}); });
if (prevPath) {
// Update URLs after file has moved
const regex = new RegExp(`${prevPath}$`);
Object.assign(state.entries[file.path], {
rawPath: file.rawPath.replace(regex, file.name),
permalink: file.permalink.replace(regex, file.name),
commitsPath: file.commitsPath.replace(regex, file.name),
blamePath: file.blamePath.replace(regex, file.name),
});
}
}, },
[types.BURST_UNUSED_SEAL](state) { [types.BURST_UNUSED_SEAL](state) {
Object.assign(state, { Object.assign(state, {
...@@ -186,6 +201,28 @@ export default { ...@@ -186,6 +201,28 @@ export default {
state.changedFiles = state.changedFiles.concat(entry); state.changedFiles = state.changedFiles.concat(entry);
parent.tree = parent.tree.filter(f => f.path !== entry.path); parent.tree = parent.tree.filter(f => f.path !== entry.path);
}, },
[types.RENAME_ENTRY](state, { path, name }) {
const oldEntry = state.entries[path];
const parent = oldEntry.parentPath
? state.entries[oldEntry.parentPath]
: state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
const nameRegex = new RegExp(`${oldEntry.name}$`);
const newPath = path.replace(nameRegex, name);
state.entries[newPath] = {
...oldEntry,
id: newPath,
key: `${name}-${oldEntry.type}-${oldEntry.id}`,
path: newPath,
name,
tempFile: true,
prevPath: path,
url: oldEntry.url.replace(nameRegex, name),
};
oldEntry.moved = true;
parent.tree = parent.tree.concat(state.entries[newPath]);
state.changedFiles = state.changedFiles.concat(state.entries[newPath]);
},
...projectMutations, ...projectMutations,
...mergeRequestMutation, ...mergeRequestMutation,
...fileMutations, ...fileMutations,
......
...@@ -47,6 +47,8 @@ export const dataStructure = () => ({ ...@@ -47,6 +47,8 @@ export const dataStructure = () => ({
lastOpenedAt: 0, lastOpenedAt: 0,
mrChange: null, mrChange: null,
deleted: false, deleted: false,
prevPath: '',
moved: false,
}); });
export const decorateData = entity => { export const decorateData = entity => {
...@@ -107,7 +109,9 @@ export const setPageTitle = title => { ...@@ -107,7 +109,9 @@ export const setPageTitle = title => {
}; };
export const commitActionForFile = file => { export const commitActionForFile = file => {
if (file.deleted) { if (file.prevPath !== '') {
return 'move';
} else if (file.deleted) {
return 'delete'; return 'delete';
} else if (file.tempFile) { } else if (file.tempFile) {
return 'create'; return 'create';
...@@ -118,6 +122,8 @@ export const commitActionForFile = file => { ...@@ -118,6 +122,8 @@ export const commitActionForFile = file => {
export const getCommitFiles = (stagedFiles, deleteTree = false) => export const getCommitFiles = (stagedFiles, deleteTree = false) =>
stagedFiles.reduce((acc, file) => { stagedFiles.reduce((acc, file) => {
if (file.moved) return acc;
if ((file.deleted || deleteTree) && file.type === 'tree') { if ((file.deleted || deleteTree) && file.type === 'tree') {
return acc.concat(getCommitFiles(file.tree, true)); return acc.concat(getCommitFiles(file.tree, true));
} }
...@@ -134,9 +140,10 @@ export const createCommitPayload = ({ branch, getters, newBranch, state, rootSta ...@@ -134,9 +140,10 @@ export const createCommitPayload = ({ branch, getters, newBranch, state, rootSta
actions: getCommitFiles(rootState.stagedFiles).map(f => ({ actions: getCommitFiles(rootState.stagedFiles).map(f => ({
action: commitActionForFile(f), action: commitActionForFile(f),
file_path: f.path, file_path: f.path,
previous_path: f.prevPath === '' ? undefined : f.prevPath,
content: f.content, content: f.content,
encoding: f.base64 ? 'base64' : 'text', encoding: f.base64 ? 'base64' : 'text',
last_commit_id: newBranch || f.deleted ? undefined : f.lastCommitSha, last_commit_id: newBranch || f.deleted || f.prevPath ? undefined : f.lastCommitSha,
})), })),
start_branch: newBranch ? rootState.currentBranchId : undefined, start_branch: newBranch ? rootState.currentBranchId : undefined,
}); });
......
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