diff options
Diffstat (limited to 'app/assets/javascripts/ide/stores')
15 files changed, 127 insertions, 49 deletions
diff --git a/app/assets/javascripts/ide/stores/actions.js b/app/assets/javascripts/ide/stores/actions.js index b8d59f8bd36..1496170447d 100644 --- a/app/assets/javascripts/ide/stores/actions.js +++ b/app/assets/javascripts/ide/stores/actions.js @@ -5,7 +5,7 @@ import { visitUrl } from '~/lib/utils/url_utility'; import { deprecatedCreateFlash as flash } from '~/flash'; import * as types from './mutation_types'; import { decorateFiles } from '../lib/files'; -import { stageKeys } from '../constants'; +import { stageKeys, commitActionTypes } from '../constants'; import service from '../services'; import eventHub from '../eventhub'; @@ -242,7 +242,7 @@ export const renameEntry = ({ dispatch, commit, state, getters }, { path, name, } } - dispatch('triggerFilesChange'); + dispatch('triggerFilesChange', { type: commitActionTypes.move, path, newPath }); }; export const getBranchData = ({ commit, state }, { projectId, branchId, force = false } = {}) => diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js index a0df85540f9..4b9b958ddd6 100644 --- a/app/assets/javascripts/ide/stores/actions/file.js +++ b/app/assets/javascripts/ide/stores/actions/file.js @@ -164,26 +164,6 @@ export const changeFileContent = ({ commit, state, getters }, { path, content }) } }; -export const setFileLanguage = ({ getters, commit }, { fileLanguage }) => { - if (getters.activeFile) { - commit(types.SET_FILE_LANGUAGE, { file: getters.activeFile, fileLanguage }); - } -}; - -export const setEditorPosition = ({ getters, commit }, { editorRow, editorColumn }) => { - if (getters.activeFile) { - commit(types.SET_FILE_POSITION, { - file: getters.activeFile, - editorRow, - editorColumn, - }); - } -}; - -export const setFileViewMode = ({ commit }, { file, viewMode }) => { - commit(types.SET_FILE_VIEWMODE, { file, viewMode }); -}; - export const restoreOriginalFile = ({ dispatch, state, commit }, path) => { const file = state.entries[path]; const isDestructiveDiscard = file.tempFile || file.prevPath; @@ -289,7 +269,7 @@ export const removePendingTab = ({ commit }, file) => { eventHub.$emit(`editor.update.model.dispose.${file.key}`); }; -export const triggerFilesChange = () => { +export const triggerFilesChange = (ctx, payload = {}) => { // Used in EE for file mirroring - eventHub.$emit('ide.files.change'); + eventHub.$emit('ide.files.change', payload); }; diff --git a/app/assets/javascripts/ide/stores/index.js b/app/assets/javascripts/ide/stores/index.js index 324c5b0c6e4..d543209716a 100644 --- a/app/assets/javascripts/ide/stores/index.js +++ b/app/assets/javascripts/ide/stores/index.js @@ -12,6 +12,8 @@ import fileTemplates from './modules/file_templates'; import paneModule from './modules/pane'; import clientsideModule from './modules/clientside'; import routerModule from './modules/router'; +import editorModule from './modules/editor'; +import { setupFileEditorsSync } from './modules/editor/setup'; Vue.use(Vuex); @@ -29,7 +31,14 @@ export const createStoreOptions = () => ({ rightPane: paneModule(), clientside: clientsideModule(), router: routerModule, + editor: editorModule, }, }); -export const createStore = () => new Vuex.Store(createStoreOptions()); +export const createStore = () => { + const store = new Vuex.Store(createStoreOptions()); + + setupFileEditorsSync(store); + + return store; +}; diff --git a/app/assets/javascripts/ide/stores/modules/commit/getters.js b/app/assets/javascripts/ide/stores/modules/commit/getters.js index 37f887bcf0a..416ca88d6c9 100644 --- a/app/assets/javascripts/ide/stores/modules/commit/getters.js +++ b/app/assets/javascripts/ide/stores/modules/commit/getters.js @@ -14,6 +14,8 @@ const createTranslatedTextForFiles = (files, text) => { export const discardDraftButtonDisabled = state => state.commitMessage === '' || state.submitCommitLoading; +// Note: If changing the structure of the placeholder branch name, please also +// update #patch_branch_name in app/helpers/tree_helper.rb export const placeholderBranchName = (state, _, rootState) => `${gon.current_username}-${rootState.currentBranchId}-patch-${`${new Date().getTime()}`.substr( -BRANCH_SUFFIX_COUNT, diff --git a/app/assets/javascripts/ide/stores/modules/editor/actions.js b/app/assets/javascripts/ide/stores/modules/editor/actions.js new file mode 100644 index 00000000000..cc23a655235 --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/actions.js @@ -0,0 +1,19 @@ +import * as types from './mutation_types'; + +/** + * Action to update the current file editor info at the given `path` with the given `data` + * + * @param {} vuex + * @param {{ path: String, data: any }} payload + */ +export const updateFileEditor = ({ commit }, payload) => { + commit(types.UPDATE_FILE_EDITOR, payload); +}; + +export const removeFileEditor = ({ commit }, path) => { + commit(types.REMOVE_FILE_EDITOR, path); +}; + +export const renameFileEditor = ({ commit }, payload) => { + commit(types.RENAME_FILE_EDITOR, payload); +}; diff --git a/app/assets/javascripts/ide/stores/modules/editor/getters.js b/app/assets/javascripts/ide/stores/modules/editor/getters.js new file mode 100644 index 00000000000..dabaafa453a --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/getters.js @@ -0,0 +1,13 @@ +import { getFileEditorOrDefault } from './utils'; + +export const activeFileEditor = (state, getters, rootState, rootGetters) => { + const { activeFile } = rootGetters; + + if (!activeFile) { + return null; + } + + const { path } = rootGetters.activeFile; + + return getFileEditorOrDefault(state.fileEditors, path); +}; diff --git a/app/assets/javascripts/ide/stores/modules/editor/index.js b/app/assets/javascripts/ide/stores/modules/editor/index.js new file mode 100644 index 00000000000..8a7437b427d --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/index.js @@ -0,0 +1,12 @@ +import * as actions from './actions'; +import * as getters from './getters'; +import state from './state'; +import mutations from './mutations'; + +export default { + namespaced: true, + actions, + state, + mutations, + getters, +}; diff --git a/app/assets/javascripts/ide/stores/modules/editor/mutation_types.js b/app/assets/javascripts/ide/stores/modules/editor/mutation_types.js new file mode 100644 index 00000000000..89b7e9cbc76 --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/mutation_types.js @@ -0,0 +1,3 @@ +export const UPDATE_FILE_EDITOR = 'UPDATE_FILE_EDITOR'; +export const REMOVE_FILE_EDITOR = 'REMOVE_FILE_EDITOR'; +export const RENAME_FILE_EDITOR = 'RENAME_FILE_EDITOR'; diff --git a/app/assets/javascripts/ide/stores/modules/editor/mutations.js b/app/assets/javascripts/ide/stores/modules/editor/mutations.js new file mode 100644 index 00000000000..f332fe9dce9 --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/mutations.js @@ -0,0 +1,25 @@ +import Vue from 'vue'; +import * as types from './mutation_types'; +import { getFileEditorOrDefault } from './utils'; + +export default { + [types.UPDATE_FILE_EDITOR](state, { path, data }) { + const editor = getFileEditorOrDefault(state.fileEditors, path); + + Vue.set(state.fileEditors, path, Object.assign(editor, data)); + }, + [types.REMOVE_FILE_EDITOR](state, path) { + Vue.delete(state.fileEditors, path); + }, + [types.RENAME_FILE_EDITOR](state, { path, newPath }) { + const existing = state.fileEditors[path]; + + // Gracefully do nothing if fileEditor isn't found. + if (!existing) { + return; + } + + Vue.delete(state.fileEditors, path); + Vue.set(state.fileEditors, newPath, existing); + }, +}; diff --git a/app/assets/javascripts/ide/stores/modules/editor/setup.js b/app/assets/javascripts/ide/stores/modules/editor/setup.js new file mode 100644 index 00000000000..c5a613c6baa --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/setup.js @@ -0,0 +1,19 @@ +import eventHub from '~/ide/eventhub'; +import { commitActionTypes } from '~/ide/constants'; + +const removeUnusedFileEditors = store => { + Object.keys(store.state.editor.fileEditors) + .filter(path => !store.state.entries[path]) + .forEach(path => store.dispatch('editor/removeFileEditor', path)); +}; + +export const setupFileEditorsSync = store => { + eventHub.$on('ide.files.change', ({ type, ...payload } = {}) => { + if (type === commitActionTypes.move) { + store.dispatch('editor/renameFileEditor', payload); + } else { + // The files have changed, but the specific change is not known. + removeUnusedFileEditors(store); + } + }); +}; diff --git a/app/assets/javascripts/ide/stores/modules/editor/state.js b/app/assets/javascripts/ide/stores/modules/editor/state.js new file mode 100644 index 00000000000..484aeec5cc3 --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/state.js @@ -0,0 +1,8 @@ +export default () => ({ + // Object which represents a dictionary of filePath to editor specific properties, including: + // - fileLanguage + // - editorRow + // - editorCol + // - viewMode + fileEditors: {}, +}); diff --git a/app/assets/javascripts/ide/stores/modules/editor/utils.js b/app/assets/javascripts/ide/stores/modules/editor/utils.js new file mode 100644 index 00000000000..bef21d04b2b --- /dev/null +++ b/app/assets/javascripts/ide/stores/modules/editor/utils.js @@ -0,0 +1,11 @@ +import { FILE_VIEW_MODE_EDITOR } from '../../../constants'; + +export const createDefaultFileEditor = () => ({ + editorRow: 1, + editorColumn: 1, + fileLanguage: '', + viewMode: FILE_VIEW_MODE_EDITOR, +}); + +export const getFileEditorOrDefault = (fileEditors, path) => + fileEditors[path] || createDefaultFileEditor(); diff --git a/app/assets/javascripts/ide/stores/mutation_types.js b/app/assets/javascripts/ide/stores/mutation_types.js index ae119c2b1fd..22ff29e8866 100644 --- a/app/assets/javascripts/ide/stores/mutation_types.js +++ b/app/assets/javascripts/ide/stores/mutation_types.js @@ -36,9 +36,6 @@ export const SET_FILE_ACTIVE = 'SET_FILE_ACTIVE'; export const SET_FILE_RAW_DATA = 'SET_FILE_RAW_DATA'; export const SET_FILE_BASE_RAW_DATA = 'SET_FILE_BASE_RAW_DATA'; export const UPDATE_FILE_CONTENT = 'UPDATE_FILE_CONTENT'; -export const SET_FILE_LANGUAGE = 'SET_FILE_LANGUAGE'; -export const SET_FILE_POSITION = 'SET_FILE_POSITION'; -export const SET_FILE_VIEWMODE = 'SET_FILE_VIEWMODE'; export const DISCARD_FILE_CHANGES = 'DISCARD_FILE_CHANGES'; export const ADD_FILE_TO_CHANGED = 'ADD_FILE_TO_CHANGED'; export const REMOVE_FILE_FROM_CHANGED = 'REMOVE_FILE_FROM_CHANGED'; diff --git a/app/assets/javascripts/ide/stores/mutations/file.js b/app/assets/javascripts/ide/stores/mutations/file.js index a981f86fa40..61a55d45128 100644 --- a/app/assets/javascripts/ide/stores/mutations/file.js +++ b/app/assets/javascripts/ide/stores/mutations/file.js @@ -95,17 +95,6 @@ export default { changed, }); }, - [types.SET_FILE_LANGUAGE](state, { file, fileLanguage }) { - Object.assign(state.entries[file.path], { - fileLanguage, - }); - }, - [types.SET_FILE_POSITION](state, { file, editorRow, editorColumn }) { - Object.assign(state.entries[file.path], { - editorRow, - editorColumn, - }); - }, [types.SET_FILE_MERGE_REQUEST_CHANGE](state, { file, mrChange }) { let diffMode = diffModes.replaced; if (mrChange.new_file) { @@ -122,11 +111,6 @@ export default { }, }); }, - [types.SET_FILE_VIEWMODE](state, { file, viewMode }) { - Object.assign(state.entries[file.path], { - viewMode, - }); - }, [types.DISCARD_FILE_CHANGES](state, path) { const stagedFile = state.stagedFiles.find(f => f.path === path); const entry = state.entries[path]; diff --git a/app/assets/javascripts/ide/stores/utils.js b/app/assets/javascripts/ide/stores/utils.js index b7ced3a271a..96f3caf1e98 100644 --- a/app/assets/javascripts/ide/stores/utils.js +++ b/app/assets/javascripts/ide/stores/utils.js @@ -1,4 +1,4 @@ -import { commitActionTypes, FILE_VIEW_MODE_EDITOR } from '../constants'; +import { commitActionTypes } from '../constants'; import { relativePathToAbsolute, isAbsolute, @@ -25,10 +25,6 @@ export const dataStructure = () => ({ rawPath: '', raw: '', content: '', - editorRow: 1, - editorColumn: 1, - fileLanguage: '', - viewMode: FILE_VIEW_MODE_EDITOR, size: 0, parentPath: null, lastOpenedAt: 0, |