king
2020-07-06 e0e5aa28cbd509579c7a83672a93241c9a5ed7e9
src/mob/controller/index.jsx
@@ -1,7 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Collapse, Form, Input, Col, Icon, InputNumber, Select } from 'antd'
import { Collapse, Form, Input, Col, Icon, InputNumber, Select, Radio } from 'antd'
import zhCN from '@/locales/zh-CN/mob.js'
import enUS from '@/locales/en-US/mob.js'
@@ -14,22 +14,153 @@
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'
  }
  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()
        this.setState({
          card: _card,
          fontColor: _card.color || '#000000',
          backgroundColor: _card.backgroundColor || '#ffffff'
        })
      })
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
    return !is(fromJS(this.state), fromJS(nextState))
  }
  /**
   * @description 字体大小切换,超出范围忽略
   */
  changeFontSize = (val) => {
    const { card } = this.state
    let value = parseInt(val)
    if (isNaN(value) || value < 12 || value > 100) return
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {fontSize: `${value}px`}})
  }
  /**
   * @description 修改行间距,超出范围忽略
   */
  changeLineHeight = (val) => {
    const { card } = this.state
    let value = parseFloat(val)
    if (isNaN(value) || value < 1 || value > 10) return
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {lineHeight: value}})
  }
  /**
   * @description 字体间距修改,超出范围忽略
   */
  changeLetterSpacing = (val) => {
    const { card } = this.state
    let value = parseFloat(val)
    if (isNaN(value) || value < 0 || value > 100) return
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {letterSpacing: `${value}px`}})
  }
  /**
   * @description 修改字体粗细
   */
  boldChange = (val) => {
    console.log(val)
    const { card } = this.state
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {fontWeight: val}})
  }
  /**
   * @description 修改字体颜色 ,颜色控件
   */
  changeFontColor = (val) => {
    const { card } = this.state
    this.setState({
      fontColor: val
    })
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {color: val}})
  }
  /**
   * @description 修改字体颜色 ,手动输入
   */
  changeFontColorInput = (e) => {
    this.setState({
      fontColor: e.target.value
    })
  }
  /**
   * @description 字体对齐
   */
  changeTextAlign = (e) => {
    const { card } = this.state
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {textAlign: e.target.value}})
  }
  /**
   * @description 字体样式,倾斜
   */
  changeFontStyle = (e) => {
    const { card } = this.state
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {fontStyle: e.target.value}})
  }
  /**
   * @description 字体装饰,下划线、贯穿线、上划线
   */
  changeTextDecoration = (e) => {
    const { card } = this.state
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {textDecoration: e.target.value}})
  }
  /**
   * @description 修改背景颜色 ,颜色控件
   */
  changeBackgroundColor = (val) => {
    const { card } = this.state
    this.setState({
      backgroundColor: val
    })
    this.props.updateStyle({componentId: card.componentId, uuid: card.uuid, style: {backgroundColor: val}})
  }
  /**
   * @description 修改字体颜色 ,手动输入
   */
  changeBackgroundColorInput = (e) => {
    this.setState({
      backgroundColor: e.target.value
    })
  }
  render () {
    const { editElem } = this.props
    const { card, fontColor, backgroundColor } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
@@ -40,19 +171,20 @@
        sm: { span: 16 }
      }
    }
    return (
      <div className="mob-controller">
        <Form {...formItemLayout}>
          {editElem ? <Collapse expandIconPosition="right" accordion={true}>
            {editElem.items.includes('font') ? <Panel header="字体" key="0">
          {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 min={12} max={100} precision={0} />
                  <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="normal" onChange={this.boldChange}>
                  <Select defaultValue={card.fontWeight || 'normal'} onChange={this.boldChange}>
                    <Option value="normal">normal</Option>
                    <Option value="bold">bold</Option>
                    <Option value="bolder">bolder</Option>
@@ -71,12 +203,12 @@
              </Col>
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="行间距" type="line-height" />}>
                  <InputNumber min={1} max={10} precision={1} />
                  <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 min={0} max={100} precision={0} />
                  <InputNumber defaultValue={card.letterSpacing || 0} min={0} max={100} precision={0} onChange={this.changeLetterSpacing}/>
                </Form.Item>
              </Col>
              <Col span={24}>
@@ -85,19 +217,73 @@
                  label={<Icon title="字体颜色" type="font-colors" />}
                  labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} }
                >
                  <ColorSketch  />
                  <Input  />
                  <ColorSketch color={card.color || '#000000'} changeColor={this.changeFontColor} />
                  <Input value={fontColor} onChange={this.changeFontColorInput} />
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item colon={false} label={<Icon title="字体粗细" type="bold" />}>
                  <Input  />
              <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}
            <Panel header="背景" key="1">
              背景
            </Panel>
            {card.items.includes('background') ? <Panel header="背景" key="1">
              <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 color={card.backgroundColor || '#ffffff'} changeColor={this.changeBackgroundColor} />
                  <Input value={backgroundColor} onChange={this.changeBackgroundColorInput} />
                </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 }} }
                >
                  <ColorSketch color={card.backgroundColor || '#ffffff'} changeColor={this.changeBackgroundColor} />
                  <Input value={backgroundColor} onChange={this.changeBackgroundColorInput} />
                </Form.Item>
              </Col>
            </Panel> : null}
            <Panel header="边距" key="2">
              边距
            </Panel>