index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <div
  3. ref="bs-render-wrap"
  4. :key="`${pageInfo.pageConfig.w}${pageInfo.pageConfig.h}`"
  5. class="bs-render-wrap design-drag-wrap render-theme-wrap"
  6. :style="{
  7. width: pageInfo.pageConfig.w + 'px',
  8. height: pageInfo.pageConfig.h + 'px',
  9. backgroundColor: pageInfo.pageConfig.bgColor,
  10. backgroundImage: `url(${pageInfo.pageConfig.bg})`
  11. }"
  12. @drop="drop($event)"
  13. @dragover.prevent
  14. >
  15. <vdr
  16. v-for="chart in chartList"
  17. :id="chart.code"
  18. :key="chart.updateKey || chart.code"
  19. class="drag-item"
  20. :class="{
  21. 'multiple-selected': activeCodes.includes(chart.code),
  22. }"
  23. :scale-ratio="scale"
  24. :x="chart.x"
  25. :y="chart.y"
  26. :w="chart.w"
  27. :h="chart.h"
  28. :min-width="10"
  29. :min-height="10"
  30. :draggable="!chart.locked"
  31. :resizable="!chart.locked"
  32. :parent="true"
  33. :debug="false"
  34. :is-conflict-check="false"
  35. :snap="true"
  36. :snap-tolerance="2"
  37. :style="{ zIndex: chart.z || 0 }"
  38. :grid="[1,1]"
  39. @activated="activated(...arguments, chart)"
  40. @dragging="onDrag(...arguments, chart)"
  41. @resizing="onResize(...arguments, chart)"
  42. @resizestop="resizestop(...arguments, chart)"
  43. @dragstop="dragstop(...arguments, chart)"
  44. @refLineParams="getRefLineParams"
  45. @mouseleave.native="resetPresetLineDelay"
  46. >
  47. <Configuration
  48. v-if="isInit"
  49. :config="chart"
  50. @openRightPanel="openRightPanel"
  51. >
  52. <RenderCard
  53. :ref="'RenderCard' + chart.code"
  54. :config="chart"
  55. />
  56. </Configuration>
  57. </vdr>
  58. <span
  59. v-for="(vl, index) in vLine"
  60. v-show="vl.display"
  61. :key="index + 'vLine'"
  62. class="ref-line v-line"
  63. :style="{ left: vl.position, top: vl.origin, height: vl.lineLength }"
  64. />
  65. <span
  66. v-for="(hl, index) in hLine"
  67. v-show="hl.display"
  68. :key="index + 'hLine'"
  69. class="ref-line h-line"
  70. :style="{ top: hl.position, left: hl.origin, width: hl.lineLength }"
  71. />
  72. </div>
  73. </template>
  74. <script>
  75. import { mapState, mapMutations } from 'vuex'
  76. import RenderCard from './RenderCard.vue'
  77. import Configuration from './Configuration.vue'
  78. // import _ from 'lodash'
  79. import cloneDeep from 'lodash/cloneDeep'
  80. import vdr from 'vue-draggable-resizable-gorkys'
  81. import 'vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css'
  82. import { randomString } from '../js/utils'
  83. import { compile } from 'tiny-sass-compiler/dist/tiny-sass-compiler.esm-browser.prod.js'
  84. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  85. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  86. export default {
  87. name: 'BigScreenRender',
  88. components: {
  89. RenderCard,
  90. Configuration,
  91. vdr
  92. },
  93. props: {
  94. ruleKey: {
  95. type: Number,
  96. default: 0
  97. }
  98. },
  99. data () {
  100. return {
  101. vLine: [],
  102. hLine: [],
  103. themeCss: '',
  104. // 临时冻结拖拽
  105. freeze: false,
  106. plotList,
  107. rawChart: []
  108. }
  109. },
  110. computed: {
  111. ...mapState({
  112. pageConfig: (state) => state.bigScreen.pageInfo.pageConfig,
  113. pageInfo: (state) => state.bigScreen.pageInfo,
  114. chartList: (state) => state.bigScreen.pageInfo.chartList,
  115. activeCode: (state) => state.bigScreen.activeCode,
  116. activeCodes: (state) => state.bigScreen.activeCodes,
  117. hoverCode: (state) => state.bigScreen.hoverCode,
  118. themeJson: (state) => state.bigScreen.pageInfo.pageConfig.themeJson,
  119. isInit: (state) => !state.bigScreen.pageLoading,
  120. scale: (state) => state.bigScreen.zoom / 100
  121. })
  122. },
  123. watch: {
  124. pageConfig: {
  125. handler (pageConfig) {
  126. this.$nextTick(() => {
  127. const style = document.createElement('style')
  128. if (
  129. pageConfig &&
  130. pageConfig.themeJson &&
  131. pageConfig.themeJson.themeCss
  132. ) {
  133. const themeCss = pageConfig.themeJson.themeCss
  134. if (themeCss) {
  135. const themeStr = compile(themeCss).code
  136. style.type = 'text/css'
  137. style.innerText = themeStr
  138. document.getElementsByTagName('head')[0].appendChild(style)
  139. }
  140. }
  141. })
  142. },
  143. deep: true,
  144. immediate: true
  145. }
  146. },
  147. mounted () {
  148. this.styleSet()
  149. this.plotList = [...this.plotList, ...getCustomPlots()]
  150. },
  151. methods: {
  152. ...mapMutations('bigScreen', [
  153. 'changeLayout',
  154. 'changeActiveCode',
  155. 'changeChartConfig',
  156. 'changeActiveItemConfig',
  157. 'changeActiveItemWH',
  158. 'addItem',
  159. 'delItem',
  160. 'resetPresetLine',
  161. 'changeGridShow',
  162. 'setPresetLine',
  163. 'saveTimeLine'
  164. ]),
  165. // 获取到后端传来的主题样式并进行修改
  166. styleSet () {
  167. const style = document.createElement('style')
  168. if (this.themeJson && this.themeJson.themeCss) {
  169. const styleStr = this.themeJson.themeCss
  170. const themeCss = compile(styleStr).code
  171. style.type = 'text/css'
  172. style.innerText = themeCss
  173. document.getElementsByTagName('head')[0].appendChild(style)
  174. } else {
  175. style.remove()
  176. }
  177. },
  178. resetPresetLineDelay () {
  179. setTimeout(() => {
  180. this.resetPresetLine()
  181. }, 500)
  182. },
  183. // 点击当前组件时打开右侧面板
  184. openRightPanel (config) {
  185. this.$emit('openRightPanel', config)
  186. },
  187. drop (e) {
  188. e.preventDefault()
  189. // 解决:火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片的问题。
  190. e.stopPropagation()
  191. const transferData = e.dataTransfer.getData('dragComponent')
  192. if (transferData) {
  193. this.addChart(transferData, { x: e?.x, y: e?.y })
  194. }
  195. },
  196. /**
  197. * 改变组件大小
  198. * @param x
  199. * @param y
  200. * @param width
  201. * @param height
  202. * @param chart
  203. */
  204. onResize (x, y, width, height, chart) {
  205. chart.x = x
  206. chart.y = y
  207. chart.w = width
  208. chart.h = height
  209. this.changeGridShow(true)
  210. this.setPresetLine({
  211. ...chart
  212. })
  213. },
  214. /**
  215. *
  216. * @param x
  217. * @param y
  218. * @param chart
  219. */
  220. onDrag (x, y, chart) {
  221. // 防止事件冒泡
  222. event.stopPropagation()
  223. if (chart.group) {
  224. // 查找和自己是一个组合的组件
  225. this.dragGroupChart(x, y, chart)
  226. } else {
  227. chart.x = x
  228. chart.y = y
  229. }
  230. this.changeGridShow(true)
  231. this.setPresetLine({
  232. ...chart
  233. })
  234. },
  235. resizestop (left, top, width, height, chart) {
  236. this.changeChartConfig({
  237. ...chart,
  238. w: width,
  239. h: height,
  240. x: left,
  241. y: top
  242. })
  243. this.changeActiveItemConfig({
  244. ...chart,
  245. w: width,
  246. h: height,
  247. x: left,
  248. y: top
  249. })
  250. if (chart.code === this.activeCode) {
  251. this.changeActiveItemWH({
  252. code: chart.code,
  253. w: width,
  254. h: height
  255. })
  256. }
  257. this.saveTimeLine(`改变${chart?.title}大小`)
  258. this.changeGridShow(false)
  259. },
  260. activated (chart) {
  261. this.rawChart = cloneDeep(chart)
  262. },
  263. dragstop (left, top, chart) {
  264. if (!this.freeze) {
  265. if (this.rawChart.x !== left || this.rawChart.y !== top) {
  266. this.changeChartConfig({
  267. ...chart,
  268. x: left,
  269. y: top
  270. })
  271. this.changeActiveItemConfig({
  272. ...chart,
  273. x: left,
  274. y: top
  275. })
  276. if (chart.code === this.activeCode) {
  277. this.changeActiveItemWH({
  278. code: chart.code,
  279. x: left,
  280. y: top
  281. })
  282. }
  283. this.rawChart = cloneDeep(chart)
  284. }
  285. } else {
  286. const index = this.chartList.findIndex(
  287. (_chart) => _chart.code === chart.code
  288. )
  289. this.$set(this.chartList, index, chart)
  290. this.changeChartConfig({
  291. ...chart,
  292. updateKey: new Date().getTime()
  293. })
  294. }
  295. this.changeGridShow(false)
  296. this.freeze = false
  297. this.saveTimeLine(`拖拽${chart?.title}`)
  298. },
  299. // 辅助线
  300. getRefLineParams (params) {
  301. const { vLine, hLine } = params
  302. this.vLine = vLine
  303. this.hLine = hLine
  304. },
  305. // 新增元素
  306. addChart (chart, position) {
  307. const { left, top } = this.$el.getBoundingClientRect()
  308. const _chart = !chart.code ? JSON.parse(chart) : chart
  309. let option = _chart.option
  310. if (_chart.type === 'customComponent') {
  311. option = {
  312. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  313. theme: this.pageConfig.customTheme
  314. }
  315. }
  316. const config = {
  317. ..._chart,
  318. x: parseInt(!chart.code
  319. ? (position.x - left - _chart.offsetX) / this.scale
  320. : position.x),
  321. y: parseInt(!chart.code
  322. ? (position.y - top - _chart.offsetX) / this.scale
  323. : position.y),
  324. width: 200 * this.scale,
  325. height: 200 * this.scale,
  326. code: !chart.code ? randomString(8) : chart.code,
  327. option
  328. }
  329. config.key = config.code
  330. // TODO:新添加一个组件时应该有默认的两套主题
  331. console.log('add')
  332. // 先暂时只考虑g2组件
  333. if (['customComponent'].includes(chart.type)){
  334. config.theme = settingToTheme(config, 'dark')
  335. config.theme = settingToTheme(config, 'light')
  336. }
  337. this.addItem(config)
  338. },
  339. addSourceChart (chart, position) {
  340. const { left, top } = this.$el.getBoundingClientRect()
  341. const _chart = JSON.parse(chart)
  342. let option = _chart.option
  343. if (_chart.type === 'customComponent') {
  344. option = {
  345. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  346. theme: this.pageConfig.customTheme
  347. }
  348. }
  349. const config = {
  350. ..._chart,
  351. x: parseInt((position.x - left) / this.scale),
  352. y: parseInt((position.y - top) / this.scale),
  353. width: 200 * this.scale,
  354. height: 200 * this.scale,
  355. code: randomString(8),
  356. option
  357. }
  358. config.key = config.code
  359. this.addItem(config)
  360. },
  361. /**
  362. * 拖拽相同组合的组件
  363. * @param x 组合元素当前x
  364. * @param y 组合元素当前y
  365. * @param chart
  366. */
  367. dragGroupChart (x, y, chart) {
  368. if (chart.group) {
  369. const diffX = x - chart.x
  370. const diffY = y - chart.y
  371. const group = chart.group
  372. // 找到相同group的组件,并找到边界
  373. const groupChartList = this.chartList.filter(
  374. (groupChart) => groupChart.group === group
  375. )
  376. const groupMinX = Math.min(
  377. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  378. )
  379. const groupMinY = Math.min(
  380. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  381. )
  382. const groupMaxX = Math.max(
  383. ...groupChartList?.map(
  384. (groupChart) => groupChart.x + diffX + groupChart.w
  385. )
  386. )
  387. const groupMaxY = Math.max(
  388. ...groupChartList?.map(
  389. (groupChart) => groupChart.y + diffY + groupChart.h
  390. )
  391. )
  392. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  393. if (
  394. (groupMinX <= 0 ||
  395. groupMinY <= 0 ||
  396. groupMaxX >= this.pageConfig.w ||
  397. groupMaxY >= this.pageConfig.h) &&
  398. // 偏移的绝对值要大于0
  399. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  400. ) {
  401. this.freeze = true
  402. return
  403. }
  404. // 移动相应的diff距离
  405. groupChartList?.map((groupChart) => {
  406. this.changeChartConfig({
  407. ...groupChart,
  408. x: groupChart.x + diffX,
  409. y: groupChart.y + diffY
  410. })
  411. })
  412. }
  413. }
  414. }
  415. }
  416. </script>
  417. <style lang="scss" scoped>
  418. .bs-render-wrap {
  419. position: relative;
  420. background-size: cover;
  421. .drag-item {
  422. cursor: move;
  423. }
  424. ::v-deep .vdr {
  425. border: none;
  426. }
  427. .h-line {
  428. border-bottom: 1px dashed #0089d0;
  429. }
  430. .v-line {
  431. border-left: 1px dashed #0089d0;
  432. }
  433. .ref-line {
  434. background-color: transparent;
  435. }
  436. }
  437. .design-drag-wrap {
  438. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  439. }
  440. .multiple-selected {
  441. border: 1px dashed #fff !important;
  442. }
  443. </style>