mirror of
https://github.com/doocs/md.git
synced 2024-11-24 19:10:34 +08:00
feat: add Mac-style code block (#183)
close #168 Co-authored-by: yanglbme <szuyanglb@outlook.com>
This commit is contained in:
parent
f5dd481425
commit
d5fdd04be9
@ -13,13 +13,10 @@ export function solveWeChatImage() {
|
|||||||
image.style.height = height
|
image.style.height = height
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export function solveHtml() {
|
|
||||||
const element = document.getElementById(`output-wrapper`)
|
export function mergeCss(html) {
|
||||||
let html = element.innerHTML
|
return juice(html, {
|
||||||
let res = ``
|
|
||||||
res = juice.inlineContent(html, {
|
|
||||||
inlinePseudoElements: true,
|
inlinePseudoElements: true,
|
||||||
preserveImportant: true,
|
preserveImportant: true,
|
||||||
})
|
})
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
@ -147,6 +147,13 @@
|
|||||||
<el-dropdown-item class="padding-left-3" @click.native="customStyle">
|
<el-dropdown-item class="padding-left-3" @click.native="customStyle">
|
||||||
自定义 CSS
|
自定义 CSS
|
||||||
</el-dropdown-item>
|
</el-dropdown-item>
|
||||||
|
<el-dropdown-item divided @click.native="codeBlockChanged">
|
||||||
|
<i
|
||||||
|
class="el-icon-check"
|
||||||
|
:style="{ opacity: isMacCodeBlock ? 1 : 0 }"
|
||||||
|
></i>
|
||||||
|
Mac 代码块
|
||||||
|
</el-dropdown-item>
|
||||||
<el-dropdown-item
|
<el-dropdown-item
|
||||||
divided
|
divided
|
||||||
class="padding-left-3"
|
class="padding-left-3"
|
||||||
@ -219,7 +226,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { setFontSize, setColorWithCustomTemplate } from '@/assets/scripts/util'
|
import { setFontSize, setColorWithCustomTemplate } from '@/assets/scripts/util'
|
||||||
import { solveWeChatImage, solveHtml } from '@/assets/scripts/converter'
|
import { solveWeChatImage, mergeCss } from '@/assets/scripts/converter'
|
||||||
import DEFAULT_CSS_CONTENT from '@/assets/example/theme-css.txt'
|
import DEFAULT_CSS_CONTENT from '@/assets/example/theme-css.txt'
|
||||||
import config from '@/assets/scripts/config'
|
import config from '@/assets/scripts/config'
|
||||||
import ResetDialog from './ResetDialog'
|
import ResetDialog from './ResetDialog'
|
||||||
@ -239,11 +246,12 @@ export default {
|
|||||||
},
|
},
|
||||||
config: config,
|
config: config,
|
||||||
citeStatus: false,
|
citeStatus: false,
|
||||||
|
isMacCodeBlock: true,
|
||||||
showResetConfirm: false,
|
showResetConfirm: false,
|
||||||
selectFont: ``,
|
selectFont: ``,
|
||||||
selectSize: ``,
|
selectSize: ``,
|
||||||
selectColor: ``,
|
selectColor: ``,
|
||||||
selectCodeTheme: config.codeThemeOption[0].value,
|
selectCodeTheme: config.codeThemeOption[2].value,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
@ -270,6 +278,7 @@ export default {
|
|||||||
codeTheme: (state) => state.codeTheme,
|
codeTheme: (state) => state.codeTheme,
|
||||||
nightMode: (state) => state.nightMode,
|
nightMode: (state) => state.nightMode,
|
||||||
currentCiteStatus: (state) => state.citeStatus,
|
currentCiteStatus: (state) => state.citeStatus,
|
||||||
|
currentIsMacCodeBlock: (state) => state.isMacCodeBlock,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@ -349,13 +358,25 @@ export default {
|
|||||||
this.setCiteStatus(this.citeStatus)
|
this.setCiteStatus(this.citeStatus)
|
||||||
this.$emit(`refresh`)
|
this.$emit(`refresh`)
|
||||||
},
|
},
|
||||||
|
codeBlockChanged() {
|
||||||
|
this.isMacCodeBlock = !this.isMacCodeBlock
|
||||||
|
this.setIsMacCodeBlock(this.isMacCodeBlock)
|
||||||
|
this.$emit(`refresh`)
|
||||||
|
},
|
||||||
// 复制到微信公众号
|
// 复制到微信公众号
|
||||||
copy() {
|
copy() {
|
||||||
this.$emit(`startCopy`)
|
this.$emit(`startCopy`)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
let clipboardDiv = document.getElementById(`output`)
|
|
||||||
solveWeChatImage()
|
solveWeChatImage()
|
||||||
solveHtml()
|
|
||||||
|
const clipboardDiv = document.getElementById(`output`)
|
||||||
|
clipboardDiv.innerHTML = mergeCss(clipboardDiv.innerHTML)
|
||||||
|
if (this.isMacCodeBlock) {
|
||||||
|
clipboardDiv.innerHTML = clipboardDiv.innerHTML.replaceAll(
|
||||||
|
/(<code class="prettyprint.+?(?<=style="))/g,
|
||||||
|
`$1font-family: Menlo, 'Operator Mono', Consolas, Monaco, monospace;`
|
||||||
|
)
|
||||||
|
}
|
||||||
clipboardDiv.focus()
|
clipboardDiv.focus()
|
||||||
window.getSelection().removeAllRanges()
|
window.getSelection().removeAllRanges()
|
||||||
let range = document.createRange()
|
let range = document.createRange()
|
||||||
@ -397,6 +418,7 @@ export default {
|
|||||||
},
|
},
|
||||||
// 重置样式
|
// 重置样式
|
||||||
confirmReset() {
|
confirmReset() {
|
||||||
|
this.showResetConfirm = false
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
this.cssEditor.setValue(DEFAULT_CSS_CONTENT)
|
this.cssEditor.setValue(DEFAULT_CSS_CONTENT)
|
||||||
this.citeStatus = false
|
this.citeStatus = false
|
||||||
@ -404,13 +426,15 @@ export default {
|
|||||||
this.fontChanged(this.config.builtinFonts[0].value)
|
this.fontChanged(this.config.builtinFonts[0].value)
|
||||||
this.colorChanged(this.config.colorOption[0].value)
|
this.colorChanged(this.config.colorOption[0].value)
|
||||||
this.sizeChanged(this.config.sizeOption[2].value)
|
this.sizeChanged(this.config.sizeOption[2].value)
|
||||||
this.codeThemeChanged(this.config.codeThemeOption[0].value)
|
this.codeThemeChanged(this.config.codeThemeOption[2].value)
|
||||||
this.$emit(`cssChanged`)
|
this.$emit(`cssChanged`)
|
||||||
this.selectFont = this.currentFont
|
this.selectFont = this.currentFont
|
||||||
this.selectSize = this.currentSize
|
this.selectSize = this.currentSize
|
||||||
this.selectColor = this.currentColor
|
this.selectColor = this.currentColor
|
||||||
this.showResetConfirm = false
|
|
||||||
this.selectCodeTheme = this.codeTheme
|
this.selectCodeTheme = this.codeTheme
|
||||||
|
|
||||||
|
this.isMacCodeBlock = false
|
||||||
|
this.codeBlockChanged()
|
||||||
},
|
},
|
||||||
cancelReset() {
|
cancelReset() {
|
||||||
this.showResetConfirm = false
|
this.showResetConfirm = false
|
||||||
@ -425,6 +449,7 @@ export default {
|
|||||||
`setCssEditorValue`,
|
`setCssEditorValue`,
|
||||||
`setCurrentCodeTheme`,
|
`setCurrentCodeTheme`,
|
||||||
`setWxRendererOptions`,
|
`setWxRendererOptions`,
|
||||||
|
`setIsMacCodeBlock`,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
@ -433,6 +458,7 @@ export default {
|
|||||||
this.selectColor = this.currentColor
|
this.selectColor = this.currentColor
|
||||||
this.selectCodeTheme = this.codeTheme
|
this.selectCodeTheme = this.codeTheme
|
||||||
this.citeStatus = this.currentCiteStatus
|
this.citeStatus = this.currentCiteStatus
|
||||||
|
this.isMacCodeBlock = this.currentIsMacCodeBlock
|
||||||
|
|
||||||
const fileInput = this.$refs.fileInput
|
const fileInput = this.$refs.fileInput
|
||||||
fileInput.onchange = () => {
|
fileInput.onchange = () => {
|
||||||
|
@ -19,9 +19,10 @@ const state = {
|
|||||||
currentFont: ``,
|
currentFont: ``,
|
||||||
currentSize: ``,
|
currentSize: ``,
|
||||||
currentColor: ``,
|
currentColor: ``,
|
||||||
citeStatus: 0,
|
citeStatus: false,
|
||||||
nightMode: false,
|
nightMode: false,
|
||||||
codeTheme: config.codeThemeOption[0].value,
|
codeTheme: config.codeThemeOption[2].value,
|
||||||
|
isMacCodeBlock: true,
|
||||||
}
|
}
|
||||||
const mutations = {
|
const mutations = {
|
||||||
setEditorValue(state, data) {
|
setEditorValue(state, data) {
|
||||||
@ -53,6 +54,10 @@ const mutations = {
|
|||||||
state.codeTheme = data
|
state.codeTheme = data
|
||||||
localStorage.setItem(`codeTheme`, data)
|
localStorage.setItem(`codeTheme`, data)
|
||||||
},
|
},
|
||||||
|
setIsMacCodeBlock(state, data) {
|
||||||
|
state.isMacCodeBlock = data
|
||||||
|
localStorage.setItem(`isMacCodeBlock`, data)
|
||||||
|
},
|
||||||
themeChanged(state) {
|
themeChanged(state) {
|
||||||
state.nightMode = !state.nightMode
|
state.nightMode = !state.nightMode
|
||||||
localStorage.setItem(`nightMode`, state.nightMode)
|
localStorage.setItem(`nightMode`, state.nightMode)
|
||||||
@ -65,9 +70,10 @@ const mutations = {
|
|||||||
state.currentSize =
|
state.currentSize =
|
||||||
localStorage.getItem(`size`) || config.sizeOption[2].value
|
localStorage.getItem(`size`) || config.sizeOption[2].value
|
||||||
state.codeTheme =
|
state.codeTheme =
|
||||||
localStorage.getItem(`codeTheme`) || config.codeThemeOption[0].value
|
localStorage.getItem(`codeTheme`) || config.codeThemeOption[2].value
|
||||||
state.citeStatus = localStorage.getItem(`citeStatus`) === `true`
|
state.citeStatus = localStorage.getItem(`citeStatus`) === `true`
|
||||||
state.nightMode = localStorage.getItem(`nightMode`) === `true`
|
state.nightMode = localStorage.getItem(`nightMode`) === `true`
|
||||||
|
state.isMacCodeBlock = !(localStorage.getItem(`isMacCodeBlock`) === `false`)
|
||||||
state.wxRenderer = new WxRenderer({
|
state.wxRenderer = new WxRenderer({
|
||||||
theme: setColor(state.currentColor),
|
theme: setColor(state.currentColor),
|
||||||
fonts: state.currentFont,
|
fonts: state.currentFont,
|
||||||
@ -135,7 +141,7 @@ const mutations = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
editorRefresh(state) {
|
editorRefresh(state) {
|
||||||
let renderer = state.wxRenderer.getRenderer(state.citeStatus)
|
const renderer = state.wxRenderer.getRenderer(state.citeStatus)
|
||||||
marked.setOptions({ renderer })
|
marked.setOptions({ renderer })
|
||||||
let output = marked.parse(state.editor.getValue(0))
|
let output = marked.parse(state.editor.getValue(0))
|
||||||
|
|
||||||
@ -147,6 +153,35 @@ const mutations = {
|
|||||||
// 附加的一些 style
|
// 附加的一些 style
|
||||||
output += state.wxRenderer.buildAddition()
|
output += state.wxRenderer.buildAddition()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state.isMacCodeBlock) {
|
||||||
|
output += `
|
||||||
|
<style>
|
||||||
|
.hljs.code__pre::before {
|
||||||
|
position: initial;
|
||||||
|
padding: initial;
|
||||||
|
content: '';
|
||||||
|
display: block;
|
||||||
|
height: 25px;
|
||||||
|
background-color: transparent;
|
||||||
|
background-image: url("https://doocs.oss-cn-shenzhen.aliyuncs.com/img/123.svg");
|
||||||
|
background-position: 14px 10px;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-size: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs.code__pre {
|
||||||
|
padding: 0!important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs.code__pre code {
|
||||||
|
display: -webkit-box;
|
||||||
|
padding: 0.5em 1em 1em;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
`
|
||||||
|
}
|
||||||
state.output = output
|
state.output = output
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -526,6 +526,7 @@ export default {
|
|||||||
exportEditorContent() {
|
exportEditorContent() {
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
exportHTML()
|
exportHTML()
|
||||||
|
document.getElementById(`output`).innerHTML = this.output
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// 导入 Markdown 文档
|
// 导入 Markdown 文档
|
||||||
|
Loading…
Reference in New Issue
Block a user