Preview.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div
  3. v-loading="loading"
  4. class="bs-remote-preview"
  5. element-loading-text="远程组件加载中..."
  6. >
  7. <div class="remote-preview-inner-wrap">
  8. <component
  9. :is="remoteComponent"
  10. :config="config"
  11. />
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. import remoteVueLoader from 'remote-vue2-loader'
  17. import _ from 'lodash'
  18. import { getBizComponentInfo } from 'data-room-ui/js/api/bigScreenApi'
  19. import innerRemoteComponents, { getRemoteComponents } from 'data-room-ui/RemoteComponents/remoteComponentsList'
  20. export default {
  21. name: 'BsComponentPreview',
  22. props: {
  23. vueContent: {
  24. type: String,
  25. default: ''
  26. },
  27. settingContent: {
  28. type: String,
  29. default: ''
  30. }
  31. },
  32. computed: {
  33. config: {
  34. get () {
  35. // eslint-disable-next-line prefer-const
  36. let option = {}
  37. // eslint-disable-next-line prefer-const
  38. let setting = []
  39. // eslint-disable-next-line prefer-const, no-unused-vars
  40. let title = []
  41. // eslint-disable-next-line prefer-const, no-unused-vars
  42. let data = []
  43. // eslint-disable-next-line prefer-const
  44. let settingContent = this.settingContentInner?.replaceAll('const ', '')
  45. // 去掉 export default及后面代码
  46. settingContent = settingContent?.replace(/export default[\s\S]*/, '')
  47. eval(settingContent)
  48. return {
  49. option,
  50. setting
  51. }
  52. },
  53. set (val) {}
  54. }
  55. },
  56. watch: {
  57. settingContentInner () {
  58. this.getRemoteComponent()
  59. },
  60. vueContentInner () {
  61. this.getRemoteComponent()
  62. },
  63. vueContent (newVal) {
  64. this.vueContentInner = newVal
  65. },
  66. settingContent (newVal) {
  67. this.settingContentInner = newVal
  68. }
  69. },
  70. data () {
  71. return {
  72. loading: false,
  73. remoteComponent: null,
  74. vueContentInner: this.vueContent,
  75. settingContentInner: this.settingContent?.replaceAll('const ', '')
  76. }
  77. },
  78. created () {
  79. this.viewComponent()
  80. },
  81. methods: {
  82. viewComponent () {
  83. // 如果有编码,则获取组件信息
  84. if (this.$route.query?.code) {
  85. getBizComponentInfo(this.$route.query?.code).then(data => {
  86. this.vueContentInner = data.vueContent
  87. this.settingContentInner = data.settingContent
  88. this.dataFormatting(this.config)
  89. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(this.vueContentInner))
  90. }).finally(() => {
  91. this.loading = false
  92. })
  93. }
  94. // 如果有组件的dirName,则获取系统组件信息
  95. if (this.$route.query?.dirName) {
  96. const dirName = this.$route.query?.dirName
  97. const remoteComponentList = [...innerRemoteComponents, ...getRemoteComponents()]
  98. const config = remoteComponentList?.find(item => item.customize.vueSysComponentDirName === dirName)
  99. this.config.option = config?.option
  100. const vueFile = config.customize?.vueFile
  101. this.remoteComponent = vueFile
  102. this.loading = false
  103. }
  104. },
  105. // 尝试渲染远程文件或远程字符串
  106. getRemoteComponent () {
  107. this.loading = true
  108. this.dataFormatting(this.config)
  109. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(this.vueContentInner))
  110. this.loading = false
  111. },
  112. /**
  113. * 组件的配置
  114. * @returns {Promise<unknown>}
  115. */
  116. dataFormatting (config, data) {
  117. config = _.cloneDeep(config)
  118. // 遍历config.setting,将config.setting中的值赋值给config.option中对应的optionField
  119. config.setting.forEach(set => {
  120. if (set.optionField) {
  121. const optionField = set.optionField.split('.')
  122. let option = config.option
  123. optionField.forEach((field, index) => {
  124. if (index === optionField.length - 1) {
  125. // 数据配置时,必须有值才更新
  126. if ((set.tabName === 'data' && set.value) || set.tabName === 'custom') {
  127. option[field] = set.value
  128. }
  129. } else {
  130. option = option[field]
  131. }
  132. })
  133. }
  134. })
  135. // eslint-disable-next-line no-unused-vars
  136. const option = config.option
  137. // eslint-disable-next-line no-unused-vars
  138. const setting = config.setting
  139. return config
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .bs-remote-preview {
  146. position: absolute;
  147. min-height: 100%;
  148. min-width: 100%;
  149. overflow: hidden;
  150. box-sizing: border-box;
  151. .remote-preview-inner-wrap {
  152. position: absolute;
  153. height: 100%;
  154. width: 100%;
  155. overflow: auto;
  156. padding: 20px;
  157. background-color: var(--bs-background-1);
  158. }
  159. }
  160. </style>