diff --git a/assets/scripts/editor.js b/assets/scripts/editor.js index 3aa4e34..ac810d5 100644 --- a/assets/scripts/editor.js +++ b/assets/scripts/editor.js @@ -6,9 +6,9 @@ let app = new Vue({ output: '', source: '', editorThemes: [ - { label: '淡雅', value: 'xq-light'}, - { label: '精美', value: 'eclipse'}, - { label: '暗绿', value: 'oceanic-next'} + { label: '淡雅', value: 'xq-light' }, + { label: '精美', value: 'eclipse' }, + { label: '暗绿', value: 'oceanic-next' } ], editor: null, builtinFonts: [ @@ -28,8 +28,8 @@ let app = new Vue({ ], colorOption: [ { label: '橘红', value: 'rgba(255, 95, 46, 0.9)', hex: '#FF5F2E' }, - { label: '淡绿', value: 'rgba(66, 185, 131, 0.9)', hex: '#42B983'}, - { label: '暗青', value: 'rgba(0, 139, 139, 0.9)', hex: '#008B8B'} + { label: '淡绿', value: 'rgba(66, 185, 131, 0.9)', hex: '#42B983' }, + { label: '暗青', value: 'rgba(0, 139, 139, 0.9)', hex: '#008B8B' } ], aboutDialogVisible: false }; @@ -40,7 +40,6 @@ let app = new Vue({ return d; }, mounted() { - let self = this; this.editor = CodeMirror.fromTextArea( document.getElementById('editor'), { @@ -51,9 +50,9 @@ let app = new Vue({ mode: 'text/x-markdown', } ); - this.editor.on("change", function (cm, change) { - self.refresh(); - self.saveEditorContent(); + this.editor.on("change", (cm, change) => { + this.refresh(); + this.saveEditorContent(); }); this.wxRenderer = new WxRenderer({ theme: setColor(this.currentColor), @@ -67,13 +66,13 @@ let app = new Vue({ axios({ method: 'get', url: './assets/default-content.md', - }).then(function (resp) { - self.editor.setValue(resp.data) + }).then(resp => { + this.editor.setValue(resp.data) }) } }, methods: { - renderWeChat: function (source) { + renderWeChat(source) { let output = marked(source, { renderer: this.wxRenderer.getRenderer() }); if (this.wxRenderer.hasFootnotes()) { // 去除第一行的 margin-top @@ -85,22 +84,22 @@ let app = new Vue({ } return output }, - editorThemeChanged: function (editorTheme) { + editorThemeChanged(editorTheme) { this.editor.setOption('theme', editorTheme) }, - fontChanged: function (fonts) { + fontChanged(fonts) { this.wxRenderer.setOptions({ fonts: fonts }); this.refresh() }, - sizeChanged: function (size) { + sizeChanged(size) { this.wxRenderer.setOptions({ size: size }); this.refresh() }, - colorChanged: function (color) { + colorChanged(color) { let theme = setColor(color) this.wxRenderer.setOptions({ theme: theme @@ -108,19 +107,19 @@ let app = new Vue({ this.refresh() }, // 刷新右侧预览 - refresh: function () { + refresh() { this.output = this.renderWeChat(this.editor.getValue(0)) }, // 将左侧编辑器内容保存到 LocalStorage - saveEditorContent: function () { + saveEditorContent() { let content = this.editor.getValue(0); - if (content){ + if (content) { localStorage.setItem("__editor_content", content); } else { localStorage.removeItem("__editor_content"); } }, - copy: function () { + copy() { let clipboardDiv = document.getElementById('output'); clipboardDiv.focus(); window.getSelection().removeAllRanges(); @@ -157,13 +156,13 @@ let app = new Vue({ }); } }, - openWindow: function (url) { + openWindow(url) { window.open(url); } }, - updated: function () { - this.$nextTick(function () { - prettyPrint() + updated() { + this.$nextTick(() => { + prettyPrint(); }) } }); diff --git a/assets/scripts/renderers/wx-renderer.js b/assets/scripts/renderers/wx-renderer.js index b9f1a44..87b6725 100644 --- a/assets/scripts/renderers/wx-renderer.js +++ b/assets/scripts/renderers/wx-renderer.js @@ -9,11 +9,9 @@ let WxRenderer = function (opts) { let CODE_FONT_FAMILY = "Menlo, Operator Mono, Consolas, Monaco, monospace"; - let merge = function (base, extend) { - return Object.assign({}, base, extend) - }; + let merge = (base, extend) => Object.assign({}, base, extend); - this.buildTheme = function (themeTpl) { + this.buildTheme = themeTpl => { let mapping = {}; let base = merge(themeTpl.BASE, { 'font-family': this.opts.fonts, @@ -30,6 +28,7 @@ let WxRenderer = function (opts) { mapping[ele] = merge(base, style) } } + for (let ele in themeTpl.block) { if (themeTpl.block.hasOwnProperty(ele)) { let style = themeTpl.block[ele]; @@ -42,7 +41,7 @@ let WxRenderer = function (opts) { return mapping }; - let getStyles = function (tokenName, addition) { + let getStyles = (tokenName, addition) => { let arr = []; let dict = styleMapping[tokenName]; if (!dict) return ''; @@ -52,14 +51,13 @@ let WxRenderer = function (opts) { return `style="${arr.join(';') + (addition || '')}"` }; - let addFootnote = function (title, link) { - footnoteIndex += 1; - footnotes.push([footnoteIndex, title, link]); - return footnoteIndex + let addFootnote = (title, link) => { + footnotes.push([++footnoteIndex, title, link]); + return footnoteIndex; }; - this.buildFootnotes = function () { - let footnoteArray = footnotes.map(function (x) { + this.buildFootnotes = () => { + let footnoteArray = footnotes.map(x => { if (x[1] === x[2]) { return `[${x[0]}]: ${x[1]}
` } @@ -68,31 +66,33 @@ let WxRenderer = function (opts) { return `

引用链接

${footnoteArray.join('\n')}

` }; - this.buildAddition = function () { - return '' - }; + this.buildAddition = () => { + return ` + + `; + } - this.setOptions = function (newOpts) { + this.setOptions = newOpts => { this.opts = merge(this.opts, newOpts) }; - this.hasFootnotes = function () { - return footnotes.length !== 0 - }; + this.hasFootnotes = () => footnotes.length !== 0; - this.getRenderer = function () { + this.getRenderer = () => { footnotes = []; footnoteIndex = 0; @@ -100,7 +100,7 @@ let WxRenderer = function (opts) { let renderer = new marked.Renderer(); FuriganaMD.register(renderer); - renderer.heading = function (text, level) { + renderer.heading = (text, level) => { switch (level) { case 1: return `

${text}

`; @@ -112,39 +112,40 @@ let WxRenderer = function (opts) { return `

${text}

`; } }; - renderer.paragraph = function (text) { - return `

${text}

` - }; - renderer.blockquote = function (text) { + renderer.paragraph = text => `

${text}

`; + + renderer.blockquote = text => { text = text.replace(//, `

`); return `

${text}
` }; - renderer.code = function (text, infoString) { + renderer.code = (text, infoString) => { text = text.replace(//g, ">"); let lines = text.split('\n'); let codeLines = []; let numbers = []; + for (let i = 0; i < lines.length; i++) { const line = lines[i]; codeLines.push(`${(line || '
')}
`); numbers.push('
  • ') } let lang = infoString || ''; - return `
    ` - + `` - + `
    `
    -        + codeLines.join('')
    -        + `
    ` + + return ` +
    + +
    +                ${codeLines.join('')}
    +            
    +
    + `; }; - renderer.codespan = function (text, infoString) { - return `${text}` - }; - renderer.listitem = function (text) { - return `<%s/>${text}`; - }; - renderer.list = function (text, ordered, start) { + renderer.codespan = (text, infoString) => `${text}`; + renderer.listitem = text => `<%s/>${text}`; + + renderer.list = (text, ordered, start) => { text = text.replace(/<\/*p.*?>/g, ''); let segments = text.split(`<%s/>`); if (!ordered) { @@ -157,7 +158,7 @@ let WxRenderer = function (opts) { } return `

    ${text}

    `; }; - renderer.image = function (href, title, text) { + renderer.image = (href, title, text) => { let subText = ''; if (text) { subText = `
    ${text}
    ` @@ -166,7 +167,7 @@ let WxRenderer = function (opts) { let imgStyles = getStyles(ENV_STRETCH_IMAGE ? 'image' : 'image_org'); return `
    ${text}${subText}
    ` }; - renderer.link = function (href, title, text) { + renderer.link = (href, title, text) => { if (href.indexOf('https://mp.weixin.qq.com') === 0) { return `${text}`; } else if (href === text) { @@ -180,21 +181,12 @@ let WxRenderer = function (opts) { } } }; - renderer.strong = function (text) { - return `${text}`; - }; - renderer.em = function (text) { - return `

    ${text}

    ` - }; - renderer.table = function (header, body) { - return `${header}${body}
    `; - }; - renderer.tablecell = function (text, flags) { - return `${text}`; - }; - renderer.hr = function () { - return `
    `; - }; + renderer.strong = text => `${text}`; + renderer.em = text => `

    ${text}

    `; + renderer.table = (header, body) => `${header}${body}
    `; + renderer.tablecell = (text, flags) => `${text}`; + renderer.hr = () => `
    `; + return renderer } }; diff --git a/assets/scripts/sync-scroll.js b/assets/scripts/sync-scroll.js index cbcb5f3..bf454fc 100644 --- a/assets/scripts/sync-scroll.js +++ b/assets/scripts/sync-scroll.js @@ -1,13 +1,11 @@ // 左右栏同步滚动 -$(document).ready(function () { - +$(document).ready(() => { let timeout; - $('div.CodeMirror-scroll, #preview').on("scroll", function callback() { clearTimeout(timeout); - let source = $(this), - target = $(source.is("#preview") ? 'div.CodeMirror-scroll' : '#preview'); + let source = $(this), + target = $(source.is("#preview") ? 'div.CodeMirror-scroll' : '#preview'); target.off("scroll"); @@ -18,9 +16,8 @@ $(document).ready(function () { let height = percentage * (target0.scrollHeight - target0.offsetHeight); target0.scrollTo(0, height); - timeout = setTimeout(function () { + timeout = setTimeout(() => { target.on("scroll", callback); }, 100); }); - });