diff --git a/lib/api/src/modules/shortcuts.ts b/lib/api/src/modules/shortcuts.ts index c6f4ee463109..d78bebde5a6f 100644 --- a/lib/api/src/modules/shortcuts.ts +++ b/lib/api/src/modules/shortcuts.ts @@ -118,8 +118,11 @@ export default function initShortcuts({ store }: Module) { handleShortcutFeature(fullApi, feature) { const { layout: { isFullscreen, showNav, showPanel }, + ui: { enableShortcuts }, } = store.getState(); - + if (!enableShortcuts) { + return; + } switch (feature) { case 'escape': { if (isFullscreen) { @@ -274,7 +277,6 @@ export default function initShortcuts({ store }: Module) { event.target.getAttribute('contenteditable') !== null ); } - // Listen for keydown events in the manager document.addEventListener('keydown', (event: Event) => { if (!focusInInput(event)) { diff --git a/lib/ui/src/containers/nav.js b/lib/ui/src/containers/nav.js index d460cc2e455f..2c80936eb661 100755 --- a/lib/ui/src/containers/nav.js +++ b/lib/ui/src/containers/nav.js @@ -15,85 +15,90 @@ const focusableUIElements = { storyPanelRoot: 'storybook-panel-root', }; -const createMenu = memoize(1)((api, shortcutKeys, isFullscreen, showPanel, showNav) => [ - { - id: 'S', - title: 'Show sidebar', - onClick: () => api.toggleNav(), - right: shortcutToHumanString(shortcutKeys.toggleNav), - left: showNav ? : , - }, - { - id: 'A', - title: 'Show addons', - onClick: () => api.togglePanel(), - right: shortcutToHumanString(shortcutKeys.togglePanel), - left: showPanel ? : , - }, - { - id: 'D', - title: 'Change addons orientation', - onClick: () => api.togglePanelPosition(), - right: shortcutToHumanString(shortcutKeys.panelPosition), - left: , - }, - { - id: 'F', - title: 'Go full screen', - onClick: api.toggleFullscreen, - right: shortcutToHumanString(shortcutKeys.fullScreen), - left: isFullscreen ? 'check' : , - }, - { - id: '/', - title: 'Search', - onClick: () => api.focusOnUIElement(focusableUIElements.storySearchField), - right: shortcutToHumanString(shortcutKeys.search), - left: , - }, - { - id: 'up', - title: 'Previous component', - onClick: () => api.jumpToComponent(-1), - right: shortcutToHumanString(shortcutKeys.prevComponent), - left: , - }, - { - id: 'down', - title: 'Next component', - onClick: () => api.jumpToComponent(1), - right: shortcutToHumanString(shortcutKeys.nextComponent), - left: , - }, - { - id: 'prev', - title: 'Previous story', - onClick: () => api.jumpToStory(-1), - right: shortcutToHumanString(shortcutKeys.prevStory), - left: , - }, - { - id: 'next', - title: 'Next story', - onClick: () => api.jumpToStory(1), - right: shortcutToHumanString(shortcutKeys.nextStory), - left: , - }, - { - id: 'about', - title: 'About your Storybook', - onClick: () => api.navigate('/settings/about'), - right: api.versionUpdateAvailable() && Update, - left: , - }, - { - id: 'shortcuts', - title: 'Keyboard shortcuts', - onClick: () => api.navigate('/settings/shortcuts'), - right: shortcutToHumanString(shortcutKeys.shortcutsPage), - left: , - }, -]); +const shortcutToHumanStringIfEnabled = (shortcuts, enableShortcuts) => + enableShortcuts ? shortcutToHumanString(shortcuts) : null; + +const createMenu = memoize(1)( + (api, shortcutKeys, isFullscreen, showPanel, showNav, enableShortcuts) => [ + { + id: 'S', + title: 'Show sidebar', + onClick: () => api.toggleNav(), + right: shortcutToHumanStringIfEnabled(shortcutKeys.toggleNav, enableShortcuts), + left: showNav ? : , + }, + { + id: 'A', + title: 'Show addons', + onClick: () => api.togglePanel(), + right: shortcutToHumanStringIfEnabled(shortcutKeys.togglePanel, enableShortcuts), + left: showPanel ? : , + }, + { + id: 'D', + title: 'Change addons orientation', + onClick: () => api.togglePanelPosition(), + right: shortcutToHumanStringIfEnabled(shortcutKeys.panelPosition, enableShortcuts), + left: , + }, + { + id: 'F', + title: 'Go full screen', + onClick: api.toggleFullscreen, + right: shortcutToHumanStringIfEnabled(shortcutKeys.fullScreen, enableShortcuts), + left: isFullscreen ? 'check' : , + }, + { + id: '/', + title: 'Search', + onClick: () => api.focusOnUIElement(focusableUIElements.storySearchField), + right: shortcutToHumanStringIfEnabled(shortcutKeys.search, enableShortcuts), + left: , + }, + { + id: 'up', + title: 'Previous component', + onClick: () => api.jumpToComponent(-1), + right: shortcutToHumanStringIfEnabled(shortcutKeys.prevComponent, enableShortcuts), + left: , + }, + { + id: 'down', + title: 'Next component', + onClick: () => api.jumpToComponent(1), + right: shortcutToHumanStringIfEnabled(shortcutKeys.nextComponent, enableShortcuts), + left: , + }, + { + id: 'prev', + title: 'Previous story', + onClick: () => api.jumpToStory(-1), + right: shortcutToHumanStringIfEnabled(shortcutKeys.prevStory, enableShortcuts), + left: , + }, + { + id: 'next', + title: 'Next story', + onClick: () => api.jumpToStory(1), + right: shortcutToHumanStringIfEnabled(shortcutKeys.nextStory, enableShortcuts), + left: , + }, + { + id: 'about', + title: 'About your Storybook', + onClick: () => api.navigate('/settings/about'), + right: api.versionUpdateAvailable() && Update, + left: , + }, + { + id: 'shortcuts', + title: 'Keyboard shortcuts', + onClick: () => api.navigate('/settings/shortcuts'), + right: shortcutToHumanStringIfEnabled(shortcutKeys.shortcutsPage, enableShortcuts), + left: , + }, + ] +); const collapseDocsOnlyStories = storiesHash => { // keep track of component IDs that have been rewritten to the ID of their first leaf child @@ -140,16 +145,14 @@ const collapseDocsOnlyStories = storiesHash => { export const mapper = ({ state, api }) => { const { - ui: { name, url }, + ui: { name, url, enableShortcuts }, viewMode, storyId, - layout: { isFullscreen, showPanel, showNav, panelPosition }, + layout: { isFullscreen, showPanel, showNav }, storiesHash, storiesConfigured, } = state; - const stories = collapseDocsOnlyStories(storiesHash); - const shortcutKeys = api.getShortcutKeys(); return { loading: !storiesConfigured, @@ -158,7 +161,7 @@ export const mapper = ({ state, api }) => { stories, storyId, viewMode, - menu: createMenu(api, shortcutKeys, isFullscreen, showPanel, showNav, panelPosition), + menu: createMenu(api, shortcutKeys, isFullscreen, showPanel, showNav, enableShortcuts), menuHighlighted: api.versionUpdateAvailable(), }; };