diff --git a/src/components/ContentParserFactory.js b/src/components/ContentParserFactory.js index fe16336..e03d579 100644 --- a/src/components/ContentParserFactory.js +++ b/src/components/ContentParserFactory.js @@ -1,9 +1,11 @@ -import { MarkdownParser } from './plugin/MarkdownParser' +import MarkdownIt from 'markdown-it' function contentParserFactory (parsers) { - return content => { - return MarkdownParser(content, parsers) - } + let converter = MarkdownIt() + parsers.forEach(parser => { + converter = converter.use(parser) + }) + return content => converter.render(content) } export { contentParserFactory } diff --git a/src/components/DefaultConfig.js b/src/components/DefaultConfig.js index 84895d2..8d3b0e9 100644 --- a/src/components/DefaultConfig.js +++ b/src/components/DefaultConfig.js @@ -27,7 +27,8 @@ export const defaultConfig = { mode: 'markdown', lineNumbers: true, lineWrapping: true - } + }, + scrollSync: true } export function getConfig (config) { diff --git a/src/components/InputArea.vue b/src/components/InputArea.vue index 9082256..9a16417 100644 --- a/src/components/InputArea.vue +++ b/src/components/InputArea.vue @@ -32,6 +32,7 @@ \ No newline at end of file + diff --git a/src/components/MarkdownPalettes.vue b/src/components/MarkdownPalettes.vue index 1e74f19..5468f82 100644 --- a/src/components/MarkdownPalettes.vue +++ b/src/components/MarkdownPalettes.vue @@ -13,14 +13,18 @@ ref="inputArea" @input="updateCode" @finish="insertCode = null" + @scroll-sync="doScrollSync('editor', $event)" :insertCode="insertCode" - :editorOption="editorConfig.editorOption"> + :editorOption="editorConfig.editorOption" + :scrollSync="scrollSync">
- +
@@ -89,6 +93,7 @@ import EditorDialog from './Dialog.vue' import { defaultConfig, getConfig } from './DefaultConfig' import { contentParserFactory } from './ContentParserFactory' +import InjectLnParser from './plugins/InjectLnParser.js' export default { name: 'markdown-palettes', @@ -113,7 +118,8 @@ export default { editorConfig: config, editorHeight: '500px', fullScreen: config.fullScreen, - contentParser: contentParserFactory(config.parsers) + contentParser: contentParserFactory([...config.parsers, InjectLnParser]), + scrollSync: config.scrollSync } }, mounted () { @@ -159,6 +165,16 @@ export default { this.fullScreen = false } } + if (operation === 'scrollSync') { + this.scrollSync = !this.scrollSync + } + }, + doScrollSync (emitter, info) { + if (emitter === 'editor') { + this.$refs.previewArea.updateScrollSync(info) + } else if (emitter === 'preview') { + this.$refs.inputArea.updateScrollSync(info) + } } }, watch: { @@ -168,4 +184,4 @@ export default { } } } - \ No newline at end of file + diff --git a/src/components/PreviewArea.vue b/src/components/PreviewArea.vue index b7bcecb..d5b7070 100644 --- a/src/components/PreviewArea.vue +++ b/src/components/PreviewArea.vue @@ -1,6 +1,6 @@ @@ -80,6 +80,8 @@ \ No newline at end of file + diff --git a/src/components/plugin/MarkdownParser.js b/src/components/plugin/MarkdownParser.js deleted file mode 100644 index 77f5e99..0000000 --- a/src/components/plugin/MarkdownParser.js +++ /dev/null @@ -1,9 +0,0 @@ -import MarkdownIt from 'markdown-it' - -export function MarkdownParser (code, parsers) { - let converter = MarkdownIt() - parsers.forEach(parser => { - converter = converter.use(parser) - }) - return converter.render(code) -} diff --git a/src/components/plugins/InjectLnParser.js b/src/components/plugins/InjectLnParser.js new file mode 100644 index 0000000..859c91b --- /dev/null +++ b/src/components/plugins/InjectLnParser.js @@ -0,0 +1,37 @@ +/* +Copyright (c) 2014 Vitaly Puzrin, Alex Kocharin. + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +*/ + +export default mdHtml => { + function injectLineNumbers (tokens, idx, options, env, slf) { + let line + if (tokens[idx].map && tokens[idx].level === 0) { + line = tokens[idx].map[0] + tokens[idx].attrSet('data-line', String(line)) + } + return slf.renderToken(tokens, idx, options, env, slf) + } + + mdHtml.renderer.rules.paragraph_open = mdHtml.renderer.rules.heading_open = injectLineNumbers +} diff --git a/src/components/toolbar-button/btn-scrollsync.js b/src/components/toolbar-button/btn-scrollsync.js new file mode 100644 index 0000000..85f13e2 --- /dev/null +++ b/src/components/toolbar-button/btn-scrollsync.js @@ -0,0 +1,8 @@ +export default { + name: 'scrollSync', + icon: 'fa-unlock-alt', + title: '同步滚动', + action: { + event: 'scrollSync' + } +} diff --git a/src/components/toolbar-button/toolbarBtn.js b/src/components/toolbar-button/toolbarBtn.js index 11453af..e14b341 100644 --- a/src/components/toolbar-button/toolbarBtn.js +++ b/src/components/toolbar-button/toolbarBtn.js @@ -12,6 +12,7 @@ import BtnHr from './btn-hr' import BtnCode from './btn-code' import BtnHide from './btn-hide' import BtnFullscreen from './btn-fullscreen' +import BtnScrollsync from './btn-scrollsync' import BtnInfo from './btn-info' export const defaultBtns = [ @@ -37,6 +38,7 @@ export const defaultBtns = [ Divider, BtnHide, BtnFullscreen, + BtnScrollsync, Divider, BtnInfo ]