From c3b04aac210c7455b5e9e4c70b723df510b3f837 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 10 Nov 2019 16:59:23 +0800 Subject: [PATCH] feat: code optimisation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码优化 --- assets/scripts/editor.js | 24 +++++++++---------- assets/scripts/renderers/wx-renderer.js | 32 ++++++++++++------------- assets/scripts/sync-scroll.js | 2 +- assets/scripts/themes/default-theme.js | 16 +++++++++++++ index.html | 12 ++++++---- libs/scripts/FuriganaMD.js | 10 ++++---- 6 files changed, 57 insertions(+), 39 deletions(-) diff --git a/assets/scripts/editor.js b/assets/scripts/editor.js index ac810d5..bda8901 100644 --- a/assets/scripts/editor.js +++ b/assets/scripts/editor.js @@ -24,7 +24,7 @@ let app = new Vue({ sizeOption: [ { label: '13px', value: '13px', desc: '稍小' }, { label: '14px', value: '14px', desc: '推荐' }, - { label: '15px', value: '15px', desc: '稍大' }, + { label: '15px', value: '15px', desc: '稍大' } ], colorOption: [ { label: '橘红', value: 'rgba(255, 95, 46, 0.9)', hex: '#FF5F2E' }, @@ -47,7 +47,7 @@ let app = new Vue({ lineWrapping: true, styleActiveLine: true, theme: this.currentEditorTheme, - mode: 'text/x-markdown', + mode: 'text/x-markdown' } ); this.editor.on("change", (cm, change) => { @@ -65,9 +65,9 @@ let app = new Vue({ } else { axios({ method: 'get', - url: './assets/default-content.md', + url: './assets/default-content.md' }).then(resp => { - this.editor.setValue(resp.data) + this.editor.setValue(resp.data); }) } }, @@ -82,33 +82,33 @@ let app = new Vue({ // 附加的一些 style output += this.wxRenderer.buildAddition(); } - return output + return output; }, editorThemeChanged(editorTheme) { - this.editor.setOption('theme', editorTheme) + this.editor.setOption('theme', editorTheme); }, fontChanged(fonts) { this.wxRenderer.setOptions({ fonts: fonts }); - this.refresh() + this.refresh(); }, sizeChanged(size) { this.wxRenderer.setOptions({ size: size }); - this.refresh() + this.refresh(); }, colorChanged(color) { let theme = setColor(color) this.wxRenderer.setOptions({ theme: theme - }) - this.refresh() + }); + this.refresh(); }, // 刷新右侧预览 refresh() { - this.output = this.renderWeChat(this.editor.getValue(0)) + this.output = this.renderWeChat(this.editor.getValue(0)); }, // 将左侧编辑器内容保存到 LocalStorage saveEditorContent() { @@ -156,7 +156,7 @@ let app = new Vue({ }); } }, - openWindow(url) { + visit(url) { window.open(url); } }, diff --git a/assets/scripts/renderers/wx-renderer.js b/assets/scripts/renderers/wx-renderer.js index 87b6725..b2fa4d0 100644 --- a/assets/scripts/renderers/wx-renderer.js +++ b/assets/scripts/renderers/wx-renderer.js @@ -7,7 +7,7 @@ let WxRenderer = function (opts) { let footnoteIndex = 0; let styleMapping = null; - let CODE_FONT_FAMILY = "Menlo, Operator Mono, Consolas, Monaco, monospace"; + const CODE_FONT_FAMILY = "Menlo, Operator Mono, Consolas, Monaco, monospace"; let merge = (base, extend) => Object.assign({}, base, extend); @@ -25,7 +25,7 @@ let WxRenderer = function (opts) { style['font-family'] = CODE_FONT_FAMILY; style['white-space'] = 'normal'; } - mapping[ele] = merge(base, style) + mapping[ele] = merge(base, style); } } @@ -33,12 +33,12 @@ let WxRenderer = function (opts) { if (themeTpl.block.hasOwnProperty(ele)) { let style = themeTpl.block[ele]; if (ele === 'code') { - style['font-family'] = CODE_FONT_FAMILY + style['font-family'] = CODE_FONT_FAMILY; } - mapping[ele] = merge(base_block, style) + mapping[ele] = merge(base_block, style); } } - return mapping + return mapping; }; let getStyles = (tokenName, addition) => { @@ -46,9 +46,9 @@ let WxRenderer = function (opts) { let dict = styleMapping[tokenName]; if (!dict) return ''; for (const key in dict) { - arr.push(key + ':' + dict[key]) + arr.push(key + ':' + dict[key]); } - return `style="${arr.join(';') + (addition || '')}"` + return `style="${arr.join(';') + (addition || '')}"`; }; let addFootnote = (title, link) => { @@ -59,11 +59,11 @@ let WxRenderer = function (opts) { this.buildFootnotes = () => { let footnoteArray = footnotes.map(x => { if (x[1] === x[2]) { - return `[${x[0]}]: ${x[1]}
` + return `[${x[0]}]: ${x[1]}
`; } - return `[${x[0]}] ${x[1]}: ${x[2]}
` + return `[${x[0]}] ${x[1]}: ${x[2]}
`; }); - return `

引用链接

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

` + return `

引用链接

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

`; }; this.buildAddition = () => { @@ -87,7 +87,7 @@ let WxRenderer = function (opts) { } this.setOptions = newOpts => { - this.opts = merge(this.opts, newOpts) + this.opts = merge(this.opts, newOpts); }; this.hasFootnotes = () => footnotes.length !== 0; @@ -116,7 +116,7 @@ let WxRenderer = function (opts) { renderer.blockquote = text => { text = text.replace(//, `

`); - return `

${text}
` + return `
${text}
`; }; renderer.code = (text, infoString) => { text = text.replace(/${(line || '
')}
`); - numbers.push('
  • ') + numbers.push('
  • '); } let lang = infoString || ''; @@ -161,11 +161,11 @@ let WxRenderer = function (opts) { renderer.image = (href, title, text) => { let subText = ''; if (text) { - subText = `
    ${text}
    ` + subText = `
    ${text}
    `; } let figureStyles = getStyles('figure'); let imgStyles = getStyles(ENV_STRETCH_IMAGE ? 'image' : 'image_org'); - return `
    ${text}${subText}
    ` + return `
    ${text}${subText}
    `; }; renderer.link = (href, title, text) => { if (href.indexOf('https://mp.weixin.qq.com') === 0) { @@ -187,6 +187,6 @@ let WxRenderer = function (opts) { renderer.tablecell = (text, flags) => `${text}`; renderer.hr = () => `
    `; - return renderer + return renderer; } }; diff --git a/assets/scripts/sync-scroll.js b/assets/scripts/sync-scroll.js index bf454fc..c9e31bb 100644 --- a/assets/scripts/sync-scroll.js +++ b/assets/scripts/sync-scroll.js @@ -4,7 +4,7 @@ $(document).ready(() => { $('div.CodeMirror-scroll, #preview').on("scroll", function callback() { clearTimeout(timeout); - let source = $(this), + let source = $(this), target = $(source.is("#preview") ? 'div.CodeMirror-scroll' : '#preview'); target.off("scroll"); diff --git a/assets/scripts/themes/default-theme.js b/assets/scripts/themes/default-theme.js index 917c950..94c9e0e 100644 --- a/assets/scripts/themes/default-theme.js +++ b/assets/scripts/themes/default-theme.js @@ -65,6 +65,7 @@ let default_theme = { 'background': 'rgba(27,31,35,.05)', 'margin': '2em 8px' }, + blockquote_p: { 'letter-spacing': '0.1em', 'color': 'rgb(80, 80, 80)', @@ -72,6 +73,7 @@ let default_theme = { 'font-size': '1em', 'display': 'inline', }, + code: { 'font-size': '80%', 'overflow': 'auto', @@ -83,29 +85,35 @@ let default_theme = { 'border': '1px solid rgb(236,236,236)', 'margin': '20px 0', }, + image: { 'border-radius': '4px', 'display': 'block', 'margin': '0.1em auto 0.5em', 'width': '100% !important', }, + image_org: { 'border-radius': '4px', 'display': 'block' }, + ol: { 'margin-left': '0', 'padding-left': '1em' }, + ul: { 'margin-left': '0', 'padding-left': '1em', 'list-style': 'circle' }, + footnotes: { 'margin': '0.5em 8px', 'font-size': '80%' }, + figure: { 'margin': '1.5em 8px', } @@ -116,6 +124,7 @@ let default_theme = { 'display': 'block', 'margin': '0.2em 8px' }, + codespan: { 'font-size': '90%', 'color': '#d14', @@ -123,9 +132,11 @@ let default_theme = { 'padding': '3px 5px', 'border-radius': '4px', }, + link: { 'color': '#576b95' }, + wx_link: { 'color': '#576b95', 'text-decoration': 'none', @@ -136,22 +147,27 @@ let default_theme = { 'color': 'rgba(255, 95, 46, 0.9)', 'font-weight': 'bold', }, + table: { 'border-collapse': 'collapse', 'text-align': 'center', 'margin': '1em 8px' }, + thead: { 'background': 'rgba(0, 0, 0, 0.05)' }, + td: { 'font-size': '80%', 'border': '1px solid #dfdfdf', 'padding': '0.25em 0.5em' }, + footnote: { 'font-size': '12px' }, + figcaption: { 'text-align': 'center', 'color': '#888', diff --git a/index.html b/index.html index c8209a7..d21aea8 100644 --- a/index.html +++ b/index.html @@ -3,8 +3,10 @@ - - + + + + 微信 Markdown 编辑器 @@ -88,16 +90,16 @@
    -

    一款简洁美观的微信 Markdown 编辑器

    +

    一款高度简洁的微信 Markdown 编辑器

    扫码关注我的公众号,原创技术文章第一时间推送!

    - GitHub 仓库 + GitHub 仓库 - Gitee 仓库 + Gitee 仓库
    diff --git a/libs/scripts/FuriganaMD.js b/libs/scripts/FuriganaMD.js index c0dafd9..93abc5a 100644 --- a/libs/scripts/FuriganaMD.js +++ b/libs/scripts/FuriganaMD.js @@ -1,7 +1,7 @@ -// 注音功能来自于 -// https://github.com/amclees/furigana-markdown -// 详见上述文档 - +/** + * 注音功能来自于:https://github.com/amclees/furigana-markdown + * 详见上述文档 + */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : @@ -9,7 +9,7 @@ }(this, (function () { 'use strict'; -// This function escapes special characters for use in a regex constructor. + // This function escapes special characters for use in a regex constructor. function escapeForRegex(string) { return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); }