19
19
</div >
20
20
<div class =" code-box-description" v-html =" docHtml" ></div >
21
21
<div class =" code-box-actions" >
22
+ <a-tooltip :title =" $t('app.demo.codesandbox')" >
23
+ <CodeSandboxOutlined
24
+ class =" code-box-code-copy code-box-code-action"
25
+ @click =" handleCodeSandbox"
26
+ />
27
+ </a-tooltip >
22
28
<a-tooltip :title =" $t(`app.demo.type.${type === 'JS' ? 'js' : 'ts'}`)" >
23
29
<span
24
30
class =" code-expand-icon code-box-code-action"
72
78
</div >
73
79
</section >
74
80
<section :class =" highlightClass" >
75
- <div class =" highlight" >
81
+ <div class =" highlight" ref = " codeRef " >
76
82
<slot v-if =" type === 'TS'" name =" htmlCode" />
77
83
<slot v-else name =" jsVersionHtml" />
78
84
</div >
84
90
import type { GlobalConfig } from ' ../App.vue' ;
85
91
import { GLOBAL_CONFIG } from ' ../SymbolKey' ;
86
92
import { computed , defineComponent , inject , onMounted , ref } from ' vue' ;
87
- import { CheckOutlined , SnippetsOutlined } from ' @ant-design/icons-vue' ;
93
+ import { CheckOutlined , SnippetsOutlined , CodeSandboxOutlined } from ' @ant-design/icons-vue' ;
94
+ import { getCodeSandboxParams } from ' ../utils/generateOnlineDemo' ;
95
+ import packageInfo from ' ../../../package.json' ;
96
+
88
97
// import { Modal } from 'ant-design-vue';
89
98
export default defineComponent ({
90
99
name: ' DemoBox' ,
91
100
components: {
92
101
CheckOutlined ,
93
102
SnippetsOutlined ,
103
+ CodeSandboxOutlined ,
94
104
},
95
105
props: {
96
106
jsfiddle: Object ,
@@ -100,6 +110,7 @@ export default defineComponent({
100
110
const type = ref (' TS' );
101
111
const copyTooltipVisible = ref (false );
102
112
const copied = ref (false );
113
+ const codeRef = ref <HTMLDivElement >();
103
114
const sectionId = computed (() => {
104
115
const relativePath = props .jsfiddle ?.relativePath || ' ' ;
105
116
return ` ${relativePath .split (' /' ).join (' -' ).replace (' .vue' , ' ' )} ` ;
@@ -170,6 +181,21 @@ export default defineComponent({
170
181
}
171
182
type .value = type .value === ' TS' ? ' JS' : ' TS' ;
172
183
};
184
+ const handleCodeSandbox = () => {
185
+ const code = codeRef .value ! .innerText ;
186
+ const params = getCodeSandboxParams (code , {
187
+ title: ` ${title .value } - ant-design-vue@${packageInfo .version } ` ,
188
+ });
189
+ const div = document .createElement (' div' );
190
+ div .style .display = ' none' ;
191
+ div .innerHTML = ` <form action="https://codesandbox.io/api/v1/sandboxes/define" method="POST" target="_blank">
192
+ <input type="hidden" name="parameters" value="${params }" />
193
+ <input type="submit" value="Open in sandbox" />
194
+ </form> ` ;
195
+ document .body .appendChild (div );
196
+ (div .firstElementChild as HTMLFormElement ).submit ();
197
+ document .body .removeChild (div );
198
+ };
173
199
const highlightClass = computed (() => {
174
200
return {
175
201
' highlight-wrapper' : true ,
@@ -207,6 +233,8 @@ export default defineComponent({
207
233
highlightClass ,
208
234
sourceCode: decodeURIComponent (escape (window .atob (props .jsfiddle ?.sourceCode ))),
209
235
jsSourceCode: decodeURIComponent (escape (window .atob (props .jsfiddle ?.jsSourceCode ))),
236
+ codeRef ,
237
+ handleCodeSandbox ,
210
238
};
211
239
},
212
240
});
0 commit comments