editor-markdown.vue 26 KB

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