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

242 lines
7.3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import * as types from '../mutation_types';
2018-11-18 11:00:15 +05:30
import { sortTree } from '../utils';
2018-11-08 19:23:39 +05:30
import { diffModes } from '../../constants';
2019-09-30 21:07:59 +05:30
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
2018-05-09 12:01:36 +05:30
export default {
[types.SET_FILE_ACTIVE](state, { path, active }) {
Object.assign(state.entries[path], {
active,
2018-10-15 14:42:47 +05:30
lastOpenedAt: new Date().getTime(),
2018-05-09 12:01:36 +05:30
});
if (active && !state.entries[path].pending) {
Object.assign(state, {
2021-03-08 18:12:59 +05:30
openFiles: state.openFiles.map((f) =>
2018-05-09 12:01:36 +05:30
Object.assign(f, { active: f.pending ? false : f.active }),
),
});
}
},
[types.TOGGLE_FILE_OPEN](state, path) {
2021-01-03 14:25:43 +05:30
const entry = state.entries[path];
2018-05-09 12:01:36 +05:30
2021-01-03 14:25:43 +05:30
entry.opened = !entry.opened;
if (entry.opened && !entry.tempFile) {
entry.loading = true;
}
if (entry.opened) {
2018-05-09 12:01:36 +05:30
Object.assign(state, {
2021-03-08 18:12:59 +05:30
openFiles: state.openFiles.filter((f) => f.path !== path).concat(state.entries[path]),
2018-05-09 12:01:36 +05:30
});
} else {
Object.assign(state, {
2021-03-08 18:12:59 +05:30
openFiles: state.openFiles.filter((f) => f.key !== entry.key),
2018-05-09 12:01:36 +05:30
});
}
},
[types.SET_FILE_DATA](state, { data, file }) {
2019-09-30 21:07:59 +05:30
const stateEntry = state.entries[file.path];
2021-03-08 18:12:59 +05:30
const stagedFile = state.stagedFiles.find((f) => f.path === file.path);
const openFile = state.openFiles.find((f) => f.path === file.path);
const changedFile = state.changedFiles.find((f) => f.path === file.path);
2019-09-30 21:07:59 +05:30
2021-03-08 18:12:59 +05:30
[stateEntry, stagedFile, openFile, changedFile].forEach((f) => {
2019-09-30 21:07:59 +05:30
if (f) {
2019-10-12 21:52:04 +05:30
Object.assign(
f,
convertObjectPropsToCamelCase(data, { dropKeys: ['path', 'name', 'raw', 'baseRaw'] }),
{
raw: (stateEntry && stateEntry.raw) || null,
baseRaw: null,
},
);
2019-09-30 21:07:59 +05:30
}
2018-05-09 12:01:36 +05:30
});
},
2020-03-13 15:44:24 +05:30
[types.SET_FILE_RAW_DATA](state, { file, raw, fileDeletedAndReadded = false }) {
2018-11-18 11:00:15 +05:30
const openPendingFile = state.openFiles.find(
2021-03-08 18:12:59 +05:30
(f) =>
2020-03-13 15:44:24 +05:30
f.path === file.path && f.pending && !(f.tempFile && !f.prevPath && !fileDeletedAndReadded),
2018-11-18 11:00:15 +05:30
);
2021-03-08 18:12:59 +05:30
const stagedFile = state.stagedFiles.find((f) => f.path === file.path);
2018-11-18 11:00:15 +05:30
2020-03-13 15:44:24 +05:30
if (file.tempFile && file.content === '' && !fileDeletedAndReadded) {
Object.assign(state.entries[file.path], { content: raw });
} else if (fileDeletedAndReadded) {
Object.assign(stagedFile, { raw });
2018-11-18 11:00:15 +05:30
} else {
2020-03-13 15:44:24 +05:30
Object.assign(state.entries[file.path], { raw });
2018-11-18 11:00:15 +05:30
}
if (!openPendingFile) return;
if (!openPendingFile.tempFile) {
openPendingFile.raw = raw;
2020-03-13 15:44:24 +05:30
} else if (openPendingFile.tempFile && !fileDeletedAndReadded) {
2018-11-18 11:00:15 +05:30
openPendingFile.content = raw;
2020-03-13 15:44:24 +05:30
} else if (fileDeletedAndReadded) {
Object.assign(stagedFile, { raw });
2018-11-18 11:00:15 +05:30
}
2018-05-09 12:01:36 +05:30
},
[types.SET_FILE_BASE_RAW_DATA](state, { file, baseRaw }) {
Object.assign(state.entries[file.path], {
baseRaw,
});
},
[types.UPDATE_FILE_CONTENT](state, { path, content }) {
2021-03-08 18:12:59 +05:30
const stagedFile = state.stagedFiles.find((f) => f.path === path);
2018-10-15 14:42:47 +05:30
const rawContent = stagedFile ? stagedFile.content : state.entries[path].raw;
const changed = content !== rawContent;
2018-05-09 12:01:36 +05:30
Object.assign(state.entries[path], {
content,
changed,
});
},
[types.SET_FILE_MERGE_REQUEST_CHANGE](state, { file, mrChange }) {
2018-11-08 19:23:39 +05:30
let diffMode = diffModes.replaced;
if (mrChange.new_file) {
diffMode = diffModes.new;
} else if (mrChange.deleted_file) {
diffMode = diffModes.deleted;
} else if (mrChange.renamed_file) {
diffMode = diffModes.renamed;
}
2018-05-09 12:01:36 +05:30
Object.assign(state.entries[file.path], {
2018-11-08 19:23:39 +05:30
mrChange: {
...mrChange,
diffMode,
},
2018-05-09 12:01:36 +05:30
});
},
[types.DISCARD_FILE_CHANGES](state, path) {
2021-03-08 18:12:59 +05:30
const stagedFile = state.stagedFiles.find((f) => f.path === path);
2018-11-18 11:00:15 +05:30
const entry = state.entries[path];
2020-03-13 15:44:24 +05:30
const { deleted } = entry;
2018-10-15 14:42:47 +05:30
2018-05-09 12:01:36 +05:30
Object.assign(state.entries[path], {
2018-10-15 14:42:47 +05:30
content: stagedFile ? stagedFile.content : state.entries[path].raw,
2018-05-09 12:01:36 +05:30
changed: false,
2018-11-18 11:00:15 +05:30
deleted: false,
2018-05-09 12:01:36 +05:30
});
2018-11-18 11:00:15 +05:30
if (deleted) {
const parent = entry.parentPath
? state.entries[entry.parentPath]
: state.trees[`${state.currentProjectId}/${state.currentBranchId}`];
parent.tree = sortTree(parent.tree.concat(entry));
}
2018-05-09 12:01:36 +05:30
},
[types.ADD_FILE_TO_CHANGED](state, path) {
Object.assign(state, {
changedFiles: state.changedFiles.concat(state.entries[path]),
});
},
[types.REMOVE_FILE_FROM_CHANGED](state, path) {
Object.assign(state, {
2021-03-08 18:12:59 +05:30
changedFiles: state.changedFiles.filter((f) => f.path !== path),
2018-05-09 12:01:36 +05:30
});
},
2020-03-13 15:44:24 +05:30
[types.STAGE_CHANGE](state, { path, diffInfo }) {
2021-03-08 18:12:59 +05:30
const stagedFile = state.stagedFiles.find((f) => f.path === path);
2018-10-15 14:42:47 +05:30
Object.assign(state, {
2021-03-08 18:12:59 +05:30
changedFiles: state.changedFiles.filter((f) => f.path !== path),
2018-10-15 14:42:47 +05:30
entries: Object.assign(state.entries, {
[path]: Object.assign(state.entries[path], {
2020-03-13 15:44:24 +05:30
staged: diffInfo.exists,
changed: diffInfo.changed,
tempFile: diffInfo.tempFile,
deleted: diffInfo.deleted,
2018-10-15 14:42:47 +05:30
}),
}),
});
if (stagedFile) {
2020-03-13 15:44:24 +05:30
Object.assign(stagedFile, { ...state.entries[path] });
2018-10-15 14:42:47 +05:30
} else {
2020-03-13 15:44:24 +05:30
state.stagedFiles = [...state.stagedFiles, { ...state.entries[path] }];
}
if (!diffInfo.exists) {
2021-03-08 18:12:59 +05:30
state.stagedFiles = state.stagedFiles.filter((f) => f.path !== path);
2018-10-15 14:42:47 +05:30
}
},
2020-03-13 15:44:24 +05:30
[types.UNSTAGE_CHANGE](state, { path, diffInfo }) {
2021-03-08 18:12:59 +05:30
const changedFile = state.changedFiles.find((f) => f.path === path);
const stagedFile = state.stagedFiles.find((f) => f.path === path);
2018-10-15 14:42:47 +05:30
if (!changedFile && stagedFile) {
Object.assign(state.entries[path], {
...stagedFile,
key: state.entries[path].key,
active: state.entries[path].active,
opened: state.entries[path].opened,
changed: true,
});
2020-03-13 15:44:24 +05:30
state.changedFiles = state.changedFiles.concat(state.entries[path]);
}
if (!diffInfo.exists) {
2021-03-08 18:12:59 +05:30
state.changedFiles = state.changedFiles.filter((f) => f.path !== path);
2018-10-15 14:42:47 +05:30
}
Object.assign(state, {
2021-03-08 18:12:59 +05:30
stagedFiles: state.stagedFiles.filter((f) => f.path !== path),
2018-10-15 14:42:47 +05:30
entries: Object.assign(state.entries, {
[path]: Object.assign(state.entries[path], {
staged: false,
2020-03-13 15:44:24 +05:30
changed: diffInfo.changed,
tempFile: diffInfo.tempFile,
deleted: diffInfo.deleted,
2018-10-15 14:42:47 +05:30
}),
}),
});
},
2018-05-09 12:01:36 +05:30
[types.TOGGLE_FILE_CHANGED](state, { file, changed }) {
Object.assign(state.entries[file.path], {
changed,
});
},
[types.ADD_PENDING_TAB](state, { file, keyPrefix = 'pending' }) {
2018-10-15 14:42:47 +05:30
state.entries[file.path].opened = false;
state.entries[file.path].active = false;
state.entries[file.path].lastOpenedAt = new Date().getTime();
2021-03-08 18:12:59 +05:30
state.openFiles.forEach((f) =>
2018-10-15 14:42:47 +05:30
Object.assign(f, {
opened: false,
active: false,
}),
2018-05-09 12:01:36 +05:30
);
2018-10-15 14:42:47 +05:30
state.openFiles = [
{
...file,
key: `${keyPrefix}-${file.key}`,
pending: true,
opened: true,
active: true,
},
];
2018-05-09 12:01:36 +05:30
},
[types.REMOVE_PENDING_TAB](state, file) {
Object.assign(state, {
2021-03-08 18:12:59 +05:30
openFiles: state.openFiles.filter((f) => f.key !== file.key),
2018-05-09 12:01:36 +05:30
});
},
2019-12-21 20:55:43 +05:30
[types.REMOVE_FILE_FROM_STAGED_AND_CHANGED](state, file) {
Object.assign(state, {
2021-03-08 18:12:59 +05:30
changedFiles: state.changedFiles.filter((f) => f.key !== file.key),
stagedFiles: state.stagedFiles.filter((f) => f.key !== file.key),
2019-12-21 20:55:43 +05:30
});
Object.assign(state.entries[file.path], {
changed: false,
staged: false,
});
},
2018-05-09 12:01:36 +05:30
};