Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add addon keyboard shortcuts & create shortcuts for addon-viewport #14658

Merged
merged 5 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion addons/viewport/src/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { styled, Global, Theme, withTheme } from '@storybook/theming';

import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components';

import { useParameter, useAddonState } from '@storybook/api';
import { useStorybookApi, useParameter, useAddonState } from '@storybook/api';
import { registerShortcuts } from './shortcuts';
import { PARAM_KEY, ADDON_ID } from './constants';
import { MINIMAL_VIEWPORTS } from './defaults';
import { ViewportAddonParameter, ViewportMap, ViewportStyles, Styles } from './models';
Expand Down Expand Up @@ -135,13 +136,19 @@ export const ViewportTool: FunctionComponent = memo(
});

const list = toList(viewports);
const api = useStorybookApi();

if (!list.find((i) => i.id === defaultViewport)) {
// eslint-disable-next-line no-console
console.warn(
`Cannot find "defaultViewport" of "${defaultViewport}" in addon-viewport configs, please check the "viewports" setting in the configuration.`
);
}

useEffect(() => {
registerShortcuts(api, setState);
}, []);

useEffect(() => {
setState({
selected:
Expand Down
59 changes: 59 additions & 0 deletions addons/viewport/src/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { API } from '@storybook/api';
import { ADDON_ID } from './constants';
import { MINIMAL_VIEWPORTS } from './defaults';

const viewportsKeys = Object.keys(MINIMAL_VIEWPORTS);
Copy link
Contributor

@kaelig kaelig Apr 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a custom list of viewports is set, will it be registered? (It's a dynamic list, and can completely change between two stories)

Copy link
Member Author

@Dschungelabenteuer Dschungelabenteuer Apr 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch, I had not thought of that at all! Thank you for pointing that out, I'll be fixing it as soon as possible! Edit: custom viewports are now correctly registered 🎉

const getCurrentViewportIndex = (current: string): number => viewportsKeys.indexOf(current);
const getNextViewport = (current: string): string => {
const currentViewportIndex = getCurrentViewportIndex(current);
return currentViewportIndex === viewportsKeys.length - 1
? viewportsKeys[0]
: viewportsKeys[currentViewportIndex + 1];
};
const getPreviousViewport = (current: string): string => {
const currentViewportIndex = getCurrentViewportIndex(current);
return currentViewportIndex < 1
? viewportsKeys[viewportsKeys.length - 1]
: viewportsKeys[currentViewportIndex - 1];
};

export const registerShortcuts = async (api: API, setState: any) => {
await api.setAddonShortcut(ADDON_ID, {
label: 'Previous viewport',
defaultShortcut: ['shift', 'V'],
actionName: 'previous',
action: () => {
const { selected, isRotated } = api.getAddonState(ADDON_ID);
setState({
selected: getPreviousViewport(selected),
isRotated,
});
},
});

await api.setAddonShortcut(ADDON_ID, {
label: 'Next viewport',
defaultShortcut: ['V'],
actionName: 'next',
action: () => {
const { selected, isRotated } = api.getAddonState(ADDON_ID);
setState({
selected: getNextViewport(selected),
isRotated,
});
},
});

await api.setAddonShortcut(ADDON_ID, {
label: 'Reset viewport',
defaultShortcut: ['control', 'V'],
actionName: 'reset',
action: () => {
const { isRotated } = api.getAddonState(ADDON_ID);
setState({
selected: 'reset',
isRotated,
});
},
});
};
8 changes: 8 additions & 0 deletions docs/essentials/viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,11 @@ You can change your story through [parameters](../writing-stories/parameters.md)
/>

<!-- prettier-ignore-end -->

### Keyboard shortcuts

* Previous viewport: `shift + v`
Dschungelabenteuer marked this conversation as resolved.
Show resolved Hide resolved
* Next viewport: `v`
* Reset viewport: `control + v`

These shortcuts can be edited in Storybook's Keyboard shortcuts page.
56 changes: 54 additions & 2 deletions lib/api/src/modules/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export interface SubState {

export interface SubAPI {
getShortcutKeys(): Shortcuts;
getDefaultShortcuts(): Shortcuts | AddonShortcutDefaults;
getAddonsShortcuts(): AddonShortcuts;
getAddonsShortcutLabels(): AddonShortcutLabels;
getAddonsShortcutDefaults(): AddonShortcutDefaults;
setShortcuts(shortcuts: Shortcuts): Promise<Shortcuts>;
setShortcut(action: Action, value: KeyCollection): Promise<KeyCollection>;
setAddonShortcut(addon: string, shortcut: AddonShortcut): Promise<AddonShortcut>;
restoreAllDefaultShortcuts(): Promise<Shortcuts>;
restoreDefaultShortcut(action: Action): Promise<KeyCollection>;
handleKeydownEvent(event: Event): void;
Expand Down Expand Up @@ -52,6 +57,17 @@ export interface Shortcuts {

export type Action = keyof Shortcuts;

interface AddonShortcut {
label: string;
defaultShortcut: KeyCollection;
actionName: string;
showInMenu?: boolean;
action: (...args: any[]) => any;
}
type AddonShortcuts = Record<string, AddonShortcut>;
type AddonShortcutLabels = Record<string, string>;
type AddonShortcutDefaults = Record<string, KeyCollection>;

export const defaultShortcuts: Shortcuts = Object.freeze({
fullScreen: ['F'],
togglePanel: ['A'],
Expand All @@ -73,6 +89,7 @@ export const defaultShortcuts: Shortcuts = Object.freeze({
expandAll: [controlOrMetaKey(), 'shift', 'ArrowDown'],
});

const addonsShortcuts: AddonShortcuts = {};
export interface Event extends KeyboardEvent {
target: {
tagName: string;
Expand All @@ -96,20 +113,54 @@ export const init: ModuleFn = ({ store, fullAPI }) => {
getShortcutKeys(): Shortcuts {
return store.getState().shortcuts;
},
getDefaultShortcuts(): Shortcuts | AddonShortcutDefaults {
return {
...defaultShortcuts,
...api.getAddonsShortcutDefaults(),
};
},
getAddonsShortcuts(): AddonShortcuts {
return addonsShortcuts;
},
getAddonsShortcutLabels(): AddonShortcutLabels {
const labels: AddonShortcutLabels = {};
Object.entries(api.getAddonsShortcuts()).forEach(([actionName, { label }]) => {
labels[actionName] = label;
});

return labels;
},
getAddonsShortcutDefaults(): AddonShortcutDefaults {
const defaults: AddonShortcutDefaults = {};
Object.entries(api.getAddonsShortcuts()).forEach(([actionName, { defaultShortcut }]) => {
defaults[actionName] = defaultShortcut;
});

return defaults;
},
async setShortcuts(shortcuts: Shortcuts) {
await store.setState({ shortcuts }, { persistence: 'permanent' });
return shortcuts;
},
async restoreAllDefaultShortcuts() {
return api.setShortcuts(defaultShortcuts);
return api.setShortcuts(api.getDefaultShortcuts() as Shortcuts);
},
async setShortcut(action, value) {
const shortcuts = api.getShortcutKeys();
await api.setShortcuts({ ...shortcuts, [action]: value });
return value;
},
async setAddonShortcut(addon: string, shortcut: AddonShortcut) {
const shortcuts = api.getShortcutKeys();
await api.setShortcuts({
...shortcuts,
[`${addon}-${shortcut.actionName}`]: shortcut.defaultShortcut,
});
addonsShortcuts[`${addon}-${shortcut.actionName}`] = shortcut;
return shortcut;
},
async restoreDefaultShortcut(action) {
const defaultShortcut = defaultShortcuts[action];
const defaultShortcut = api.getDefaultShortcuts()[action];
return api.setShortcut(action, defaultShortcut);
},

Expand Down Expand Up @@ -277,6 +328,7 @@ export const init: ModuleFn = ({ store, fullAPI }) => {
break;
}
default:
addonsShortcuts[feature].action();
break;
}
},
Expand Down
Loading