md/src/store/index.js

160 lines
5.0 KiB
JavaScript
Raw Normal View History

2020-01-13 22:16:04 +08:00
import Vue from 'vue'
import Vuex from 'vuex'
2020-05-01 21:30:25 +08:00
import config from '../scripts/config';
import WxRenderer from '../scripts/renderers/wx-renderer'
import marked from 'marked'
import CodeMirror from 'codemirror/lib/codemirror'
import DEFAULT_CONTENT from '../scripts/default-content'
import DEFAULT_CSS_CONTENT from '../scripts/themes/default-theme-css'
import {
2020-05-31 17:30:44 +08:00
setColor,
formatDoc
2020-05-01 21:30:25 +08:00
} from '../scripts/util'
2020-01-13 22:16:04 +08:00
Vue.use(Vuex)
2020-05-01 21:30:25 +08:00
const state = {
wxRenderer: null,
output: '',
2020-07-04 09:41:47 +08:00
html: '',
2020-05-01 21:30:25 +08:00
editor: null,
cssEditor: null,
2020-05-01 22:35:39 +08:00
currentFont: '',
currentSize: '',
2020-05-02 11:50:26 +08:00
currentColor: '',
2020-05-17 16:53:21 +08:00
citeStatus: 0,
2020-05-17 18:33:36 +08:00
nightMode: false
2020-05-01 21:30:25 +08:00
};
const mutations = {
2020-07-04 09:41:47 +08:00
setHtmL(state, data) {
state.html = data;
2020-05-02 17:40:03 +08:00
},
2020-05-01 21:30:25 +08:00
setEditorValue(state, data) {
state.editor.setValue(data)
},
setCssEditorValue(state, data) {
state.cssEditor.setValue(data)
},
setWxRendererOptions(state, data) {
state.wxRenderer.setOptions(data);
},
2020-05-02 11:50:26 +08:00
setCiteStatus(state, data) {
state.citeStatus = data;
localStorage.setItem('citeStatus', data)
},
2020-05-01 21:30:25 +08:00
setCurrentFont(state, data) {
state.currentFont = data;
localStorage.setItem('fonts', data)
},
setCurrentSize(state, data) {
state.currentSize = data;
localStorage.setItem('size', data)
},
setCurrentColor(state, data) {
state.currentColor = data;
localStorage.setItem('color', data)
},
2020-05-17 16:53:21 +08:00
themeChanged(state) {
state.nightMode = !state.nightMode;
},
2020-05-01 21:30:25 +08:00
initEditorState(state) {
state.currentFont = localStorage.getItem('fonts') || config.builtinFonts[0].value
state.currentColor = localStorage.getItem('color') || config.colorOption[1].value
state.currentSize = localStorage.getItem('size') || config.sizeOption[2].value
2020-05-02 11:50:26 +08:00
state.citeStatus = localStorage.getItem('citeStatus') === 'true'
2020-05-01 21:30:25 +08:00
state.wxRenderer = new WxRenderer({
theme: setColor(state.currentColor),
fonts: state.currentFont,
size: state.currentSize,
2020-05-02 11:50:26 +08:00
status: state.citeStatus
2020-05-01 21:30:25 +08:00
})
},
initEditorEntity(state) {
state.editor = CodeMirror.fromTextArea(
document.getElementById('editor'),
2020-05-01 21:30:25 +08:00
{
value: '',
mode: 'text/x-markdown',
theme: 'xq-light',
lineNumbers: false,
lineWrapping: true,
styleActiveLine: true,
autoCloseBrackets: true,
extraKeys: {
'Ctrl-F': function autoFormat(editor) {
2020-05-31 17:30:44 +08:00
const doc = formatDoc(editor.getValue(0))
localStorage.setItem('__editor_content', doc)
editor.setValue(doc)
}
}
2020-05-01 21:30:25 +08:00
}
)
// 如果有编辑器内容被保存则读取,否则加载默认内容
if (localStorage.getItem('__editor_content')) {
state.editor.setValue(localStorage.getItem('__editor_content'))
} else {
2020-05-31 17:30:44 +08:00
const doc = formatDoc(DEFAULT_CONTENT)
state.editor.setValue(doc)
2020-05-01 21:30:25 +08:00
}
},
initCssEditorEntity(state) {
state.cssEditor = CodeMirror.fromTextArea(
document.getElementById('cssEditor'),
{
2020-05-01 21:30:25 +08:00
value: '',
mode: 'css',
theme: 'style-mirror',
lineNumbers: false,
lineWrapping: true,
matchBrackets: true,
autofocus: true,
extraKeys: {
'Ctrl-F': function autoFormat(editor) {
const totalLines = editor.lineCount()
2020-05-02 11:50:26 +08:00
editor.autoFormatRange({
2020-05-01 21:30:25 +08:00
line: 0,
ch: 0
}, {
line: totalLines
})
}
}
}
)
// 如果有编辑器内容被保存则读取,否则加载默认内容
if (localStorage.getItem('__css_content')) {
state.cssEditor.setValue(localStorage.getItem('__css_content'))
} else {
state.cssEditor.setValue(DEFAULT_CSS_CONTENT)
}
},
editorRefresh(state) {
let output = marked(state.editor.getValue(0), {
2020-05-02 11:50:26 +08:00
renderer: state.wxRenderer.getRenderer(state.citeStatus)
2020-05-01 21:30:25 +08:00
})
// 去除第一行的 margin-top
output = output.replace(/(style=".*?)"/, '$1;margin-top: 0"')
2020-05-02 11:50:26 +08:00
if (state.citeStatus) {
2020-05-01 21:30:25 +08:00
// 引用脚注
output += state.wxRenderer.buildFootnotes()
// 附加的一些 style
output += state.wxRenderer.buildAddition()
}
state.output = output
},
clearEditorToDefault(state) {
2020-05-31 17:43:54 +08:00
const doc = formatDoc(DEFAULT_CONTENT)
state.editor.setValue(doc)
2020-05-01 21:30:25 +08:00
state.cssEditor.setValue(DEFAULT_CSS_CONTENT)
}
}
2020-01-13 22:16:04 +08:00
export default new Vuex.Store({
2020-05-01 21:30:25 +08:00
state,
mutations,
actions: {},
modules: {}
2020-01-13 22:16:04 +08:00
})