diff options
Diffstat (limited to 'app/assets/javascripts/ide/stores/modules')
9 files changed, 112 insertions, 0 deletions
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(); |