|
1 |
| -import { |
2 |
| - createIntegration, |
3 |
| - createComponent, |
4 |
| - FetchEventCallback, |
5 |
| - RuntimeContext, |
6 |
| -} from '@gitbook/runtime'; |
| 1 | +import { createIntegration, createComponent } from '@gitbook/runtime'; |
7 | 2 |
|
8 |
| -import { createProject } from './stackblitz'; |
| 3 | +const defaultContent = 'Starter Javascript Project'; |
9 | 4 |
|
10 |
| -type IntegrationContext = {} & RuntimeContext; |
11 |
| - |
12 |
| -const handleFetchEvent: FetchEventCallback<IntegrationContext> = async (request, context) => { |
13 |
| - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
14 |
| - const { api } = context; |
15 |
| - const user = api.user.getAuthenticatedUser(); |
16 |
| - |
17 |
| - return new Response(JSON.stringify(user)); |
18 |
| -}; |
19 |
| - |
20 |
| -const exampleBlock = createComponent({ |
21 |
| - componentId: 'stackblitz', |
| 5 | +const diagramBlock = createComponent< |
| 6 | + { |
| 7 | + content?: string; |
| 8 | + }, |
| 9 | + { |
| 10 | + content: string; |
| 11 | + } |
| 12 | +>({ |
| 13 | + componentId: 'diagram', |
22 | 14 | initialState: (props) => {
|
23 | 15 | return {
|
24 |
| - message: 'Click Me', |
| 16 | + content: props.content || defaultContent, |
25 | 17 | };
|
26 | 18 | },
|
27 |
| - action: async (element, action, context) => { |
28 |
| - switch (action.action) { |
29 |
| - case 'click': |
30 |
| - console.log('Button Clicked'); |
31 |
| - return {}; |
32 |
| - } |
33 |
| - }, |
34 |
| - render: async (element, action, context) => { |
35 |
| - console.log('RENDER'); |
36 |
| - try{ |
37 |
| - await createProject(); |
38 |
| - } catch (e) { |
39 |
| - console.log(e); |
40 |
| - } |
| 19 | + async render(element, { environment }) { |
| 20 | + const { editable } = element.context; |
| 21 | + const { content } = element.state; |
| 22 | + |
| 23 | + element.setCache({ |
| 24 | + maxAge: 86400, |
| 25 | + }); |
| 26 | + |
| 27 | + const output = ( |
| 28 | + <webframe |
| 29 | + source={{ |
| 30 | + url: environment.integration.urls.publicEndpoint, |
| 31 | + }} |
| 32 | + aspectRatio={16 / 9} |
| 33 | + data={{ |
| 34 | + content: element.dynamicState('content'), |
| 35 | + }} |
| 36 | + /> |
| 37 | + ); |
| 38 | + |
41 | 39 | return (
|
42 | 40 | <block>
|
43 |
| - <button label={element.state.message} onPress={{ action: 'click' }} /> |
| 41 | + {editable ? ( |
| 42 | + <codeblock |
| 43 | + state="content" |
| 44 | + content={content} |
| 45 | + syntax="stackblitz" |
| 46 | + onContentChange={{ |
| 47 | + action: '@editor.node.updateProps', |
| 48 | + props: { |
| 49 | + content: element.dynamicState('content'), |
| 50 | + }, |
| 51 | + }} |
| 52 | + footer={[output]} |
| 53 | + /> |
| 54 | + ) : ( |
| 55 | + output |
| 56 | + )} |
44 | 57 | </block>
|
45 | 58 | );
|
46 | 59 | },
|
47 | 60 | });
|
48 | 61 |
|
| 62 | +// Required Form Fields |
| 63 | + |
| 64 | +// project[title] = Project title |
| 65 | +// project[description] = Project description |
| 66 | +// project[files][FILE_PATH] = Contents of file, specify file path as key |
| 67 | +// project[files][ANOTHER_FILE_PATH] = Contents of file, specify file path as key |
| 68 | +// project[dependencies] = JSON string of dependencies field from package.json |
| 69 | +// project[template] = Can be one of: typescript, angular-cli, create-react-app, javascript |
| 70 | + |
49 | 71 | export default createIntegration({
|
50 |
| - fetch: handleFetchEvent, |
51 |
| - components: [exampleBlock], |
52 |
| - events: {}, |
| 72 | + fetch: async () => { |
| 73 | + return new Response( |
| 74 | + `<html lang='en'> |
| 75 | +<head></head> |
| 76 | +<body> |
| 77 | +
|
| 78 | +<form id='mainForm' method='post' action='https://stackblitz.com/run' target='_self'> |
| 79 | +<input type='hidden' name='project[files][index.ts]' value="import { Observable } from 'rxjs/Observable'; |
| 80 | +import 'rxjs/add/observable/fromEvent'; |
| 81 | +import 'rxjs/add/operator/scan'; |
| 82 | +
|
| 83 | +var button = document.querySelector('button'); |
| 84 | +Observable.fromEvent(button, 'click') |
| 85 | + .scan((count: number) => count + 1, 0) |
| 86 | + .subscribe(count => console.log(\`Clicked times\`)); |
| 87 | +"> |
| 88 | +<input type='hidden' name='project[files][index.html]' value='<button>Click Me</button> |
| 89 | +'> |
| 90 | +<input type='hidden' name='project[description]' value='RxJS Example'> |
| 91 | +<input type='hidden' name='project[dependencies]' value='{"rxjs":"5.5.6"}'> |
| 92 | +<input type='hidden' name='project[template]' value='typescript'> |
| 93 | +<input type='hidden' name='project[settings]' value='{"compile":{"clearConsole":false}}'> |
| 94 | +</form> |
| 95 | +<script>document.getElementById("mainForm").submit();</script> |
| 96 | +
|
| 97 | +</body></html>`, |
| 98 | + { |
| 99 | + headers: { |
| 100 | + 'Content-Type': 'text/html', |
| 101 | + 'Cache-Control': 'public, max-age=86400', |
| 102 | + }, |
| 103 | + } |
| 104 | + ); |
| 105 | + }, |
| 106 | + components: [diagramBlock], |
53 | 107 | });
|
0 commit comments