editor-code.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template lang='pug'>
  2. .editor-code
  3. v-toolbar(color='blue', flat, dense, dark)
  4. v-icon(color='blue lighten-5') edit
  5. v-toolbar-title.white--text Sample Page
  6. .editor-code-toolbar
  7. .editor-code-toolbar-group
  8. .editor-code-toolbar-item
  9. svg.icons.is-18(role='img')
  10. title Bold
  11. use(xlink:href='#fa-bold')
  12. .editor-code-toolbar-item
  13. svg.icons.is-18(role='img')
  14. title Italic
  15. use(xlink:href='#fa-italic')
  16. .editor-code-toolbar-item
  17. svg.icons.is-18(role='img')
  18. title Strikethrough
  19. use(xlink:href='#fa-strikethrough')
  20. .editor-code-toolbar-group
  21. v-menu(offset-y, open-on-hover)
  22. .editor-code-toolbar-item.is-dropdown(slot='activator')
  23. svg.icons.is-18(role='img')
  24. title Heading
  25. use(xlink:href='#fa-heading')
  26. v-list
  27. v-list-tile(v-for='(n, idx) in 6', @click='', :key='idx')
  28. v-list-tile-action: v-icon format_size
  29. v-list-tile-title Heading {{n}}
  30. .editor-code-toolbar-item
  31. svg.icons.is-18(role='img')
  32. title Blockquote
  33. use(xlink:href='#fa-quote-left')
  34. .editor-code-toolbar-group
  35. .editor-code-toolbar-item
  36. svg.icons.is-18(role='img')
  37. title Unordered List
  38. use(xlink:href='#fa-list-ul')
  39. .editor-code-toolbar-item
  40. svg.icons.is-18(role='img')
  41. title Ordered List
  42. use(xlink:href='#fa-list-ol')
  43. .editor-code-toolbar-group
  44. .editor-code-toolbar-item
  45. svg.icons.is-18(role='img')
  46. title Link
  47. use(xlink:href='#fa-link')
  48. .editor-code-toolbar-group
  49. .editor-code-toolbar-item
  50. svg.icons.is-18(role='img')
  51. title Inline Code
  52. use(xlink:href='#fa-terminal')
  53. .editor-code-toolbar-item
  54. svg.icons.is-18(role='img')
  55. title Code Block
  56. use(xlink:href='#fa-code')
  57. .editor-code-toolbar-group
  58. .editor-code-toolbar-item
  59. svg.icons.is-18(role='img')
  60. title Horizontal Bar
  61. use(xlink:href='#fa-minus')
  62. .editor-code-main
  63. .editor-code-editor
  64. .editor-code-editor-title Editor
  65. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready='onCmReady', @input='onCmInput')
  66. .editor-code-preview
  67. .editor-code-preview-title Preview
  68. .editor-code-preview-content.markdown-content(ref='editorPreview', v-html='previewHTML')
  69. v-speed-dial(v-model='fabInsertMenu', :open-on-hover='true', direction='top', transition='slide-y-reverse-transition', :fixed='true', :right='!isMobile', :left='isMobile', :bottom='true')
  70. v-btn(color='blue', fab, dark, v-model='fabInsertMenu', slot='activator')
  71. v-icon add_circle
  72. v-icon close
  73. v-btn(color='teal', fab, dark): v-icon image
  74. v-btn(color='pink', fab, dark): v-icon insert_drive_file
  75. v-btn(color='red', fab, dark): v-icon play_circle_outline
  76. v-btn(color='purple', fab, dark): v-icon multiline_chart
  77. v-btn(color='indigo', fab, dark): v-icon functions
  78. v-speed-dial(v-model='fabMainMenu', :open-on-hover='true', :direction='saveMenuDirection', transition='slide-x-reverse-transition', :fixed='true', :right='true', :top='!isMobile', :bottom='isMobile')
  79. v-btn(color='white', fab, light, v-model='fabMainMenu' slot='activator')
  80. v-icon(color='blue darken-2') blur_on
  81. v-icon(color='blue darken-2') close
  82. v-btn(color='blue-grey', fab, dark, @click.native.stop='$parent.openModal(`properties`)'): v-icon sort_by_alpha
  83. v-btn(color='green', fab, dark, @click.native.stop='$parent.save()'): v-icon save
  84. v-btn(color='red', fab, dark, small): v-icon not_interested
  85. v-btn(color='orange', fab, dark, small, @click.native.stop='$parent.openModal(`access`)'): v-icon vpn_lock
  86. v-btn(color='indigo', fab, dark, small): v-icon restore
  87. v-btn(color='brown', fab, dark, small): v-icon archive
  88. </template>
  89. <script>
  90. import _ from 'lodash'
  91. import { codemirror } from 'vue-codemirror'
  92. import 'codemirror/lib/codemirror.css'
  93. // Theme
  94. import 'codemirror/theme/base16-dark.css'
  95. // Language
  96. import 'codemirror/mode/markdown/markdown.js'
  97. // Addons
  98. import 'codemirror/addon/selection/active-line.js'
  99. import 'codemirror/addon/display/fullscreen.js'
  100. import 'codemirror/addon/display/fullscreen.css'
  101. import 'codemirror/addon/selection/mark-selection.js'
  102. import 'codemirror/addon/scroll/annotatescrollbar.js'
  103. import 'codemirror/addon/search/matchesonscrollbar.js'
  104. import 'codemirror/addon/search/searchcursor.js'
  105. import 'codemirror/addon/search/match-highlighter.js'
  106. // Markdown-it
  107. import MarkdownIt from 'markdown-it'
  108. import mdEmoji from 'markdown-it-emoji'
  109. import mdTaskLists from 'markdown-it-task-lists'
  110. import mdExpandTabs from 'markdown-it-expand-tabs'
  111. import mdAbbr from 'markdown-it-abbr'
  112. import mdSup from 'markdown-it-sup'
  113. import mdSub from 'markdown-it-sub'
  114. import mdMark from 'markdown-it-mark'
  115. import mdImsize from 'markdown-it-imsize'
  116. // Prism (Syntax Highlighting)
  117. import Prism from '../libs/prism/prism.js'
  118. const md = new MarkdownIt({
  119. html: true,
  120. breaks: true,
  121. linkify: true,
  122. typography: true,
  123. highlight(str, lang) {
  124. return `<pre class="line-numbers"><code class="language-${lang}">${str}</code></pre>`
  125. }
  126. })
  127. .use(mdEmoji)
  128. .use(mdTaskLists)
  129. .use(mdExpandTabs)
  130. .use(mdAbbr)
  131. .use(mdSup)
  132. .use(mdSub)
  133. .use(mdMark)
  134. .use(mdImsize)
  135. // Inject line numbers for preview scroll sync
  136. let linesMap = []
  137. function injectLineNumbers (tokens, idx, options, env, slf) {
  138. let line
  139. if (tokens[idx].map && tokens[idx].level === 0) {
  140. line = tokens[idx].map[0]
  141. tokens[idx].attrJoin('class', 'line')
  142. tokens[idx].attrSet('data-line', String(line))
  143. linesMap.push(line)
  144. }
  145. return slf.renderToken(tokens, idx, options, env, slf)
  146. }
  147. md.renderer.rules.paragraph_open = injectLineNumbers
  148. md.renderer.rules.heading_open = injectLineNumbers
  149. export default {
  150. components: {
  151. codemirror
  152. },
  153. data() {
  154. return {
  155. fabMainMenu: false,
  156. fabInsertMenu: false,
  157. code: '# Header 1\n\nSample **Text**\nhttp://wiki.js.org\n:rocket: :) :( :| :P\n\n## Header 2\nSample Text\n\n```javascript\nvar test = require("test");\n\n// some comment\nconst foo = bar(\'param\') + 1.234;\n```\n\n### Header 3\nLorem *ipsum* ~~text~~',
  158. cmOptions: {
  159. tabSize: 2,
  160. mode: 'text/markdown',
  161. theme: 'base16-dark',
  162. lineNumbers: true,
  163. lineWrapping: true,
  164. line: true,
  165. styleActiveLine: true,
  166. highlightSelectionMatches: {
  167. annotateScrollbar: true
  168. },
  169. viewportMargin: 50
  170. },
  171. previewHTML: ''
  172. }
  173. },
  174. computed: {
  175. cm() {
  176. return this.$refs.cm.codemirror
  177. },
  178. isMobile() {
  179. return this.$vuetify.breakpoint.smAndDown
  180. },
  181. saveMenuDirection() {
  182. return this.isMobile ? 'top' : 'bottom'
  183. }
  184. },
  185. methods: {
  186. onCmReady(cm) {
  187. let self = this
  188. cm.setSize(null, 'calc(100vh - 100px)')
  189. cm.setOption('extraKeys', {
  190. 'F11'(cm) {
  191. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  192. },
  193. 'Esc'(cm) {
  194. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  195. },
  196. 'Ctrl-S'(cm) {
  197. self.$parent.save()
  198. }
  199. })
  200. cm.on('cursorActivity', this.scrollSync)
  201. this.onCmInput(this.code)
  202. },
  203. onCmInput: _.debounce(function (newContent) {
  204. linesMap = []
  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 scroll sync
  213. */
  214. scrollSync: _.debounce(function (cm) {
  215. if (cm.somethingSelected()) { return }
  216. let currentLine = cm.getCursor().line
  217. if (currentLine < 3) {
  218. this.Velocity(this.$refs.editorPreview, 'stop', true)
  219. this.Velocity(this.$refs.editorPreview.firstChild, 'scroll', { offset: '-50', duration: 1000, container: this.$refs.editorPreview })
  220. } else {
  221. let closestLine = _.findLast(linesMap, n => n <= currentLine)
  222. let destElm = this.$refs.editorPreview.querySelector(`[data-line='${closestLine}']`)
  223. if (destElm) {
  224. this.Velocity(this.$refs.editorPreview, 'stop', true)
  225. this.Velocity(destElm, 'scroll', { offset: '-100', duration: 1000, container: this.$refs.editorPreview })
  226. }
  227. }
  228. }, 500)
  229. }
  230. }
  231. </script>
  232. <style lang='scss'>
  233. .editor-code {
  234. &-main {
  235. display: flex;
  236. width: 100%;
  237. }
  238. &-editor {
  239. background-color: darken(mc('grey', '900'), 4.5%);
  240. flex: 1 1 50%;
  241. display: block;
  242. height: calc(100vh - 100px);
  243. position: relative;
  244. &-title {
  245. background-color: mc('grey', '800');
  246. border-bottom-left-radius: 5px;
  247. display: inline-flex;
  248. height: 30px;
  249. justify-content: center;
  250. align-items: center;
  251. padding: 0 1rem;
  252. color: mc('grey', '500');
  253. position: absolute;
  254. top: 0;
  255. right: 0;
  256. z-index: 7;
  257. text-transform: uppercase;
  258. font-size: .7rem;
  259. @include until($tablet) {
  260. display: none;
  261. }
  262. }
  263. }
  264. &-preview {
  265. flex: 1 1 50%;
  266. background-color: mc('grey', '100');
  267. position: relative;
  268. height: calc(100vh - 100px);
  269. overflow: hidden;
  270. @include until($tablet) {
  271. display: none;
  272. }
  273. &-content {
  274. height: calc(100vh - 100px);
  275. overflow-y: scroll;
  276. padding: 30px 1rem 1rem 1rem;
  277. width: calc(100% + 1rem + 17px)
  278. // -ms-overflow-style: none;
  279. // &::-webkit-scrollbar {
  280. // width: 0px;
  281. // background: transparent;
  282. // }
  283. }
  284. &-title {
  285. background-color: rgba(mc('blue', '100'), .75);
  286. border-bottom-right-radius: 5px;
  287. display: inline-flex;
  288. height: 30px;
  289. justify-content: center;
  290. align-items: center;
  291. padding: 0 1rem;
  292. color: mc('blue', '800');
  293. position: absolute;
  294. top: 0;
  295. left: 0;
  296. z-index: 2;
  297. text-transform: uppercase;
  298. font-size: .7rem;
  299. }
  300. }
  301. &-toolbar {
  302. background-color: mc('blue', '700');
  303. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  304. height: 50px;
  305. color: #FFF;
  306. display: flex;
  307. @include until($tablet) {
  308. justify-content: center;
  309. }
  310. &-group {
  311. display: flex;
  312. + .editor-code-toolbar-group {
  313. border-left: 1px solid rgba(mc('blue', '600'), .5);
  314. }
  315. }
  316. &-item {
  317. width: 40px;
  318. height: 50px;
  319. display: flex;
  320. justify-content: center;
  321. align-items: center;
  322. transition: all .4s ease;
  323. cursor: pointer;
  324. &:first-child {
  325. padding-left: .5rem;
  326. }
  327. &:last-child {
  328. padding-right: .5rem;
  329. }
  330. &:hover {
  331. background-color: mc('blue', '600');
  332. }
  333. @include until($tablet) {
  334. width: 35px;
  335. }
  336. }
  337. svg {
  338. use {
  339. color: #FFF;
  340. }
  341. }
  342. }
  343. // Fix FAB revealing under codemirror
  344. .speed-dial--fixed {
  345. z-index: 5;
  346. }
  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. </style>