Skip to content

Commit 7e55ffd

Browse files
committed
在代码中加入头部标签配置
1 parent 2e64872 commit 7e55ffd

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

src/utils/editor/handleInstanceView.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import store from '@store'
2-
import Loader from '../loader'
3-
const loader = new Loader()
42

5-
/**
6-
* 处理实例iframe的代码执行
7-
*/
3+
// 处理实例iframe的代码执行
84
class IframeHandler {
95
constructor(iframe) {
106
if (!IframeHandler.instance) {
@@ -35,24 +31,15 @@ class IframeHandler {
3531
const linkStr = `<script src="${JSLinks[i]}"></script>\n`
3632
extJS += linkStr
3733
}
38-
const compiledCode = `
39-
<!DOCTYPE html>
40-
<html lang="en">
41-
<head>
42-
<meta charset="UTF-8">
43-
<meta name="viewport" content="width=device-width, initial-scale=1">
34+
let head = `
4435
${headTags}
4536
${extCss}
4637
${extJS}
47-
<title></title>
4838
<style>
4939
${CSSCode}
5040
</style>
51-
<body>
52-
${HTMLCode}
53-
</body>
54-
</html>
55-
`
41+
`.trim()
42+
const compiledCode = this.jointHTML(head, HTMLCode)
5643
iDoc.open()
5744
iDoc.write(compiledCode)
5845
iDoc.close()
@@ -63,7 +50,12 @@ class IframeHandler {
6350
this.renderFlowchart()
6451
}
6552
resolve(() => {
66-
store.commit('setCompiledCode', compiledCode)
53+
// 为了让截图中的文字不产生变化,在编译后的代码中加上默认文字样式
54+
head = `
55+
<style>body{font-family:"Microsoft YaHei";}</style>
56+
${head}
57+
`.trim()
58+
store.commit('setCompiledCode', this.jointHTML(head, HTMLCode))
6759
this.insertScript(JSCode)
6860
})
6961
}
@@ -81,9 +73,7 @@ class IframeHandler {
8173
doc.body.appendChild(script)
8274
}
8375

84-
/**
85-
* 渲染markdown中的数学公式
86-
*/
76+
// 渲染markdown中的数学公式
8777
async renderMathFormula () {
8878
const iBody = this.iframe.contentWindow.document.body
8979
let KaTeX
@@ -104,9 +94,7 @@ class IframeHandler {
10494
})
10595
}
10696

107-
/**
108-
* 渲染markdown中的流程图
109-
*/
97+
// 渲染markdown中的流程图
11098
renderFlowchart () {
11199
const iframeWindow = this.iframe.contentWindow
112100
const flows = iframeWindow.document.querySelectorAll('.language-flow')
@@ -120,7 +108,26 @@ class IframeHandler {
120108
iframeWindow.flowchart.parse(code).drawSVG(`flow${i}`)
121109
}
122110
}
123-
clearIframe () {
111+
112+
// 拼接html代码
113+
jointHTML (head, body) {
114+
return `
115+
<!DOCTYPE html>
116+
<html lang="en">
117+
<head>
118+
<meta charset="UTF-8">
119+
<meta name="viewport" content="width=device-width, initial-scale=1">
120+
<title></title>
121+
${head}
122+
</head>
123+
<body>
124+
${body}
125+
</body>
126+
</html>
127+
`.trim()
128+
}
129+
130+
static clearIframe () {
124131
IframeHandler.instance = null
125132
}
126133
}

src/utils/editor/instanceDownloader.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ export default class InstanceDownloader {
88
* @param {Object} code 代码集合
99
* @param {Object} links 外部链接
1010
* @param {Array} prep 预处理
11+
* @param {String} headTags 头部标签
1112
* @param {Boolean} dwPrep 是否下载未编译版本
1213
*/
13-
constructor(code, links, prep, dwPrep) {
14+
constructor(code, links, prep, headTags, dwPrep) {
1415
this.code = code
1516
this.links = links
1617
this.prep = prep
18+
this.headTags = headTags
1719
this.dwPrep = dwPrep
1820
}
1921

@@ -26,6 +28,7 @@ export default class InstanceDownloader {
2628
const prep = this.prep
2729
const { cssLinks, JSLinks } = this.links
2830
const dwPrep = this.dwPrep
31+
const headTags = this.headTags
2932
let HTMLCode,
3033
CSSCode,
3134
JSCode,
@@ -49,15 +52,15 @@ export default class InstanceDownloader {
4952
extJS += linkStr
5053
}
5154
if (type === 'single') {
52-
this.single({ HTMLCode, CSSCode, JSCode }, { extCss, extJS })
55+
this.single({ HTMLCode, CSSCode, JSCode }, { extCss, extJS }, headTags)
5356
if (dwPrep) {
5457
const typeObj = this.judgePrep()
5558
typeObj.html && this.download(HTML, `index.${typeObj.html}`)
5659
typeObj.css && this.download(CSS, `index.${typeObj.css}`)
5760
typeObj.js && this.download(JavaScript, `index.${typeObj.js}`)
5861
}
5962
} else if (type === 'multiple') {
60-
this.multiple({ HTMLCode, CSSCode, JSCode }, { extCss, extJS })
63+
this.multiple({ HTMLCode, CSSCode, JSCode }, { extCss, extJS }, headTags)
6164
}
6265
}
6366

@@ -66,25 +69,25 @@ export default class InstanceDownloader {
6669
* @param {Object} code 代码集合
6770
* @param {Object} links 外部链接字符串集合
6871
*/
69-
single (code, links) {
72+
single (code, links, headTags) {
7073
const { HTMLCode, CSSCode, JSCode } = code
7174
const { extCss = '', extJS = '' } = links
7275
const htmlCode = `<!DOCTYPE html>
7376
<html lang="en">
7477
<head>
7578
<meta charset="UTF-8">
7679
<meta name="viewport" content="width=device-width, initial-scale=1">
80+
${headTags}
7781
${extCss}
7882
${extJS}
7983
<title></title>
8084
<style>
8185
${CSSCode}
8286
</style>
87+
</head>
8388
<body>
8489
${HTMLCode}
85-
<script>
86-
${JSCode}
87-
</script>
90+
<script>${JSCode}</script>
8891
</body>
8992
</html>
9093
`
@@ -96,23 +99,25 @@ export default class InstanceDownloader {
9699
* @param {Object} code 代码集合
97100
* @param {Object} links 外部链接字符串集合
98101
*/
99-
multiple (code, links) {
102+
multiple (code, links, headTags) {
100103
const { HTML, CSS, JavaScript } = this.code
101104
const { HTMLCode, CSSCode, JSCode } = code
102105
const { extCss, extJS } = links
103106
const zip = new JSZip()
104107
const zipFolder = zip.folder('code')
105108
const htmlCode = `
106109
<!DOCTYPE html>
107-
<html lang="en">
110+
<html lang="en">
108111
<head>
109112
<meta charset="UTF-8">
110113
<meta name="viewport" content="width=device-width, initial-scale=1">
114+
${headTags}
111115
${extCss}
112116
${extJS}
113117
<link rel="stylesheet" href="./index.css">
114118
<script src="./index.js"><\/script>
115119
<title></title>
120+
</head>
116121
<body>
117122
${HTMLCode}
118123
</body>

0 commit comments

Comments
 (0)