From 8ebea0e853544f3963c0a34271814e045faf41a9 Mon Sep 17 00:00:00 2001 From: Yang Zhen Date: Tue, 25 Apr 2017 14:03:27 +0800 Subject: [PATCH] Fix https://github.com/Coding/WebIDE/issues/142 Fix https://github.com/Coding/WebIDE/issues/174 Fix https://github.com/Coding/WebIDE/issues/140 --- app/components/FileTree/state.js | 3 + .../FileTree/subscribeToFileChange.js | 60 +++++++++++++++++-- app/components/Git/GitHistoryView.jsx | 4 +- app/components/Git/actions.js | 3 +- 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/app/components/FileTree/state.js b/app/components/FileTree/state.js index 4896a378..9837de98 100644 --- a/app/components/FileTree/state.js +++ b/app/components/FileTree/state.js @@ -33,6 +33,9 @@ const state = observable({ get root () { return this.nodes.get(ROOT_PATH) }, + get gitStatus () { + return this.nodes.values().filter(node => !node.isDir && node.gitStatus !== 'CLEAN') + }, toJS () { return stateToJS(this) } diff --git a/app/components/FileTree/subscribeToFileChange.js b/app/components/FileTree/subscribeToFileChange.js index cdb734b1..dbc3305c 100644 --- a/app/components/FileTree/subscribeToFileChange.js +++ b/app/components/FileTree/subscribeToFileChange.js @@ -1,23 +1,68 @@ /* @flow weak */ import config from '../../config' import api from '../../backendAPI' -import store, { dispatch } from '../../store' +import store, { getState, dispatch } from '../../store' import mobxStore from '../../mobxStore' import * as FileTreeActions from './actions' +import * as GitActions from '../Git/actions' import * as TabActions from 'commons/Tab/actions' +function handleGitFiles (node) { + const path = node.path + const pathArray = path.split('/.git/refs/heads/') + if (pathArray.length > 1) { + const branchName = pathArray[1] + const gitState = getState().GitState + const current = gitState.branches.current + if (branchName === current) { + const history = gitState.history + const focusedNodes = Object.values(getState().FileTreeState.nodes).filter(node => node.isFocused) + const historyPath = focusedNodes[0] ? focusedNodes[0].path : '/' + dispatch(GitActions.fetchHistory({ + path: historyPath, + size: history.size, + page: 0, + reset: true + })) + api.gitStatus().then(({ files, clean }) => { + const gitStatus = mobxStore.FileTreeState.gitStatus + const result = gitStatus.map(node => { + const file = files.find(file => ('/' + file.name) === node.path) + return { + ...node, + gitStatus: file ? file.status : 'CLEAN', + } + }) + dispatch( + FileTreeActions.loadNodeData( + result + ) + ) + }) + } + return true + } + return false +} + export default function subscribeToFileChange () { - return api.websocketConnectedPromise.then(client => - client.subscribe(`/topic/ws/${config.spaceKey}/change`, frame => { + return api.websocketConnectedPromise.then(client => { + client.subscribe(`/topic/ws/${config.spaceKey}/change`, (frame) => { const data = JSON.parse(frame.body) const node = data.fileInfo switch (data.changeType) { case 'create': + if (handleGitFiles(node)) { + break + } dispatch(FileTreeActions.loadNodeData([node])) break case 'modify': + if (handleGitFiles(node)) { + break + } dispatch(FileTreeActions.loadNodeData([node])) - var tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path) + const tabsToUpdate = mobxStore.EditorTabState.tabs.values().filter(tab => tab.path === node.path) if (tabsToUpdate.length) { api.readFile(node.path).then(({ content }) => { dispatch(TabActions.updateTabByPath({ @@ -32,5 +77,10 @@ export default function subscribeToFileChange () { break } }) - ) + + client.subscribe(`/topic/git/${config.spaceKey}/checkout`, (frame) => { + const data = JSON.parse(frame.body) + dispatch(GitActions.updateCurrentBranch({ name: data.branch })) + }) + }) } diff --git a/app/components/Git/GitHistoryView.jsx b/app/components/Git/GitHistoryView.jsx index f2caaa06..77766fc2 100644 --- a/app/components/Git/GitHistoryView.jsx +++ b/app/components/Git/GitHistoryView.jsx @@ -95,7 +95,7 @@ class History extends Component { }) this.fitHistoryTable() } - if (nextProps.focusedNode !== this.props.focusedNode) { + if (nextProps.focusedNode && nextProps.focusedNode.path !== this.state.path) { this.state.path = nextProps.focusedNode ? nextProps.focusedNode.path : '/' this.fetchHistory({ reset: true }) } @@ -257,7 +257,7 @@ class History extends Component { this.setState({ page: this.state.page + 1 }) - this.fetchHistory() + this.fetchHistory({ reset: false }) } } } diff --git a/app/components/Git/actions.js b/app/components/Git/actions.js index be276bc0..67595621 100644 --- a/app/components/Git/actions.js +++ b/app/components/Git/actions.js @@ -57,7 +57,8 @@ export function checkoutBranch (branch, remoteBranch) { return dispatch => { api.gitCheckout(branch, remoteBranch).then(data => { if (data.status === 'OK') { - dispatch(createAction(GIT_CHECKOUT)({ branch })) + // 完全由 ws 里的 checkout 事件来改变显示 + // dispatch(createAction(GIT_CHECKOUT)({ branch })) dispatch(notify({message: `Check out ${branch}`})) } else if (data.status === 'CONFLICTS') { dispatch(createAction(GIT_CHECKOUT_FAILED)({ branch }))