Skip to content

Commit 8bc06d5

Browse files
author
shangbin
committed
update: 支持输出单页html
1 parent 5dd1db1 commit 8bc06d5

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

src/components/Code.vue

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
<template>
22
<el-dialog title="代码预览" v-model="codeDialogVisible" width="70%" top="10vh" :before-close="handleClose" :center=true>
33
<!-- 这里加v-if是因为CodeEditor内部不支持watch数据监测 -->
4-
<CodeEditor v-if="codeDialogVisible" style="max-height: 65vh;" ref="codeEditor" :initCode="prettyCode" mode="text/html"></CodeEditor>
4+
<CodeEditor v-if="codeDialogVisible" style="max-height: 65vh;" ref="codeEditor" :initCode="outputCode"
5+
mode="text/html"></CodeEditor>
56
<div style="color: #666; font-size: 12px; text-align:center; margin: 5px;">使用代码前请确认相应的组件库已集成至项目</div>
6-
<template v-slot:footer>
7-
<span>
8-
<el-tooltip effect="dark" content="拷贝" placement="left">
9-
<img class="round-icon" :src="iconCopy" alt="" @click="copyCheck">
10-
</el-tooltip>
11-
<el-tooltip effect="dark" content="下载" placement="right">
12-
<img class="round-icon" :src="iconDownload" alt="" @click="download">
13-
</el-tooltip>
14-
</span>
15-
</template>
7+
<div style="text-aligin:center;">
8+
<el-row>
9+
<el-col :span="12">
10+
<el-radio-group v-model="outputMode">
11+
<el-radio label="vue">Vue</el-radio>
12+
<el-radio label="html">单页Html</el-radio>
13+
</el-radio-group>
14+
</el-col>
15+
<el-col :span="12">
16+
<el-tooltip effect="dark" content="拷贝" placement="left">
17+
<img class="round-icon" :src="iconCopy" alt="" @click="copyCheck">
18+
</el-tooltip>
19+
<el-tooltip effect="dark" content="下载" placement="right">
20+
<img class="round-icon" :src="iconDownload" alt="" @click="download">
21+
</el-tooltip>
22+
</el-col>
23+
</el-row>
24+
</div>
1625
</el-dialog>
1726

1827
</template>
@@ -25,6 +34,7 @@ import copy from 'copy-to-clipboard';
2534
import { saveAs } from "file-saver";
2635
2736
import CodeEditor from './CodeEditor.vue'
37+
import singleIndexOutput from '../libs/singleIndexOutput.js';
2838
2939
export default {
3040
props: ['rawCode', 'codeDialogVisible'],
@@ -35,7 +45,7 @@ export default {
3545
data() {
3646
return {
3747
// 在此自动生成
38-
48+
outputMode: "vue",
3949
iconCopy: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/copy-outline.svg"),
4050
iconDownload: ("https://static.imonkey.xueersi.com/download/vcc-resource/icon/code-download-outline.svg"),
4151
};
@@ -59,14 +69,14 @@ export default {
5969
this.copy();
6070
},
6171
copy() {
62-
if (copy(this.prettyCode)) {
72+
if (copy(this.outputCode)) {
6373
this.$message.success("代码已复制到剪贴板");
6474
} else {
6575
this.$message.error("代码复制有点问题?");
6676
}
6777
},
6878
download() {
69-
let blob = new Blob([this.prettyCode], {
79+
let blob = new Blob([this.outputCode], {
7080
type: "text/plain;charset=utf-8",
7181
});
7282
saveAs(blob, "VueComponent.vue");
@@ -83,6 +93,14 @@ export default {
8393
}
8494
},
8595
computed: {
96+
isVueMode() {
97+
return this.outputMode === 'vue';
98+
},
99+
outputCode() {
100+
return this.isVueMode ? this.prettyCode : this.singleIndex;
101+
},
102+
103+
86104
prettyCode() {
87105
try {
88106
return prettier.format(this.rawCode, {
@@ -95,6 +113,19 @@ export default {
95113
}
96114
97115
},
116+
117+
singleIndex() {
118+
const htmlCode = singleIndexOutput(this.rawCode);
119+
try {
120+
return prettier.format(htmlCode, {
121+
parser: "html",
122+
plugins: [parserHtml],
123+
vueIndentScriptAndStyle: true,
124+
});
125+
} catch (error) {
126+
return htmlCode;
127+
}
128+
}
98129
},
99130
fillter: {},
100131
};

src/libs/singleIndexOutput.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { parseComponent } from 'vue-template-compiler/browser';
2+
3+
const outputVue2Template = `<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<meta charset="UTF-8">
7+
<title>VCC预览</title>
8+
<!-- import CSS -->
9+
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
10+
<style stype="text/css">#styleTemplate</style>
11+
</head>
12+
<body>
13+
<div id="app">
14+
#templateHolder
15+
</div>
16+
</body>
17+
<!-- import Vue before Element -->
18+
<script src="https://unpkg.com/vue/dist/vue.js"></script>
19+
<!-- import JavaScript -->
20+
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
21+
<script>
22+
new Vue(#logicHolder).$mount("#app");
23+
</script>
24+
</html>`
25+
26+
export default function (vueCode) {
27+
const { template, script, styles, customBlocks } = parseComponent(vueCode);
28+
29+
let newScript = script.content.replace(/\s*export default\s*/, "");
30+
31+
let output = outputVue2Template;
32+
33+
output = output.replace("#templateHolder", template.content);
34+
output = output.replace("#logicHolder", newScript);
35+
output = output.replace("#styleTemplate", styles[0].content);
36+
37+
return output;
38+
}
39+

0 commit comments

Comments
 (0)