|
| 1 | +import { Meta, StoryObj } from '@storybook/react'; |
| 2 | +import { |
| 3 | + createMakeCodeURL, |
| 4 | + MakeCodeFrameDriver, |
| 5 | + Options, |
| 6 | +} from '../vanilla/makecode-frame-driver.js'; |
| 7 | +import { Project } from '../vanilla/pxt.js'; |
| 8 | +import StoryWrapper from './StoryWrapper.js'; |
| 9 | +import { defaultMakeCodeProject } from '../vanilla/examples.js'; |
| 10 | + |
| 11 | +interface StoryArgs { |
| 12 | + options?: { |
| 13 | + version?: string; |
| 14 | + lang?: string; |
| 15 | + controller?: 1 | 2; |
| 16 | + queryParams?: Record<string, string>; |
| 17 | + }; |
| 18 | + project?: Project; |
| 19 | + callbacks?: Partial<Options>; |
| 20 | +} |
| 21 | + |
| 22 | +const meta: Meta<StoryArgs> = { |
| 23 | + title: 'makeCodeFrameDriver', |
| 24 | +}; |
| 25 | + |
| 26 | +export default meta; |
| 27 | + |
| 28 | +type Story = StoryObj<StoryArgs>; |
| 29 | + |
| 30 | +const renderEditor = (args: StoryArgs) => { |
| 31 | + const elementId = 'story-wrapper'; |
| 32 | + |
| 33 | + // Create an iframe element. |
| 34 | + const iframe = document.createElement('iframe'); |
| 35 | + iframe.allow = 'usb; autoplay; camera; microphone;'; |
| 36 | + iframe.src = createMakeCodeURL( |
| 37 | + 'https://makecode.microbit.org', |
| 38 | + args.options?.version === 'default' ? undefined : args.options?.version, |
| 39 | + args.options?.lang, |
| 40 | + args.options?.controller ?? 1, |
| 41 | + args.options?.queryParams |
| 42 | + ); |
| 43 | + iframe.width = '100%'; |
| 44 | + iframe.height = '100%'; |
| 45 | + |
| 46 | + // Create and initialise an instance of MakeCodeFrameDriver. |
| 47 | + const driverRef = new MakeCodeFrameDriver( |
| 48 | + { |
| 49 | + initialProjects: async () => (args.project ? [args.project] : []), |
| 50 | + onEditorContentLoaded: (e) => console.log('editorContentLoaded', e), |
| 51 | + onWorkspaceLoaded: (e) => console.log('workspaceLoaded', e), |
| 52 | + onWorkspaceSync: (e) => console.log('workspaceSync', e), |
| 53 | + onWorkspaceReset: (e) => console.log('workspaceReset', e), |
| 54 | + onWorkspaceEvent: (e) => console.log('workspaceEvent', e), |
| 55 | + onWorkspaceSave: (e) => { |
| 56 | + console.log(e.project!.header!.id, e.project); |
| 57 | + }, |
| 58 | + onTutorialEvent: (e) => console.log('tutorialEvent', e), |
| 59 | + ...(args.callbacks ?? {}), |
| 60 | + }, |
| 61 | + () => iframe |
| 62 | + ); |
| 63 | + |
| 64 | + const waitForElementLoaded = () => { |
| 65 | + const targetEl = document.getElementById(elementId); |
| 66 | + if (!targetEl) { |
| 67 | + window.setTimeout(waitForElementLoaded, 500); |
| 68 | + return; |
| 69 | + } |
| 70 | + targetEl.replaceChildren(iframe); |
| 71 | + driverRef.initialize(); |
| 72 | + }; |
| 73 | + waitForElementLoaded(); |
| 74 | + |
| 75 | + return <StoryWrapper id={elementId} />; |
| 76 | +}; |
| 77 | + |
| 78 | +export const MakeCodeEditorWithControlsStory: Story = { |
| 79 | + name: 'MakeCode Editor with controls', |
| 80 | + render: renderEditor, |
| 81 | + args: { |
| 82 | + options: { version: 'default', queryParams: { hideMenu: '' } }, |
| 83 | + project: defaultMakeCodeProject, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +export const MakeCodeEditorControllerAppModeStory: Story = { |
| 88 | + name: 'MakeCode Editor with controller=2 mode', |
| 89 | + render: renderEditor, |
| 90 | + args: { |
| 91 | + options: { version: 'default', controller: 2 }, |
| 92 | + callbacks: { |
| 93 | + onDownload: (download) => console.log('download', download), |
| 94 | + onSave: (save) => console.log('save', save), |
| 95 | + onBack: () => console.log('back'), |
| 96 | + onBackLongPress: () => console.log('back long'), |
| 97 | + }, |
| 98 | + }, |
| 99 | +}; |
0 commit comments