Skip to content

Commit dd8cd89

Browse files
committed
feat(stackblitz): change to post method
1 parent 384a13a commit dd8cd89

File tree

2 files changed

+97
-40
lines changed

2 files changed

+97
-40
lines changed

integrations/stackblitz/gitbook-manifest.yaml

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ scopes:
1212
- space:metadata:write
1313
- space:views:read
1414
blocks:
15-
- id: stackblitz
15+
- id: diagram
1616
title: Stackblitz
17-
description: My GitBook Integration
17+
description: TODO Stackblitz Integration Description
18+
markdown:
19+
codeblock: stackblitz
20+
body: content
1821
secrets: {}

integrations/stackblitz/src/index.tsx

+92-38
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,107 @@
1-
import {
2-
createIntegration,
3-
createComponent,
4-
FetchEventCallback,
5-
RuntimeContext,
6-
} from '@gitbook/runtime';
1+
import { createIntegration, createComponent } from '@gitbook/runtime';
72

8-
import { createProject } from './stackblitz';
3+
const defaultContent = 'Starter Javascript Project';
94

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',
2214
initialState: (props) => {
2315
return {
24-
message: 'Click Me',
16+
content: props.content || defaultContent,
2517
};
2618
},
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+
4139
return (
4240
<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+
)}
4457
</block>
4558
);
4659
},
4760
});
4861

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+
4971
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='{&quot;rxjs&quot;:&quot;5.5.6&quot;}'>
92+
<input type='hidden' name='project[template]' value='typescript'>
93+
<input type='hidden' name='project[settings]' value='{&quot;compile&quot;:{&quot;clearConsole&quot;: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],
53107
});

0 commit comments

Comments
 (0)