DragSetting.js 6.2 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4 5 6 7 8 9
/**
 * 钟是志
 * 2020年5月28日 18:15:56
 * 通过绑定3个事件 鼠标down 鼠标move 鼠标up 实现拖到
 * 在down 的时候 注册 哪个配置项被点击
 * 在move 的时候 计算偏移量 让鼠标的偏移量 和 配置项dom的偏移量一致
 * 在up 的时候 更新元素dom的新位置到props
 * */

钟是志's avatar
钟是志 committed
10
import React, { Component, Fragment } from 'react';
11
import styles from './index.less';
钟是志's avatar
钟是志 committed
12
import { InputNumber } from 'antd';
13
import { imageStyleAll, A4Height, A4Width, styleCount } from './ViewPrint/config';
14

钟是志's avatar
钟是志 committed
15 16
let domIdTemp = '';

17
const HandleDetailFunction = ({ isDownObj, updateOnePostion, config }) => {
钟是志's avatar
钟是志 committed
18 19 20 21 22 23 24 25
  for (let item in isDownObj) {
    if (isDownObj[item]) {
      domIdTemp = item;
    }
  }
  if (!domIdTemp) {
    return null;
  }
26 27 28 29
  const id = domIdTemp.replace('dragKey-', '');
  const itemConfig = config.find((y) => {
    return y.id + '' === id;
  });
钟是志's avatar
钟是志 committed
30
  const dom = document.getElementById(domIdTemp);
31 32
  const x = itemConfig.x;
  const y = itemConfig.y;
33 34 35
  if(!dom){
    return null;
  }
36 37 38
  return <Fragment>
    <span dangerouslySetInnerHTML={{ __html: dom.innerHTML }}/>
    <span>  偏移量(x,y)</span>
钟是志's avatar
钟是志 committed
39
    <InputNumber precision={0}
40 41 42
                 onChange={(value) => {
                   updateOnePostion({ id, x: value, y });
                 }}
钟是志's avatar
钟是志 committed
43 44
                 value={x}/>
    <InputNumber precision={0}
45 46 47
                 onChange={(value) => {
                   updateOnePostion({ id, y: value, x });
                 }}
钟是志's avatar
钟是志 committed
48 49 50 51 52
                 value={y}/>
  </Fragment>;
};


53
export default class Index extends Component {
钟是志's avatar
钟是志 committed
54 55 56 57 58 59 60 61 62 63 64 65 66
  constructor(props) {
    super(props);
    this.time1 = new Date().getTime();
    this.state = {
      isDownObj: {},
      drag: {
        objX: '0px',
        objY: '0px',
        mouseY: 0,
        mouseX: 0,
      },
    };
  }
67

钟是志's avatar
钟是志 committed
68 69 70 71 72 73 74 75 76 77 78 79 80
  handleOnMouseDown = (e, id) => { // 鼠标按钮 按下
    const div = document.getElementById(id);
    const { isDownObj } = this.state;
    const newDrag = {
      objX: div.style.left,
      objY: div.style.top,
      mouseX: e.clientX,
      mouseY: e.clientY,
    };
    for (let item in isDownObj) {
      isDownObj[item] = false;
    }
    isDownObj[id] = true;
81

钟是志's avatar
钟是志 committed
82 83 84 85 86
    this.setState({
      drag: newDrag,
      isDownObj,
    });
  };
87

钟是志's avatar
钟是志 committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  handleOnMouseMove = (e) => { // 鼠标 移动
    const { drag, isDownObj } = this.state;
    if (new Date().getTime() - this.time1 < 17) {
      this.time1 = new Date().getTime();
      return false;
    }
    let id = '';
    for (let item in isDownObj) {
      if (isDownObj[item]) {
        id = item;
      }
    }
    if (!id) {
      return false;
    }
钟是志's avatar
钟是志 committed
103

钟是志's avatar
钟是志 committed
104 105 106 107 108 109 110 111 112 113 114 115
    let { mouseX, mouseY, objX, objY } = drag;
    const div = document.getElementById(id);
    let x = e.clientX;
    let y = e.clientY;
    if (isDownObj && isDownObj[id]) {
      const styleLeft =
        parseInt(objX, 10) + parseInt(x, 10) - parseInt(mouseX, 10); // 计算偏移量
      const styleTop =
        parseInt(objY, 10) + parseInt(y, 10) - parseInt(mouseY, 10); // 计算偏移量
      const dropZone = document.getElementById('dropZone');
      if ( // 阻止拖拽到图片外部
        styleLeft > dropZone.width - 15
钟是志's avatar
钟是志 committed
116 117 118
        || (styleLeft < 15)
        || (styleTop > dropZone.height - 15)
        || styleTop < 15
钟是志's avatar
钟是志 committed
119 120 121 122 123 124 125 126
      ) {
        console.error('拖拽到了图片区域外部,不能进行拖拽');
        return false;
      }
      div.style.left = styleLeft + 'px';
      div.style.top = styleTop + 'px';
    }
  };
钟是志's avatar
钟是志 committed
127

128 129 130 131 132 133 134 135 136 137
  updateOnePostion = ({ id, x, y }) => {
    const { updateConfig } = this.props;
    updateConfig({
      id,
      x,
      y,
    });
  };

  handleOnMouseUp = (e, id) => { // 鼠标 按钮 收起 更新数据到props
钟是志's avatar
钟是志 committed
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
    const { drag, isDownObj } = this.state;
    let { mouseX, mouseY, objX, objY } = drag;
    if (isDownObj[id]) {
      let x = e.clientX;
      let y = e.clientY;
      let div = document.getElementById(id);
      const styleLeft = parseInt(x, 10) - parseInt(mouseX, 10) + parseInt(objX, 10);
      const styleTop = parseInt(y, 10) - parseInt(mouseY, 10) + parseInt(objY, 10);
      div.style.left = `${styleLeft}px`;
      div.style.top = `${styleTop}px`;
      mouseX = x;
      mouseY = y;
      const { updateConfig } = this.props;
      updateConfig({
        id: id.replace('dragKey-', ''),
        x: styleLeft,
        y: styleTop,
      });
      this.setState({
        drag: {
          mouseX,
          mouseY,
          objX,
          objY,
        },
        isDownObj: {},
      });
    }
  };
钟是志's avatar
钟是志 committed
167

钟是志's avatar
钟是志 committed
168 169 170 171 172 173
  render() {
    const {
      backgroundUrl,
      configAll: { config, wide, high },
    } = this.props;
    const { isDownObj } = this.state;
钟是志's avatar
钟是志 committed
174
    const imageStyle = {
175 176
      height: `${high}cm` || A4Height,
      width: `${wide}cm` || A4Width,
钟是志's avatar
钟是志 committed
177
    };
钟是志's avatar
钟是志 committed
178 179
    return (
      <div className={styles.outSideDiv}>
180 181 182 183 184 185
        <div style={{ marginLeft: '45%', height: '100px' }}>
          <HandleDetailFunction isDownObj={isDownObj}
                                config={config}
                                updateOnePostion={this.updateOnePostion}
          />
        </div>
钟是志's avatar
钟是志 committed
186 187 188 189 190
        <div
          onMouseMove={(e) => {
            this.handleOnMouseMove(e);
          }}
          style={{
钟是志's avatar
钟是志 committed
191
            ...imageStyle,
192
            position: 'relative',
钟是志's avatar
钟是志 committed
193 194 195 196 197 198
          }}>
          <img
            src={backgroundUrl}
            id={'dropZone'}
            draggable={false}
            className={styles.bgimage}
199
            style={imageStyleAll}
钟是志's avatar
钟是志 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            alt={'背景图'}
          />
          {config.map((item, index) => {
            const domId = `dragKey-${item.id}`;
            return (
              <div
                draggable={false}
                className={styles.inSideItem}
                key={item.id}
                onMouseDown={(e) => {
                  this.handleOnMouseDown(e, domId);
                  console.log(domId);
                }}
                onMouseUp={(e) => {
                  this.handleOnMouseUp(e, domId);
                }}
                id={domId}

                style={{
                  top: `${item.y || 20 + index * 40}px`,
                  left: `${item.x || 20}px`,
                }}>
222 223 224 225
								<span style={{
                  ...styleCount(item),
                  fontWeight: 'bold',
                }}>
钟是志's avatar
钟是志 committed
226 227
									{item.title}
								</span>
钟是志's avatar
钟是志 committed
228 229 230 231 232 233 234
              </div>
            );
          })}
        </div>
      </div>
    );
  }
235
}