editor-markdown.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <template lang='pug'>
  2. .editor-markdown
  3. v-toolbar.editor-markdown-toolbar(dense, color='primary', dark, flat, style='overflow-x: hidden;')
  4. template(v-if='isModalShown')
  5. v-spacer
  6. v-btn.animated.fadeInRight(text, @click='closeAllModal')
  7. v-icon(left) mdi-arrow-left-circle
  8. span {{$t('editor:backToEditor')}}
  9. template(v-else)
  10. v-tooltip(bottom, color='primary')
  11. template(v-slot:activator='{ on }')
  12. v-btn.animated.fadeIn(icon, tile, v-on='on', @click='toggleMarkup({ start: `**` })').mx-0
  13. v-icon mdi-format-bold
  14. span {{$t('editor:markup.bold')}}
  15. v-tooltip(bottom, color='primary')
  16. template(v-slot:activator='{ on }')
  17. v-btn.animated.fadeIn.wait-p1s(icon, tile, v-on='on', @click='toggleMarkup({ start: `*` })').mx-0
  18. v-icon mdi-format-italic
  19. span {{$t('editor:markup.italic')}}
  20. v-tooltip(bottom, color='primary')
  21. template(v-slot:activator='{ on }')
  22. v-btn.animated.fadeIn.wait-p2s(icon, tile, v-on='on', @click='toggleMarkup({ start: `~~` })').mx-0
  23. v-icon mdi-format-strikethrough
  24. span {{$t('editor:markup.strikethrough')}}
  25. v-menu(offset-y, open-on-hover)
  26. template(v-slot:activator='{ on }')
  27. v-btn.animated.fadeIn.wait-p3s(icon, tile, v-on='on').mx-0
  28. v-icon mdi-format-header-pound
  29. v-list.py-0
  30. template(v-for='(n, idx) in 6')
  31. v-list-item(@click='setHeaderLine(n)', :key='idx')
  32. v-list-item-action
  33. v-icon(:size='24 - (idx - 1) * 2') mdi-format-header-{{n}}
  34. v-list-item-title {{$t('editor:markup.heading', { level: n })}}
  35. v-divider(v-if='idx < 5')
  36. v-tooltip(bottom, color='primary')
  37. template(v-slot:activator='{ on }')
  38. v-btn.animated.fadeIn.wait-p4s(icon, tile, v-on='on', @click='toggleMarkup({ start: `~` })').mx-0
  39. v-icon mdi-format-subscript
  40. span {{$t('editor:markup.subscript')}}
  41. v-tooltip(bottom, color='primary')
  42. template(v-slot:activator='{ on }')
  43. v-btn.animated.fadeIn.wait-p5s(icon, tile, v-on='on', @click='toggleMarkup({ start: `^` })').mx-0
  44. v-icon mdi-format-superscript
  45. span {{$t('editor:markup.superscript')}}
  46. v-menu(offset-y, open-on-hover)
  47. template(v-slot:activator='{ on }')
  48. v-btn.animated.fadeIn.wait-p6s(icon, tile, v-on='on').mx-0
  49. v-icon mdi-alpha-t-box-outline
  50. v-list.py-0
  51. v-list-item(@click='insertBeforeEachLine({ content: `> `})')
  52. v-list-item-action
  53. v-icon mdi-alpha-t-box-outline
  54. v-list-item-title {{$t('editor:markup.blockquote')}}
  55. v-divider
  56. v-list-item(@click='insertBeforeEachLine({ content: `> `, after: `{.is-info}`})')
  57. v-list-item-action
  58. v-icon(color='blue') mdi-alpha-i-box-outline
  59. v-list-item-title {{$t('editor:markup.blockquoteInfo')}}
  60. v-divider
  61. v-list-item(@click='insertBeforeEachLine({ content: `> `, after: `{.is-success}`})')
  62. v-list-item-action
  63. v-icon(color='success') mdi-alpha-s-box-outline
  64. v-list-item-title {{$t('editor:markup.blockquoteSuccess')}}
  65. v-divider
  66. v-list-item(@click='insertBeforeEachLine({ content: `> `, after: `{.is-warning}`})')
  67. v-list-item-action
  68. v-icon(color='warning') mdi-alpha-w-box-outline
  69. v-list-item-title {{$t('editor:markup.blockquoteWarning')}}
  70. v-divider
  71. v-list-item(@click='insertBeforeEachLine({ content: `> `, after: `{.is-danger}`})')
  72. v-list-item-action
  73. v-icon(color='error') mdi-alpha-e-box-outline
  74. v-list-item-title {{$t('editor:markup.blockquoteError')}}
  75. v-divider
  76. v-tooltip(bottom, color='primary')
  77. template(v-slot:activator='{ on }')
  78. v-btn.animated.fadeIn.wait-p7s(icon, tile, v-on='on', @click='insertBeforeEachLine({ content: `- `})').mx-0
  79. v-icon mdi-format-list-bulleted
  80. span {{$t('editor:markup.unorderedList')}}
  81. v-tooltip(bottom, color='primary')
  82. template(v-slot:activator='{ on }')
  83. v-btn.animated.fadeIn.wait-p8s(icon, tile, v-on='on', @click='insertBeforeEachLine({ content: `1. `})').mx-0
  84. v-icon mdi-format-list-numbered
  85. span {{$t('editor:markup.orderedList')}}
  86. v-tooltip(bottom, color='primary')
  87. template(v-slot:activator='{ on }')
  88. v-btn.animated.fadeIn.wait-p9s(icon, tile, v-on='on', @click='toggleMarkup({ start: "`" })').mx-0
  89. v-icon mdi-code-tags
  90. span {{$t('editor:markup.inlineCode')}}
  91. v-tooltip(bottom, color='primary')
  92. template(v-slot:activator='{ on }')
  93. v-btn.animated.fadeIn.wait-p10s(icon, tile, v-on='on', @click='toggleMarkup({ start: `<kbd>`, end: `</kbd>` })').mx-0
  94. v-icon mdi-keyboard-variant
  95. span {{$t('editor:markup.keyboardKey')}}
  96. v-tooltip(bottom, color='primary')
  97. template(v-slot:activator='{ on }')
  98. v-btn.animated.fadeIn.wait-p11s(icon, tile, v-on='on', @click='insertAfter({ content: `---`, newLine: true })').mx-0
  99. v-icon mdi-minus
  100. span {{$t('editor:markup.horizontalBar')}}
  101. template(v-if='$vuetify.breakpoint.mdAndUp')
  102. v-spacer
  103. v-tooltip(bottom, color='primary')
  104. template(v-slot:activator='{ on }')
  105. v-btn.animated.fadeIn.wait-p11s(icon, tile, v-on='on', @click='previewShown = !previewShown').mx-0
  106. v-icon mdi-book-open-outline
  107. span {{$t('editor:markup.togglePreviewPane')}}
  108. .editor-markdown-main
  109. .editor-markdown-sidebar
  110. v-tooltip(right, color='teal')
  111. template(v-slot:activator='{ on }')
  112. v-btn.animated.fadeInLeft(icon, tile, v-on='on', dark, disabled).mx-0
  113. v-icon mdi-link-plus
  114. span {{$t('editor:markup.insertLink')}}
  115. v-tooltip(right, color='teal')
  116. template(v-slot:activator='{ on }')
  117. v-btn.animated.fadeInLeft.wait-p1s(icon, tile, v-on='on', dark, @click='toggleModal(`editorModalMedia`)').mx-0
  118. v-icon(:color='activeModal === `editorModalMedia` ? `teal` : ``') mdi-folder-multiple-image
  119. span {{$t('editor:markup.insertAssets')}}
  120. v-tooltip(right, color='teal')
  121. template(v-slot:activator='{ on }')
  122. v-btn.animated.fadeInLeft.wait-p2s(icon, tile, v-on='on', dark, @click='toggleModal(`editorModalBlocks`)').mx-0
  123. v-icon(:color='activeModal === `editorModalBlocks` ? `teal` : ``') mdi-view-dashboard-outline
  124. span {{$t('editor:markup.insertBlock')}}
  125. v-tooltip(right, color='teal')
  126. template(v-slot:activator='{ on }')
  127. v-btn.animated.fadeInLeft.wait-p3s(icon, tile, v-on='on', dark, disabled).mx-0
  128. v-icon mdi-code-braces
  129. span {{$t('editor:markup.insertCodeBlock')}}
  130. v-tooltip(right, color='teal')
  131. template(v-slot:activator='{ on }')
  132. v-btn.animated.fadeInLeft.wait-p4s(icon, tile, v-on='on', dark, disabled).mx-0
  133. v-icon mdi-library-video
  134. span {{$t('editor:markup.insertVideoAudio')}}
  135. v-tooltip(right, color='teal')
  136. template(v-slot:activator='{ on }')
  137. v-btn.animated.fadeInLeft.wait-p5s(icon, tile, v-on='on', dark, disabled).mx-0
  138. v-icon mdi-chart-multiline
  139. span {{$t('editor:markup.insertDiagram')}}
  140. v-tooltip(right, color='teal')
  141. template(v-slot:activator='{ on }')
  142. v-btn.animated.fadeInLeft.wait-p6s(icon, tile, v-on='on', dark, disabled).mx-0
  143. v-icon mdi-function-variant
  144. span {{$t('editor:markup.insertMathExpression')}}
  145. v-tooltip(right, color='teal')
  146. template(v-slot:activator='{ on }')
  147. v-btn.animated.fadeInLeft.wait-p7s(icon, tile, v-on='on', dark, disabled).mx-0
  148. v-icon mdi-table-plus
  149. span {{$t('editor:markup.tableHelper')}}
  150. template(v-if='$vuetify.breakpoint.mdAndUp')
  151. v-spacer
  152. v-tooltip(right, color='teal')
  153. template(v-slot:activator='{ on }')
  154. v-btn.animated.fadeInLeft.wait-p8s(icon, tile, v-on='on', dark, @click='toggleFullscreen').mx-0
  155. v-icon mdi-arrow-expand-all
  156. span {{$t('editor:markup.distractionFreeMode')}}
  157. v-tooltip(right, color='teal')
  158. template(v-slot:activator='{ on }')
  159. v-btn.animated.fadeInLeft.wait-p9s(icon, tile, v-on='on', dark, @click='toggleHelp').mx-0
  160. v-icon(:color='helpShown ? `teal` : ``') mdi-help-circle
  161. span {{$t('editor:markup.markdownFormattingHelp')}}
  162. .editor-markdown-editor
  163. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready='onCmReady', @input='onCmInput')
  164. transition(name='editor-markdown-preview')
  165. .editor-markdown-preview(v-if='previewShown')
  166. .editor-markdown-preview-content.contents
  167. div(ref='editorPreview', v-html='previewHTML')
  168. v-system-bar.editor-markdown-sysbar(dark, status, color='grey darken-3')
  169. .caption.editor-markdown-sysbar-locale {{locale.toUpperCase()}}
  170. .caption.px-3 /{{path}}
  171. template(v-if='$vuetify.breakpoint.mdAndUp')
  172. v-spacer
  173. .caption Markdown
  174. v-spacer
  175. .caption Ln {{cursorPos.line + 1}}, Col {{cursorPos.ch + 1}}
  176. markdown-help(v-if='helpShown')
  177. </template>
  178. <script>
  179. import _ from 'lodash'
  180. import { get, sync } from 'vuex-pathify'
  181. import markdownHelp from './markdown/help.vue'
  182. // ========================================
  183. // IMPORTS
  184. // ========================================
  185. // Code Mirror
  186. import { codemirror } from 'vue-codemirror'
  187. import 'codemirror/lib/codemirror.css'
  188. // Language
  189. import 'codemirror/mode/markdown/markdown.js'
  190. // Addons
  191. import 'codemirror/addon/selection/active-line.js'
  192. import 'codemirror/addon/display/fullscreen.js'
  193. import 'codemirror/addon/display/fullscreen.css'
  194. import 'codemirror/addon/selection/mark-selection.js'
  195. import 'codemirror/addon/search/searchcursor.js'
  196. // Markdown-it
  197. import MarkdownIt from 'markdown-it'
  198. import mdAttrs from 'markdown-it-attrs'
  199. import mdEmoji from 'markdown-it-emoji'
  200. import mdTaskLists from 'markdown-it-task-lists'
  201. import mdExpandTabs from 'markdown-it-expand-tabs'
  202. import mdAbbr from 'markdown-it-abbr'
  203. import mdSup from 'markdown-it-sup'
  204. import mdSub from 'markdown-it-sub'
  205. import mdMark from 'markdown-it-mark'
  206. import mdImsize from 'markdown-it-imsize'
  207. // Prism (Syntax Highlighting)
  208. import Prism from 'prismjs'
  209. // ========================================
  210. // INIT
  211. // ========================================
  212. // Platform detection
  213. const CtrlKey = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  214. // Markdown Instance
  215. const md = new MarkdownIt({
  216. html: true,
  217. breaks: true,
  218. linkify: true,
  219. typography: true,
  220. highlight(str, lang) {
  221. return `<pre class="line-numbers"><code class="language-${lang}">${str}</code></pre>`
  222. }
  223. })
  224. .use(mdAttrs)
  225. .use(mdEmoji)
  226. .use(mdTaskLists, {label: true, labelAfter: true})
  227. .use(mdExpandTabs)
  228. .use(mdAbbr)
  229. .use(mdSup)
  230. .use(mdSub)
  231. .use(mdMark)
  232. .use(mdImsize)
  233. // ========================================
  234. // HELPER FUNCTIONS
  235. // ========================================
  236. // Inject line numbers for preview scroll sync
  237. let linesMap = []
  238. function injectLineNumbers (tokens, idx, options, env, slf) {
  239. let line
  240. if (tokens[idx].map && tokens[idx].level === 0) {
  241. line = tokens[idx].map[0]
  242. tokens[idx].attrJoin('class', 'line')
  243. tokens[idx].attrSet('data-line', String(line))
  244. linesMap.push(line)
  245. }
  246. return slf.renderToken(tokens, idx, options, env, slf)
  247. }
  248. md.renderer.rules.paragraph_open = injectLineNumbers
  249. md.renderer.rules.heading_open = injectLineNumbers
  250. // ========================================
  251. // Vue Component
  252. // ========================================
  253. export default {
  254. components: {
  255. codemirror,
  256. markdownHelp
  257. },
  258. props: {
  259. save: {
  260. type: Function,
  261. default: () => {}
  262. }
  263. },
  264. data() {
  265. return {
  266. fabInsertMenu: false,
  267. code: this.$store.get('editor/content'),
  268. cmOptions: {
  269. tabSize: 2,
  270. mode: 'text/markdown',
  271. theme: 'wikijs-dark',
  272. lineNumbers: true,
  273. lineWrapping: true,
  274. line: true,
  275. styleActiveLine: true,
  276. highlightSelectionMatches: {
  277. annotateScrollbar: true
  278. },
  279. viewportMargin: 50,
  280. inputStyle: 'contenteditable',
  281. allowDropFileTypes: ['image/jpg', 'image/png', 'image/svg', 'image/jpeg', 'image/gif']
  282. },
  283. cursorPos: { ch: 0, line: 1 },
  284. previewShown: true,
  285. previewHTML: '',
  286. helpShown: false
  287. }
  288. },
  289. computed: {
  290. cm() {
  291. return this.$refs.cm.codemirror
  292. },
  293. isMobile() {
  294. return this.$vuetify.breakpoint.smAndDown
  295. },
  296. isModalShown() {
  297. return this.helpShown || this.activeModal !== ''
  298. },
  299. locale: get('page/locale'),
  300. path: get('page/path'),
  301. activeModal: sync('editor/activeModal')
  302. },
  303. methods: {
  304. toggleModal(key) {
  305. this.activeModal = (this.activeModal === key) ? '' : key
  306. this.helpShown = false
  307. },
  308. closeAllModal() {
  309. this.activeModal = ''
  310. this.helpShown = false
  311. },
  312. onCmReady(cm) {
  313. const keyBindings = {
  314. 'F11' (cm) {
  315. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  316. },
  317. 'Esc' (cm) {
  318. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  319. }
  320. }
  321. _.set(keyBindings, `${CtrlKey}-S`, cm => {
  322. this.save()
  323. return false
  324. })
  325. _.set(keyBindings, `${CtrlKey}-B`, cm => {
  326. this.toggleMarkup({ start: `**` })
  327. return false
  328. })
  329. _.set(keyBindings, `${CtrlKey}-I`, cm => {
  330. this.toggleMarkup({ start: `*` })
  331. return false
  332. })
  333. _.set(keyBindings, `${CtrlKey}-Alt-Right`, cm => {
  334. let lvl = this.getHeaderLevel(cm)
  335. if (lvl >= 6) { lvl = 5 }
  336. this.setHeaderLine(lvl + 1)
  337. return false
  338. })
  339. _.set(keyBindings, `${CtrlKey}-Alt-Left`, cm => {
  340. let lvl = this.getHeaderLevel(cm)
  341. if (lvl <= 1) { lvl = 2 }
  342. this.setHeaderLine(lvl - 1)
  343. return false
  344. })
  345. if (this.$vuetify.breakpoint.mdAndUp) {
  346. cm.setSize(null, 'calc(100vh - 112px - 24px)')
  347. } else {
  348. cm.setSize(null, 'calc(100vh - 112px - 16px)')
  349. }
  350. cm.setOption('extraKeys', keyBindings)
  351. cm.on('cursorActivity', cm => {
  352. this.positionSync(cm)
  353. this.scrollSync(cm)
  354. })
  355. cm.on('paste', this.onCmPaste)
  356. this.onCmInput(this.code)
  357. },
  358. onCmInput: _.debounce(function (newContent) {
  359. linesMap = []
  360. this.$store.set('editor/content', newContent)
  361. this.previewHTML = md.render(newContent)
  362. this.$nextTick(() => {
  363. Prism.highlightAllUnder(this.$refs.editorPreview)
  364. Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs'))
  365. this.scrollSync(this.cm)
  366. })
  367. }, 500),
  368. onCmPaste (cm, ev) {
  369. const clipItems = (ev.clipboardData || ev.originalEvent.clipboardData).items
  370. for (const clipItem of clipItems) {
  371. if (_.startsWith(clipItem.type, 'image/')) {
  372. const file = clipItem.getAsFile()
  373. const reader = new FileReader()
  374. reader.onload = evt => {
  375. this.$store.commit(`loadingStart`, 'editor-paste-image')
  376. this.insertAfter({
  377. content: `![${file.name}](${evt.target.result})`,
  378. newLine: true
  379. })
  380. }
  381. reader.readAsDataURL(file)
  382. }
  383. }
  384. },
  385. /**
  386. * Update cursor state
  387. */
  388. positionSync(cm) {
  389. this.cursorPos = cm.getCursor('head')
  390. },
  391. /**
  392. * Wrap selection with start / end tags
  393. */
  394. toggleMarkup({ start, end }) {
  395. if (!end) { end = start }
  396. if (!this.cm.doc.somethingSelected()) {
  397. return this.$store.commit('showNotification', {
  398. message: this.$t('editor:markup.noSelectionError'),
  399. style: 'warning',
  400. icon: 'warning'
  401. })
  402. }
  403. this.cm.doc.replaceSelections(this.cm.doc.getSelections().map(s => start + s + end))
  404. },
  405. /**
  406. * Set current line as header
  407. */
  408. setHeaderLine(lvl) {
  409. const curLine = this.cm.doc.getCursor('head').line
  410. let lineContent = this.cm.doc.getLine(curLine)
  411. const lineLength = lineContent.length
  412. if (_.startsWith(lineContent, '#')) {
  413. lineContent = lineContent.replace(/^(#+ )/, '')
  414. }
  415. lineContent = _.times(lvl, n => '#').join('') + ` ` + lineContent
  416. this.cm.doc.replaceRange(lineContent, { line: curLine, ch: 0 }, { line: curLine, ch: lineLength })
  417. },
  418. /**
  419. * Get the header lever of the current line
  420. */
  421. getHeaderLevel(cm) {
  422. const curLine = this.cm.doc.getCursor('head').line
  423. let lineContent = this.cm.doc.getLine(curLine)
  424. let lvl = 0
  425. const result = lineContent.match(/^(#+) /)
  426. if (result) {
  427. lvl = _.get(result, '[1]', '').length
  428. }
  429. return lvl
  430. },
  431. /**
  432. * Insert content at cursor
  433. */
  434. insertAtCursor({ content }) {
  435. const cursor = this.cm.doc.getCursor('head')
  436. this.cm.doc.replaceRange(content, cursor)
  437. },
  438. /**
  439. * Insert content after current line
  440. */
  441. insertAfter({ content, newLine }) {
  442. const curLine = this.cm.doc.getCursor('to').line
  443. const lineLength = this.cm.doc.getLine(curLine).length
  444. this.cm.doc.replaceRange(newLine ? `\n${content}\n` : content, { line: curLine, ch: lineLength + 1 })
  445. },
  446. /**
  447. * Insert content before current line
  448. */
  449. insertBeforeEachLine({ content, after }) {
  450. let lines = []
  451. if (!this.cm.doc.somethingSelected()) {
  452. lines.push(this.cm.doc.getCursor('head').line)
  453. } else {
  454. lines = _.flatten(this.cm.doc.listSelections().map(sl => {
  455. const range = Math.abs(sl.anchor.line - sl.head.line) + 1
  456. const lowestLine = (sl.anchor.line > sl.head.line) ? sl.head.line : sl.anchor.line
  457. return _.times(range, l => l + lowestLine)
  458. }))
  459. }
  460. lines.forEach(ln => {
  461. let lineContent = this.cm.doc.getLine(ln)
  462. const lineLength = lineContent.length
  463. if (_.startsWith(lineContent, content)) {
  464. lineContent = lineContent.substring(content.length)
  465. }
  466. this.cm.doc.replaceRange(content + lineContent, { line: ln, ch: 0 }, { line: ln, ch: lineLength })
  467. })
  468. if (after) {
  469. const lastLine = _.last(lines)
  470. this.cm.doc.replaceRange(`\n${after}\n`, { line: lastLine, ch: this.cm.doc.getLine(lastLine).length + 1 })
  471. }
  472. },
  473. /**
  474. * Update scroll sync
  475. */
  476. scrollSync: _.debounce(function (cm) {
  477. if (!this.previewShown || cm.somethingSelected()) { return }
  478. let currentLine = cm.getCursor().line
  479. if (currentLine < 3) {
  480. this.Velocity(this.$refs.editorPreview, 'stop', true)
  481. this.Velocity(this.$refs.editorPreview.firstChild, 'scroll', { offset: '-50', duration: 1000, container: this.$refs.editorPreview })
  482. } else {
  483. let closestLine = _.findLast(linesMap, n => n <= currentLine)
  484. let destElm = this.$refs.editorPreview.querySelector(`[data-line='${closestLine}']`)
  485. if (destElm) {
  486. this.Velocity(this.$refs.editorPreview, 'stop', true)
  487. this.Velocity(destElm, 'scroll', { offset: '-100', duration: 1000, container: this.$refs.editorPreview })
  488. }
  489. }
  490. }, 500),
  491. toggleHelp () {
  492. this.helpShown = !this.helpShown
  493. this.activeModal = ''
  494. },
  495. toggleFullscreen () {
  496. this.cm.setOption('fullScreen', true)
  497. }
  498. },
  499. mounted() {
  500. this.$root.$on('editorInsert', opts => {
  501. switch (opts.kind) {
  502. case 'IMAGE':
  503. let img = `![${opts.text}](${opts.path})`
  504. if (opts.align && opts.align !== '') {
  505. img += `{.align-${opts.align}}`
  506. }
  507. this.insertAtCursor({
  508. content: img
  509. })
  510. break
  511. case 'BINARY':
  512. this.insertAtCursor({
  513. content: `[${opts.text}](${opts.path})`
  514. })
  515. break
  516. }
  517. })
  518. },
  519. beforeDestroy() {
  520. this.$root.$off('editorInsert')
  521. }
  522. }
  523. </script>
  524. <style lang='scss'>
  525. $editor-height: calc(100vh - 112px - 24px);
  526. $editor-height-mobile: calc(100vh - 112px - 16px);
  527. .editor-markdown {
  528. &-main {
  529. display: flex;
  530. width: 100%;
  531. }
  532. &-editor {
  533. background-color: darken(mc('grey', '900'), 4.5%);
  534. flex: 1 1 50%;
  535. display: block;
  536. height: $editor-height;
  537. position: relative;
  538. @include until($tablet) {
  539. height: $editor-height-mobile;
  540. }
  541. }
  542. &-preview {
  543. flex: 1 1 50%;
  544. background-color: mc('grey', '100');
  545. position: relative;
  546. height: $editor-height;
  547. overflow: hidden;
  548. @at-root .theme--dark & {
  549. background-color: mc('grey', '900');
  550. }
  551. @include until($tablet) {
  552. display: none;
  553. }
  554. &-enter-active, &-leave-active {
  555. transition: max-width .5s ease;
  556. max-width: 50vw;
  557. .editor-code-preview-content {
  558. width: 50vw;
  559. overflow:hidden;
  560. }
  561. }
  562. &-enter, &-leave-to {
  563. max-width: 0;
  564. }
  565. &-content {
  566. height: $editor-height;
  567. overflow-y: scroll;
  568. padding: 1rem 1rem 1rem 1rem;
  569. width: calc(100% + 1rem + 17px);
  570. // -ms-overflow-style: none;
  571. // &::-webkit-scrollbar {
  572. // width: 0px;
  573. // background: transparent;
  574. // }
  575. @include until($tablet) {
  576. height: $editor-height-mobile;
  577. }
  578. }
  579. }
  580. &-toolbar {
  581. background-color: mc('blue', '700');
  582. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  583. color: #FFF;
  584. .v-toolbar__content {
  585. padding-left: 64px;
  586. @include until($tablet) {
  587. padding-left: 8px;
  588. }
  589. }
  590. }
  591. &-insert:not(.v-speed-dial--right) {
  592. @include from($tablet) {
  593. left: 50%;
  594. margin-left: -28px;
  595. }
  596. }
  597. &-sidebar {
  598. background-color: mc('grey', '900');
  599. width: 64px;
  600. display: flex;
  601. flex-direction: column;
  602. justify-content: flex-start;
  603. align-items: center;
  604. padding: 24px 0;
  605. @include until($tablet) {
  606. padding: 12px 0;
  607. width: 40px;
  608. }
  609. }
  610. &-sysbar {
  611. padding-left: 0;
  612. &-locale {
  613. background-color: rgba(255,255,255,.25);
  614. display:inline-flex;
  615. padding: 0 12px;
  616. height: 24px;
  617. width: 63px;
  618. justify-content: center;
  619. align-items: center;
  620. }
  621. }
  622. // ==========================================
  623. // Fix FAB revealing under codemirror
  624. // ==========================================
  625. .speed-dial--fixed {
  626. z-index: 8;
  627. }
  628. // ==========================================
  629. // CODE MIRROR
  630. // ==========================================
  631. .CodeMirror {
  632. height: auto;
  633. .cm-header-1 {
  634. font-size: 1.5rem;
  635. }
  636. .cm-header-2 {
  637. font-size: 1.25rem;
  638. }
  639. .cm-header-3 {
  640. font-size: 1.15rem;
  641. }
  642. .cm-header-4 {
  643. font-size: 1.1rem;
  644. }
  645. .cm-header-5 {
  646. font-size: 1.05rem;
  647. }
  648. .cm-header-6 {
  649. font-size: 1.025rem;
  650. }
  651. }
  652. .CodeMirror-focused .cm-matchhighlight {
  653. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  654. background-position: bottom;
  655. background-repeat: repeat-x;
  656. }
  657. .cm-matchhighlight {
  658. background-color: mc('grey', '800');
  659. }
  660. .CodeMirror-selection-highlight-scrollbar {
  661. background-color: mc('green', '600');
  662. }
  663. .cm-s-wikijs-dark.CodeMirror {
  664. background: darken(mc('grey','900'), 3%);
  665. color: #e0e0e0;
  666. }
  667. .cm-s-wikijs-dark div.CodeMirror-selected {
  668. background: mc('blue','800');
  669. }
  670. .cm-s-wikijs-dark .cm-matchhighlight {
  671. background: mc('blue','800');
  672. }
  673. .cm-s-wikijs-dark .CodeMirror-line::selection, .cm-s-wikijs-dark .CodeMirror-line > span::selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::selection {
  674. background: mc('red', '500');
  675. }
  676. .cm-s-wikijs-dark .CodeMirror-line::-moz-selection, .cm-s-wikijs-dark .CodeMirror-line > span::-moz-selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::-moz-selection {
  677. background: mc('red', '500');
  678. }
  679. .cm-s-wikijs-dark .CodeMirror-gutters {
  680. background: darken(mc('grey','900'), 6%);
  681. border-right: 1px solid mc('grey','900');
  682. }
  683. .cm-s-wikijs-dark .CodeMirror-guttermarker {
  684. color: #ac4142;
  685. }
  686. .cm-s-wikijs-dark .CodeMirror-guttermarker-subtle {
  687. color: #505050;
  688. }
  689. .cm-s-wikijs-dark .CodeMirror-linenumber {
  690. color: mc('grey','800');
  691. }
  692. .cm-s-wikijs-dark .CodeMirror-cursor {
  693. border-left: 1px solid #b0b0b0;
  694. }
  695. .cm-s-wikijs-dark span.cm-comment {
  696. color: mc('orange','800');
  697. }
  698. .cm-s-wikijs-dark span.cm-atom {
  699. color: #aa759f;
  700. }
  701. .cm-s-wikijs-dark span.cm-number {
  702. color: #aa759f;
  703. }
  704. .cm-s-wikijs-dark span.cm-property, .cm-s-wikijs-dark span.cm-attribute {
  705. color: #90a959;
  706. }
  707. .cm-s-wikijs-dark span.cm-keyword {
  708. color: #ac4142;
  709. }
  710. .cm-s-wikijs-dark span.cm-string {
  711. color: #f4bf75;
  712. }
  713. .cm-s-wikijs-dark span.cm-variable {
  714. color: #90a959;
  715. }
  716. .cm-s-wikijs-dark span.cm-variable-2 {
  717. color: #6a9fb5;
  718. }
  719. .cm-s-wikijs-dark span.cm-def {
  720. color: #d28445;
  721. }
  722. .cm-s-wikijs-dark span.cm-bracket {
  723. color: #e0e0e0;
  724. }
  725. .cm-s-wikijs-dark span.cm-tag {
  726. color: #ac4142;
  727. }
  728. .cm-s-wikijs-dark span.cm-link {
  729. color: #aa759f;
  730. }
  731. .cm-s-wikijs-dark span.cm-error {
  732. background: #ac4142;
  733. color: #b0b0b0;
  734. }
  735. .cm-s-wikijs-dark .CodeMirror-activeline-background {
  736. background: mc('grey','900');
  737. }
  738. .cm-s-wikijs-dark .CodeMirror-matchingbracket {
  739. text-decoration: underline;
  740. color: white !important;
  741. }
  742. }
  743. </style>