Skip to content

Commit 0f6a29a

Browse files
feat(shortcut): add shortcut for console (codesandbox#3738)
1 parent 4724e58 commit 0f6a29a

File tree

3 files changed

+84
-49
lines changed

3 files changed

+84
-49
lines changed

packages/app/src/app/pages/Sandbox/Editor/Content/index.tsx

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import css from '@styled-system/css';
22

33
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
44
import getTemplateDefinition from '@codesandbox/common/lib/templates';
5+
import { BACKTICK } from '@codesandbox/common/lib/utils/keycodes';
56
import { Icons } from 'app/components/CodeEditor/elements';
67
import { VSCode as CodeEditor } from 'app/components/CodeEditor/VSCode';
78
import { DevTools } from 'app/components/Preview/DevTools';
89
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';
1012
import QuestionIcon from 'react-icons/lib/go/question';
1113
import SplitPane from 'react-split-pane';
1214
import { 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>

packages/common/src/utils/keycodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export const ARROW_UP = 38;
77
export const ARROW_RIGHT = 39;
88
export const ARROW_DOWN = 40;
99
export const DOT = 190;
10+
export const BACKTICK = 192;

standalone-packages/vscode-textmate/package-lock.json

Lines changed: 3 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)