-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14658 from Dschungelabenteuer/fix-issue-14401
API: Add addon keyboard shortcuts & create shortcuts for addon-viewport
- Loading branch information
Showing
9 changed files
with
367 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { API } from '@storybook/api'; | ||
import { ADDON_ID } from './constants'; | ||
|
||
const getCurrentViewportIndex = (viewportsKeys: string[], current: string): number => | ||
viewportsKeys.indexOf(current); | ||
|
||
const getNextViewport = (viewportsKeys: string[], current: string): string => { | ||
const currentViewportIndex = getCurrentViewportIndex(viewportsKeys, current); | ||
return currentViewportIndex === viewportsKeys.length - 1 | ||
? viewportsKeys[0] | ||
: viewportsKeys[currentViewportIndex + 1]; | ||
}; | ||
|
||
const getPreviousViewport = (viewportsKeys: string[], current: string): string => { | ||
const currentViewportIndex = getCurrentViewportIndex(viewportsKeys, current); | ||
return currentViewportIndex < 1 | ||
? viewportsKeys[viewportsKeys.length - 1] | ||
: viewportsKeys[currentViewportIndex - 1]; | ||
}; | ||
|
||
export const registerShortcuts = async (api: API, setState: any, viewportsKeys: string[]) => { | ||
await api.setAddonShortcut(ADDON_ID, { | ||
label: 'Previous viewport', | ||
defaultShortcut: ['shift', 'V'], | ||
actionName: 'previous', | ||
action: () => { | ||
const { selected, isRotated } = api.getAddonState(ADDON_ID); | ||
setState({ | ||
selected: getPreviousViewport(viewportsKeys, 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(viewportsKeys, selected), | ||
isRotated, | ||
}); | ||
}, | ||
}); | ||
|
||
await api.setAddonShortcut(ADDON_ID, { | ||
label: 'Reset viewport', | ||
defaultShortcut: ['alt', 'V'], | ||
actionName: 'reset', | ||
action: () => { | ||
const { isRotated } = api.getAddonState(ADDON_ID); | ||
setState({ | ||
selected: 'reset', | ||
isRotated, | ||
}); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.