@@ -2,11 +2,13 @@ import css from '@styled-system/css';
2
2
3
3
import Tooltip from '@codesandbox/common/lib/components/Tooltip' ;
4
4
import getTemplateDefinition from '@codesandbox/common/lib/templates' ;
5
+ import { BACKTICK } from '@codesandbox/common/lib/utils/keycodes' ;
5
6
import { Icons } from 'app/components/CodeEditor/elements' ;
6
7
import { VSCode as CodeEditor } from 'app/components/CodeEditor/VSCode' ;
7
8
import { DevTools } from 'app/components/Preview/DevTools' ;
8
9
import { useOvermind } from 'app/overmind' ;
9
- import React , { useCallback , useEffect , useRef } from 'react' ;
10
+ import { useKey } from 'react-use' ;
11
+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
10
12
import QuestionIcon from 'react-icons/lib/go/question' ;
11
13
import SplitPane from 'react-split-pane' ;
12
14
import { ThemeProvider } from 'styled-components' ;
@@ -20,6 +22,9 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
20
22
const { state, actions, effects, reaction } = useOvermind ( ) ;
21
23
const editorEl = useRef ( null ) ;
22
24
const contentEl = useRef ( null ) ;
25
+ const [ showConsoleDevtool , setShowConsoleDevtool ] = useState ( false ) ;
26
+ const [ consoleDevtoolIndex , setConsoleDevtoolIndex ] = useState ( - 1 ) ;
27
+
23
28
const updateEditorSize = useCallback (
24
29
function updateEditorSize ( ) {
25
30
if ( editorEl . current ) {
@@ -61,12 +66,32 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
61
66
} ;
62
67
} , [ actions . editor , effects . vscode , reaction , updateEditorSize ] ) ;
63
68
69
+ const views = state . editor . devToolTabs ;
70
+
71
+ useEffect ( ( ) => {
72
+ setConsoleDevtoolIndex ( ( ) =>
73
+ views . findIndex (
74
+ ( { views : panes } ) =>
75
+ panes . findIndex ( pane => pane . id === 'codesandbox.console' ) !== - 1
76
+ )
77
+ ) ;
78
+ } , [ views ] ) ;
79
+
80
+ useKey (
81
+ e => e . ctrlKey && e . keyCode === BACKTICK ,
82
+ e => {
83
+ e . preventDefault ( ) ;
84
+ setShowConsoleDevtool ( value => ! value ) ;
85
+ } ,
86
+ { } ,
87
+ [ ]
88
+ ) ;
89
+
64
90
const { currentModule } = state . editor ;
65
91
const sandbox = state . editor . currentSandbox ;
66
92
const { preferences } = state ;
67
93
const windowVisible = state . editor . previewWindowVisible ;
68
94
const template = sandbox && getTemplateDefinition ( sandbox . template ) ;
69
- const views = state . editor . devToolTabs ;
70
95
const currentPosition = state . editor . currentDevToolsPosition ;
71
96
const modulePath = currentModule . path ;
72
97
const config = template && template . configurationFiles [ modulePath ] ;
@@ -252,46 +277,60 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
252
277
id = "csb-devtools" // used for tabs for highlighting
253
278
>
254
279
{ sandbox &&
255
- views . map ( ( v , i ) => (
256
- < DevTools
257
- // eslint-disable-next-line react/no-array-index-key
258
- key = { i }
259
- devToolIndex = { i }
260
- addedViews = { {
261
- 'codesandbox.browser' : browserConfig ,
262
- } }
263
- setDragging = { dragging => {
264
- if ( dragging ) {
265
- actions . editor . resizingStarted ( ) ;
266
- } else {
267
- actions . editor . resizingStopped ( ) ;
280
+ views . map ( ( v , i ) => {
281
+ // show console devtool if showConsoleDevtool is enabled and if it's in the current view(v)
282
+ const devToolsOpen =
283
+ showConsoleDevtool && consoleDevtoolIndex === i ;
284
+
285
+ return (
286
+ < DevTools
287
+ // eslint-disable-next-line react/no-array-index-key
288
+ key = { i }
289
+ devToolIndex = { i }
290
+ devToolsOpen = { devToolsOpen }
291
+ addedViews = { {
292
+ 'codesandbox.browser' : browserConfig ,
293
+ } }
294
+ setDragging = { dragging => {
295
+ if ( dragging ) {
296
+ actions . editor . resizingStarted ( ) ;
297
+ } else {
298
+ actions . editor . resizingStopped ( ) ;
299
+ }
300
+ } }
301
+ sandboxId = { sandbox . id }
302
+ template = { sandbox . template }
303
+ shouldExpandDevTools = { state . preferences . showDevtools }
304
+ zenMode = { preferences . settings . zenMode }
305
+ setDevToolsOpen = { open => {
306
+ actions . preferences . setDevtoolsOpen ( open ) ;
307
+
308
+ if (
309
+ consoleDevtoolIndex === i &&
310
+ showConsoleDevtool !== open
311
+ ) {
312
+ setShowConsoleDevtool ( open ) ;
313
+ }
314
+ } }
315
+ owned = { sandbox . owned }
316
+ primary = { i === 0 }
317
+ viewConfig = { v }
318
+ moveTab = { ( prevPos , nextPos ) => {
319
+ actions . editor . onDevToolsTabMoved ( { prevPos, nextPos } ) ;
320
+ } }
321
+ closeTab = { pos => {
322
+ actions . editor . onDevToolsTabClosed ( { pos } ) ;
323
+ } }
324
+ currentDevToolIndex = { currentPosition . devToolIndex }
325
+ currentTabPosition = { currentPosition . tabPosition }
326
+ setPane = { position =>
327
+ actions . editor . onDevToolsPositionChanged ( {
328
+ position,
329
+ } )
268
330
}
269
- } }
270
- sandboxId = { sandbox . id }
271
- template = { sandbox . template }
272
- shouldExpandDevTools = { state . preferences . showDevtools }
273
- zenMode = { preferences . settings . zenMode }
274
- setDevToolsOpen = { open =>
275
- actions . preferences . setDevtoolsOpen ( open )
276
- }
277
- owned = { sandbox . owned }
278
- primary = { i === 0 }
279
- viewConfig = { v }
280
- moveTab = { ( prevPos , nextPos ) => {
281
- actions . editor . onDevToolsTabMoved ( { prevPos, nextPos } ) ;
282
- } }
283
- closeTab = { pos => {
284
- actions . editor . onDevToolsTabClosed ( { pos } ) ;
285
- } }
286
- currentDevToolIndex = { currentPosition . devToolIndex }
287
- currentTabPosition = { currentPosition . tabPosition }
288
- setPane = { position =>
289
- actions . editor . onDevToolsPositionChanged ( {
290
- position,
291
- } )
292
- }
293
- />
294
- ) ) }
331
+ />
332
+ ) ;
333
+ } ) }
295
334
</ div >
296
335
</ SplitPane >
297
336
</ div >
0 commit comments