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.

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