king
2020-10-05 6257816acce548a113081578140058cd99e83160
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Collapse, Form, Input, Col, Icon, InputNumber, Select, Radio, Popover, Menu } from 'antd'
 
import zhCN from '@/locales/zh-CN/mob.js'
import enUS from '@/locales/en-US/mob.js'
import ColorSketch from '@/mob/colorsketch'
import FileUpload from '@/tabviews/zshare/fileupload'
import './index.scss'
 
const { Panel } = Collapse
const { Option } = Select
 
class MobController extends Component {
  static propTpyes = {
    editElem: PropTypes.any,
    updateStyle: PropTypes.func,
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    card: null,
    fontColor: '#000000',
    backgroundColor: '#ffffff',
    bgimages: [],
    marginTop: '',
    marginTopVal: '',
    marginBottom: '',
    marginBottomVal: '',
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.props.editElem), fromJS(nextProps.editElem))) {
      this.setState({
        card: null
      }, () => {
        if (!nextProps.editElem) return
        let _card = fromJS(nextProps.editElem).toJS()
        let bgImg = _card.backgroundImage || ''
 
        if (bgImg && /^linear-gradient/.test(bgImg)) {
          bgImg = bgImg.replace('linear-gradient(', '')
          bgImg = bgImg.replace(')', '')
        } else if (bgImg && /^url/.test(bgImg)) {
          bgImg = bgImg.replace('url(', '')
          bgImg = bgImg.replace(')', '')
        }
 
        this.setState({
          card: _card,
          fontColor: _card.color || '#000000',
          backgroundColor: _card.backgroundColor || '#ffffff',
          backgroundImage: bgImg,
          marginTop: _card.marginTop ? _card.marginTop : '',
          marginTopVal: _card.marginTop ? parseInt(_card.marginTop) : '',
          marginBottom: _card.marginBottom ? _card.marginBottom : '',
          marginBottomVal: _card.marginBottomVal ? parseInt(_card.marginBottomVal) : ''
        })
      })
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  updateStyle = (style) => {
    const { card } = this.state
 
    this.props.updateStyle({componentId: card.componentId, classId: card.classId, uuid: card.uuid, style})
  }
 
  /**
   * @description 字体大小切换,超出范围忽略
   */
  changeFontSize = (val) => {
    let value = parseInt(val)
 
    if (isNaN(value) || value < 12 || value > 100) return
 
    this.updateStyle({fontSize: `${value}px`})
  }
 
  /**
   * @description 修改行间距,超出范围忽略
   */
  changeLineHeight = (val) => {
    let value = parseFloat(val)
 
    if (isNaN(value) || value < 1 || value > 10) return
 
    this.updateStyle({lineHeight: value})
  }
 
  /**
   * @description 字体间距修改,超出范围忽略
   */
  changeLetterSpacing = (val) => {
    let value = parseFloat(val)
 
    if (isNaN(value) || value < 0 || value > 100) return
 
    this.updateStyle({letterSpacing: `${value}px`})
  }
 
  /**
   * @description 修改字体粗细
   */
  boldChange = (val) => {
    this.updateStyle({fontWeight: val})
  }
 
  /**
   * @description 修改字体颜色 ,颜色控件
   */
  changeFontColor = (val) => {
    this.setState({
      fontColor: val
    })
    this.updateStyle({color: val})
  }
 
  /**
   * @description 修改字体颜色 ,手动输入
   */
  changeFontColorInput = (e) => {
    this.setState({
      fontColor: e.target.value
    })
  }
 
  /**
   * @description 字体对齐
   */
  changeTextAlign = (e) => {
    this.updateStyle({textAlign: e.target.value})
  }
 
  /**
   * @description 字体样式,倾斜
   */
  changeFontStyle = (e) => {
    this.updateStyle({fontStyle: e.target.value})
  }
 
  /**
   * @description 字体装饰,下划线、贯穿线、上划线
   */
  changeTextDecoration = (e) => {
    this.updateStyle({textDecoration: e.target.value})
  }
 
  /**
   * @description 修改背景颜色 ,颜色控件
   */
  changeBackgroundColor = (val) => {
    this.setState({
      backgroundColor: val
    })
    this.updateStyle({backgroundColor: val})
  }
 
  /**
   * @description 修改字体颜色 ,手动输入
   */
  changeBackgroundColorInput = (e) => {
    this.setState({
      backgroundColor: e.target.value
    })
  }
 
  imgChange = (list) => {
    if (list[0] && list[0].response) {
      this.setState({
        bgimages: [],
        backgroundImage: list[0].response
      })
      this.updateStyle({backgroundImage: `url(${list[0].response})`})
    } else {
      this.setState({bgimages: list})
    }
  }
 
  changeBackgroundImageInput = (e) => {
    this.setState({
      backgroundImage: e.target.value
    })
  }
 
  submitBackgroundImage = (e) => {
    let val = e.target.value
    val = val.replace(/^\s*|\s*$/ig, '')
 
    if (/^http|^\/\//.test(val)) {
      val = `url(${val})`
    } else if (/^#|,/ig.test(val)) {
      val = `linear-gradient(${val})`
    }
 
    this.updateStyle({backgroundImage: val})
  }
 
  submitBorder = (val, type) => {
    this.updateStyle({[type]: val})
  }
 
  changeBorderRadius = (val) => {
    let value = parseFloat(val)
 
    if (isNaN(value) || value < 0 || value > 500) return
 
    this.updateStyle({borderRadius: `${value}px`})
  }
 
  changeMarginTop = (e) => {
    let val = e.target.value
    let _val = parseInt(val)
 
    this.setState({
      marginTop: val
    })
 
    if (isNaN(_val)) return
 
    this.setState({
      marginTopVal: _val
    })
  }
 
  submitMarginTop = (val) => {
    this.setState({
      marginTop: val
    })
    this.updateStyle({marginTop: val})
  }
 
  changeMarginBottom = (e) => {
    let val = e.target.value
    let _val = parseInt(val)
 
    this.setState({
      marginBottom: val
    })
 
    if (isNaN(_val)) return
 
    this.setState({
      marginBottomVal: _val
    })
  }
 
  submitMarginBottom = (val) => {
    this.setState({
      marginBottom: val
    })
    this.updateStyle({marginBottom: val})
  }
 
  render () {
    const { card, backgroundImage, bgimages, marginTop, marginTopVal, marginBottom, marginBottomVal } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      }
    }
 
    return (
      <div className="mob-controller">
        <Form {...formItemLayout}>
          {card ? <Collapse expandIconPosition="right" defaultActiveKey={card.items[0]} accordion={true}>
            {card.items.includes('font') ? <Panel header="字体" key="font">
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="字体大小" type="font-size" />}>
                  <InputNumber defaultValue={card.fontSize || 14} min={12} max={100} precision={0} onChange={this.changeFontSize} />
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="字体粗细" type="bold" />}>
                  <Select defaultValue={card.fontWeight || 'normal'} onChange={this.boldChange}>
                    <Option value="normal">normal</Option>
                    <Option value="bold">bold</Option>
                    <Option value="bolder">bolder</Option>
                    <Option value="lighter">lighter</Option>
                    <Option value="100">100</Option>
                    <Option value="200">200</Option>
                    <Option value="300">300</Option>
                    <Option value="400">400</Option>
                    <Option value="500">500</Option>
                    <Option value="600">600</Option>
                    <Option value="700">700</Option>
                    <Option value="800">800</Option>
                    <Option value="900">900</Option>
                  </Select>
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="行间距" type="line-height" />}>
                  <InputNumber defaultValue={card.lineHeight || 1.5} min={1} max={10} precision={1} onChange={this.changeLineHeight} />
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="字间距" type="column-width" />}>
                  <InputNumber defaultValue={card.letterSpacing || 0} min={0} max={100} precision={0} onChange={this.changeLetterSpacing}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="字体颜色" type="font-colors" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <ColorSketch value={card.color || '#000000'} onChange={this.changeFontColor} />
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={' '}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Radio.Group defaultValue={card.fontStyle || 'normal'} onChange={this.changeFontStyle}>
                    <Radio.Button value="normal"><span title="标准">N</span></Radio.Button>
                    <Radio.Button value="italic"><Icon title="斜体" type="italic" /></Radio.Button>
                    <Radio.Button value="oblique" style={{fontStyle: 'oblique'}}><span title="倾斜">B</span></Radio.Button>
                  </Radio.Group>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={' '}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Radio.Group className="text-align" defaultValue={card.textAlign || 'left'} onChange={this.changeTextAlign}>
                    <Radio.Button value="left"><Icon title="左对齐" type="align-left" /></Radio.Button>
                    <Radio.Button value="center"><Icon title="居中对齐" type="align-center" /></Radio.Button>
                    <Radio.Button value="right"><Icon title="右对齐" type="align-right" /></Radio.Button>
                  </Radio.Group>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={' '}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Radio.Group className="text-decoration" defaultValue={card.textDecoration || 'none'} onChange={this.changeTextDecoration}>
                    <Radio.Button value="none"><span title="标准">N</span></Radio.Button>
                    <Radio.Button value="underline"><Icon title="下划线" type="underline" /></Radio.Button>
                    <Radio.Button value="line-through"><Icon title="中划线" type="strikethrough" /></Radio.Button>
                    <Radio.Button value="overline" style={{textDecoration: 'overline'}}><span title="上划线">O</span></Radio.Button>
                  </Radio.Group>
                </Form.Item>
              </Col>
            </Panel> : null}
            {card.items.includes('background') ? <Panel header="背景" key="background">
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="背景颜色" type="bg-colors" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <ColorSketch value={card.backgroundColor || '#ffffff'} onChange={this.changeBackgroundColor} />
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="背景图片" type="picture" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <FileUpload value={bgimages} maxFile={2} fileType="text" onChange={this.imgChange}/>
                  <Input placeholder="" value={backgroundImage} autoComplete="off" onBlur={this.submitBackgroundImage} onPressEnter={this.submitBackgroundImage} onChange={this.changeBackgroundImageInput} />
                </Form.Item>
              </Col>
            </Panel> : null}
            {card.items.includes('border') ? <Panel header="边框" key="border">
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="外边框" type="border-outer" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Input placeholder="" defaultValue={card.border || ''} autoComplete="off" onBlur={(e) => this.submitBorder(e.target.value, 'border')} onPressEnter={(e) => this.submitBorder(e.target.value, 'border')}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="左边框" type="border-left" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Input placeholder="" defaultValue={card.borderLeft || ''} autoComplete="off" onBlur={(e) => this.submitBorder(e.target.value, 'borderLeft')} onPressEnter={(e) => this.submitBorder(e.target.value, 'borderLeft')}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="右边框" type="border-right" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Input placeholder="" defaultValue={card.borderRight || ''} autoComplete="off" onBlur={(e) => this.submitBorder(e.target.value, 'borderRight')} onPressEnter={(e) => this.submitBorder(e.target.value, 'borderRight')}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="上边框" type="border-top" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Input placeholder="" defaultValue={card.borderTop || ''} autoComplete="off" onBlur={(e) => this.submitBorder(e.target.value, 'borderTop')} onPressEnter={(e) => this.submitBorder(e.target.value, 'borderTop')}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="下边框" type="border-bottom" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <Input placeholder="" defaultValue={card.borderBottom || ''} autoComplete="off" onBlur={(e) => this.submitBorder(e.target.value, 'borderBottom')} onPressEnter={(e) => this.submitBorder(e.target.value, 'borderBottom')}/>
                </Form.Item>
              </Col>
              <Col span={24}>
                <Form.Item
                  colon={false}
                  label={<Icon title="圆角" type="radius-setting" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <InputNumber defaultValue={card.borderRadius || 0} min={0} max={500} precision={0} onChange={this.changeBorderRadius}/>
                </Form.Item>
              </Col>
            </Panel> : null}
            {card.items.includes('margin') ? <Panel header="外边距" key="margin">
              <Col span={12}>
                <Form.Item
                  colon={false}
                  label={<Icon title="上边距" type="vertical-align-top"/>}
                >
                  <Popover placement="bottom" overlayClassName="margin-popover" content={
                    marginTopVal !== '' ?
                    <Menu>
                      <Menu.Item onClick={() => this.submitMarginTop(`${marginTopVal}px`)}>{marginTopVal} px</Menu.Item>
                      <Menu.Item onClick={() => this.submitMarginTop(`${marginTopVal}vh`)}>{marginTopVal} vh</Menu.Item>
                    </Menu> : null
                  } trigger="hover">
                    <Input value={marginTop} onChange={this.changeMarginTop}/>
                  </Popover>
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  colon={false}
                  label={<Icon title="下边距" type="vertical-align-bottom"/>}
                >
                  <Popover placement="bottom" overlayClassName="margin-popover" content={
                    marginBottomVal !== '' ?
                    <Menu>
                      <Menu.Item onClick={() => this.submitMarginBottom(`${marginBottomVal}px`)}>{marginBottomVal} px</Menu.Item>
                      <Menu.Item onClick={() => this.submitMarginBottom(`${marginBottomVal}vh`)}>{marginBottomVal} vh</Menu.Item>
                    </Menu> : null
                  } trigger="hover">
                    <Input value={marginBottom} onChange={this.changeMarginBottom}/>
                  </Popover>
                </Form.Item>
              </Col>
            </Panel> : null}
          </Collapse> : null}
        </Form>
      </div>
    )
  }
}
 
export default MobController