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.

503 lines
13 KiB

  1. <template lang='pug'>
  2. .editor-code
  3. v-toolbar.editor-code-toolbar(dense, color='primary', dark)
  4. v-tooltip(top)
  5. v-btn(icon, slot='activator').mx-0
  6. v-icon format_bold
  7. span Bold
  8. v-tooltip(top)
  9. v-btn(icon, slot='activator').mx-0
  10. v-icon format_italic
  11. span Italic
  12. v-tooltip(top)
  13. v-btn(icon, slot='activator').mx-0
  14. v-icon format_strikethrough
  15. span Strikethrough
  16. v-menu(offset-y, open-on-hover)
  17. v-btn(icon, slot='activator').mx-0
  18. v-icon font_download
  19. v-list
  20. v-list-tile(v-for='(n, idx) in 6', @click='', :key='idx')
  21. v-list-tile-action
  22. v-icon font_download
  23. v-list-tile-title Heading {{n}}
  24. v-tooltip(top)
  25. v-btn(icon, slot='activator').mx-0
  26. v-icon format_quote
  27. span Blockquote
  28. v-tooltip(top)
  29. v-btn(icon, slot='activator').mx-0
  30. v-icon format_list_bulleted
  31. span Unordered List
  32. v-tooltip(top)
  33. v-btn(icon, slot='activator').mx-0
  34. v-icon format_list_numbered
  35. span Ordered List
  36. v-tooltip(top)
  37. v-btn(icon, slot='activator').mx-0
  38. v-icon insert_link
  39. span Link
  40. v-tooltip(top)
  41. v-btn(icon, slot='activator').mx-0
  42. v-icon space_bar
  43. span Inline Code
  44. v-tooltip(top)
  45. v-btn(icon, slot='activator').mx-0
  46. v-icon code
  47. span Code Block
  48. v-tooltip(top)
  49. v-btn(icon, slot='activator').mx-0
  50. v-icon remove
  51. span Horizontal Bar
  52. .editor-code-main
  53. .editor-code-editor
  54. .editor-code-editor-title(v-if='previewShown', @click='previewShown = false') Editor
  55. .editor-code-editor-title(v-else='previewShown', @click='previewShown = true'): v-icon(dark) drag_indicator
  56. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready='onCmReady', @input='onCmInput')
  57. transition(name='editor-code-preview')
  58. .editor-code-preview(v-if='previewShown')
  59. .editor-code-preview-title(@click='previewShown = false') Preview
  60. .editor-code-preview-content.contents(ref='editorPreview', v-html='previewHTML')
  61. v-speed-dial(v-model='fabInsertMenu', :open-on-hover='true', direction='top', transition='slide-y-reverse-transition', fixed, left, bottom)
  62. v-btn(color='blue', fab, dark, v-model='fabInsertMenu', slot='activator')
  63. v-icon add_circle
  64. v-icon close
  65. v-btn(color='teal', fab, dark): v-icon image
  66. v-btn(color='pink', fab, dark): v-icon insert_drive_file
  67. v-btn(color='red', fab, dark): v-icon play_circle_outline
  68. v-btn(color='purple', fab, dark): v-icon multiline_chart
  69. v-btn(color='indigo', fab, dark): v-icon functions
  70. </template>
  71. <script>
  72. import _ from 'lodash'
  73. // ========================================
  74. // IMPORTS
  75. // ========================================
  76. // Code Mirror
  77. import { codemirror } from 'vue-codemirror'
  78. import 'codemirror/lib/codemirror.css'
  79. // Language
  80. import 'codemirror/mode/markdown/markdown.js'
  81. // Addons
  82. import 'codemirror/addon/selection/active-line.js'
  83. import 'codemirror/addon/display/fullscreen.js'
  84. import 'codemirror/addon/display/fullscreen.css'
  85. import 'codemirror/addon/selection/mark-selection.js'
  86. import 'codemirror/addon/scroll/annotatescrollbar.js'
  87. import 'codemirror/addon/search/matchesonscrollbar.js'
  88. import 'codemirror/addon/search/searchcursor.js'
  89. import 'codemirror/addon/search/match-highlighter.js'
  90. // Markdown-it
  91. import MarkdownIt from 'markdown-it'
  92. import mdAttrs from 'markdown-it-attrs'
  93. import mdEmoji from 'markdown-it-emoji'
  94. import mdTaskLists from 'markdown-it-task-lists'
  95. import mdExpandTabs from 'markdown-it-expand-tabs'
  96. import mdAbbr from 'markdown-it-abbr'
  97. import mdSup from 'markdown-it-sup'
  98. import mdSub from 'markdown-it-sub'
  99. import mdMark from 'markdown-it-mark'
  100. import mdImsize from 'markdown-it-imsize'
  101. // Prism (Syntax Highlighting)
  102. import Prism from '@/libs/prism/prism.js'
  103. // ========================================
  104. // INIT
  105. // ========================================
  106. // Platform detection
  107. const CtrlKey = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  108. // Markdown Instance
  109. const md = new MarkdownIt({
  110. html: true,
  111. breaks: true,
  112. linkify: true,
  113. typography: true,
  114. highlight(str, lang) {
  115. return `<pre class="line-numbers"><code class="language-${lang}">${str}</code></pre>`
  116. }
  117. })
  118. .use(mdAttrs)
  119. .use(mdEmoji)
  120. .use(mdTaskLists, {label: true, labelAfter: true})
  121. .use(mdExpandTabs)
  122. .use(mdAbbr)
  123. .use(mdSup)
  124. .use(mdSub)
  125. .use(mdMark)
  126. .use(mdImsize)
  127. // ========================================
  128. // HELPER FUNCTIONS
  129. // ========================================
  130. // Inject line numbers for preview scroll sync
  131. let linesMap = []
  132. function injectLineNumbers (tokens, idx, options, env, slf) {
  133. let line
  134. if (tokens[idx].map && tokens[idx].level === 0) {
  135. line = tokens[idx].map[0]
  136. tokens[idx].attrJoin('class', 'line')
  137. tokens[idx].attrSet('data-line', String(line))
  138. linesMap.push(line)
  139. }
  140. return slf.renderToken(tokens, idx, options, env, slf)
  141. }
  142. md.renderer.rules.paragraph_open = injectLineNumbers
  143. md.renderer.rules.heading_open = injectLineNumbers
  144. // ========================================
  145. // Vue Component
  146. // ========================================
  147. export default {
  148. components: {
  149. codemirror
  150. },
  151. data() {
  152. return {
  153. fabInsertMenu: false,
  154. code: this.$store.get('editor/content'),
  155. cmOptions: {
  156. tabSize: 2,
  157. mode: 'text/markdown',
  158. theme: 'wikijs-dark',
  159. lineNumbers: true,
  160. lineWrapping: true,
  161. line: true,
  162. styleActiveLine: true,
  163. highlightSelectionMatches: {
  164. annotateScrollbar: true
  165. },
  166. viewportMargin: 50
  167. },
  168. previewShown: true,
  169. previewHTML: ''
  170. }
  171. },
  172. computed: {
  173. cm() {
  174. return this.$refs.cm.codemirror
  175. },
  176. isMobile() {
  177. return this.$vuetify.breakpoint.smAndDown
  178. }
  179. },
  180. methods: {
  181. onCmReady(cm) {
  182. let self = this
  183. const keyBindings = {
  184. 'F11' (cm) {
  185. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  186. },
  187. 'Esc' (cm) {
  188. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  189. }
  190. }
  191. _.set(keyBindings, `${CtrlKey}-S`, cm => {
  192. self.$parent.save()
  193. })
  194. cm.setSize(null, 'calc(100vh - 112px)')
  195. cm.setOption('extraKeys', keyBindings)
  196. cm.on('cursorActivity', cm => {
  197. this.toolbarSync(cm)
  198. this.scrollSync(cm)
  199. })
  200. this.onCmInput(this.code)
  201. },
  202. onCmInput: _.debounce(function (newContent) {
  203. linesMap = []
  204. this.$store.set('editor/content', newContent)
  205. this.previewHTML = md.render(newContent)
  206. this.$nextTick(() => {
  207. Prism.highlightAllUnder(this.$refs.editorPreview)
  208. this.scrollSync(this.cm)
  209. })
  210. }, 500),
  211. /**
  212. * Update toolbar state
  213. */
  214. toolbarSync(cm) {
  215. const pos = cm.getCursor('start')
  216. const token = cm.getTokenAt(pos)
  217. if (!token.type) { return }
  218. // console.info(token)
  219. },
  220. /**
  221. * Update scroll sync
  222. */
  223. scrollSync: _.debounce(function (cm) {
  224. if (!this.previewShown || cm.somethingSelected()) { return }
  225. let currentLine = cm.getCursor().line
  226. if (currentLine < 3) {
  227. this.Velocity(this.$refs.editorPreview, 'stop', true)
  228. this.Velocity(this.$refs.editorPreview.firstChild, 'scroll', { offset: '-50', duration: 1000, container: this.$refs.editorPreview })
  229. } else {
  230. let closestLine = _.findLast(linesMap, n => n <= currentLine)
  231. let destElm = this.$refs.editorPreview.querySelector(`[data-line='${closestLine}']`)
  232. if (destElm) {
  233. this.Velocity(this.$refs.editorPreview, 'stop', true)
  234. this.Velocity(destElm, 'scroll', { offset: '-100', duration: 1000, container: this.$refs.editorPreview })
  235. }
  236. }
  237. }, 500),
  238. toggleAround (before, after) {
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang='scss'>
  244. .editor-code {
  245. &-main {
  246. display: flex;
  247. width: 100%;
  248. }
  249. &-editor {
  250. background-color: darken(mc('grey', '900'), 4.5%);
  251. flex: 1 1 50%;
  252. display: block;
  253. height: calc(100vh - 112px);
  254. position: relative;
  255. &-title {
  256. background-color: mc('grey', '800');
  257. border-bottom-left-radius: 5px;
  258. display: inline-flex;
  259. height: 30px;
  260. justify-content: center;
  261. align-items: center;
  262. padding: 0 1rem;
  263. color: mc('grey', '500');
  264. position: absolute;
  265. top: 0;
  266. right: 0;
  267. z-index: 7;
  268. text-transform: uppercase;
  269. font-size: .7rem;
  270. cursor: pointer;
  271. @include until($tablet) {
  272. display: none;
  273. }
  274. }
  275. }
  276. &-preview {
  277. flex: 1 1 50%;
  278. background-color: mc('grey', '100');
  279. position: relative;
  280. height: calc(100vh - 112px);
  281. overflow: hidden;
  282. @at-root .theme--dark & {
  283. background-color: mc('grey', '900');
  284. }
  285. @include until($tablet) {
  286. display: none;
  287. }
  288. &-enter-active, &-leave-active {
  289. transition: max-width .5s ease;
  290. max-width: 50vw;
  291. .editor-code-preview-content {
  292. width: 50vw;
  293. overflow:hidden;
  294. }
  295. }
  296. &-enter, &-leave-to {
  297. max-width: 0;
  298. }
  299. &-content {
  300. height: calc(100vh - 112px);
  301. overflow-y: scroll;
  302. padding: 1rem 1rem 1rem 0;
  303. width: calc(100% + 1rem + 17px)
  304. // -ms-overflow-style: none;
  305. // &::-webkit-scrollbar {
  306. // width: 0px;
  307. // background: transparent;
  308. // }
  309. }
  310. &-title {
  311. background-color: rgba(mc('blue', '100'), .75);
  312. border-bottom-right-radius: 5px;
  313. display: inline-flex;
  314. height: 30px;
  315. justify-content: center;
  316. align-items: center;
  317. padding: 0 1rem;
  318. color: mc('blue', '800');
  319. position: absolute;
  320. top: 0;
  321. left: 0;
  322. z-index: 2;
  323. text-transform: uppercase;
  324. font-size: .7rem;
  325. cursor: pointer;
  326. }
  327. }
  328. &-toolbar {
  329. background-color: mc('blue', '700');
  330. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  331. color: #FFF;
  332. .v-toolbar__content {
  333. padding-left: 16px;
  334. @include until($tablet) {
  335. padding-left: 8px;
  336. }
  337. }
  338. }
  339. // ==========================================
  340. // Fix FAB revealing under codemirror
  341. // ==========================================
  342. .speed-dial--fixed {
  343. z-index: 8;
  344. }
  345. // ==========================================
  346. // CODE MIRROR
  347. // ==========================================
  348. .CodeMirror {
  349. height: auto;
  350. .cm-header-1 {
  351. font-size: 1.5rem;
  352. }
  353. .cm-header-2 {
  354. font-size: 1.25rem;
  355. }
  356. .cm-header-3 {
  357. font-size: 1.15rem;
  358. }
  359. .cm-header-4 {
  360. font-size: 1.1rem;
  361. }
  362. .cm-header-5 {
  363. font-size: 1.05rem;
  364. }
  365. .cm-header-6 {
  366. font-size: 1.025rem;
  367. }
  368. }
  369. .CodeMirror-focused .cm-matchhighlight {
  370. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  371. background-position: bottom;
  372. background-repeat: repeat-x;
  373. }
  374. .cm-matchhighlight {
  375. background-color: mc('grey', '800');
  376. }
  377. .CodeMirror-selection-highlight-scrollbar {
  378. background-color: mc('green', '600');
  379. }
  380. .cm-s-wikijs-dark.CodeMirror {
  381. background: darken(mc('grey','900'), 3%);
  382. color: #e0e0e0;
  383. }
  384. .cm-s-wikijs-dark div.CodeMirror-selected {
  385. background: mc('blue','800');
  386. }
  387. .cm-s-wikijs-dark .cm-matchhighlight {
  388. background: mc('blue','800');
  389. }
  390. .cm-s-wikijs-dark .CodeMirror-line::selection, .cm-s-wikijs-dark .CodeMirror-line > span::selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::selection {
  391. background: mc('red', '500');
  392. }
  393. .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 {
  394. background: mc('red', '500');
  395. }
  396. .cm-s-wikijs-dark .CodeMirror-gutters {
  397. background: darken(mc('grey','900'), 6%);
  398. border-right: 1px solid mc('grey','900');
  399. }
  400. .cm-s-wikijs-dark .CodeMirror-guttermarker {
  401. color: #ac4142;
  402. }
  403. .cm-s-wikijs-dark .CodeMirror-guttermarker-subtle {
  404. color: #505050;
  405. }
  406. .cm-s-wikijs-dark .CodeMirror-linenumber {
  407. color: mc('grey','800');
  408. }
  409. .cm-s-wikijs-dark .CodeMirror-cursor {
  410. border-left: 1px solid #b0b0b0;
  411. }
  412. .cm-s-wikijs-dark span.cm-comment {
  413. color: mc('orange','800');
  414. }
  415. .cm-s-wikijs-dark span.cm-atom {
  416. color: #aa759f;
  417. }
  418. .cm-s-wikijs-dark span.cm-number {
  419. color: #aa759f;
  420. }
  421. .cm-s-wikijs-dark span.cm-property, .cm-s-wikijs-dark span.cm-attribute {
  422. color: #90a959;
  423. }
  424. .cm-s-wikijs-dark span.cm-keyword {
  425. color: #ac4142;
  426. }
  427. .cm-s-wikijs-dark span.cm-string {
  428. color: #f4bf75;
  429. }
  430. .cm-s-wikijs-dark span.cm-variable {
  431. color: #90a959;
  432. }
  433. .cm-s-wikijs-dark span.cm-variable-2 {
  434. color: #6a9fb5;
  435. }
  436. .cm-s-wikijs-dark span.cm-def {
  437. color: #d28445;
  438. }
  439. .cm-s-wikijs-dark span.cm-bracket {
  440. color: #e0e0e0;
  441. }
  442. .cm-s-wikijs-dark span.cm-tag {
  443. color: #ac4142;
  444. }
  445. .cm-s-wikijs-dark span.cm-link {
  446. color: #aa759f;
  447. }
  448. .cm-s-wikijs-dark span.cm-error {
  449. background: #ac4142;
  450. color: #b0b0b0;
  451. }
  452. .cm-s-wikijs-dark .CodeMirror-activeline-background {
  453. background: mc('grey','900');
  454. }
  455. .cm-s-wikijs-dark .CodeMirror-matchingbracket {
  456. text-decoration: underline;
  457. color: white !important;
  458. }
  459. }
  460. </style>