fix: code white-space fix

This commit is contained in:
JimQing 2020-07-04 01:03:57 +08:00
parent 5d9c77d48a
commit bbf5510bc8
4 changed files with 34 additions and 20 deletions

View File

@ -58,7 +58,7 @@
<el-tooltip class="item" :effect="effect" content="自定义CSS样式" placement="left">
<el-button :type="btnType" plain size="medium" icon="el-icon-setting" @click="customStyle"></el-button>
</el-tooltip>
<el-button :type="btnType" plain size="medium" @click="copy">复制</el-button>
<el-button :type="btnType" plain size="medium" @click="copy" placement="bottom-start">复制</el-button>
<el-button :type="btnType" plain size="medium" class="about" @click="$emit('showAboutDialog')">关于</el-button>
<el-tooltip content="夜间模式" placement="bottom-start">
<div class="mode__switch" v-if="!nightMode" @click="themeChanged"></div>
@ -73,7 +73,8 @@
import {
setColorWithCustomTemplate,
setFontSize,
isImageIllegal
isImageIllegal,
fixCodeWhiteSpace
} from '../../scripts/util'
import fileApi from '../../api/file';
import {
@ -170,21 +171,25 @@ export default {
return false;
},
//
copy() {
copy(e) {
this.$emit('startCopy');
setTimeout(() => {
let clipboardDiv = document.getElementById('output')
solveWeChatImage()
this.setHtml(solveHtml(this.nightMode))
solveWeChatImage();
fixCodeWhiteSpace();
this.setOutput(solveHtml());
let clipboardDiv = document.getElementById('output');
clipboardDiv.focus()
window.getSelection().removeAllRanges()
let range = document.createRange()
clipboardDiv.focus();
window.getSelection().removeAllRanges();
let range = document.createRange();
range.setStartBefore(clipboardDiv.firstChild)
range.setEndAfter(clipboardDiv.lastChild)
window.getSelection().addRange(range)
document.execCommand('copy')
range.setStartBefore(clipboardDiv.firstChild);
range.setEndAfter(clipboardDiv.lastChild);
window.getSelection().addRange(range);
document.execCommand('copy');
range.setStartBefore(clipboardDiv.firstChild);
range.setEndAfter(clipboardDiv.firstChild);
fixCodeWhiteSpace('normal');
//
this.$notify({
showClose: true,
@ -192,11 +197,11 @@ export default {
offset: 80,
duration: 1600,
type: 'success'
})
clipboardDiv.innerHTML = this.output; //
});
this.$emit('refresh');
this.$emit('endCopy');
}, 350);
e.target.blur();
},
// CSS
async customStyle () {
@ -247,7 +252,7 @@ export default {
document.body.removeChild(downLink)
},
...mapMutations(['clearEditorToDefault','setCurrentColor', 'setCiteStatus', 'themeChanged',
'setHtml', 'setCurrentFont', 'setCurrentSize', 'setCssEditorValue', 'setWxRendererOptions'])
'setOutput', 'setCurrentFont', 'setCurrentSize', 'setCssEditorValue', 'setWxRendererOptions'])
},
mounted() {
this.selectFont = this.currentFont;

View File

@ -140,7 +140,7 @@ const WxRenderer = function (opts) {
return `
<section class="code-snippet__fix code-snippet__js">
<ul class="code-snippet__line-index code-snippet__js">${numbers.join('')}</ul>
<pre class="code-snippet__js" data-lang="${lang}">
<pre class="code__pre code-snippet__js" data-lang="${lang}">
${codeLines.join('')}
</pre>
</section>

View File

@ -204,3 +204,13 @@ export function formatDoc(content) {
})
return doc
}
export function fixCodeWhiteSpace(value = 'pre') {
const preDomList = document.getElementsByClassName('code__pre');
if (preDomList.length > 0) {
preDomList.forEach(pre=> {
pre.style.whiteSpace = value;
})
}
}

View File

@ -18,7 +18,6 @@ const state = {
output: '',
editor: null,
cssEditor: null,
html: '',
currentFont: '',
currentSize: '',
currentColor: '',
@ -26,8 +25,8 @@ const state = {
nightMode: false
};
const mutations = {
setHtml(state, data) {
state.html = data;
setOutput(state, data) {
state.output = data;
},
setEditorValue(state, data) {
state.editor.setValue(data)