Best JavaScript code snippet using storybook-root
shortcuts.ts
Source:shortcuts.ts
1import { navigator, document } from 'global';2import { PREVIEW_KEYDOWN } from '@storybook/core-events';3import { Module, API } from '../index';4import { shortcutMatchesShortcut, eventToShortcut } from '../lib/shortcut';5import { focusableUIElements } from './layout';6export const isMacLike = () =>7 navigator && navigator.platform ? !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) : false;8export const controlOrMetaKey = () => (isMacLike() ? 'meta' : 'control');9export function keys<O>(o: O) {10 return Object.keys(o) as (keyof O)[];11}12export interface SubState {13 shortcuts: Shortcuts;14}15export interface SubAPI {16 getShortcutKeys(): Shortcuts;17 setShortcuts(shortcuts: Shortcuts): Promise<Shortcuts>;18 setShortcut(action: Action, value: KeyCollection): Promise<KeyCollection>;19 restoreAllDefaultShortcuts(): Promise<Shortcuts>;20 restoreDefaultShortcut(action: Action): Promise<KeyCollection>;21 handleKeydownEvent(api: API, event: Event): void;22 handleShortcutFeature(api: API, feature: Action): void;23}24export type KeyCollection = string[];25export interface Shortcuts {26 fullScreen: KeyCollection;27 togglePanel: KeyCollection;28 panelPosition: KeyCollection;29 toggleNav: KeyCollection;30 toolbar: KeyCollection;31 search: KeyCollection;32 focusNav: KeyCollection;33 focusIframe: KeyCollection;34 focusPanel: KeyCollection;35 prevComponent: KeyCollection;36 nextComponent: KeyCollection;37 prevStory: KeyCollection;38 nextStory: KeyCollection;39 shortcutsPage: KeyCollection;40 aboutPage: KeyCollection;41 escape: KeyCollection;42 collapseAll: KeyCollection;43 expandAll: KeyCollection;44}45export type Action = keyof Shortcuts;46export const defaultShortcuts: Shortcuts = Object.freeze({47 fullScreen: ['F'],48 togglePanel: ['A'],49 panelPosition: ['D'],50 toggleNav: ['S'],51 toolbar: ['T'],52 search: ['/'],53 focusNav: ['1'],54 focusIframe: ['2'],55 focusPanel: ['3'],56 prevComponent: ['alt', 'ArrowUp'],57 nextComponent: ['alt', 'ArrowDown'],58 prevStory: ['alt', 'ArrowLeft'],59 nextStory: ['alt', 'ArrowRight'],60 shortcutsPage: [controlOrMetaKey(), 'shift', ','],61 aboutPage: [','],62 escape: ['escape'], // This one is not customizable63 collapseAll: [controlOrMetaKey(), 'shift', 'ArrowUp'],64 expandAll: [controlOrMetaKey(), 'shift', 'ArrowDown'],65});66export interface Event extends KeyboardEvent {67 target: {68 tagName: string;69 addEventListener(): void;70 removeEventListener(): boolean;71 dispatchEvent(event: Event): boolean;72 getAttribute(attr: string): string | null;73 };74}75export default function initShortcuts({ store }: Module) {76 const api: SubAPI = {77 // Getting and setting shortcuts78 getShortcutKeys(): Shortcuts {79 return store.getState().shortcuts;80 },81 async setShortcuts(shortcuts: Shortcuts) {82 await store.setState({ shortcuts }, { persistence: 'permanent' });83 return shortcuts;84 },85 async restoreAllDefaultShortcuts() {86 return api.setShortcuts(defaultShortcuts);87 },88 async setShortcut(action, value) {89 const shortcuts = api.getShortcutKeys();90 await api.setShortcuts({ ...shortcuts, [action]: value });91 return value;92 },93 async restoreDefaultShortcut(action) {94 const defaultShortcut = defaultShortcuts[action];95 return api.setShortcut(action, defaultShortcut);96 },97 // Listening to shortcut events98 handleKeydownEvent(fullApi, event) {99 const shortcut = eventToShortcut(event);100 const shortcuts = api.getShortcutKeys();101 const actions = keys(shortcuts);102 const matchedFeature = actions.find((feature: Action) =>103 shortcutMatchesShortcut(shortcut, shortcuts[feature])104 );105 if (matchedFeature) {106 api.handleShortcutFeature(fullApi, matchedFeature);107 }108 },109 handleShortcutFeature(fullApi, feature) {110 const {111 layout: { isFullscreen, showNav, showPanel },112 ui: { enableShortcuts },113 } = store.getState();114 if (!enableShortcuts) {115 return;116 }117 switch (feature) {118 case 'escape': {119 if (isFullscreen) {120 fullApi.toggleFullscreen();121 } else if (!showNav) {122 fullApi.toggleNav();123 }124 break;125 }126 case 'focusNav': {127 if (isFullscreen) {128 fullApi.toggleFullscreen();129 }130 if (!showNav) {131 fullApi.toggleNav();132 }133 fullApi.focusOnUIElement(focusableUIElements.storyListMenu);134 break;135 }136 case 'search': {137 if (isFullscreen) {138 fullApi.toggleFullscreen();139 }140 if (!showNav) {141 fullApi.toggleNav();142 }143 setTimeout(() => {144 fullApi.focusOnUIElement(focusableUIElements.storySearchField);145 }, 0);146 break;147 }148 case 'focusIframe': {149 const element = document.getElementById('storybook-preview-iframe');150 if (element) {151 try {152 // should be like a channel message and all that, but yolo for now153 element.contentWindow.focus();154 } catch (e) {155 //156 }157 }158 break;159 }160 case 'focusPanel': {161 if (isFullscreen) {162 fullApi.toggleFullscreen();163 }164 if (!showPanel) {165 fullApi.togglePanel();166 }167 fullApi.focusOnUIElement(focusableUIElements.storyPanelRoot);168 break;169 }170 case 'nextStory': {171 fullApi.jumpToStory(1);172 break;173 }174 case 'prevStory': {175 fullApi.jumpToStory(-1);176 break;177 }178 case 'nextComponent': {179 fullApi.jumpToComponent(1);180 break;181 }182 case 'prevComponent': {183 fullApi.jumpToComponent(-1);184 break;185 }186 case 'fullScreen': {187 fullApi.toggleFullscreen();188 break;189 }190 case 'togglePanel': {191 if (isFullscreen) {192 fullApi.toggleFullscreen();193 fullApi.resetLayout();194 }195 fullApi.togglePanel();196 break;197 }198 case 'toggleNav': {199 if (isFullscreen) {200 fullApi.toggleFullscreen();201 fullApi.resetLayout();202 }203 fullApi.toggleNav();204 break;205 }206 case 'toolbar': {207 fullApi.toggleToolbar();208 break;209 }210 case 'panelPosition': {211 if (isFullscreen) {212 fullApi.toggleFullscreen();213 }214 if (!showPanel) {215 fullApi.togglePanel();216 }217 fullApi.togglePanelPosition();218 break;219 }220 case 'aboutPage': {221 fullApi.navigate('/settings/about');222 break;223 }224 case 'shortcutsPage': {225 fullApi.navigate('/settings/shortcuts');226 break;227 }228 case 'collapseAll': {229 fullApi.collapseAll();230 break;231 }232 case 'expandAll': {233 fullApi.expandAll();234 break;235 }236 default:237 break;238 }239 },240 };241 const { shortcuts: persistedShortcuts = defaultShortcuts }: SubState = store.getState();242 const state: SubState = {243 // Any saved shortcuts that are still in our set of defaults244 shortcuts: keys(defaultShortcuts).reduce(245 (acc, key) => ({ ...acc, [key]: persistedShortcuts[key] || defaultShortcuts[key] }),246 defaultShortcuts247 ),248 };249 const init = ({ api: fullApi }: API) => {250 function focusInInput(event: Event) {251 return (252 /input|textarea/i.test(event.target.tagName) ||253 event.target.getAttribute('contenteditable') !== null254 );255 }256 // Listen for keydown events in the manager257 document.addEventListener('keydown', (event: Event) => {258 if (!focusInInput(event)) {259 fullApi.handleKeydownEvent(fullApi, event);260 }261 });262 // Also listen to keydown events sent over the channel263 fullApi.on(PREVIEW_KEYDOWN, (data: { event: Event }) => {264 fullApi.handleKeydownEvent(fullApi, data.event);265 });266 };267 const result = { api, state, init };268 return result;...
PhalApi.js
Source:PhalApi.js
1/**2 * PhalApiæ¡æ¶ JS请æ±SDK3 *4 * "ç«äº_个åª"æä¾,å客å°åw-blog.cn5 * æ好çæè§æ建议请èç³»æ-><wenzhenxi@vip.qq.com> 2015-10-206 *7 * å为3ç§è¯·æ±æ¹å¼:get,poståget_jsonp8 *9 * ææ请æ±åç»ä¸ä¼ é4个åæ°å¼(请æ±å°å,æ¥å£å称.请æ±åæ°GETä¼ éæ¼æ¥å¥½çåæ°10 * Postä¼ éæ°ç»key-valueå¼,åè°å½æ°)11 *12 * ç»ä¸ä½¿ç¨æ¹å¼å¦ä¸13 * var url = '';14 * var api = '';15 * var data = '';16 * query_get(url, api, data, function(rs){17 * //åè°å½æ° rs为è¿åç»æå·²ç»åjsonå18 * if(rs.ret == 200){19 * æåå¤ç20 * }else{21 * 失败å¤ç22 * }23 * });24 *25 */26//-------------------------------é
置项------------------------------------27var debug = true; //è°è¯æ¨¡å¼28//-------------------------------é
置项------------------------------------29/**30 * æ®éçpost请æ±æ¹æ³31 **/32function query_post(api_url, api_name, data, callback){33 //æ¼æ¥è¯·æ±çURLå°å34 var fullapi = api_url + '?service=' + api_name;35 //æ§è¡è¯·æ±36 $.ajax({37 url : fullapi, //请æ±å°å38 method : 'POST', //请æ±æ¹å¼39 crossDomain: true,40 data : data, //请æ±åæ°41 complete : function(rs){42 //åJsonå43 rs = JSON.parse(rs.response || rs.responseText);44 //æè¿åç»æè¿åå°æ§å¶å°(debug模å¼èªå¨å¼å¯)45 if(debug == true){46 console.log(fullapi, 'back', rs);47 }48 //åè°å½æ°49 callback(rs);50 }51 });52}53/**54 * æ®éçget请æ±æ¹æ³55 **/56function query_get(api_url, api_name, data, callback){57 //æ¼æ¥è¯·æ±çURLå°å58 var fullapi = api_url + '?service=' + api_name + data;59 //æ§è¡è¯·æ±60 $.ajax({61 url : fullapi, //请æ±å°å62 method : 'GET', //请æ±æ¹å¼63 complete: function(rs){64 //åJsonå65 rs = JSON.parse(rs.response || rs.responseText);66 //æè¿åç»æè¿åå°æ§å¶å°(debug模å¼èªå¨å¼å¯)67 if(debug == true){68 console.log(fullapi, 'back', rs);69 }70 //åè°å½æ°71 callback(rs);72 }73 });74}75/**76 * JsonP请æ±æ¹æ³(ç¨äºè·¨å请æ±,åªè½è¿è¡get请æ±)77 **/78function query_jsonp(api_url, api_name, data, callback){79 //æ¼æ¥è¯·æ±çURLå°å(&callback=1æ¯Phalapié»è®¤ä½¿ç¨JsonPæ ¼å¼)80 var fullapi = api_url + '?service=' + api_name + '&callback=1' + data;81 //æ§è¡è¯·æ±82 $.ajax({83 type : "get",84 async : false,85 url : fullapi, //请æ±åæ°86 dataType: "jsonp",87 jsonp : "callback", //ä¼ éç»è¯·æ±å¤çç¨åºæ页é¢çï¼ç¨ä»¥è·å¾jsonpåè°å½æ°åçåæ°å(ä¸è¬é»è®¤ä¸º:callback)88 success : function(rs){89 //æè¿åç»æè¿åå°æ§å¶å°(debug模å¼èªå¨å¼å¯)90 if(debug == true){91 console.log(fullapi, 'back', rs);92 }93 //åè°å½æ°94 callback(rs);95 },96 error : function(error){97 alert('fail');98 }99 });...
Using AI Code Generation
1import { addDecorator } from "@storybook/react";2import { withFullAPI } from "storybook-root-decorator";3addDecorator(withFullAPI);4import { addDecorator } from "@storybook/react";5import { withCustomDecorator } from "storybook-root-decorator";6addDecorator(withCustomDecorator);7import { addDecorator } from "@storybook/react";8import { withCustomDecoratorWithProps } from "storybook-root-decorator";9addDecorator(withCustomDecoratorWithProps);10import { addDecorator } from "@storybook/react";11import { withCustomDecoratorWithPropsAndContext } from "storybook-root-decorator";12addDecorator(withCustomDecoratorWithPropsAndContext);13import { addDecorator } from "@storybook/react";14import { withCustomDecoratorWithPropsContextAndFullAPI } from "storybook-root-decorator";15addDecorator(withCustomDecoratorWithPropsContextAndFullAPI
Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { fullAPI } from 'storybook-root-decorator';4const stories = storiesOf('Test', module);5stories.add('test', () => (6));7fullAPI(stories);8MIT © [kylemcdonald](
Using AI Code Generation
1import { addDecorator } from "@storybook/react";2import { withRootDecorator } from "storybook-root-decorator";3addDecorator(withRootDecorator);4import { addDecorator } from "@storybook/react";5import { muiTheme } from "storybook-addon-material-ui";6import theme from "./theme";7addDecorator(muiTheme([theme]));
Using AI Code Generation
1import { fullAPI } from 'storybook-root';2fullAPI()3 .then(api => console.log(api))4 .catch(error => console.error(error));5import { fullAPI } from 'storybook-root';6fullAPI()7 .then(api => console.log(api))8 .catch(error => console.error(error));9### `fullAPI()`10### `storybookRoot()`11Returns the path to the root directory of the Storybook instance. This is useful for importing modules from the Storybook instance, for example:12import { storybookRoot } from 'storybook-root';13const { fullAPI } = require(`${storybookRoot()}/lib/api`);14fullAPI()15 .then(api => console.log(api))16 .catch(error => console.error(error));17### `storybookVersion()`18If you have any questions or suggestions, feel free to [open an issue](
Using AI Code Generation
1import { addDecorator } from '@storybook/react';2import { withRootDecorator } from 'storybook-root-decorator';3addDecorator(withRootDecorator);4import { addDecorator } from '@storybook/react';5import { addWrapper } from 'storybook-root-decorator';6import { Provider } from 'react-redux';7const store = createStore();8addDecorator(addWrapper(Provider, { store }));9import { addDecorator } from '@storybook/react';10import { addWrapper } from 'storybook-root-decorator';11import { Provider } from 'react-redux';12import { ThemeProvider } from 'styled-components';13const store = createStore();14const theme = { color: 'red' };15addDecorator(addWrapper(Provider, { store }));16addDecorator(addWrapper(ThemeProvider, { theme }));17import { addDecorator } from '@storybook/react';18import { addWrapper } from 'storybook-root-decorator';19import { Provider } from 'react-redux';20import { ThemeProvider } from 'styled-components';21const store = createStore();22const theme = { color: 'red' };23addDecorator(addWrapper(Provider, { store }, 'ReduxProvider'));24addDecorator(addWrapper(ThemeProvider, { theme }, 'StyledComponentsThemeProvider'));25import { addDecorator } from '@storybook/react';26import { addWrapper } from 'storybook-root-decorator';27import { Provider } from 'react-redux';28import { ThemeProvider } from 'styled-components';29const store = createStore();30const theme = { color: 'red' };31addDecorator(addWrapper(Provider, { store }, 'ReduxProvider', 1));32addDecorator(addWrapper(ThemeProvider, { theme }, 'StyledComponentsThemeProvider', 0));
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!