You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

903 lines
28 KiB

5 years ago
5 years ago
5 years ago
  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.mt-3.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.mt-3.animated.fadeInLeft.wait-p2s(icon, tile, v-on='on', dark, @click='toggleModal(`editorModalBlocks`)', disabled).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.mt-3.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.mt-3.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.mt-3.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.mt-3.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.mt-3.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.mt-3.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.mt-3.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. textarea(ref='cm')
  164. transition(name='editor-markdown-preview')
  165. .editor-markdown-preview(v-if='previewShown')
  166. .editor-markdown-preview-content.contents(ref='editorPreviewContainer')
  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. /* global siteConfig */
  183. // ========================================
  184. // IMPORTS
  185. // ========================================
  186. // Code Mirror
  187. import CodeMirror from 'codemirror'
  188. import 'codemirror/lib/codemirror.css'
  189. // Language
  190. import 'codemirror/mode/markdown/markdown.js'
  191. // Addons
  192. import 'codemirror/addon/selection/active-line.js'
  193. import 'codemirror/addon/display/fullscreen.js'
  194. import 'codemirror/addon/display/fullscreen.css'
  195. import 'codemirror/addon/selection/mark-selection.js'
  196. import 'codemirror/addon/search/searchcursor.js'
  197. // Markdown-it
  198. import MarkdownIt from 'markdown-it'
  199. import mdAttrs from 'markdown-it-attrs'
  200. import mdEmoji from 'markdown-it-emoji'
  201. import mdTaskLists from 'markdown-it-task-lists'
  202. import mdExpandTabs from 'markdown-it-expand-tabs'
  203. import mdAbbr from 'markdown-it-abbr'
  204. import mdSup from 'markdown-it-sup'
  205. import mdSub from 'markdown-it-sub'
  206. import mdMark from 'markdown-it-mark'
  207. import mdFootnote from 'markdown-it-footnote'
  208. import mdImsize from 'markdown-it-imsize'
  209. import katex from 'katex'
  210. import twemoji from 'twemoji'
  211. // Prism (Syntax Highlighting)
  212. import Prism from 'prismjs'
  213. // Helpers
  214. import katexHelper from './common/katex'
  215. // ========================================
  216. // INIT
  217. // ========================================
  218. // Platform detection
  219. const CtrlKey = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  220. // Prism Config
  221. Prism.plugins.autoloader.languages_path = '/js/prism/'
  222. Prism.plugins.NormalizeWhitespace.setDefaults({
  223. 'remove-trailing': true,
  224. 'remove-indent': true,
  225. 'left-trim': true,
  226. 'right-trim': true,
  227. 'remove-initial-line-feed': true,
  228. 'tabs-to-spaces': 2
  229. })
  230. // Markdown Instance
  231. const md = new MarkdownIt({
  232. html: true,
  233. breaks: true,
  234. linkify: true,
  235. typography: true,
  236. highlight(str, lang) {
  237. return `<pre class="line-numbers"><code class="language-${lang}">${_.escape(str)}</code></pre>`
  238. }
  239. })
  240. .use(mdAttrs, {
  241. allowedAttributes: ['id', 'class', 'target']
  242. })
  243. .use(mdEmoji)
  244. .use(mdTaskLists, {label: true, labelAfter: true})
  245. .use(mdExpandTabs)
  246. .use(mdAbbr)
  247. .use(mdSup)
  248. .use(mdSub)
  249. .use(mdMark)
  250. .use(mdFootnote)
  251. .use(mdImsize)
  252. // ========================================
  253. // HELPER FUNCTIONS
  254. // ========================================
  255. // Inject line numbers for preview scroll sync
  256. let linesMap = []
  257. function injectLineNumbers (tokens, idx, options, env, slf) {
  258. let line
  259. if (tokens[idx].map && tokens[idx].level === 0) {
  260. line = tokens[idx].map[0]
  261. tokens[idx].attrJoin('class', 'line')
  262. tokens[idx].attrSet('data-line', String(line))
  263. linesMap.push(line)
  264. }
  265. return slf.renderToken(tokens, idx, options, env, slf)
  266. }
  267. md.renderer.rules.paragraph_open = injectLineNumbers
  268. md.renderer.rules.heading_open = injectLineNumbers
  269. md.renderer.rules.blockquote_open = injectLineNumbers
  270. // ========================================
  271. // KATEX
  272. // ========================================
  273. md.inline.ruler.after('escape', 'katex_inline', katexHelper.katexInline)
  274. md.renderer.rules.katex_inline = (tokens, idx) => {
  275. try {
  276. return katex.renderToString(tokens[idx].content, {
  277. displayMode: false
  278. })
  279. } catch (err) {
  280. console.warn(err)
  281. return tokens[idx].content
  282. }
  283. }
  284. md.block.ruler.after('blockquote', 'katex_block', katexHelper.katexBlock, {
  285. alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
  286. })
  287. md.renderer.rules.katex_block = (tokens, idx) => {
  288. try {
  289. return `<p>` + katex.renderToString(tokens[idx].content, {
  290. displayMode: true
  291. }) + `</p>`
  292. } catch (err) {
  293. console.warn(err)
  294. return tokens[idx].content
  295. }
  296. }
  297. // ========================================
  298. // TWEMOJI
  299. // ========================================
  300. md.renderer.rules.emoji = (token, idx) => {
  301. return twemoji.parse(token[idx].content, {
  302. callback (icon, opts) {
  303. return `/svg/twemoji/${icon}.svg`
  304. }
  305. })
  306. }
  307. // ========================================
  308. // Vue Component
  309. // ========================================
  310. export default {
  311. components: {
  312. markdownHelp
  313. },
  314. props: {
  315. save: {
  316. type: Function,
  317. default: () => {}
  318. }
  319. },
  320. data() {
  321. return {
  322. fabInsertMenu: false,
  323. cm: null,
  324. cursorPos: { ch: 0, line: 1 },
  325. previewShown: true,
  326. previewHTML: '',
  327. helpShown: false
  328. }
  329. },
  330. computed: {
  331. isMobile() {
  332. return this.$vuetify.breakpoint.smAndDown
  333. },
  334. isModalShown() {
  335. return this.helpShown || this.activeModal !== ''
  336. },
  337. locale: get('page/locale'),
  338. path: get('page/path'),
  339. mode: get('editor/mode'),
  340. activeModal: sync('editor/activeModal')
  341. },
  342. watch: {
  343. previewShown (newValue, oldValue) {
  344. if (newValue && !oldValue) {
  345. this.$nextTick(() => {
  346. Prism.highlightAllUnder(this.$refs.editorPreview)
  347. Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs'))
  348. })
  349. }
  350. }
  351. },
  352. methods: {
  353. toggleModal(key) {
  354. this.activeModal = (this.activeModal === key) ? '' : key
  355. this.helpShown = false
  356. },
  357. closeAllModal() {
  358. this.activeModal = ''
  359. this.helpShown = false
  360. },
  361. onCmInput: _.debounce(function (newContent) {
  362. linesMap = []
  363. this.$store.set('editor/content', newContent)
  364. this.previewHTML = md.render(newContent)
  365. this.$nextTick(() => {
  366. Prism.highlightAllUnder(this.$refs.editorPreview)
  367. Array.from(this.$refs.editorPreview.querySelectorAll('pre.line-numbers')).forEach(pre => pre.classList.add('prismjs'))
  368. this.scrollSync(this.cm)
  369. })
  370. }, 500),
  371. onCmPaste (cm, ev) {
  372. // const clipItems = (ev.clipboardData || ev.originalEvent.clipboardData).items
  373. // for (let clipItem of clipItems) {
  374. // if (_.startsWith(clipItem.type, 'image/')) {
  375. // const file = clipItem.getAsFile()
  376. // const reader = new FileReader()
  377. // reader.onload = evt => {
  378. // this.$store.commit(`loadingStart`, 'editor-paste-image')
  379. // this.insertAfter({
  380. // content: `![${file.name}](${evt.target.result})`,
  381. // newLine: true
  382. // })
  383. // }
  384. // reader.readAsDataURL(file)
  385. // }
  386. // }
  387. },
  388. /**
  389. * Update cursor state
  390. */
  391. positionSync(cm) {
  392. this.cursorPos = cm.getCursor('head')
  393. },
  394. /**
  395. * Wrap selection with start / end tags
  396. */
  397. toggleMarkup({ start, end }) {
  398. if (!end) { end = start }
  399. if (!this.cm.doc.somethingSelected()) {
  400. return this.$store.commit('showNotification', {
  401. message: this.$t('editor:markup.noSelectionError'),
  402. style: 'warning',
  403. icon: 'warning'
  404. })
  405. }
  406. this.cm.doc.replaceSelections(this.cm.doc.getSelections().map(s => start + s + end))
  407. },
  408. /**
  409. * Set current line as header
  410. */
  411. setHeaderLine(lvl) {
  412. const curLine = this.cm.doc.getCursor('head').line
  413. let lineContent = this.cm.doc.getLine(curLine)
  414. const lineLength = lineContent.length
  415. if (_.startsWith(lineContent, '#')) {
  416. lineContent = lineContent.replace(/^(#+ )/, '')
  417. }
  418. lineContent = _.times(lvl, n => '#').join('') + ` ` + lineContent
  419. this.cm.doc.replaceRange(lineContent, { line: curLine, ch: 0 }, { line: curLine, ch: lineLength })
  420. },
  421. /**
  422. * Get the header lever of the current line
  423. */
  424. getHeaderLevel(cm) {
  425. const curLine = this.cm.doc.getCursor('head').line
  426. let lineContent = this.cm.doc.getLine(curLine)
  427. let lvl = 0
  428. const result = lineContent.match(/^(#+) /)
  429. if (result) {
  430. lvl = _.get(result, '[1]', '').length
  431. }
  432. return lvl
  433. },
  434. /**
  435. * Insert content at cursor
  436. */
  437. insertAtCursor({ content }) {
  438. const cursor = this.cm.doc.getCursor('head')
  439. this.cm.doc.replaceRange(content, cursor)
  440. },
  441. /**
  442. * Insert content after current line
  443. */
  444. insertAfter({ content, newLine }) {
  445. const curLine = this.cm.doc.getCursor('to').line
  446. const lineLength = this.cm.doc.getLine(curLine).length
  447. this.cm.doc.replaceRange(newLine ? `\n${content}\n` : content, { line: curLine, ch: lineLength + 1 })
  448. },
  449. /**
  450. * Insert content before current line
  451. */
  452. insertBeforeEachLine({ content, after }) {
  453. let lines = []
  454. if (!this.cm.doc.somethingSelected()) {
  455. lines.push(this.cm.doc.getCursor('head').line)
  456. } else {
  457. lines = _.flatten(this.cm.doc.listSelections().map(sl => {
  458. const range = Math.abs(sl.anchor.line - sl.head.line) + 1
  459. const lowestLine = (sl.anchor.line > sl.head.line) ? sl.head.line : sl.anchor.line
  460. return _.times(range, l => l + lowestLine)
  461. }))
  462. }
  463. lines.forEach(ln => {
  464. let lineContent = this.cm.doc.getLine(ln)
  465. const lineLength = lineContent.length
  466. if (_.startsWith(lineContent, content)) {
  467. lineContent = lineContent.substring(content.length)
  468. }
  469. this.cm.doc.replaceRange(content + lineContent, { line: ln, ch: 0 }, { line: ln, ch: lineLength })
  470. })
  471. if (after) {
  472. const lastLine = _.last(lines)
  473. this.cm.doc.replaceRange(`\n${after}\n`, { line: lastLine, ch: this.cm.doc.getLine(lastLine).length + 1 })
  474. }
  475. },
  476. /**
  477. * Update scroll sync
  478. */
  479. scrollSync: _.debounce(function (cm) {
  480. if (!this.previewShown || cm.somethingSelected()) { return }
  481. let currentLine = cm.getCursor().line
  482. if (currentLine < 3) {
  483. this.Velocity(this.$refs.editorPreview, 'stop', true)
  484. this.Velocity(this.$refs.editorPreview.firstChild, 'scroll', { offset: '-50', duration: 1000, container: this.$refs.editorPreviewContainer })
  485. } else {
  486. let closestLine = _.findLast(linesMap, n => n <= currentLine)
  487. let destElm = this.$refs.editorPreview.querySelector(`[data-line='${closestLine}']`)
  488. if (destElm) {
  489. this.Velocity(this.$refs.editorPreview, 'stop', true)
  490. this.Velocity(destElm, 'scroll', { offset: '-100', duration: 1000, container: this.$refs.editorPreviewContainer })
  491. }
  492. }
  493. }, 500),
  494. toggleHelp () {
  495. this.helpShown = !this.helpShown
  496. this.activeModal = ''
  497. },
  498. toggleFullscreen () {
  499. this.cm.setOption('fullScreen', true)
  500. },
  501. refresh() {
  502. this.$nextTick(() => {
  503. this.cm.refresh()
  504. })
  505. }
  506. },
  507. mounted() {
  508. this.$store.set('editor/editorKey', 'markdown')
  509. if (this.mode === 'create' && !this.$store.get('editor/content')) {
  510. this.$store.set('editor/content', '# Header\nYour content here')
  511. }
  512. // Initialize CodeMirror
  513. this.cm = CodeMirror.fromTextArea(this.$refs.cm, {
  514. tabSize: 2,
  515. mode: 'text/markdown',
  516. theme: 'wikijs-dark',
  517. lineNumbers: true,
  518. lineWrapping: true,
  519. line: true,
  520. styleActiveLine: true,
  521. highlightSelectionMatches: {
  522. annotateScrollbar: true
  523. },
  524. viewportMargin: 50,
  525. inputStyle: 'contenteditable',
  526. allowDropFileTypes: ['image/jpg', 'image/png', 'image/svg', 'image/jpeg', 'image/gif'],
  527. direction: siteConfig.rtl ? 'rtl' : 'ltr'
  528. })
  529. this.cm.setValue(this.$store.get('editor/content'))
  530. this.cm.on('change', c => {
  531. this.$store.set('editor/content', c.getValue())
  532. this.onCmInput(this.$store.get('editor/content'))
  533. })
  534. if (this.$vuetify.breakpoint.mdAndUp) {
  535. this.cm.setSize(null, 'calc(100vh - 112px - 24px)')
  536. } else {
  537. this.cm.setSize(null, 'calc(100vh - 112px - 16px)')
  538. }
  539. // Set Keybindings
  540. const keyBindings = {
  541. 'F11' (c) {
  542. c.setOption('fullScreen', !c.getOption('fullScreen'))
  543. },
  544. 'Esc' (c) {
  545. if (c.getOption('fullScreen')) c.setOption('fullScreen', false)
  546. }
  547. }
  548. _.set(keyBindings, `${CtrlKey}-S`, c => {
  549. this.save()
  550. return false
  551. })
  552. _.set(keyBindings, `${CtrlKey}-B`, c => {
  553. this.toggleMarkup({ start: `**` })
  554. return false
  555. })
  556. _.set(keyBindings, `${CtrlKey}-I`, c => {
  557. this.toggleMarkup({ start: `*` })
  558. return false
  559. })
  560. _.set(keyBindings, `${CtrlKey}-Alt-Right`, c => {
  561. let lvl = this.getHeaderLevel(c)
  562. if (lvl >= 6) { lvl = 5 }
  563. this.setHeaderLine(lvl + 1)
  564. return false
  565. })
  566. _.set(keyBindings, `${CtrlKey}-Alt-Left`, c => {
  567. let lvl = this.getHeaderLevel(c)
  568. if (lvl <= 1) { lvl = 2 }
  569. this.setHeaderLine(lvl - 1)
  570. return false
  571. })
  572. this.cm.setOption('extraKeys', keyBindings)
  573. // Handle cursor movement
  574. this.cm.on('cursorActivity', c => {
  575. this.positionSync(c)
  576. this.scrollSync(c)
  577. })
  578. // Handle special paste
  579. this.cm.on('paste', this.onCmPaste)
  580. // Render initial preview
  581. this.onCmInput(this.$store.get('editor/content'))
  582. this.refresh()
  583. this.$root.$on('editorInsert', opts => {
  584. switch (opts.kind) {
  585. case 'IMAGE':
  586. let img = `![${opts.text}](${opts.path})`
  587. if (opts.align && opts.align !== '') {
  588. img += `{.align-${opts.align}}`
  589. }
  590. this.insertAtCursor({
  591. content: img
  592. })
  593. break
  594. case 'BINARY':
  595. this.insertAtCursor({
  596. content: `[${opts.text}](${opts.path})`
  597. })
  598. break
  599. }
  600. })
  601. // Handle save conflict
  602. this.$root.$on('saveConflict', () => {
  603. this.toggleModal(`editorModalConflict`)
  604. })
  605. this.$root.$on('overwriteEditorContent', () => {
  606. this.cm.setValue(this.$store.get('editor/content'))
  607. })
  608. },
  609. beforeDestroy() {
  610. this.$root.$off('editorInsert')
  611. }
  612. }
  613. </script>
  614. <style lang='scss'>
  615. $editor-height: calc(100vh - 112px - 24px);
  616. $editor-height-mobile: calc(100vh - 112px - 16px);
  617. .editor-markdown {
  618. &-main {
  619. display: flex;
  620. width: 100%;
  621. }
  622. &-editor {
  623. background-color: darken(mc('grey', '900'), 4.5%);
  624. flex: 1 1 50%;
  625. display: block;
  626. height: $editor-height;
  627. position: relative;
  628. @include until($tablet) {
  629. height: $editor-height-mobile;
  630. }
  631. }
  632. &-preview {
  633. flex: 1 1 50%;
  634. background-color: mc('grey', '100');
  635. position: relative;
  636. height: $editor-height;
  637. overflow: hidden;
  638. padding: 1rem;
  639. @at-root .theme--dark & {
  640. background-color: mc('grey', '900');
  641. }
  642. @include until($tablet) {
  643. display: none;
  644. }
  645. &-enter-active, &-leave-active {
  646. transition: max-width .5s ease;
  647. max-width: 50vw;
  648. .editor-code-preview-content {
  649. width: 50vw;
  650. overflow:hidden;
  651. }
  652. }
  653. &-enter, &-leave-to {
  654. max-width: 0;
  655. }
  656. &-content {
  657. height: $editor-height;
  658. overflow-y: scroll;
  659. padding: 0;
  660. width: calc(100% + 17px);
  661. // -ms-overflow-style: none;
  662. // &::-webkit-scrollbar {
  663. // width: 0px;
  664. // background: transparent;
  665. // }
  666. @include until($tablet) {
  667. height: $editor-height-mobile;
  668. }
  669. }
  670. }
  671. &-toolbar {
  672. background-color: mc('blue', '700');
  673. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  674. color: #FFF;
  675. .v-toolbar__content {
  676. padding-left: 64px;
  677. @include until($tablet) {
  678. padding-left: 8px;
  679. }
  680. }
  681. }
  682. &-insert:not(.v-speed-dial--right) {
  683. @include from($tablet) {
  684. left: 50%;
  685. margin-left: -28px;
  686. }
  687. }
  688. &-sidebar {
  689. background-color: mc('grey', '900');
  690. width: 64px;
  691. display: flex;
  692. flex-direction: column;
  693. justify-content: flex-start;
  694. align-items: center;
  695. padding: 24px 0;
  696. @include until($tablet) {
  697. padding: 12px 0;
  698. width: 40px;
  699. }
  700. }
  701. &-sysbar {
  702. padding-left: 0;
  703. &-locale {
  704. background-color: rgba(255,255,255,.25);
  705. display:inline-flex;
  706. padding: 0 12px;
  707. height: 24px;
  708. width: 63px;
  709. justify-content: center;
  710. align-items: center;
  711. }
  712. }
  713. // ==========================================
  714. // Fix FAB revealing under codemirror
  715. // ==========================================
  716. .speed-dial--fixed {
  717. z-index: 8;
  718. }
  719. // ==========================================
  720. // CODE MIRROR
  721. // ==========================================
  722. .CodeMirror {
  723. height: auto;
  724. font-family: 'Roboto Mono', monospace;
  725. font-size: .9rem;
  726. .cm-header-1 {
  727. font-size: 1.5rem;
  728. }
  729. .cm-header-2 {
  730. font-size: 1.25rem;
  731. }
  732. .cm-header-3 {
  733. font-size: 1.15rem;
  734. }
  735. .cm-header-4 {
  736. font-size: 1.1rem;
  737. }
  738. .cm-header-5 {
  739. font-size: 1.05rem;
  740. }
  741. .cm-header-6 {
  742. font-size: 1.025rem;
  743. }
  744. }
  745. .CodeMirror-focused .cm-matchhighlight {
  746. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  747. background-position: bottom;
  748. background-repeat: repeat-x;
  749. }
  750. .cm-matchhighlight {
  751. background-color: mc('grey', '800');
  752. }
  753. .CodeMirror-selection-highlight-scrollbar {
  754. background-color: mc('green', '600');
  755. }
  756. .cm-s-wikijs-dark.CodeMirror {
  757. background: darken(mc('grey','900'), 3%);
  758. color: #e0e0e0;
  759. }
  760. .cm-s-wikijs-dark div.CodeMirror-selected {
  761. background: mc('blue','800');
  762. }
  763. .cm-s-wikijs-dark .cm-matchhighlight {
  764. background: mc('blue','800');
  765. }
  766. .cm-s-wikijs-dark .CodeMirror-line::selection, .cm-s-wikijs-dark .CodeMirror-line > span::selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::selection {
  767. background: mc('amber', '500');
  768. }
  769. .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 {
  770. background: mc('amber', '500');
  771. }
  772. .cm-s-wikijs-dark .CodeMirror-gutters {
  773. background: darken(mc('grey','900'), 6%);
  774. border-right: 1px solid mc('grey','900');
  775. }
  776. .cm-s-wikijs-dark .CodeMirror-guttermarker {
  777. color: #ac4142;
  778. }
  779. .cm-s-wikijs-dark .CodeMirror-guttermarker-subtle {
  780. color: #505050;
  781. }
  782. .cm-s-wikijs-dark .CodeMirror-linenumber {
  783. color: mc('grey','800');
  784. }
  785. .cm-s-wikijs-dark .CodeMirror-cursor {
  786. border-left: 1px solid #b0b0b0;
  787. }
  788. .cm-s-wikijs-dark span.cm-comment {
  789. color: mc('orange','800');
  790. }
  791. .cm-s-wikijs-dark span.cm-atom {
  792. color: #aa759f;
  793. }
  794. .cm-s-wikijs-dark span.cm-number {
  795. color: #aa759f;
  796. }
  797. .cm-s-wikijs-dark span.cm-property, .cm-s-wikijs-dark span.cm-attribute {
  798. color: #90a959;
  799. }
  800. .cm-s-wikijs-dark span.cm-keyword {
  801. color: #ac4142;
  802. }
  803. .cm-s-wikijs-dark span.cm-string {
  804. color: #f4bf75;
  805. }
  806. .cm-s-wikijs-dark span.cm-variable {
  807. color: #90a959;
  808. }
  809. .cm-s-wikijs-dark span.cm-variable-2 {
  810. color: #6a9fb5;
  811. }
  812. .cm-s-wikijs-dark span.cm-def {
  813. color: #d28445;
  814. }
  815. .cm-s-wikijs-dark span.cm-bracket {
  816. color: #e0e0e0;
  817. }
  818. .cm-s-wikijs-dark span.cm-tag {
  819. color: #ac4142;
  820. }
  821. .cm-s-wikijs-dark span.cm-link {
  822. color: #aa759f;
  823. }
  824. .cm-s-wikijs-dark span.cm-error {
  825. background: #ac4142;
  826. color: #b0b0b0;
  827. }
  828. .cm-s-wikijs-dark .CodeMirror-activeline-background {
  829. background: mc('grey','900');
  830. }
  831. .cm-s-wikijs-dark .CodeMirror-matchingbracket {
  832. text-decoration: underline;
  833. color: white !important;
  834. }
  835. }
  836. </style>