Skip to content

Commit

Permalink
feat: add Mac-style code block (doocs#183)
Browse files Browse the repository at this point in the history
close doocs#168

Co-authored-by: yanglbme <[email protected]>
  • Loading branch information
YangFong and yanglbme authored Aug 16, 2022
1 parent f5dd481 commit d5fdd04
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/assets/scripts/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ export function solveWeChatImage() {
image.style.height = height
}
}
export function solveHtml() {
const element = document.getElementById(`output-wrapper`)
let html = element.innerHTML
let res = ``
res = juice.inlineContent(html, {

export function mergeCss(html) {
return juice(html, {
inlinePseudoElements: true,
preserveImportant: true,
})
return res
}
38 changes: 32 additions & 6 deletions src/components/CodemirrorEditor/EditorHeader/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
<el-dropdown-item class="padding-left-3" @click.native="customStyle">
自定义 CSS
</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
divided
class="padding-left-3"
Expand Down Expand Up @@ -219,7 +226,7 @@

<script>
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 config from '@/assets/scripts/config'
import ResetDialog from './ResetDialog'
Expand All @@ -239,11 +246,12 @@ export default {
},
config: config,
citeStatus: false,
isMacCodeBlock: true,
showResetConfirm: false,
selectFont: ``,
selectSize: ``,
selectColor: ``,
selectCodeTheme: config.codeThemeOption[0].value,
selectCodeTheme: config.codeThemeOption[2].value,
}
},
components: {
Expand All @@ -270,6 +278,7 @@ export default {
codeTheme: (state) => state.codeTheme,
nightMode: (state) => state.nightMode,
currentCiteStatus: (state) => state.citeStatus,
currentIsMacCodeBlock: (state) => state.isMacCodeBlock,
}),
},
methods: {
Expand Down Expand Up @@ -349,13 +358,25 @@ export default {
this.setCiteStatus(this.citeStatus)
this.$emit(`refresh`)
},
codeBlockChanged() {
this.isMacCodeBlock = !this.isMacCodeBlock
this.setIsMacCodeBlock(this.isMacCodeBlock)
this.$emit(`refresh`)
},
// 复制到微信公众号
copy() {
this.$emit(`startCopy`)
setTimeout(() => {
let clipboardDiv = document.getElementById(`output`)
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()
window.getSelection().removeAllRanges()
let range = document.createRange()
Expand Down Expand Up @@ -397,20 +418,23 @@ export default {
},
// 重置样式
confirmReset() {
this.showResetConfirm = false
localStorage.clear()
this.cssEditor.setValue(DEFAULT_CSS_CONTENT)
this.citeStatus = false
this.statusChanged(false)
this.fontChanged(this.config.builtinFonts[0].value)
this.colorChanged(this.config.colorOption[0].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.selectFont = this.currentFont
this.selectSize = this.currentSize
this.selectColor = this.currentColor
this.showResetConfirm = false
this.selectCodeTheme = this.codeTheme
this.isMacCodeBlock = false
this.codeBlockChanged()
},
cancelReset() {
this.showResetConfirm = false
Expand All @@ -425,6 +449,7 @@ export default {
`setCssEditorValue`,
`setCurrentCodeTheme`,
`setWxRendererOptions`,
`setIsMacCodeBlock`,
]),
},
mounted() {
Expand All @@ -433,6 +458,7 @@ export default {
this.selectColor = this.currentColor
this.selectCodeTheme = this.codeTheme
this.citeStatus = this.currentCiteStatus
this.isMacCodeBlock = this.currentIsMacCodeBlock
const fileInput = this.$refs.fileInput
fileInput.onchange = () => {
Expand Down
43 changes: 39 additions & 4 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const state = {
currentFont: ``,
currentSize: ``,
currentColor: ``,
citeStatus: 0,
citeStatus: false,
nightMode: false,
codeTheme: config.codeThemeOption[0].value,
codeTheme: config.codeThemeOption[2].value,
isMacCodeBlock: true,
}
const mutations = {
setEditorValue(state, data) {
Expand Down Expand Up @@ -53,6 +54,10 @@ const mutations = {
state.codeTheme = data
localStorage.setItem(`codeTheme`, data)
},
setIsMacCodeBlock(state, data) {
state.isMacCodeBlock = data
localStorage.setItem(`isMacCodeBlock`, data)
},
themeChanged(state) {
state.nightMode = !state.nightMode
localStorage.setItem(`nightMode`, state.nightMode)
Expand All @@ -65,9 +70,10 @@ const mutations = {
state.currentSize =
localStorage.getItem(`size`) || config.sizeOption[2].value
state.codeTheme =
localStorage.getItem(`codeTheme`) || config.codeThemeOption[0].value
localStorage.getItem(`codeTheme`) || config.codeThemeOption[2].value
state.citeStatus = localStorage.getItem(`citeStatus`) === `true`
state.nightMode = localStorage.getItem(`nightMode`) === `true`
state.isMacCodeBlock = !(localStorage.getItem(`isMacCodeBlock`) === `false`)
state.wxRenderer = new WxRenderer({
theme: setColor(state.currentColor),
fonts: state.currentFont,
Expand Down Expand Up @@ -135,7 +141,7 @@ const mutations = {
})
},
editorRefresh(state) {
let renderer = state.wxRenderer.getRenderer(state.citeStatus)
const renderer = state.wxRenderer.getRenderer(state.citeStatus)
marked.setOptions({ renderer })
let output = marked.parse(state.editor.getValue(0))

Expand All @@ -147,6 +153,35 @@ const mutations = {
// 附加的一些 style
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
},
}
Expand Down
1 change: 1 addition & 0 deletions src/views/CodemirrorEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ export default {
exportEditorContent() {
this.$nextTick(() => {
exportHTML()
document.getElementById(`output`).innerHTML = this.output
})
},
// 导入 Markdown 文档
Expand Down

0 comments on commit d5fdd04

Please sign in to comment.