Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: 新增 CodeSandbox 支持 #3422

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"@types/sortablejs": "^1.15.1",
"@types/tinycolor2": "^1.4.3",
"@types/validator": "^13.7.17",
"codesandbox": "^2.2.3",
uyarn marked this conversation as resolved.
Show resolved Hide resolved
"dayjs": "^1.11.9",
"lodash": "^4.17.21",
"mitt": "^3.0.1",
Expand Down Expand Up @@ -203,4 +204,4 @@
"attributes": "helper/attributes.json"
},
"web-types": "helper/web-types.json"
}
}
1 change: 1 addition & 0 deletions site/plugin-doc/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function renderDemo(md, container) {
const tpl = `
<td-doc-demo component-name="${componentName.trim()}" :code=${demoCodeDefName} demo-name="${demoName}" language="markup">
<div slot="action">
<CodeSandbox demo-name="${demoName}" component-name="${componentName}" :code=${demoCodeDefName} />
uyarn marked this conversation as resolved.
Show resolved Hide resolved
<Stackblitz demo-name="${demoName}" component-name="${componentName}" :code=${demoCodeDefName} />
</div>
<div class="tdesign-demo-item__body">
Expand Down
77 changes: 77 additions & 0 deletions site/src/components/codeSandbox/getCodeSandboxParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { getParameters } from 'codesandbox/lib/api/define';
import orgPkg from '../../../../package.json';

const indexHtml = `<!DOCTYPE html>
<html lang="en">
<head>
<title>TDesign Demo</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
`;

const App = `
<template>
<demo />
</template>

<script>
import { defineComponent } from "vue";
import Demo from "./Demo.vue";

export default defineComponent({
components: {
Demo,
},
});
</script>`;

const mainJs = `import { createApp } from 'vue';
import TDesign from 'tdesign-vue-next';
import App from './App.vue';
import 'tdesign-vue-next/es/style/index.css';

const app = createApp(App)
app.use(TDesign);
app.mount("#app");
`;

export function getCodeSandboxParams(code, meta) {
return getParameters({
files: {
'package.json': {
content: JSON.stringify({
title: meta.title,
dependencies: {
vue: orgPkg.devDependencies.vue,
less: orgPkg.devDependencies.less,
'tdesign-vue-next': orgPkg.version,
'tdesign-icons-vue-next': orgPkg.dependencies['tdesign-icons-vue-next'],
},
devDependencies: {
'@vue/cli-plugin-babel': '~4.5.0',
},
}),
isBinary: false,
},
'index.html': {
content: indexHtml,
isBinary: false,
},
'src/Demo.vue': {
content: code,
isBinary: false,
},
'src/App.vue': {
content: App,
isBinary: false,
},
'src/main.js': {
content: mainJs,
isBinary: false,
},
},
});
}
63 changes: 63 additions & 0 deletions site/src/components/codeSandbox/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<template>
<t-tooltip content="在 CodeSandbox 中打开">
<form
ref="codeformRef"
action="https://codesandbox.io/api/v1/sandboxes/define"
method="POST"
target="_blank"
@click="submit"
>
<input type="hidden" name="parameters" :value="params" />

<div class="action-online">
<svg
focusable="false"
data-icon="code-sandbox"
width="1em"
height="1em"
fill="currentColor"
aria-hidden="true"
viewBox="64 64 896 896"
>
<path
d="M709.6 210l.4-.2h.2L512 96 313.9 209.8h-.2l.7.3L151.5 304v416L512 928l360.5-208V304l-162.9-94zM482.7 843.6L339.6 761V621.4L210 547.8V372.9l272.7 157.3v313.4zM238.2 321.5l134.7-77.8 138.9 79.7 139.1-79.9 135.2 78-273.9 158-274-158zM814 548.3l-128.8 73.1v139.1l-143.9 83V530.4L814 373.1v175.2z"
></path>
</svg>
</div>
</form>
</t-tooltip>
</template>

<script>
import { defineComponent, computed, ref } from 'vue';
import { getCodeSandboxParams } from './getCodeSandboxParams';

export default defineComponent({
name: 'Stackblitz',
props: {
code: String,
demoName: String,
componentName: String,
},
setup(props) {
const params = computed(() =>
getCodeSandboxParams(props.code, {
title: `${props.demoName} - ${props.componentName}`,
}),
);
const codeformRef = ref(null);

const submit = () => {
console.log(111);
codeformRef.value.submit();
};
return {
params,
codeformRef,
submit,
};
},
});
</script>

<style lang="scss" scoped></style>
2 changes: 2 additions & 0 deletions site/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import App from './app.vue';
import router from './routes';

import Stackblitz from './components/stackblitz/index.vue';
import CodeSandbox from './components/codeSandbox/index.vue';
import BaseUsage from './components/base-usage.vue';

// import tdesign style
Expand All @@ -27,6 +28,7 @@ registerLocaleChange();
const app = createApp(App);

app.component('Stackblitz', Stackblitz);
app.component('CodeSandbox', CodeSandbox);
app.component('BaseUsage', BaseUsage);

app.use(TDesign).use(router).mount('#app');
Loading