Skip to content

Commit d8c953c

Browse files
author
Yang Zhen
committed
Fix close term with exit command
1 parent 95da6b1 commit d8c953c

File tree

10 files changed

+94
-14
lines changed

10 files changed

+94
-14
lines changed

app/backendAPI/websocketClients.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class TtySocketClient {
126126
connect () {
127127
if (!config.isPlatform) return
128128
// Need to make sure EVERY ATTEMPT to connect has ensured `fsSocketConnected == true`
129-
if (this.socket.connected || this.connectingPromise) return this.connectingPromise
129+
if (!this.socket || this.socket.connected || this.connectingPromise) return this.connectingPromise
130130
let resolve, reject
131131
this.connectingPromise = new Promise((rsv, rjt) => { resolve = rsv; reject = rjt })
132132
const dispose = autorun(() => { if (config.fsSocketConnected) resolve(true) })
@@ -163,6 +163,7 @@ class TtySocketClient {
163163
close () {
164164
if (config.ttySocketConnected) {
165165
this.socket.disconnect('manual')
166+
TtySocketClient.$$singleton = null
166167
}
167168
}
168169
}

app/commands/commandBindings/file.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ function createFolderAtPath (path) {
4545
}
4646

4747

48-
export function openFile (obj) {
48+
export function openFile (obj, callback) {
4949
if (!obj.path) return
5050
// 做一些encoding的调度
5151
if (FileState.initData.get('_init')) {
5252
when(() => !FileState.initData.get('_init'), () => {
5353
const { encoding } = FileState.initData.get(obj.path) || {}
54-
openFileWithEncoding({ ...obj, encoding })
54+
openFileWithEncoding({ ...obj, encoding, callback })
5555
FileState.initData.set(obj.path, {})
5656
})
5757
} else {
5858
const { encoding } = FileState.initData.get(obj.path) || {}
59-
openFileWithEncoding({ ...obj, encoding })
59+
openFileWithEncoding({ ...obj, encoding, callback })
6060
FileState.initData.set(obj.path, {})
6161
}
6262
}
6363

64-
export function openFileWithEncoding ({ path, editor = {}, others = {}, allGroup = false, encoding }) {
64+
export function openFileWithEncoding ({ path, editor = {}, others = {}, allGroup = false, encoding, callback }) {
6565
const { encoding: currentEncoding } = FileStore.get(path) || {}
6666
return api.readFile(path, encoding || currentEncoding)
6767
.then((data) => {
@@ -79,6 +79,7 @@ export function openFileWithEncoding ({ path, editor = {}, others = {}, allGroup
7979
existingTab.editor.gitBlame = editor.gitBlame
8080
}
8181
existingTab.activate()
82+
if (callback) callback()
8283
} else {
8384
TabStore.createTab({
8485
icon: icons.getClassWithColor(path.split('/').pop()) || 'fa fa-file-text-o',
@@ -88,6 +89,9 @@ export function openFileWithEncoding ({ path, editor = {}, others = {}, allGroup
8889
},
8990
...others
9091
})
92+
if (callback) {
93+
callback()
94+
}
9195
}
9296
})
9397
}
@@ -162,8 +166,10 @@ const fileCommands = {
162166
},
163167
'file:highlight_line': (c) => {
164168
console.log('file:highlight_line', c)
165-
// const { path, lineNumber } = c.data
166-
emitter.emit(FILE_HIGHLIGHT, c.data)
169+
const { path, lineNumber } = c.data
170+
openFile({ path, allGroup: true }, () => {
171+
emitter.emit(FILE_HIGHLIGHT, c.data)
172+
})
167173
},
168174
'file:new_file': (c) => {
169175
const node = c.context

app/commands/commandBindings/misc.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,15 @@ export default {
4040
}
4141
SideBar.activateSidePanelView('SIDEBAR.BOTTOM.terminal')
4242
// $d(Tab.createTabInGroup('tab_group_terminal'))
43-
}
43+
},
44+
'tools:terminal:open_panel': (c) => {
45+
const terminalGroup = terminalState.tabGroups.get('terminalGroup')
46+
if (terminalGroup) {
47+
Terminal.openTerminal()
48+
}
49+
SideBar.activateSidePanelView('SIDEBAR.BOTTOM.terminal')
50+
},
51+
'tools:terminal:close_panel': (c) => {
52+
SideBar.hideSidePanelView('SIDEBAR.BOTTOM.terminal')
53+
},
4454
}

app/components/Editor/components/CodeEditor/BaseCodeEditor.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ class BaseCodeEditor extends Component {
5151
if (this.breakpointHighlight) {
5252
this.cm.removeLineClass(this.breakpointHighlight, 'background', 'breakpoint-highlight')
5353
}
54-
this.breakpointHighlight = this.cm.addLineClass(lineNumber - 1, 'background', 'breakpoint-highlight')
54+
if (lineNumber > 0) {
55+
this.breakpointHighlight = this.cm.addLineClass(lineNumber - 1, 'background', 'breakpoint-highlight')
56+
}
5557
}
5658
}
5759
}

app/components/Panel/SideBar/actions.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SIDEBAR } from '../../Plugins/constants'
77
export const SIDEBAR_ACTIVATE_VIEW = 'SIDEBAR_ACTIVATE_VIEW'
88
export const SIDEBAR_TOGGLE_VIEW = 'SIDEBAR_TOGGLE_VIEW'
99
export const SIDEBAR_SHOW_VIEW = 'SIDEBAR_SHOW_VIEW'
10-
10+
export const SIDEBAR_HIDE_VIEW = 'SIDEBAR_HIDE_VIEW'
1111

1212
const positionToPanel = {
1313
[SIDEBAR.RIGHT]: 'RIGHT',
@@ -30,24 +30,43 @@ const _toggleSidePanelView = (viewId, shouldShow) => {
3030
}
3131
})
3232
targetPanel.show()
33+
emitter.emit(E.PANEL_SHOW, targetPanel)
3334
// 通知插件
3435
if (targetPlugin.actions.onSidebarActive) {
3536
targetPlugin.actions.onSidebarActive()
3637
}
3738
} else {
3839
targetPlugin.status.set('active', false)
3940
targetPanel.hide()
41+
emitter.emit(E.PANEL_HIDE, targetPanel)
4042
// 通知插件
4143
if (targetPlugin.actions.onSidebarDeactive) {
4244
targetPlugin.actions.onSidebarDeactive()
4345
}
4446
}
4547
}
4648

49+
const _hideSidePanelView = (viewId) => {
50+
setTimeout(() => emitter.emit(E.PANEL_RESIZED), 0)
51+
const targetPlugin = pluginState.plugins.get(viewId) || {}
52+
const targetPanel = panelState.panels.get(`PANEL_${positionToPanel[targetPlugin.position]}`)
53+
targetPlugin.status.set('active', false)
54+
targetPanel.hide()
55+
emitter.emit(E.PANEL_HIDE, targetPanel)
56+
// 通知插件
57+
if (targetPlugin.actions.onSidebarDeactive) {
58+
targetPlugin.actions.onSidebarDeactive()
59+
}
60+
}
61+
4762
export const activateSidePanelView = registerAction(SIDEBAR_ACTIVATE_VIEW, (viewId) => {
4863
_toggleSidePanelView(viewId, true)
4964
})
5065

66+
export const hideSidePanelView = registerAction(SIDEBAR_HIDE_VIEW, (viewId) => {
67+
_hideSidePanelView(viewId)
68+
})
69+
5170
export const showSidePanelView = registerAction(SIDEBAR_SHOW_VIEW, ({ viewId, shouldShow }) => {
5271
const plugin = pluginState.plugins.get(viewId)
5372
if (shouldShow) {

app/components/Terminal/Terminal.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Term extends Component {
5858
componentWillUnmount() {
5959
emitter.removeListener(E.PANEL_RESIZED, this.onResize)
6060
emitter.removeListener(E.THEME_CHANGED, this.onTheme)
61+
TerminalState.terminalManager.remove(this.terminal)
6162
}
6263

6364
render() {

app/components/Terminal/TerminalContainer.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { observer } from 'mobx-react'
55
import { TabBar, TabContent, TabContentItem } from 'commons/Tab'
66
import Terminal from './Terminal'
77
import { Tab, TabGroup } from './state'
8+
import { emitter, E } from 'utils'
89

910
import * as Actions from './actions'
1011

@@ -33,6 +34,16 @@ class TerminalContainer extends Component {
3334
this.tabGroup.addTab(tab)
3435
}
3536

37+
componentDidMount () {
38+
emitter.on(E.PANEL_SHOW, this.onShow)
39+
emitter.on(E.PANEL_HIDE, this.onHide)
40+
}
41+
42+
componentWillUnmount () {
43+
emitter.removeListener(E.PANEL_SHOW, this.onShow)
44+
emitter.removeListener(E.PANEL_HIDE, this.onHide)
45+
}
46+
3647
render () {
3748
return (
3849
<div className='tab-container'>
@@ -49,6 +60,15 @@ class TerminalContainer extends Component {
4960
</div>
5061
)
5162
}
63+
64+
onShow (panel) {
65+
if (panel.id === 'PANEL_BOTTOM') {
66+
Actions.openTerminal()
67+
}
68+
}
69+
70+
onHide (panel) {
71+
}
5272
}
5373

5474
export default TerminalContainer

app/components/Terminal/actions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,15 @@ export function inputPath (inputPath) {
108108
export const addTerminal = registerAction('terminal:add', () => {
109109
state.tabGroups.get('terminalGroup').addTab(new Tab())
110110
})
111+
112+
// 如果没有,添加一个
113+
export const openTerminal = registerAction('terminal:open', () => {
114+
const terminalTabGroup = state.tabGroups.get('terminalGroup')
115+
if (terminalTabGroup.tabs.length === 0) {
116+
state.tabGroups.get('terminalGroup').addTab(new Tab())
117+
}
118+
})
119+
120+
export const removeTerminal = registerAction('terminal:remove', (tabId) => {
121+
state.tabGroups.get('terminalGroup').removeTab(tabId)
122+
})

app/components/Terminal/terminal-client.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import _ from 'lodash'
22
import config from 'config'
33
import { autorun, observalbe, runInAction } from 'mobx'
44
import { TtySocketClient } from 'backendAPI/websocketClients'
5-
5+
import * as TabActions from 'components/Tab/actions'
6+
import * as TermActions from './actions'
7+
import * as SideBar from 'components/Panel/SideBar/actions'
68
const WORKSPACE_PATH = '/home/coding/workspace'
79
const BASE_PATH = '~/workspace'
810

@@ -50,7 +52,8 @@ class TerminalClient extends TtySocketClient {
5052
let term
5153
term = _.find(terms, term => term.id === data.id)
5254
if (term) {
53-
return this.actions.removeTab(term.tabId)
55+
this.remove(term)
56+
return TermActions.removeTerminal(term.tabId)
5457
}
5558
})
5659

@@ -125,11 +128,15 @@ class TerminalClient extends TtySocketClient {
125128

126129
remove (removedTerm) {
127130
_.remove(terms, { id: removedTerm.id })
128-
this.socket.emit('term.close', { id: removedTerm.id })
131+
if (this.socket) {
132+
this.socket.emit('term.close', { id: removedTerm.id })
133+
}
129134
if (terms.length == 0) {
130-
this.socket.disconnect()
135+
// this.socket.disconnect()
136+
this.close()
131137
if (this.unbindSocketEvent) this.unbindSocketEvent()
132138
this.socket = null
139+
SideBar.hideSidePanelView('SIDEBAR.BOTTOM.terminal')
133140
}
134141
}
135142

app/utils/emitter/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import EventEmitter from 'eventemitter3'
33
export default new EventEmitter()
44

55
export const PANEL_RESIZED = 'PANEL_RESIZED'
6+
export const PANEL_SHOW = 'PANEL_SHOW'
7+
export const PANEL_HIDE = 'PANEL_HIDE'
68
export const THEME_CHANGED = 'THEME_CHANGED'
79
export const SOCKET_TRIED_FAILED = 'SOCKET_TRIED_FAILED'
810
export const SOCKET_RETRY = 'SOCKET_RETRY'

0 commit comments

Comments
 (0)