@@ -2,11 +2,13 @@ import css from '@styled-system/css';
22
33import Tooltip from '@codesandbox/common/lib/components/Tooltip' ;
44import getTemplateDefinition from '@codesandbox/common/lib/templates' ;
5+ import { BACKTICK } from '@codesandbox/common/lib/utils/keycodes' ;
56import { Icons } from 'app/components/CodeEditor/elements' ;
67import { VSCode as CodeEditor } from 'app/components/CodeEditor/VSCode' ;
78import { DevTools } from 'app/components/Preview/DevTools' ;
89import { 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' ;
1012import QuestionIcon from 'react-icons/lib/go/question' ;
1113import SplitPane from 'react-split-pane' ;
1214import { ThemeProvider } from 'styled-components' ;
@@ -20,6 +22,9 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
2022 const { state, actions, effects, reaction } = useOvermind ( ) ;
2123 const editorEl = useRef ( null ) ;
2224 const contentEl = useRef ( null ) ;
25+ const [ showConsoleDevtool , setShowConsoleDevtool ] = useState ( false ) ;
26+ const [ consoleDevtoolIndex , setConsoleDevtoolIndex ] = useState ( - 1 ) ;
27+
2328 const updateEditorSize = useCallback (
2429 function updateEditorSize ( ) {
2530 if ( editorEl . current ) {
@@ -61,12 +66,32 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
6166 } ;
6267 } , [ actions . editor , effects . vscode , reaction , updateEditorSize ] ) ;
6368
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+
6490 const { currentModule } = state . editor ;
6591 const sandbox = state . editor . currentSandbox ;
6692 const { preferences } = state ;
6793 const windowVisible = state . editor . previewWindowVisible ;
6894 const template = sandbox && getTemplateDefinition ( sandbox . template ) ;
69- const views = state . editor . devToolTabs ;
7095 const currentPosition = state . editor . currentDevToolsPosition ;
7196 const modulePath = currentModule . path ;
7297 const config = template && template . configurationFiles [ modulePath ] ;
@@ -252,46 +277,60 @@ export const MainWorkspace: React.FC<{ theme: any }> = ({ theme }) => {
252277 id = "csb-devtools" // used for tabs for highlighting
253278 >
254279 { 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+ } )
268330 }
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+ } ) }
295334 </ div >
296335 </ SplitPane >
297336 </ div >
0 commit comments