king
2020-12-23 e9640ccdc9fe57f91919e3f51462c780e44fadb0
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
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Icon, Popover, Button } from 'antd'
import './index.scss'
 
const Card = ({ id, card, moveCard, findCard, editCard, delCard, profileCard, changeStyle, doubleClickCard }) => {
  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 || draggedId === id) return
 
      const { index: originIndex } = findCard(draggedId)
      if (originIndex === -1) return
 
      const { index: overIndex } = findCard(id)
      moveCard(draggedId, overIndex)
    },
  })
 
  let _style = {opacity: isDragging ? 0 : 1}
 
  if (card.style) {
    _style = {...card.style, opacity: isDragging ? 0 : 1}
  }
 
  let hasProfile = false
  if (['pop', 'prompt', 'exec'].includes(card.OpenType)) {
    hasProfile = true
  } else if (card.OpenType === 'excelIn' || card.OpenType === 'excelOut') {
    hasProfile = true
  } else if (card.funcType === 'print') {
    hasProfile = true
  }
 
  let btnElement = null
  if (card.show === 'icon') {
    btnElement = (<Button style={card.btnstyle} type="link"><Icon type={card.icon}/></Button>)
  } else if (card.show === 'link') {
    btnElement = (<Button style={card.btnstyle} type="link">{card.label}{card.icon ? <Icon type={card.icon}/> : null}</Button>)
  } else {
    btnElement = (<Button icon={card.icon} style={card.btnstyle}> {card.label} </Button>)
  }
 
  return (
    <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
      <div className="mk-popover-control">
        <Icon className="edit" title="编辑" type="edit" onClick={() => editCard(id)} />
        <Icon className="close" title="删除" type="close" onClick={() => delCard(id)} />
        <Icon className="style" title="调整样式" onClick={() => changeStyle(id)} type="font-colors" />
        {hasProfile ? <Icon className="profile" title="setting" type="profile" onClick={() => profileCard(id)} /> : null}
      </div>
    } trigger="hover">
      <div ref={node => drag(drop(node))} className={'ant-col card-button-cell ant-col-' + card.width} onDoubleClick={() => doubleClickCard(id)}>
        <div style={_style}>
          {btnElement}
        </div>
      </div>
    </Popover>
  )
}
export default Card