-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcdffdb
commit 7d0427f
Showing
3 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<template> | ||
<IxTooltip :title="lang === 'zh' ? '在 CodeSandbox 中打开' : 'Open on CodeSandbox'"> | ||
<IxIcon name="code" @click="onClick" /> | ||
</IxTooltip> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, inject } from 'vue' | ||
import { appContextToken } from '../../context' | ||
import { getCsbParams } from '../../global/codesandbox' | ||
export default defineComponent({ | ||
name: 'GlobalCodeSandbox', | ||
props: { | ||
code: { | ||
type: String, | ||
default: '', | ||
}, | ||
}, | ||
setup(props) { | ||
const { lang } = inject(appContextToken)! | ||
const onClick = () => { | ||
const div = document.createElement('div') | ||
div.style.display = 'none' | ||
const parameters = getCsbParams(props.code) | ||
const url = `https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}` | ||
div.innerHTML = `<a href=${url} target="_blank" rel="noopener noreferrer"></a>` | ||
document.body.appendChild(div) | ||
div.querySelector<HTMLElement>('a')?.click() | ||
document.body.removeChild(div) | ||
} | ||
return { | ||
onClick, | ||
lang, | ||
} | ||
}, | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { getParameters } from 'codesandbox/lib/api/define' | ||
|
||
export const getCsbParams = (code: string): string => { | ||
return getParameters({ | ||
template: 'node', | ||
files: { | ||
'package.json': { | ||
content: { | ||
scripts: { | ||
dev: 'vite', | ||
}, | ||
dependencies: { | ||
vue: 'next', | ||
'@idux/components': 'latest', | ||
'@idux/pro': 'latest', | ||
'@idux/cdk': 'latest', | ||
}, | ||
devDependencies: { | ||
'@vitejs/plugin-vue': 'latest', | ||
less: 'latest', | ||
typescript: 'latest', | ||
vite: 'latest', | ||
}, | ||
}, | ||
isBinary: false, | ||
}, | ||
'tsconfig.json': { | ||
content: tsconfigJson, | ||
isBinary: false, | ||
}, | ||
'vite.config.ts': { | ||
content: viteConfigTs, | ||
isBinary: false, | ||
}, | ||
'index.html': { | ||
content: indexHtml, | ||
isBinary: false, | ||
}, | ||
'src/Demo.vue': { | ||
content: decodeURIComponent(code), | ||
isBinary: false, | ||
}, | ||
'src/App.vue': { | ||
content: appVue, | ||
isBinary: false, | ||
}, | ||
'src/main.ts': { | ||
content: mainTs, | ||
isBinary: false, | ||
}, | ||
'src/idux.ts': { | ||
content: iduxTs, | ||
isBinary: false, | ||
}, | ||
'src/env.d.ts': { | ||
content: envDTs, | ||
isBinary: false, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const indexHtml = ` | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html>` | ||
|
||
const mainTs = ` | ||
import { createApp } from 'vue'; | ||
import Idux from './idux'; | ||
import App from './App.vue'; | ||
createApp(App).use(Idux).mount('#app');` | ||
|
||
const appVue = ` | ||
<template> | ||
<IxDrawerProvider ref="drawerProviderRef"> | ||
<IxModalProvider ref="modalProviderRef"> | ||
<IxNotificationProvider> | ||
<IxMessageProvider> | ||
<Demo /> | ||
</IxMessageProvider> | ||
</IxNotificationProvider> | ||
</IxModalProvider> | ||
</IxDrawerProvider> | ||
</template> | ||
<script> | ||
import { defineComponent } from 'vue'; | ||
import Demo from './Demo.vue'; | ||
export default defineComponent({ | ||
components: { | ||
Demo, | ||
}, | ||
}); | ||
</script> | ||
<style> | ||
#app { | ||
margin-top: 40px; | ||
margin-left: 20px; | ||
} | ||
</style>` | ||
|
||
const iduxTs = ` | ||
import { App } from 'vue'; | ||
import IduxCdk from '@idux/cdk'; | ||
import IduxComponents from '@idux/components'; | ||
import IduxPro from '@idux/pro'; | ||
import '@idux/components/default.min.css'; | ||
import '@idux/pro/default.min.css'; | ||
// import { useLocale, en_US } from '@idux/components/i18n'; | ||
// useLocale(en_US); | ||
import { | ||
IDUX_ICON_DEPENDENCIES, | ||
addIconDefinitions | ||
} from '@idux/components/icon'; | ||
addIconDefinitions(IDUX_ICON_DEPENDENCIES); | ||
const install = (app: App) => { | ||
app.use(IduxCdk).use(IduxComponents).use(IduxPro); | ||
}; | ||
export default { install };` | ||
|
||
const envDTs = ` | ||
/// <reference types="vite/client" /> | ||
/// <reference types="@idux/cdk/types" /> | ||
/// <reference types="@idux/components/types" /> | ||
/// <reference types="@idux/pro/types" /> | ||
declare module '*.vue' { | ||
import { DefineComponent } from 'vue' | ||
const component: DefineComponent<{}, {}, any> | ||
export default component | ||
}` | ||
|
||
const tsconfigJson = ` | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"useDefineForClassFields": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"jsx": "preserve", | ||
"sourceMap": true, | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"lib": ["esnext", "dom"] | ||
}, | ||
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] | ||
}` | ||
|
||
const viteConfigTs = ` | ||
import { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
export default defineConfig({ | ||
plugins: [vue()], | ||
css: { | ||
preprocessorOptions: { | ||
less: { | ||
javascriptEnabled: true, | ||
additionalData: "@import '@idux/components/style/variable/index.less';", | ||
}, | ||
}, | ||
}, | ||
})` |