1
1
<template >
2
2
<el-dialog title =" 代码预览" v-model =" codeDialogVisible" width =" 70%" top =" 10vh" :before-close =" handleClose" :center =true >
3
3
<!-- 这里加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 >
5
6
<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 >
16
25
</el-dialog >
17
26
18
27
</template >
@@ -25,6 +34,7 @@ import copy from 'copy-to-clipboard';
25
34
import { saveAs } from " file-saver" ;
26
35
27
36
import CodeEditor from ' ./CodeEditor.vue'
37
+ import singleIndexOutput from ' ../libs/singleIndexOutput.js' ;
28
38
29
39
export default {
30
40
props: [' rawCode' , ' codeDialogVisible' ],
@@ -35,7 +45,7 @@ export default {
35
45
data () {
36
46
return {
37
47
// 在此自动生成
38
-
48
+ outputMode : " vue " ,
39
49
iconCopy: (" https://static.imonkey.xueersi.com/download/vcc-resource/icon/copy-outline.svg" ),
40
50
iconDownload: (" https://static.imonkey.xueersi.com/download/vcc-resource/icon/code-download-outline.svg" ),
41
51
};
@@ -59,14 +69,14 @@ export default {
59
69
this .copy ();
60
70
},
61
71
copy () {
62
- if (copy (this .prettyCode )) {
72
+ if (copy (this .outputCode )) {
63
73
this .$message .success (" 代码已复制到剪贴板" );
64
74
} else {
65
75
this .$message .error (" 代码复制有点问题?" );
66
76
}
67
77
},
68
78
download () {
69
- let blob = new Blob ([this .prettyCode ], {
79
+ let blob = new Blob ([this .outputCode ], {
70
80
type: " text/plain;charset=utf-8" ,
71
81
});
72
82
saveAs (blob, " VueComponent.vue" );
@@ -83,6 +93,14 @@ export default {
83
93
}
84
94
},
85
95
computed: {
96
+ isVueMode () {
97
+ return this .outputMode === ' vue' ;
98
+ },
99
+ outputCode () {
100
+ return this .isVueMode ? this .prettyCode : this .singleIndex ;
101
+ },
102
+
103
+
86
104
prettyCode () {
87
105
try {
88
106
return prettier .format (this .rawCode , {
@@ -95,6 +113,19 @@ export default {
95
113
}
96
114
97
115
},
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
+ }
98
129
},
99
130
fillter: {},
100
131
};
0 commit comments