2019-11-01 17:16:40 +08:00
|
|
|
let app = new Vue({
|
|
|
|
el: '#app',
|
|
|
|
data: function () {
|
|
|
|
let d = {
|
|
|
|
aboutOutput: '',
|
|
|
|
output: '',
|
|
|
|
source: '',
|
|
|
|
editorThemes: [
|
2019-11-10 15:52:46 +08:00
|
|
|
{ label: '淡雅', value: 'xq-light' },
|
|
|
|
{ label: '精美', value: 'eclipse' },
|
|
|
|
{ label: '暗绿', value: 'oceanic-next' }
|
2019-11-01 17:16:40 +08:00
|
|
|
],
|
|
|
|
editor: null,
|
|
|
|
builtinFonts: [
|
|
|
|
{
|
|
|
|
label: '无衬线',
|
|
|
|
value: "-apple-system-font,BlinkMacSystemFont, Helvetica Neue, PingFang SC, Hiragino Sans GB , Microsoft YaHei UI , Microsoft YaHei ,Arial,sans-serif"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '衬线',
|
|
|
|
value: "Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
sizeOption: [
|
2019-11-07 14:06:59 +08:00
|
|
|
{ label: '13px', value: '13px', desc: '稍小' },
|
2019-11-03 16:05:04 +08:00
|
|
|
{ label: '14px', value: '14px', desc: '推荐' },
|
2019-11-07 14:06:59 +08:00
|
|
|
{ label: '15px', value: '15px', desc: '稍大' },
|
2019-11-01 17:16:40 +08:00
|
|
|
],
|
2019-11-05 15:16:42 +08:00
|
|
|
colorOption: [
|
2019-11-06 17:13:48 +08:00
|
|
|
{ label: '橘红', value: 'rgba(255, 95, 46, 0.9)', hex: '#FF5F2E' },
|
2019-11-10 15:52:46 +08:00
|
|
|
{ label: '淡绿', value: 'rgba(66, 185, 131, 0.9)', hex: '#42B983' },
|
|
|
|
{ label: '暗青', value: 'rgba(0, 139, 139, 0.9)', hex: '#008B8B' }
|
2019-11-05 15:16:42 +08:00
|
|
|
],
|
2019-11-01 17:16:40 +08:00
|
|
|
aboutDialogVisible: false
|
|
|
|
};
|
|
|
|
d.currentEditorTheme = d.editorThemes[0].value;
|
|
|
|
d.currentFont = d.builtinFonts[0].value;
|
2019-11-07 14:06:59 +08:00
|
|
|
d.currentSize = d.sizeOption[1].value;
|
2019-11-05 21:32:50 +08:00
|
|
|
d.currentColor = d.colorOption[1].value;
|
2019-11-01 17:16:40 +08:00
|
|
|
return d;
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.editor = CodeMirror.fromTextArea(
|
|
|
|
document.getElementById('editor'),
|
|
|
|
{
|
|
|
|
lineNumbers: false,
|
|
|
|
lineWrapping: true,
|
|
|
|
styleActiveLine: true,
|
|
|
|
theme: this.currentEditorTheme,
|
|
|
|
mode: 'text/x-markdown',
|
|
|
|
}
|
|
|
|
);
|
2019-11-10 15:52:46 +08:00
|
|
|
this.editor.on("change", (cm, change) => {
|
|
|
|
this.refresh();
|
|
|
|
this.saveEditorContent();
|
2019-11-01 17:16:40 +08:00
|
|
|
});
|
|
|
|
this.wxRenderer = new WxRenderer({
|
2019-11-05 21:32:50 +08:00
|
|
|
theme: setColor(this.currentColor),
|
2019-11-01 17:16:40 +08:00
|
|
|
fonts: this.currentFont,
|
|
|
|
size: this.currentSize
|
|
|
|
});
|
|
|
|
// 如果有编辑内容被保存则读取,否则加载默认文档
|
|
|
|
if (localStorage.getItem("__editor_content")) {
|
|
|
|
this.editor.setValue(localStorage.getItem("__editor_content"));
|
|
|
|
} else {
|
|
|
|
axios({
|
|
|
|
method: 'get',
|
|
|
|
url: './assets/default-content.md',
|
2019-11-10 15:52:46 +08:00
|
|
|
}).then(resp => {
|
|
|
|
this.editor.setValue(resp.data)
|
2019-11-01 17:16:40 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-11-10 15:52:46 +08:00
|
|
|
renderWeChat(source) {
|
2019-11-01 17:16:40 +08:00
|
|
|
let output = marked(source, { renderer: this.wxRenderer.getRenderer() });
|
|
|
|
if (this.wxRenderer.hasFootnotes()) {
|
|
|
|
// 去除第一行的 margin-top
|
|
|
|
output = output.replace(/(style=".*?)"/, '$1;margin-top: 0"');
|
2019-11-08 10:30:32 +08:00
|
|
|
// 引用脚注
|
2019-11-01 17:16:40 +08:00
|
|
|
output += this.wxRenderer.buildFootnotes();
|
|
|
|
// 附加的一些 style
|
|
|
|
output += this.wxRenderer.buildAddition();
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
editorThemeChanged(editorTheme) {
|
2019-11-01 17:16:40 +08:00
|
|
|
this.editor.setOption('theme', editorTheme)
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
fontChanged(fonts) {
|
2019-11-01 17:16:40 +08:00
|
|
|
this.wxRenderer.setOptions({
|
|
|
|
fonts: fonts
|
|
|
|
});
|
|
|
|
this.refresh()
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
sizeChanged(size) {
|
2019-11-01 17:16:40 +08:00
|
|
|
this.wxRenderer.setOptions({
|
|
|
|
size: size
|
|
|
|
});
|
|
|
|
this.refresh()
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
colorChanged(color) {
|
2019-11-05 21:32:50 +08:00
|
|
|
let theme = setColor(color)
|
2019-11-01 17:16:40 +08:00
|
|
|
this.wxRenderer.setOptions({
|
2019-11-05 21:32:50 +08:00
|
|
|
theme: theme
|
|
|
|
})
|
2019-11-01 17:16:40 +08:00
|
|
|
this.refresh()
|
|
|
|
},
|
|
|
|
// 刷新右侧预览
|
2019-11-10 15:52:46 +08:00
|
|
|
refresh() {
|
2019-11-01 17:16:40 +08:00
|
|
|
this.output = this.renderWeChat(this.editor.getValue(0))
|
|
|
|
},
|
|
|
|
// 将左侧编辑器内容保存到 LocalStorage
|
2019-11-10 15:52:46 +08:00
|
|
|
saveEditorContent() {
|
2019-11-01 17:16:40 +08:00
|
|
|
let content = this.editor.getValue(0);
|
2019-11-10 15:52:46 +08:00
|
|
|
if (content) {
|
2019-11-01 17:16:40 +08:00
|
|
|
localStorage.setItem("__editor_content", content);
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem("__editor_content");
|
|
|
|
}
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
copy() {
|
2019-11-01 17:16:40 +08:00
|
|
|
let clipboardDiv = document.getElementById('output');
|
|
|
|
clipboardDiv.focus();
|
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
let range = document.createRange();
|
|
|
|
range.setStartBefore(clipboardDiv.firstChild);
|
|
|
|
range.setEndAfter(clipboardDiv.lastChild);
|
|
|
|
window.getSelection().addRange(range);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (document.execCommand('copy')) {
|
2019-11-07 14:58:14 +08:00
|
|
|
this.$notify({
|
2019-11-05 19:31:36 +08:00
|
|
|
showClose: true,
|
2019-11-07 14:58:14 +08:00
|
|
|
message: '已复制文章到剪贴板,可直接到公众号后台粘贴',
|
|
|
|
offset: 80,
|
|
|
|
duration: 1600,
|
2019-11-05 19:31:36 +08:00
|
|
|
type: 'success'
|
|
|
|
});
|
2019-11-01 17:16:40 +08:00
|
|
|
} else {
|
2019-11-07 14:58:14 +08:00
|
|
|
this.$notify({
|
2019-11-05 19:31:36 +08:00
|
|
|
showClose: true,
|
2019-11-07 14:58:14 +08:00
|
|
|
message: '未能复制文章到剪贴板,请全选后右键复制',
|
|
|
|
offset: 80,
|
|
|
|
duration: 1600,
|
2019-11-05 19:31:36 +08:00
|
|
|
type: 'warning'
|
|
|
|
});
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-11-07 14:58:14 +08:00
|
|
|
this.$notify({
|
2019-11-05 19:31:36 +08:00
|
|
|
showClose: true,
|
2019-11-07 14:58:14 +08:00
|
|
|
message: '未能复制文章到剪贴板,请全选后右键复制',
|
|
|
|
offset: 80,
|
|
|
|
duration: 1600,
|
2019-11-05 19:31:36 +08:00
|
|
|
type: 'warning'
|
|
|
|
});
|
2019-11-01 17:16:40 +08:00
|
|
|
}
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
openWindow(url) {
|
2019-11-01 17:16:40 +08:00
|
|
|
window.open(url);
|
|
|
|
}
|
|
|
|
},
|
2019-11-10 15:52:46 +08:00
|
|
|
updated() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
prettyPrint();
|
2019-11-01 17:16:40 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|