Skip to content

Commit

Permalink
docs(vue/element): add codesandbox support (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
muuyao authored Sep 21, 2021
1 parent c8b513e commit 07739bb
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 3 deletions.
114 changes: 114 additions & 0 deletions packages/element/docs/.vuepress/components/createCodeSandBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { getParameters } from 'codesandbox/lib/api/define'

const CodeSandBoxHTML = '<div id="app"></div>'
const CodeSandBoxJS = `
import Vue from 'vue'
import App from './App.vue'
import Element from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
Vue.use(Element, { size: 'small' });
new Vue({
render: h => h(App),
}).$mount('#app')`

const createForm = ({ method, action, data }) => {
const form = document.createElement('form') // 构造 form
form.style.display = 'none' // 设置为不显示
form.target = '_blank' // 指向 iframe

// 构造 formdata
Object.keys(data).forEach((key) => {
const input = document.createElement('input') // 创建 input

input.name = key // 设置 name
input.value = data[key] // 设置 value

form.appendChild(input)
})

form.method = method // 设置方法
form.action = action // 设置地址

document.body.appendChild(form)

// 对该 form 执行提交
form.submit()

document.body.removeChild(form)
}

export function createCodeSandBox(codeStr) {
const parameters = getParameters({
files: {
'sandbox.config.json': {
content: {
template: 'node',
infiniteLoopProtection: true,
hardReloadOnChange: false,
view: 'browser',
container: {
port: 8080,
node: '14',
},
},
},
'package.json': {
content: {
scripts: {
serve: 'vue-cli-service serve',
build: 'vue-cli-service build',
lint: 'vue-cli-service lint',
},
dependencies: {
'@formily/core': 'latest',
'@formily/vue': 'latest',
'@formily/element': 'latest',
axios: '^0.21.1',
'core-js': '^3.6.5',
'element-ui': 'latest',
'vue-demi': 'latest',
vue: '^2.6.11',
},
devDependencies: {
'@vue/cli-plugin-babel': '~4.5.0',
'@vue/cli-service': '~4.5.0',
'@vue/composition-api': 'latest',
'vue-template-compiler': '^2.6.11',
sass: '^1.34.1',
'sass-loader': '^8.0.2',
},
babel: {
presets: ['@vue/cli-plugin-babel/preset'],
},
vue: {
devServer: {
host: '0.0.0.0',
disableHostCheck: true, // 必须
},
},
},
},
'src/App.vue': {
content: codeStr,
},
'src/main.js': {
content: CodeSandBoxJS,
},
'public/index.html': {
content: CodeSandBoxHTML,
},
},
})

createForm({
method: 'post',
action: 'https://codesandbox.io/api/v1/sandboxes/define',
data: {
parameters,
query: 'file=/src/App.vue',
},
})
}
20 changes: 19 additions & 1 deletion packages/element/docs/.vuepress/components/dumi-previewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
</div>

<div class="dumi-previewer-actions">
<div></div>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
class="dumi-previewer-actions__icon"
viewBox="0 0 256 296"
@click="openCodeSandBox"
>
<path
d="M115.498 261.088v-106.61L23.814 101.73v60.773l41.996 24.347v45.7l49.688 28.54zm23.814.627l50.605-29.151V185.78l42.269-24.495v-60.011l-92.874 53.621v106.82zm80.66-180.887l-48.817-28.289l-42.863 24.872l-43.188-24.897l-49.252 28.667l91.914 52.882l92.206-53.235zM0 222.212V74.495L127.987 0L256 74.182v147.797l-128.016 73.744L0 222.212z"
fill="#000"
></path>
</svg>
</div>

<div>
<svg
Expand Down Expand Up @@ -71,6 +84,7 @@
<script>
import copy from 'copy-to-clipboard'
import highlight from './highlight'
import { createCodeSandBox } from './createCodeSandBox'
export default {
name: 'dumi-previewer',
Expand Down Expand Up @@ -148,6 +162,10 @@ export default {
this.copied = false
}, 2000)
},
openCodeSandBox() {
createCodeSandBox(this.demoStr)
},
},
}
</script>
Expand Down
3 changes: 2 additions & 1 deletion packages/element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"ttypescript": "^1.5.12",
"vue": "^2.6.0",
"vuepress": "^1.8.2",
"vuepress-plugin-typescript": "^0.3.1"
"vuepress-plugin-typescript": "^0.3.1",
"codesandbox": "^2.2.3"
},
"dependencies": {
"@formily/core": "2.0.0-rc.9",
Expand Down
112 changes: 112 additions & 0 deletions packages/vue/docs/.vuepress/components/createCodeSandBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { getParameters } from 'codesandbox/lib/api/define'

const CodeSandBoxHTML = '<div id="app"></div>'
const CodeSandBoxJS = `
import Vue from 'vue'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import App from './App.vue'
Vue.config.productionTip = false;
Vue.use(Antd);
new Vue({
render: h => h(App),
}).$mount('#app')`

const createForm = ({ method, action, data }) => {
const form = document.createElement('form') // 构造 form
form.style.display = 'none' // 设置为不显示
form.target = '_blank' // 指向 iframe

// 构造 formdata
Object.keys(data).forEach((key) => {
const input = document.createElement('input') // 创建 input

input.name = key // 设置 name
input.value = data[key] // 设置 value

form.appendChild(input)
})

form.method = method // 设置方法
form.action = action // 设置地址

document.body.appendChild(form)

// 对该 form 执行提交
form.submit()

document.body.removeChild(form)
}

export function createCodeSandBox(codeStr) {
const parameters = getParameters({
files: {
'sandbox.config.json': {
content: {
template: 'node',
infiniteLoopProtection: true,
hardReloadOnChange: false,
view: 'browser',
container: {
port: 8080,
node: '14',
},
},
},
'package.json': {
content: {
scripts: {
serve: 'vue-cli-service serve',
build: 'vue-cli-service build',
lint: 'vue-cli-service lint',
},
dependencies: {
'@formily/core': 'latest',
'@formily/vue': 'latest',
'core-js': '^3.6.5',
'ant-design-vue': 'latest',
'vue-demi': 'latest',
vue: '^2.6.11',
},
devDependencies: {
'@vue/cli-plugin-babel': '~4.5.0',
'@vue/cli-service': '~4.5.0',
'@vue/composition-api': 'latest',
'vue-template-compiler': '^2.6.11',
},
babel: {
presets: ['@vue/cli-plugin-babel/preset'],
},
vue: {
devServer: {
host: '0.0.0.0',
disableHostCheck: true, // 必须
},
},
},
},
'src/App.vue': {
content: codeStr,
},
'src/main.js': {
content: CodeSandBoxJS,
},
'public/index.html': {
content: CodeSandBoxHTML,
},
},
})

createForm({
method: 'post',
action: 'https://codesandbox.io/api/v1/sandboxes/define',
data: {
parameters,
query: 'file=/src/App.vue',
},
})
}
20 changes: 19 additions & 1 deletion packages/vue/docs/.vuepress/components/dumi-previewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
</div>

<div class="dumi-previewer-actions">
<div></div>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
class="dumi-previewer-actions__icon"
viewBox="0 0 256 296"
@click="openCodeSandBox"
>
<path
d="M115.498 261.088v-106.61L23.814 101.73v60.773l41.996 24.347v45.7l49.688 28.54zm23.814.627l50.605-29.151V185.78l42.269-24.495v-60.011l-92.874 53.621v106.82zm80.66-180.887l-48.817-28.289l-42.863 24.872l-43.188-24.897l-49.252 28.667l91.914 52.882l92.206-53.235zM0 222.212V74.495L127.987 0L256 74.182v147.797l-128.016 73.744L0 222.212z"
fill="#000"
></path>
</svg>
</div>

<div>
<svg
Expand Down Expand Up @@ -71,6 +84,7 @@
<script>
import copy from 'copy-to-clipboard'
import highlight from './highlight'
import { createCodeSandBox } from './createCodeSandBox'
export default {
name: 'dumi-previewer',
Expand Down Expand Up @@ -144,6 +158,10 @@ export default {
this.copied = false
}, 2000)
},
openCodeSandBox() {
createCodeSandBox(this.demoStr)
},
},
}
</script>
Expand Down
1 change: 1 addition & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vuepress/plugin-back-to-top": "^1.8.2",
"@vuepress/plugin-medium-zoom": "^1.8.2",
"ant-design-vue": "^1.7.3",
"codesandbox": "^2.2.3",
"core-js": "^2.4.0",
"vue": "^2.6.12",
"vuepress": "^1.8.2",
Expand Down

0 comments on commit 07739bb

Please sign in to comment.