main.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import Vue from 'vue'
  2. import { loadScriptQueue } from '@/utils/loadScript'
  3. import axios from 'axios'
  4. import Tinymce from '@/components/tinymce/index.vue'
  5. Vue.component('tinymce', Tinymce)
  6. Vue.prototype.$axios = axios
  7. const $previewApp = document.getElementById('previewApp')
  8. const childAttrs = {
  9. file: '',
  10. dialog: ' width="600px" class="dialog-width" v-if="visible" :visible.sync="visible" :modal-append-to-body="false" '
  11. }
  12. window.addEventListener('message', init, false)
  13. function buildLinks(links) {
  14. let strs = ''
  15. links.forEach(url => {
  16. strs += `<link href="${url}" rel="stylesheet">`
  17. })
  18. return strs
  19. }
  20. function init(event) {
  21. if (event.data.type === 'refreshFrame') {
  22. const code = event.data.data
  23. const attrs = childAttrs[code.generateConf.type]
  24. let links = ''
  25. if (Array.isArray(code.links) && code.links.length > 0) {
  26. links = buildLinks(code.links)
  27. }
  28. $previewApp.innerHTML = `${links}<style>${code.css}</style><div id="app"></div>`
  29. if (Array.isArray(code.scripts) && code.scripts.length > 0) {
  30. loadScriptQueue(code.scripts, () => {
  31. newVue(attrs, code.js, code.html)
  32. })
  33. } else {
  34. newVue(attrs, code.js, code.html)
  35. }
  36. }
  37. }
  38. function newVue(attrs, main, html) {
  39. main = eval(`(${main})`)
  40. main.template = `<div>${html}</div>`
  41. new Vue({
  42. components: {
  43. child: main
  44. },
  45. data() {
  46. return {
  47. visible: true
  48. }
  49. },
  50. template: `<div><child ${attrs}/></div>`
  51. }).$mount('#app')
  52. }