DragSetting.js 5.5 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 }) => {
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
	for (let item in isDownObj) {
		if (isDownObj[item]) {
			domIdTemp = item;
		}
	}
	if (!domIdTemp) {
		return null;
	}
	const id = domIdTemp.replace('dragKey-', '');
	const itemConfig = config.find((y) => {
		return y.id + '' === id;
	});
	const dom = document.getElementById(domIdTemp);
	const x = itemConfig.x;
	const y = itemConfig.y;
	if (!dom) {
		return null;
	}
	return (
		<Fragment>
			<span dangerouslySetInnerHTML={{ __html: dom.innerHTML }} />
			<span> 偏移量(x,y)</span>
			<InputNumber
				precision={0}
				onChange={(value) => {
					updateOnePostion({ id, x: value, y });
				}}
				value={x}
			/>
			<InputNumber
				precision={0}
				onChange={(value) => {
					updateOnePostion({ id, y: value, x });
				}}
				value={y}
			/>
		</Fragment>
	);
钟是志's avatar
钟是志 committed
56 57
};

58
export default class Index extends Component {
59 60 61 62 63 64 65 66 67 68 69 70 71
	constructor(props) {
		super(props);
		this.time1 = new Date().getTime();
		this.state = {
			isDownObj: {},
			drag: {
				objX: '0px',
				objY: '0px',
				mouseY: 0,
				mouseX: 0,
			},
		};
	}
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86
	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;
87

88 89 90 91 92
		this.setState({
			drag: newDrag,
			isDownObj,
		});
	};
钟是志's avatar
钟是志 committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	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
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		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 ||
				styleLeft < 15 ||
				styleTop > dropZone.height - 15 ||
				styleTop < 15
			) {
				console.error('拖拽到了图片区域外部,不能进行拖拽');
				return false;
			}
			div.style.left = styleLeft + 'px';
			div.style.top = styleTop + 'px';
		}
	};
133

134 135 136 137 138 139 140 141
	updateOnePostion = ({ id, x, y }) => {
		const { updateConfig } = this.props;
		updateConfig({
			id,
			x,
			y,
		});
	};
钟是志's avatar
钟是志 committed
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
	handleOnMouseUp = (e, id) => {
		// 鼠标 按钮 收起 更新数据到props
		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
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
	render() {
		const {
			backgroundUrl,
			configAll: { config, wide, high },
		} = this.props;
		const { isDownObj } = this.state;
		const imageStyle = {
			height: `${high}cm` || A4Height,
			width: `${wide}cm` || A4Width,
		};
		return (
			<div className={styles.outSideDiv}>
				<div style={{ marginLeft: '45%', height: '100px' }}>
					<HandleDetailFunction
						isDownObj={isDownObj}
						config={config}
						updateOnePostion={this.updateOnePostion}
					/>
				</div>
				<div
					onMouseMove={(e) => {
						this.handleOnMouseMove(e);
					}}
					style={{
						...imageStyle,
						position: 'relative',
					}}>
					<img
						src={backgroundUrl}
						id={'dropZone'}
						draggable={false}
						className={styles.bgimage}
						style={imageStyleAll}
						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`,
								}}>
								<span
									style={{
										...styleCount(item),
										fontWeight: 'bold',
									}}>
钟是志's avatar
钟是志 committed
234 235
									{item.title}
								</span>
236 237 238 239 240 241 242
							</div>
						);
					})}
				</div>
			</div>
		);
	}
243
}