debian-mirror-gitlab/app/assets/javascripts/ide/stores/actions/file.js

300 lines
9 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import { joinPaths, escapeFileUrl } from '~/lib/utils/url_utility';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2018-05-09 12:01:36 +05:30
import eventHub from '../../eventhub';
import service from '../../services';
import * as types from '../mutation_types';
import router from '../../ide_router';
2020-01-01 13:55:28 +05:30
import { addFinalNewlineIfNeeded, setPageTitleForFile } from '../utils';
2018-11-20 20:47:30 +05:30
import { viewerTypes, stageKeys } from '../../constants';
2018-05-09 12:01:36 +05:30
export const closeFile = ({ commit, state, dispatch }, file) => {
2018-11-08 19:23:39 +05:30
const { path } = file;
2018-05-09 12:01:36 +05:30
const indexOfClosedFile = state.openFiles.findIndex(f => f.key === file.key);
const fileWasActive = file.active;
if (file.pending) {
commit(types.REMOVE_PENDING_TAB, file);
} else {
commit(types.TOGGLE_FILE_OPEN, path);
commit(types.SET_FILE_ACTIVE, { path, active: false });
}
if (state.openFiles.length > 0 && fileWasActive) {
const nextIndexToOpen = indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
const nextFileToOpen = state.openFiles[nextIndexToOpen];
if (nextFileToOpen.pending) {
2018-10-15 14:42:47 +05:30
dispatch('updateViewer', viewerTypes.diff);
dispatch('openPendingTab', {
file: nextFileToOpen,
keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged',
});
2018-05-09 12:01:36 +05:30
} else {
router.push(`/project${nextFileToOpen.url}`);
}
} else if (!state.openFiles.length) {
router.push(`/project/${file.projectId}/tree/${file.branchId}/`);
}
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
};
export const setFileActive = ({ commit, state, getters, dispatch }, path) => {
const file = state.entries[path];
const currentActiveFile = getters.activeFile;
if (file.active) return;
if (currentActiveFile) {
commit(types.SET_FILE_ACTIVE, {
path: currentActiveFile.path,
active: false,
});
}
commit(types.SET_FILE_ACTIVE, { path, active: true });
dispatch('scrollToTab');
};
2019-02-15 15:39:39 +05:30
export const getFileData = (
2019-12-26 22:10:19 +05:30
{ state, commit, dispatch, getters },
2019-02-15 15:39:39 +05:30
{ path, makeFileActive = true, openFile = makeFileActive },
) => {
2018-05-09 12:01:36 +05:30
const file = state.entries[path];
2020-03-09 13:42:32 +05:30
const fileDeletedAndReadded = getters.isFileDeletedAndReadded(path);
2018-11-18 11:00:15 +05:30
2020-03-09 13:42:32 +05:30
if (file.raw || (file.tempFile && !file.prevPath && !fileDeletedAndReadded))
return Promise.resolve();
2018-11-18 11:00:15 +05:30
2018-05-09 12:01:36 +05:30
commit(types.TOGGLE_LOADING, { entry: file });
2018-11-18 11:00:15 +05:30
2019-12-26 22:10:19 +05:30
const url = joinPaths(
gon.relative_url_root || '/',
state.currentProjectId,
2020-03-09 13:42:32 +05:30
'-',
2019-12-26 22:10:19 +05:30
file.type,
getters.lastCommit && getters.lastCommit.id,
escapeFileUrl(file.prevPath || file.path),
);
2018-11-18 11:00:15 +05:30
2018-05-09 12:01:36 +05:30
return service
2019-12-26 22:10:19 +05:30
.getFileData(url)
.then(({ data }) => {
setPageTitleForFile(state, file);
2018-05-09 12:01:36 +05:30
2019-02-15 15:39:39 +05:30
if (data) commit(types.SET_FILE_DATA, { data, file });
if (openFile) commit(types.TOGGLE_FILE_OPEN, path);
2018-05-09 12:01:36 +05:30
if (makeFileActive) dispatch('setFileActive', path);
commit(types.TOGGLE_LOADING, { entry: file });
})
.catch(() => {
commit(types.TOGGLE_LOADING, { entry: file });
2018-11-08 19:23:39 +05:30
dispatch('setErrorMessage', {
2020-03-09 13:42:32 +05:30
text: __('An error occurred while loading the file.'),
2018-11-08 19:23:39 +05:30
action: payload =>
dispatch('getFileData', payload).then(() => dispatch('setErrorMessage', null)),
actionText: __('Please try again'),
actionPayload: { path, makeFileActive },
});
2018-05-09 12:01:36 +05:30
});
};
2018-11-08 19:23:39 +05:30
export const setFileMrChange = ({ commit }, { file, mrChange }) => {
2018-05-09 12:01:36 +05:30
commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file, mrChange });
};
2018-11-18 11:00:15 +05:30
export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) => {
2018-05-09 12:01:36 +05:30
const file = state.entries[path];
2020-03-09 13:42:32 +05:30
const stagedFile = state.stagedFiles.find(f => f.path === path);
2018-05-09 12:01:36 +05:30
return new Promise((resolve, reject) => {
2020-03-09 13:42:32 +05:30
const fileDeletedAndReadded = getters.isFileDeletedAndReadded(path);
2018-05-09 12:01:36 +05:30
service
2020-03-09 13:42:32 +05:30
.getRawFileData(fileDeletedAndReadded ? stagedFile : file)
2018-05-09 12:01:36 +05:30
.then(raw => {
2020-03-09 13:42:32 +05:30
if (!(file.tempFile && !file.prevPath && !fileDeletedAndReadded))
commit(types.SET_FILE_RAW_DATA, { file, raw, fileDeletedAndReadded });
2018-05-09 12:01:36 +05:30
if (file.mrChange && file.mrChange.new_file === false) {
2018-11-18 11:00:15 +05:30
const baseSha =
(getters.currentMergeRequest && getters.currentMergeRequest.baseCommitSha) || '';
2018-05-09 12:01:36 +05:30
service
.getBaseRawFileData(file, baseSha)
.then(baseRaw => {
commit(types.SET_FILE_BASE_RAW_DATA, {
file,
baseRaw,
});
resolve(raw);
})
.catch(e => {
reject(e);
});
} else {
resolve(raw);
}
})
.catch(() => {
2018-11-08 19:23:39 +05:30
dispatch('setErrorMessage', {
2020-03-09 13:42:32 +05:30
text: __('An error occurred while loading the file content.'),
2018-11-08 19:23:39 +05:30
action: payload =>
dispatch('getRawFileData', payload).then(() => dispatch('setErrorMessage', null)),
actionText: __('Please try again'),
2018-11-18 11:00:15 +05:30
actionPayload: { path },
2018-11-08 19:23:39 +05:30
});
2018-05-09 12:01:36 +05:30
reject();
});
});
};
2020-03-09 13:42:32 +05:30
export const changeFileContent = ({ commit, state, getters }, { path, content }) => {
2018-05-09 12:01:36 +05:30
const file = state.entries[path];
2019-12-26 22:10:19 +05:30
commit(types.UPDATE_FILE_CONTENT, {
path,
content: addFinalNewlineIfNeeded(content),
});
2018-05-09 12:01:36 +05:30
const indexOfChangedFile = state.changedFiles.findIndex(f => f.path === path);
if (file.changed && indexOfChangedFile === -1) {
2020-03-09 13:42:32 +05:30
commit(types.STAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
} else if (!file.changed && !file.tempFile && indexOfChangedFile !== -1) {
2018-05-09 12:01:36 +05:30
commit(types.REMOVE_FILE_FROM_CHANGED, path);
}
};
export const setFileLanguage = ({ getters, commit }, { fileLanguage }) => {
if (getters.activeFile) {
commit(types.SET_FILE_LANGUAGE, { file: getters.activeFile, fileLanguage });
}
};
export const setFileEOL = ({ getters, commit }, { eol }) => {
if (getters.activeFile) {
commit(types.SET_FILE_EOL, { file: getters.activeFile, eol });
}
};
export const setEditorPosition = ({ getters, commit }, { editorRow, editorColumn }) => {
if (getters.activeFile) {
commit(types.SET_FILE_POSITION, {
file: getters.activeFile,
editorRow,
editorColumn,
});
}
};
2018-11-08 19:23:39 +05:30
export const setFileViewMode = ({ commit }, { file, viewMode }) => {
2018-05-09 12:01:36 +05:30
commit(types.SET_FILE_VIEWMODE, { file, viewMode });
};
2020-03-09 13:42:32 +05:30
export const restoreOriginalFile = ({ dispatch, state, commit }, path) => {
2018-05-09 12:01:36 +05:30
const file = state.entries[path];
2020-03-09 13:42:32 +05:30
const isDestructiveDiscard = file.tempFile || file.prevPath;
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
if (file.deleted && file.parentPath) {
dispatch('restoreTree', file.parentPath);
}
2020-03-09 13:42:32 +05:30
if (isDestructiveDiscard) {
dispatch('closeFile', file);
}
if (file.tempFile) {
dispatch('deleteEntry', file.path);
} else {
commit(types.DISCARD_FILE_CHANGES, file.path);
}
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
if (file.prevPath) {
2020-03-09 13:42:32 +05:30
dispatch('renameEntry', {
path: file.path,
name: file.prevName,
parentPath: file.prevParentPath,
});
2018-11-18 11:00:15 +05:30
}
2020-03-09 13:42:32 +05:30
};
2018-11-18 11:00:15 +05:30
2020-03-09 13:42:32 +05:30
export const discardFileChanges = ({ dispatch, state, commit, getters }, path) => {
const file = state.entries[path];
const isDestructiveDiscard = file.tempFile || file.prevPath;
dispatch('restoreOriginalFile', path);
if (!isDestructiveDiscard && file.path === getters.activeFile?.path) {
2018-10-15 14:42:47 +05:30
dispatch('updateDelayViewerUpdated', true)
.then(() => {
router.push(`/project${file.url}`);
})
.catch(e => {
throw e;
});
2018-05-09 12:01:36 +05:30
}
2020-03-09 13:42:32 +05:30
commit(types.REMOVE_FILE_FROM_CHANGED, path);
2018-10-15 14:42:47 +05:30
eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content);
eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
2018-05-09 12:01:36 +05:30
};
2020-03-09 13:42:32 +05:30
export const stageChange = ({ commit, dispatch, getters }, path) => {
const stagedFile = getters.getStagedFile(path);
const openFile = getters.getOpenFile(path);
2018-10-15 14:42:47 +05:30
2020-03-09 13:42:32 +05:30
commit(types.STAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
2018-10-15 14:42:47 +05:30
commit(types.SET_LAST_COMMIT_MSG, '');
if (stagedFile) {
eventHub.$emit(`editor.update.model.new.content.staged-${stagedFile.key}`, stagedFile.content);
2018-05-09 12:01:36 +05:30
}
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
const file = getters.getStagedFile(path);
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
if (openFile && openFile.active && file) {
2018-11-20 20:47:30 +05:30
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.staged,
});
}
2018-10-15 14:42:47 +05:30
};
2020-03-09 13:42:32 +05:30
export const unstageChange = ({ commit, dispatch, getters }, path) => {
const openFile = getters.getOpenFile(path);
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
commit(types.UNSTAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
const file = getters.getChangedFile(path);
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
if (openFile && openFile.active && file) {
2018-11-20 20:47:30 +05:30
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.unstaged,
});
}
2018-10-15 14:42:47 +05:30
};
2018-11-20 20:47:30 +05:30
export const openPendingTab = ({ commit, getters, state }, { file, keyPrefix }) => {
2018-10-15 14:42:47 +05:30
if (getters.activeFile && getters.activeFile.key === `${keyPrefix}-${file.key}`) return false;
state.openFiles.forEach(f => eventHub.$emit(`editor.update.model.dispose.${f.key}`));
2018-05-09 12:01:36 +05:30
2018-10-15 14:42:47 +05:30
commit(types.ADD_PENDING_TAB, { file, keyPrefix });
2018-05-09 12:01:36 +05:30
router.push(`/project/${file.projectId}/tree/${state.currentBranchId}/`);
return true;
};
export const removePendingTab = ({ commit }, file) => {
commit(types.REMOVE_PENDING_TAB, file);
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
};
2019-09-04 21:01:54 +05:30
export const triggerFilesChange = () => {
// Used in EE for file mirroring
eventHub.$emit('ide.files.change');
};