king
2020-09-28 e812829d83b1fd296b25fbc244f89e9b38f687a9
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
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Icon, Popover } from 'antd'
import './index.scss'
 
import demo1 from '@/assets/img/demo1.jpg'
import demo2 from '@/assets/img/demo2.jpg'
import demo3 from '@/assets/img/demo3.jpg'
import demo4 from '@/assets/img/demo4.jpg'
import demo5 from '@/assets/img/demo5.jpg'
 
const Card = ({ id, cardIds, card, moveCard, findCard, editCard, delCard }) => {
  const originalIndex = findCard(id).index
  const [{ isDragging }, drag] = useDrag({
    item: { type: 'action', id, originalIndex },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  })
  const [, drop] = useDrop({
    accept: 'action',
    canDrop: () => true,
    drop({ id: draggedId }) {
      if (!draggedId) return
      if (!cardIds.includes(draggedId)) return
 
      if (draggedId !== id) {
        const { index: overIndex } = findCard(id)
        moveCard(draggedId, overIndex)
      }
    },
  })
 
  let _style = {opacity: isDragging ? 0 : 1}
 
  _style.textAlign = card.align
  _style.color = card.color
  
  if (card.padding) {
    _style.padding = card.padding
  }
  if (card.fontSize) {
    _style.fontSize = card.fontSize + 'px'
  }
  if (card.fontWeight) {
    _style.fontWeight = card.fontWeight
  }
 
  const getContent = () => {
    if (card.eleType === 'text' || card.eleType === 'number') {
      let val = `${card.prefix}${card.value}${card.postfix}`
      return val
    } else if (card.eleType === 'icon') {
      return (<Icon type={card.icon}/>)
    } else if (card.eleType === 'slider') {
      return (
        <div className="ant-mk-slider">
          <div className="ant-mk-slider-rail"></div>
          <div className="ant-mk-slider-track" style={{width: '30%', backgroundColor: card.color}}></div>
          <div className="ant-mk-slider-handle" style={{left: '30%', borderColor: card.color}}></div>
        </div>
      )
    } else if (card.eleType === 'picture') {
      let _imagestyle = {}
 
      if (card.url) {
        _imagestyle = {backgroundImage: `url('${card.url}')`}
      } else {
        let index = card.uuid.match(/\d{1}/g)
        index = index[index.length - 1] % 5
        let demos = [demo1, demo2, demo3, demo4, demo5]
 
        _imagestyle = {backgroundImage: `url('${demos[index]}')`}
      }
 
      if (card.radius === 'true') {
        _imagestyle.borderRadius = '50%'
      }
 
      if (card.lenWidRadio === '16:9') {
        _imagestyle.paddingTop = '56.25%'
      } else if (card.lenWidRadio === '3:2') {
        _imagestyle.paddingTop = '66.67%'
      } else if (card.lenWidRadio === '4:3') {
        _imagestyle.paddingTop = '75%'
      } else {
        _imagestyle.paddingTop = '100%'
      }
 
      return (
        <div className="ant-mk-picture" style={_imagestyle}></div>
      )
    } else if (card.eleType === 'splitline') {
      return (
        <div className="ant-mk-splitline" style={{backgroundColor: card.color}}></div>
      )
    }
  }
 
  return (
    <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
      <div className="mk-popover-control">
        <Icon className="edit" title="edit" type="edit" onClick={() => editCard(id)} />
        <Icon className="close" title="close" type="close" onClick={() => delCard(id)} />
        {/* <Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" /> */}
      </div>
    } trigger="hover">
      <div ref={node => drag(drop(node))} className={'ant-col card-cell ant-col-' + card.width} style={_style}>
        {getContent()}
      </div>
    </Popover>
  )
}
export default Card