-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-editor-frame-driver.stories.tsx
More file actions
120 lines (109 loc) · 3.06 KB
/
Copy pathpython-editor-frame-driver.stories.tsx
File metadata and controls
120 lines (109 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Meta, StoryObj } from '@storybook/react';
import { useRef } from 'react';
import {
PythonEditorFrameDriver,
PythonProject,
createPythonEditorURL,
} from '../../vanilla/index.js';
import { controllerId } from '../config.js';
import { defaultPythonProject, multiFilePythonProject } from '../fixtures.js';
import PythonEditorToolbar from '../PythonEditorToolbar.js';
import StoryWrapper from '../StoryWrapper.js';
interface StoryArgs {
version?: string;
lang?: string;
controller?: 1 | 2;
queryParams?: Record<string, string>;
project?: PythonProject;
}
const meta: Meta<StoryArgs> = {
title: 'stories/VanillaJS/pythonEditorFrameDriver',
argTypes: {
version: {
options: ['2.0.0', '2.2.2', '3', 'beta'],
defaultValue: '2.0.0',
name: 'version',
control: { type: 'radio' },
},
},
};
export default meta;
type Story = StoryObj<StoryArgs>;
const renderEditor = (args: StoryArgs) => {
const savedProjects = useRef<Map<string, PythonProject>>(new Map());
const ref = useRef<PythonEditorFrameDriver | null>(null);
const cbRef = (div: HTMLElement | null) => {
if (!div) {
return;
}
// Create an iframe element.
const iframe = document.createElement('iframe');
iframe.title = 'PythonEditor';
iframe.allow = 'usb; autoplay; camera; microphone;';
const url = createPythonEditorURL(
args.version ?? '3',
args.lang,
args.controller ?? 1,
args.queryParams
);
iframe.src = url;
iframe.width = '100%';
iframe.height = '100%';
div.replaceChildren(iframe);
// Create and initialise an instance of PythonEditorFrameDriver.
ref.current = new PythonEditorFrameDriver(
{
initialProjects: async () =>
args.project ? [args.project] : [defaultPythonProject],
onWorkspaceLoaded: (e) => console.log('workspaceLoaded', e),
onWorkspaceSync: (e) => console.log('workspaceSync', e),
onWorkspaceSave: (e) => {
savedProjects.current.set(controllerId, e.project);
console.log(savedProjects);
},
},
() => iframe
);
ref.current.initialize();
};
return (
<StoryWrapper>
<PythonEditorToolbar driver={ref} />
<div ref={cbRef} style={{ flexGrow: 1 }} />
</StoryWrapper>
);
};
export const PythonEditorStory: Story = {
name: 'Python Editor versions',
render: renderEditor,
args: {
version: '3',
project: defaultPythonProject,
},
};
// Broken out separately from the above to test the multiFilePythonProject.
// This should also work with the defaultPythonProject.
export const MultiFileProject: Story = {
name: 'Python Editor multi-file project',
render: renderEditor,
args: {
version: '3',
project: multiFilePythonProject,
},
};
export const V3French: Story = {
name: 'Python Editor V3 lang via query string',
render: renderEditor,
args: {
version: '3',
lang: 'fr',
},
};
export const V3NoLangFlag: Story = {
name: 'Python Editor V3 noLang flag',
render: renderEditor,
args: {
version: '3',
queryParams: { flag: 'noLang' },
},
};