copy.js 6.72 KB
Newer Older
Tippi.Rao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import store from '@/store/index'
import toast from '@/components/canvas/utils/toast'
import generateID from '@/components/canvas/utils/generateID'
import { deepCopy } from '@/components/canvas/utils/utils'
import { chartBatchCopy, chartCopy } from '@/api/chart/chart'
import { uuid } from 'vue-uuid'
import { adaptCurThemeCommonStyle } from '@/components/canvas/utils/style'
import Vue from 'vue'

export default {
  state: {
    copyData: null, // 复制粘贴剪切
    isCut: false,
    baseStyle: {
      width: 533,
      height: 300,
      top: 0,
      left: 0
    },
    viewBase: {
      x: 1,
      y: 216,
      sizex: 48,
      sizey: 24
    }
  },
  mutations: {
    // 复制到粘贴板
    copyToClipboard(state) {
      if (state.curComponent) {
        Vue.prototype.$copyText('datease-component-' + state.curComponent.id)
      }
    },
    passFromClipboard(state, componentId) {
      state.componentData.forEach(item => {
        if (item.id === componentId) {
          state.copyData = {
            data: deepCopy(item),
            index: state.componentData.length
          }
        }
      })
      state.isCut = false
      this.commit('paste')
    },
    copyMultiplexingComponents(state) {
      let pYMax = 0
      const _this = this
      state.isCut = false
      state.componentData.forEach(item => {
        if (item.y > pYMax) {
          pYMax = item.y
        }
      })
      const canvasStyleData = state.canvasStyleData
      const curCanvasScaleSelf = state.curCanvasScaleMap['canvas-main']
      const componentGap = state.componentGap
      Object.keys(state.curMultiplexingComponents).forEach(function(componentId, index) {
        const component =
          {
            ...deepCopy(state.curMultiplexingComponents[componentId]),
            ...deepCopy(state.viewBase),
            'auxiliaryMatrix': canvasStyleData.auxiliaryMatrix
          }

        component.style = {
          ...component.style,
          ...deepCopy(state.baseStyle)
        }

        const tilePosition = index % 3
        const divisiblePosition = parseInt(index / 3)
        if (canvasStyleData.auxiliaryMatrix) {
          const width = component.sizex * curCanvasScaleSelf.matrixStyleOriginWidth
          // 取余 平铺4个 此处x 位置偏移
          component.x = component.x + component.sizex * tilePosition
          // Y 方向根据当前应该放置的最大值 加上50矩阵余量
          component.y = pYMax + 50 + state.viewBase.sizex * divisiblePosition
          component.style.left = (component.x - 1) * curCanvasScaleSelf.matrixStyleOriginWidth
          component.style.top = (component.y - 1) * curCanvasScaleSelf.matrixStyleOriginHeight
          component.style.width = width
          component.style.height = component.sizey * curCanvasScaleSelf.matrixStyleOriginHeight
        } else {
          const width = component.style.width
          const height = component.style.height
          component.style.top = component.style.top + divisiblePosition * (height + componentGap)
          component.style.left = component.style.left + tilePosition * (width + componentGap)
          component.style.width = width
          component.style.height = height
        }
        component['canvasId'] = 'canvas-main'
        component['canvasPid'] = '0'
        state.copyData = {
          data: component,
          index: index
        }
        _this.commit('paste', true)
      })
    },
    copy(state) {
      if (!state.curComponent) return
      state.copyData = {
        data: deepCopy(state.curComponent),
        index: state.curComponentIndex
      }

      state.isCut = false
    },

    paste(state, needAdaptor) {
      if (!state.copyData) {
        return
      }
      const data = state.copyData.data
      // 仪表板复制的组件默认不在移动端部署中mobileSelected = false
      data.mobileSelected = false
      if (!state.curComponent.auxiliaryMatrix) {
        data.style.top = Number(data.style.top) + 20
        data.style.left = Number(data.style.left) + 20
      }
      data.id = generateID()
      // 如果是用户视图 测先进行底层复制
      if (data.type === 'view') {
        chartCopy(data.propValue.viewId, state.panel.panelInfo.id).then(res => {
          const newView = deepCopy(data)
126
          Vue.set(newView, 'needAdaptor', store.state.multiplexingStyleAdapt ? needAdaptor : false)
Tippi.Rao committed
127 128 129 130 131 132 133
          newView.id = uuid.v1()
          newView.propValue.viewId = res.data
          newView['canvasId'] = data.canvasId
          newView['canvasPid'] = data.canvasPid
          if (newView.filters && newView.filters.length) {
            newView.filters = []
          }
134
          if (needAdaptor && store.state.multiplexingStyleAdapt) {
Tippi.Rao committed
135 136
            adaptCurThemeCommonStyle(newView)
          }
Tippi.Rao committed
137 138 139 140 141 142 143 144 145 146 147
          store.commit('addComponent', { component: newView })
        })
      } else if (data.type === 'de-tabs') {
        const sourceAndTargetIds = {}
        const newCop = deepCopy(data)
        newCop.id = uuid.v1()
        newCop.options.tabList.forEach((item) => {
          if (item.content && item.content.type === 'view') {
            const newViewId = uuid.v1()
            sourceAndTargetIds[item.content.propValue.viewId] = newViewId
            item.content.propValue.viewId = newViewId
148
            Vue.set(item.content, 'needAdaptor', store.state.multiplexingStyleAdapt ? needAdaptor : false)
Tippi.Rao committed
149 150 151 152 153 154
            if (item.content.filters && item.content.filters.length) {
              item.content.filters = []
            }
          }
        })
        chartBatchCopy({ 'sourceAndTargetIds': sourceAndTargetIds }, state.panel.panelInfo.id).then((rsp) => {
155 156
          if (needAdaptor && store.state.multiplexingStyleAdapt) {
            adaptCurThemeCommonStyle(newCop, 'copy')
Tippi.Rao committed
157
          }
Tippi.Rao committed
158 159 160 161 162
          store.commit('addComponent', { component: newCop })
        })
      } else {
        const newCop = deepCopy(data)
        newCop.id = uuid.v1()
163 164
        if (needAdaptor && store.state.multiplexingStyleAdapt) {
          adaptCurThemeCommonStyle(newCop, 'copy')
Tippi.Rao committed
165
        }
Tippi.Rao committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        store.commit('addComponent', { component: newCop })
      }
      if (state.isCut) {
        state.copyData = null
      }
    },

    cut(state) {
      if (!state.curComponent) {
        toast('请选择组件')
        return
      }

      if (state.copyData) {
        const data = deepCopy(state.copyData.data)
        const index = state.copyData.index
        data.id = generateID()
        store.commit('addComponent', { component: data, index })
        if (state.curComponentIndex >= index) {
          // 如果当前组件索引大于等于插入索引,需要加一,因为当前组件往后移了一位
          state.curComponentIndex++
        }
      }

      store.commit('copy')
      store.commit('deleteComponent')
      state.isCut = true
    }
  }
}