king
2024-04-02 25bf481493bcf18fca79e4d250f9c91dedf832e5
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Popover } from 'antd'
import { EditOutlined, CopyOutlined, CloseOutlined, FontColorsOutlined, CaretUpOutlined, CaretDownOutlined } from '@ant-design/icons'
 
import MkIcon from '@/components/mk-icon'
import moment from 'moment'
 
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'
import MKEmitter from '@/utils/events.js'
import asyncComponent from '@/utils/asyncComponent'
import asyncIconComponent from '@/utils/asyncIconComponent'
import { resetStyle } from '@/utils/utils-custom.js'
import './index.scss'
 
const BarCode = asyncComponent(() => import('@/components/barcode'))
const MkProgress = asyncComponent(() => import('./mkProgress'))
const QrCode = asyncComponent(() => import('@/components/qrcode'))
const Video = asyncComponent(() => import('@/components/video'))
const MarkColumn = asyncIconComponent(() => import('@/menu/components/share/markcomponent'))
const PicRadio = {
  '4:3': '75%', '3:2': '66.67%', '16:9': '56.25%', '2:1': '50%', '3:1': '33.33%', '4:1': '25%',
  '5:1': '20%', '6:1': '16.67%', '7:1': '14.29%', '8:1': '12.5%', '9:1': '11.11%',
  '10:1': '10%', '3:4': '133.33%', '2:3': '150%', '9:16': '177.78%'
}
const appType = sessionStorage.getItem('appType')
 
const Card = ({ id, parent, fields, card, moveCard, findCard, editCard, delCard, copyCard, changeStyle, updateMarks }) => {
  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(item) {
      const { id: draggedId } = item
 
      if (item.$init) {
        item.overIndex = id
      }
 
      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.3 : 1}
  
  if (card.style && card.eleType !== 'tag') {
    _style = {...card.style, opacity: isDragging ? 0.3 : 1}
    _style = resetStyle(_style)
  }
  if (card.eleType === 'picture' && card.maxWidth) {
    _style.maxWidth = card.maxWidth
    let left = _style.marginLeft || 'auto'
    let right = _style.marginRight || 'auto'
 
    if (_style.marginLeft === '0px' && _style.marginRight === '0px') {
      left = 'auto'
      right = 'auto'
    }
 
    _style.margin = (_style.marginTop || 0) + ' ' + right + ' ' + (_style.marginBottom || 0) + ' ' + left
    delete _style.marginLeft
    delete _style.marginRight
    delete _style.marginTop
    delete _style.marginBottom
  }
 
  const getContent = () => {
    if (card.eleType === 'sequence') {
      return (
        <div style={{height: card.innerHeight || 'auto'}} className="ant-mk-text">{card.prefix || ''}1{card.postfix || ''}</div>
      )
    } else if (card.eleType === 'text' || card.eleType === 'number') {
      let val = `${card.prefix || ''}${card.datatype === 'static' ? (card.value || '') : (card.field || '')}${card.postfix || ''}`
      if (card.fixStyle === 'alone') {
        let _s = {fontSize: card.fixSize, color: card.fixColor, marginLeft: card.fixLeft, marginRight: card.fixRight}
        val = <><span style={_s}>{card.prefix || ''}</span>{`${card.datatype === 'static' ? (card.value || '') : (card.field || '')}`}<span style={_s}>{card.postfix || ''}</span></>
      }
      if (card.sortField) {
        val = <><span>{val}</span><span className="sort-wrap"><CaretUpOutlined /><CaretDownOutlined /></span></>
      }
      return (
        <div className={'ant-mk-text line' + (card.height || '')} style={{height: card.innerHeight || 'auto'}}>{val}</div>
      )
    } else if (card.eleType === 'icon') {
      if (card.tipType === 'text') {
        return <div className={'ant-mk-text line' + (card.height || '')} style={{height: card.innerHeight || 'auto'}}>{card.value || card.field || ''}</div>
      }
      return (<MkIcon style={{height: card.innerHeight || 'auto'}} className="ant-mk-icon" type={card.icon || 'bell'}/>)
    } else if (card.eleType === 'slider') {
      let val = card.value ? (card.value / card.maxValue) * 100 : 30
      return <MkProgress value={val} config={card}/>
      // return (
      //   <div className="ant-mk-slider">
      //     <div className="ant-mk-slider-rail"></div>
      //     <div className="ant-mk-slider-track" style={{width: `${val}%`, backgroundColor: card.color}}></div>
      //     <div className="ant-mk-slider-handle" style={{left: `${val}%`, borderColor: card.color}}></div>
      //   </div>
      // )
    } else if (card.eleType === 'picture') {
      let _imagestyle = {
        backgroundSize: card.style.backgroundSize || 'cover',
        backgroundPosition: card.style.backgroundPosition || 'center',
        backgroundRepeat: card.style.backgroundRepeat || 'no-repeat',
        borderRadius: card.style.borderRadius || 0
      }
      let url = card.url !== '@icon@' ? card.url : sessionStorage.getItem('CloudAvatar')
 
      if (url) {
        url = url.replace(/@mywebsite@\//ig, window.GLOB.baseurl)
        _imagestyle.backgroundImage = `url('${url}')`
      } else {
        let index = card.uuid.match(/\d{1}/g)
        index = index.slice(-1)[0] % 5
        let demos = [demo1, demo2, demo3, demo4, demo5]
 
        _imagestyle.backgroundImage = `url('${demos[index]}')`
      }
 
      if (PicRadio[card.lenWidRadio]) {
        _imagestyle.paddingTop = PicRadio[card.lenWidRadio]
      } else {
        _imagestyle.paddingTop = '100%'
      }
 
      return (
        <div className="ant-mk-picture" style={_imagestyle}></div>
      )
    } else if (card.eleType === 'splitline') {
      let _borderWidth = card.borderWidth === undefined ? 1 : card.borderWidth
      return (
        <div style={{paddingTop: _borderWidth ? '1px' : '10px'}}>
          <div className="ant-mk-splitline" style={{borderColor: card.color, borderWidth: _borderWidth}}></div>
        </div>
      )
    } else if (card.eleType === 'barcode') {
      return (
        <div style={{height: card.innerHeight || 25}}>
          <BarCode card={card} value={card.value || 'mksoft'}/>
        </div>
      )
    } else if (card.eleType === 'qrcode') {
      return (
        <div style={{minHeight: card.qrWidth || 50}}>
          <QrCode card={card} value={card.value || 'mksoft'}/>
        </div>
      )
    } else if (card.eleType === 'video') {
      _style.overflow = 'hidden'
      return (
        <div className="video-wrap">
          <Video card={card} poster={card.posterUrl || ''} value={card.url || 'http://qingqiumarket.cn/mkwms/Content/images/upload/20210104/trailer.mp4'}/>
        </div>
      )
    } else if (card.eleType === 'currentDate') {
      let val = `${card.prefix || ''}${moment().format(card.dateFormat)}${card.postfix || ''}`
      if (card.fixStyle === 'alone') {
        let _s = {fontSize: card.fixSize, color: card.fixColor, marginLeft: card.fixLeft, marginRight: card.fixRight}
        val = <><span style={_s}>{card.prefix || ''}</span>{moment().format(card.dateFormat)}<span style={_s}>{card.postfix || ''}</span></>
      }
      return (
        <div className="ant-mk-text line1" style={{height: card.innerHeight || 'auto'}}>
          {val}
        </div>
      )
    } else if (card.eleType === 'formula') {
      let val = `${card.prefix || ''}${card.formula}${card.postfix || ''}`
      if (card.fixStyle === 'alone') {
        let _s = {fontSize: card.fixSize, color: card.fixColor, marginLeft: card.fixLeft, marginRight: card.fixRight}
        val = <><span style={_s}>{card.prefix || ''}</span>{card.formula}<span style={_s}>{card.postfix || ''}</span></>
      }
 
      return (
        <div className="ant-mk-text" style={{height: card.innerHeight || 'auto'}}>
          {val}
        </div>
      )
    } else if (card.eleType === 'tag') {
      let vals = []
      if (card.datatype === 'static') {
        vals = card.value.split(',').filter(Boolean)
      } else {
        vals = [card.field || '']
      }
 
      let _style = resetStyle(card.style)
 
      return (
        <div className="ant-mk-tag">
          {vals.map((val, index) => <span key={index} className="tag-item" style={_style}>{val}</span>)}
        </div>
      )
    } else if (card.eleType === 'color') {
      _style.overflow = 'hidden'
      let _bgstyle = {backgroundColor: card.value || '#1890ff'}
 
      if (PicRadio[card.lenWidRadio]) {
        _bgstyle.paddingTop = PicRadio[card.lenWidRadio]
      } else {
        _bgstyle.paddingTop = '100%'
      }
 
      return (
        <div style={_bgstyle}></div>
      )
    }
  }
 
  const clickComponent = (e) => {
    if (sessionStorage.getItem('style-control') === 'true' || sessionStorage.getItem('style-control') === 'cardcell') {
      e.stopPropagation()
      MKEmitter.emit('clickComponent', card.uuid, parent.uuid)
    }
  }
 
  let able = true
  if ((appType === 'mob' || appType === 'pc') && parent.setting && (parent.setting.click === 'menu' || parent.setting.click === 'menus')) {
    able = false
  }
 
  let _style_ = null
 
  if (card.style && card.style.clear === 'left') {
    _style_ = {clear: 'left'}
  } else if (card.style && card.style.clear === 'right') {
    _style_ = {float: 'right'}
  }
 
  let mark = ['text', 'number', 'slider', 'sequence', 'formula'].includes(card.eleType)
  if (parent.setting && parent.setting.cardRole === 'header') {
    mark = false
  } else if (card.eleType === 'formula' && card.eval === 'func') {
    mark = false
  }
 
  return (
    <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
      <div className="mk-popover-control" onDoubleClick={(e) => e.stopPropagation()}>
        <EditOutlined className="edit" title="编辑" onClick={() => editCard(id)} />
        <CopyOutlined className="copy" title="复制" onClick={() => copyCard(id)} />
        <CloseOutlined className="close" title="删除" onClick={() => delCard(id)} />
        <FontColorsOutlined className="style" title="调整样式" onClick={() => changeStyle(id)}/>
        {mark ? <MarkColumn field={card.field || ''} columns={fields} type={card.eleType} marks={card.marks} onSubmit={(vals) => updateMarks({...card, marks: vals})} /> : null }
      </div>
    } trigger="hover">
      <div ref={node => drag(drop(node))} style={_style_} className={'ant-col card-cell ant-col-' + card.width}>
        <div style={_style} onClick={clickComponent} onDoubleClick={() => able && editCard(id)} id={card.uuid}>
          {getContent()}
        </div>
      </div>
    </Popover>
  )
}
export default Card