feat: code optimisation

代码优化
This commit is contained in:
yanglbme 2019-11-10 16:59:23 +08:00
parent 98df1b6325
commit c3b04aac21
6 changed files with 57 additions and 39 deletions

View File

@ -24,7 +24,7 @@ let app = new Vue({
sizeOption: [ sizeOption: [
{ label: '13px', value: '13px', desc: '稍小' }, { label: '13px', value: '13px', desc: '稍小' },
{ label: '14px', value: '14px', desc: '推荐' }, { label: '14px', value: '14px', desc: '推荐' },
{ label: '15px', value: '15px', desc: '稍大' }, { label: '15px', value: '15px', desc: '稍大' }
], ],
colorOption: [ colorOption: [
{ label: '橘红', value: 'rgba(255, 95, 46, 0.9)', hex: '#FF5F2E' }, { label: '橘红', value: 'rgba(255, 95, 46, 0.9)', hex: '#FF5F2E' },
@ -47,7 +47,7 @@ let app = new Vue({
lineWrapping: true, lineWrapping: true,
styleActiveLine: true, styleActiveLine: true,
theme: this.currentEditorTheme, theme: this.currentEditorTheme,
mode: 'text/x-markdown', mode: 'text/x-markdown'
} }
); );
this.editor.on("change", (cm, change) => { this.editor.on("change", (cm, change) => {
@ -65,9 +65,9 @@ let app = new Vue({
} else { } else {
axios({ axios({
method: 'get', method: 'get',
url: './assets/default-content.md', url: './assets/default-content.md'
}).then(resp => { }).then(resp => {
this.editor.setValue(resp.data) this.editor.setValue(resp.data);
}) })
} }
}, },
@ -82,33 +82,33 @@ let app = new Vue({
// 附加的一些 style // 附加的一些 style
output += this.wxRenderer.buildAddition(); output += this.wxRenderer.buildAddition();
} }
return output return output;
}, },
editorThemeChanged(editorTheme) { editorThemeChanged(editorTheme) {
this.editor.setOption('theme', editorTheme) this.editor.setOption('theme', editorTheme);
}, },
fontChanged(fonts) { fontChanged(fonts) {
this.wxRenderer.setOptions({ this.wxRenderer.setOptions({
fonts: fonts fonts: fonts
}); });
this.refresh() this.refresh();
}, },
sizeChanged(size) { sizeChanged(size) {
this.wxRenderer.setOptions({ this.wxRenderer.setOptions({
size: size size: size
}); });
this.refresh() this.refresh();
}, },
colorChanged(color) { colorChanged(color) {
let theme = setColor(color) let theme = setColor(color)
this.wxRenderer.setOptions({ this.wxRenderer.setOptions({
theme: theme theme: theme
}) });
this.refresh() this.refresh();
}, },
// 刷新右侧预览 // 刷新右侧预览
refresh() { refresh() {
this.output = this.renderWeChat(this.editor.getValue(0)) this.output = this.renderWeChat(this.editor.getValue(0));
}, },
// 将左侧编辑器内容保存到 LocalStorage // 将左侧编辑器内容保存到 LocalStorage
saveEditorContent() { saveEditorContent() {
@ -156,7 +156,7 @@ let app = new Vue({
}); });
} }
}, },
openWindow(url) { visit(url) {
window.open(url); window.open(url);
} }
}, },

View File

@ -7,7 +7,7 @@ let WxRenderer = function (opts) {
let footnoteIndex = 0; let footnoteIndex = 0;
let styleMapping = null; 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); let merge = (base, extend) => Object.assign({}, base, extend);
@ -25,7 +25,7 @@ let WxRenderer = function (opts) {
style['font-family'] = CODE_FONT_FAMILY; style['font-family'] = CODE_FONT_FAMILY;
style['white-space'] = 'normal'; 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)) { if (themeTpl.block.hasOwnProperty(ele)) {
let style = themeTpl.block[ele]; let style = themeTpl.block[ele];
if (ele === 'code') { 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) => { let getStyles = (tokenName, addition) => {
@ -46,9 +46,9 @@ let WxRenderer = function (opts) {
let dict = styleMapping[tokenName]; let dict = styleMapping[tokenName];
if (!dict) return ''; if (!dict) return '';
for (const key in dict) { 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) => { let addFootnote = (title, link) => {
@ -59,11 +59,11 @@ let WxRenderer = function (opts) {
this.buildFootnotes = () => { this.buildFootnotes = () => {
let footnoteArray = footnotes.map(x => { let footnoteArray = footnotes.map(x => {
if (x[1] === x[2]) { if (x[1] === x[2]) {
return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code>: <i>${x[1]}</i><br/>` return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code>: <i>${x[1]}</i><br/>`;
} }
return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code> ${x[1]}: <i>${x[2]}</i><br/>` return `<code style="font-size: 90%; opacity: 0.6;">[${x[0]}]</code> ${x[1]}: <i>${x[2]}</i><br/>`;
}); });
return `<h4 ${getStyles('h4')}>引用链接</h4><p ${getStyles('footnotes')}>${footnoteArray.join('\n')}</p>` return `<h4 ${getStyles('h4')}>引用链接</h4><p ${getStyles('footnotes')}>${footnoteArray.join('\n')}</p>`;
}; };
this.buildAddition = () => { this.buildAddition = () => {
@ -87,7 +87,7 @@ let WxRenderer = function (opts) {
} }
this.setOptions = newOpts => { this.setOptions = newOpts => {
this.opts = merge(this.opts, newOpts) this.opts = merge(this.opts, newOpts);
}; };
this.hasFootnotes = () => footnotes.length !== 0; this.hasFootnotes = () => footnotes.length !== 0;
@ -116,7 +116,7 @@ let WxRenderer = function (opts) {
renderer.blockquote = text => { renderer.blockquote = text => {
text = text.replace(/<p.*?>/, `<p ${getStyles('blockquote_p')}>`); text = text.replace(/<p.*?>/, `<p ${getStyles('blockquote_p')}>`);
return `<blockquote ${getStyles('blockquote')}>${text}</blockquote>` return `<blockquote ${getStyles('blockquote')}>${text}</blockquote>`;
}; };
renderer.code = (text, infoString) => { renderer.code = (text, infoString) => {
text = text.replace(/</g, "&lt;"); text = text.replace(/</g, "&lt;");
@ -129,7 +129,7 @@ let WxRenderer = function (opts) {
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
const line = lines[i]; const line = lines[i];
codeLines.push(`<code class="prettyprint"><span class="code-snippet_outer">${(line || '<br>')}</span></code>`); codeLines.push(`<code class="prettyprint"><span class="code-snippet_outer">${(line || '<br>')}</span></code>`);
numbers.push('<li></li>') numbers.push('<li></li>');
} }
let lang = infoString || ''; let lang = infoString || '';
@ -161,11 +161,11 @@ let WxRenderer = function (opts) {
renderer.image = (href, title, text) => { renderer.image = (href, title, text) => {
let subText = ''; let subText = '';
if (text) { if (text) {
subText = `<figcaption ${getStyles('figcaption')}>${text}</figcaption>` subText = `<figcaption ${getStyles('figcaption')}>${text}</figcaption>`;
} }
let figureStyles = getStyles('figure'); let figureStyles = getStyles('figure');
let imgStyles = getStyles(ENV_STRETCH_IMAGE ? 'image' : 'image_org'); let imgStyles = getStyles(ENV_STRETCH_IMAGE ? 'image' : 'image_org');
return `<figure ${figureStyles}><img ${imgStyles} src="${href}" title="${title}" alt="${text}"/>${subText}</figure>` return `<figure ${figureStyles}><img ${imgStyles} src="${href}" title="${title}" alt="${text}"/>${subText}</figure>`;
}; };
renderer.link = (href, title, text) => { renderer.link = (href, title, text) => {
if (href.indexOf('https://mp.weixin.qq.com') === 0) { if (href.indexOf('https://mp.weixin.qq.com') === 0) {
@ -187,6 +187,6 @@ let WxRenderer = function (opts) {
renderer.tablecell = (text, flags) => `<td ${getStyles('td')}>${text}</td>`; renderer.tablecell = (text, flags) => `<td ${getStyles('td')}>${text}</td>`;
renderer.hr = () => `<hr style="border-style: solid;border-width: 1px 0 0;border-color: rgba(0,0,0,0.1);-webkit-transform-origin: 0 0;-webkit-transform: scale(1, 0.5);transform-origin: 0 0;transform: scale(1, 0.5);">`; renderer.hr = () => `<hr style="border-style: solid;border-width: 1px 0 0;border-color: rgba(0,0,0,0.1);-webkit-transform-origin: 0 0;-webkit-transform: scale(1, 0.5);transform-origin: 0 0;transform: scale(1, 0.5);">`;
return renderer return renderer;
} }
}; };

View File

@ -65,6 +65,7 @@ let default_theme = {
'background': 'rgba(27,31,35,.05)', 'background': 'rgba(27,31,35,.05)',
'margin': '2em 8px' 'margin': '2em 8px'
}, },
blockquote_p: { blockquote_p: {
'letter-spacing': '0.1em', 'letter-spacing': '0.1em',
'color': 'rgb(80, 80, 80)', 'color': 'rgb(80, 80, 80)',
@ -72,6 +73,7 @@ let default_theme = {
'font-size': '1em', 'font-size': '1em',
'display': 'inline', 'display': 'inline',
}, },
code: { code: {
'font-size': '80%', 'font-size': '80%',
'overflow': 'auto', 'overflow': 'auto',
@ -83,29 +85,35 @@ let default_theme = {
'border': '1px solid rgb(236,236,236)', 'border': '1px solid rgb(236,236,236)',
'margin': '20px 0', 'margin': '20px 0',
}, },
image: { image: {
'border-radius': '4px', 'border-radius': '4px',
'display': 'block', 'display': 'block',
'margin': '0.1em auto 0.5em', 'margin': '0.1em auto 0.5em',
'width': '100% !important', 'width': '100% !important',
}, },
image_org: { image_org: {
'border-radius': '4px', 'border-radius': '4px',
'display': 'block' 'display': 'block'
}, },
ol: { ol: {
'margin-left': '0', 'margin-left': '0',
'padding-left': '1em' 'padding-left': '1em'
}, },
ul: { ul: {
'margin-left': '0', 'margin-left': '0',
'padding-left': '1em', 'padding-left': '1em',
'list-style': 'circle' 'list-style': 'circle'
}, },
footnotes: { footnotes: {
'margin': '0.5em 8px', 'margin': '0.5em 8px',
'font-size': '80%' 'font-size': '80%'
}, },
figure: { figure: {
'margin': '1.5em 8px', 'margin': '1.5em 8px',
} }
@ -116,6 +124,7 @@ let default_theme = {
'display': 'block', 'display': 'block',
'margin': '0.2em 8px' 'margin': '0.2em 8px'
}, },
codespan: { codespan: {
'font-size': '90%', 'font-size': '90%',
'color': '#d14', 'color': '#d14',
@ -123,9 +132,11 @@ let default_theme = {
'padding': '3px 5px', 'padding': '3px 5px',
'border-radius': '4px', 'border-radius': '4px',
}, },
link: { link: {
'color': '#576b95' 'color': '#576b95'
}, },
wx_link: { wx_link: {
'color': '#576b95', 'color': '#576b95',
'text-decoration': 'none', 'text-decoration': 'none',
@ -136,22 +147,27 @@ let default_theme = {
'color': 'rgba(255, 95, 46, 0.9)', 'color': 'rgba(255, 95, 46, 0.9)',
'font-weight': 'bold', 'font-weight': 'bold',
}, },
table: { table: {
'border-collapse': 'collapse', 'border-collapse': 'collapse',
'text-align': 'center', 'text-align': 'center',
'margin': '1em 8px' 'margin': '1em 8px'
}, },
thead: { thead: {
'background': 'rgba(0, 0, 0, 0.05)' 'background': 'rgba(0, 0, 0, 0.05)'
}, },
td: { td: {
'font-size': '80%', 'font-size': '80%',
'border': '1px solid #dfdfdf', 'border': '1px solid #dfdfdf',
'padding': '0.25em 0.5em' 'padding': '0.25em 0.5em'
}, },
footnote: { footnote: {
'font-size': '12px' 'font-size': '12px'
}, },
figcaption: { figcaption: {
'text-align': 'center', 'text-align': 'center',
'color': '#888', 'color': '#888',

View File

@ -3,8 +3,10 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta name="keywords" content="md,markdown,markdown-editor,wechat,official-account,yanglbme,cseye">
<meta name="description" content="Wechat Markdown Editor | 一款高度简洁的微信 Markdown 编辑器">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>微信 Markdown 编辑器</title> <title>微信 Markdown 编辑器</title>
<link rel="shortcut icon" href="assets/images/favicon.png"> <link rel="shortcut icon" href="assets/images/favicon.png">
<link rel="apple-touch-icon-precomposed" href="assets/images/favicon.png"> <link rel="apple-touch-icon-precomposed" href="assets/images/favicon.png">
@ -88,16 +90,16 @@
</el-container> </el-container>
<el-dialog title="关于" :visible.sync="aboutDialogVisible" width="30%" center> <el-dialog title="关于" :visible.sync="aboutDialogVisible" width="30%" center>
<div style="text-align: center;"> <div style="text-align: center;">
<h3>一款简洁美观的微信 Markdown 编辑器</h3> <h3>一款高度简洁的微信 Markdown 编辑器</h3>
</div> </div>
<div style="text-align: center;"> <div style="text-align: center;">
<img src="assets/images/qrcode.jpg" style="max-width: 300px"> <img src="assets/images/qrcode.jpg" style="max-width: 300px">
<p>扫码关注我的公众号,原创技术文章第一时间推送!</p> <p>扫码关注我的公众号,原创技术文章第一时间推送!</p>
</div> </div>
<span slot="footer" class="dialog-footer"> <span slot="footer" class="dialog-footer">
<el-button type="success" plain @click="openWindow('https://github.com/cseye/md')">GitHub 仓库 <el-button type="success" plain @click="visit('https://github.com/cseye/md')">GitHub 仓库
</el-button> </el-button>
<el-button type="success" plain @click="openWindow('https://gitee.com/cseye/md')">Gitee 仓库 <el-button type="success" plain @click="visit('https://gitee.com/cseye/md')">Gitee 仓库
</el-button> </el-button>
</span> </span>
</el-dialog> </el-dialog>

View File

@ -1,7 +1,7 @@
// 注音功能来自于 /**
// https://github.com/amclees/furigana-markdown * 注音功能来自于https://github.com/amclees/furigana-markdown
// 详见上述文档 * 详见上述文档
*/
(function (global, factory) { (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) : typeof define === 'function' && define.amd ? define(factory) :
@ -9,7 +9,7 @@
}(this, (function () { }(this, (function () {
'use strict'; '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) { function escapeForRegex(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
} }