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 33 34 35
  const x = itemConfig.x;
  const y = itemConfig.y;
  return <Fragment>
    <span dangerouslySetInnerHTML={{ __html: dom.innerHTML }}/>
    <span>  偏移量(x,y)</span>
钟是志's avatar
钟是志 committed
36
    <InputNumber precision={0}
37 38 39
                 onChange={(value) => {
                   updateOnePostion({ id, x: value, y });
                 }}
钟是志's avatar
钟是志 committed
40 41
                 value={x}/>
    <InputNumber precision={0}
42 43 44
                 onChange={(value) => {
                   updateOnePostion({ id, y: value, x });
                 }}
钟是志's avatar
钟是志 committed
45 46 47 48 49
                 value={y}/>
  </Fragment>;
};


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

钟是志's avatar
钟是志 committed
65 66 67 68 69 70 71 72 73 74 75 76 77
  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;
78

钟是志's avatar
钟是志 committed
79 80 81 82 83
    this.setState({
      drag: newDrag,
      isDownObj,
    });
  };
84

钟是志's avatar
钟是志 committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  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
100

钟是志's avatar
钟是志 committed
101 102 103 104 105 106 107 108 109 110 111 112
    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
113 114 115
        || (styleLeft < 15)
        || (styleTop > dropZone.height - 15)
        || styleTop < 15
钟是志's avatar
钟是志 committed
116 117 118 119 120 121 122 123
      ) {
        console.error('拖拽到了图片区域外部,不能进行拖拽');
        return false;
      }
      div.style.left = styleLeft + 'px';
      div.style.top = styleTop + 'px';
    }
  };
钟是志's avatar
钟是志 committed
124

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

  handleOnMouseUp = (e, id) => { // 鼠标 按钮 收起 更新数据到props
钟是志's avatar
钟是志 committed
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
    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
164

钟是志's avatar
钟是志 committed
165 166 167 168 169 170
  render() {
    const {
      backgroundUrl,
      configAll: { config, wide, high },
    } = this.props;
    const { isDownObj } = this.state;
钟是志's avatar
钟是志 committed
171
    const imageStyle = {
172 173
      height: `${high}cm` || A4Height,
      width: `${wide}cm` || A4Width,
钟是志's avatar
钟是志 committed
174
    };
钟是志's avatar
钟是志 committed
175 176
    return (
      <div className={styles.outSideDiv}>
177 178 179 180 181 182
        <div style={{ marginLeft: '45%', height: '100px' }}>
          <HandleDetailFunction isDownObj={isDownObj}
                                config={config}
                                updateOnePostion={this.updateOnePostion}
          />
        </div>
钟是志's avatar
钟是志 committed
183 184 185 186 187
        <div
          onMouseMove={(e) => {
            this.handleOnMouseMove(e);
          }}
          style={{
钟是志's avatar
钟是志 committed
188
            ...imageStyle,
189
            position: 'relative',
钟是志's avatar
钟是志 committed
190 191 192 193 194 195
          }}>
          <img
            src={backgroundUrl}
            id={'dropZone'}
            draggable={false}
            className={styles.bgimage}
196
            style={imageStyleAll}
钟是志's avatar
钟是志 committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            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`,
                }}>
219 220 221 222
								<span style={{
                  ...styleCount(item),
                  fontWeight: 'bold',
                }}>
钟是志's avatar
钟是志 committed
223 224
									{item.title}
								</span>
钟是志's avatar
钟是志 committed
225 226 227 228 229 230 231
              </div>
            );
          })}
        </div>
      </div>
    );
  }
232
}