editor-code.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 Prism from '../libs/prism/prism.js'
  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. export default {
  119. components: {
  120. codemirror
  121. },
  122. data() {
  123. return {
  124. fabMainMenu: false,
  125. fabInsertMenu: false,
  126. code: '# Header 1\n\nSample **Text**\nhttp://wiki.js.org\n\n## Header 2\nSample Text\n\n```javascript\nvar test = require("test");\n\n// some comment\nconst foo = bar(\'param\') + 1.234;\n```',
  127. cmOptions: {
  128. tabSize: 2,
  129. mode: 'text/markdown',
  130. theme: 'base16-dark',
  131. lineNumbers: true,
  132. lineWrapping: true,
  133. line: true,
  134. styleActiveLine: true,
  135. highlightSelectionMatches: {
  136. annotateScrollbar: true
  137. },
  138. viewportMargin: 50
  139. },
  140. previewHTML: ''
  141. }
  142. },
  143. computed: {
  144. cm() {
  145. return this.$refs.cm.codemirror
  146. },
  147. isMobile() {
  148. return this.$vuetify.breakpoint.smAndDown
  149. },
  150. saveMenuDirection() {
  151. return this.isMobile ? 'top' : 'bottom'
  152. }
  153. },
  154. methods: {
  155. onCmReady(cm) {
  156. let self = this
  157. cm.setSize(null, 'calc(100vh - 100px)')
  158. cm.setOption('extraKeys', {
  159. 'F11'(cm) {
  160. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  161. },
  162. 'Esc'(cm) {
  163. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  164. },
  165. 'Ctrl-S'(cm) {
  166. self.$parent.save()
  167. }
  168. })
  169. this.onCmInput(this.code)
  170. },
  171. onCmInput: _.debounce(function (newContent) {
  172. this.previewHTML = md.render(newContent)
  173. this.$nextTick(function() {
  174. Prism.highlightAllUnder(this.$refs.editorPreview)
  175. })
  176. }, 500)
  177. }
  178. }
  179. </script>
  180. <style lang='scss'>
  181. .editor-code {
  182. &-main {
  183. display: flex;
  184. width: 100%;
  185. }
  186. &-editor {
  187. flex: 1 1 50%;
  188. display: block;
  189. min-height: calc(100vh - 100px);
  190. position: relative;
  191. &-title {
  192. background-color: mc('grey', '800');
  193. border-bottom-left-radius: 5px;
  194. display: inline-flex;
  195. height: 30px;
  196. justify-content: center;
  197. align-items: center;
  198. padding: 0 1rem;
  199. color: mc('grey', '500');
  200. position: absolute;
  201. top: 0;
  202. right: 0;
  203. z-index: 2;
  204. text-transform: uppercase;
  205. font-size: .7rem;
  206. @include until($tablet) {
  207. display: none;
  208. }
  209. }
  210. }
  211. &-preview {
  212. flex: 1 1 50%;
  213. background-color: mc('grey', '100');
  214. position: relative;
  215. padding: 30px 1rem 1rem 1rem;
  216. @include until($tablet) {
  217. display: none;
  218. }
  219. &-title {
  220. background-color: mc('blue', '100');
  221. border-bottom-right-radius: 5px;
  222. display: inline-flex;
  223. height: 30px;
  224. justify-content: center;
  225. align-items: center;
  226. padding: 0 1rem;
  227. color: mc('blue', '800');
  228. position: absolute;
  229. top: 0;
  230. left: 0;
  231. z-index: 2;
  232. text-transform: uppercase;
  233. font-size: .7rem;
  234. }
  235. }
  236. &-toolbar {
  237. background-color: mc('blue', '700');
  238. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  239. height: 50px;
  240. color: #FFF;
  241. display: flex;
  242. @include until($tablet) {
  243. justify-content: center;
  244. }
  245. &-group {
  246. display: flex;
  247. + .editor-code-toolbar-group {
  248. border-left: 1px solid rgba(mc('blue', '600'), .5);
  249. }
  250. }
  251. &-item {
  252. width: 40px;
  253. height: 50px;
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. transition: all .4s ease;
  258. cursor: pointer;
  259. &:first-child {
  260. padding-left: .5rem;
  261. }
  262. &:last-child {
  263. padding-right: .5rem;
  264. }
  265. &:hover {
  266. background-color: mc('blue', '600');
  267. }
  268. @include until($tablet) {
  269. width: 35px;
  270. }
  271. }
  272. svg {
  273. use {
  274. color: #FFF;
  275. }
  276. }
  277. }
  278. }
  279. .CodeMirror {
  280. height: auto;
  281. .cm-header-1 {
  282. font-size: 1.5rem;
  283. }
  284. .cm-header-2 {
  285. font-size: 1.25rem;
  286. }
  287. .cm-header-3 {
  288. font-size: 1.15rem;
  289. }
  290. .cm-header-4 {
  291. font-size: 1.1rem;
  292. }
  293. .cm-header-5 {
  294. font-size: 1.05rem;
  295. }
  296. .cm-header-6 {
  297. font-size: 1.025rem;
  298. }
  299. }
  300. .CodeMirror-focused .cm-matchhighlight {
  301. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  302. background-position: bottom;
  303. background-repeat: repeat-x;
  304. }
  305. .cm-matchhighlight {
  306. background-color: mc('grey', '800');
  307. }
  308. .CodeMirror-selection-highlight-scrollbar {
  309. background-color: mc('green', '600');
  310. }
  311. </style>