RefactoringOld.js 4.3 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * 钟是志
 * 2021年5月9日 10:50:02
 * 重构拖拽字段组件old.js. 适用于宝鸡录取通知书打印
 * */
import React, { Fragment, Component, useRef, useState, useEffect } from 'react';
import styles from './index.less';
import ButtonDiy from '@/baseComponent/ButtonDiy';
import Shell from '@/baseComponent/Shell';
import { ModalConfirm } from '@/baseComponent/Modal';

let time1 = new Date().getTime();

export default function NewDraggableSetting(props) {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	const {
		bgImage, // 背景图url 字符串
		formatSettingObject, // 字段配置信息 数组
		saveConfig, // 保存 方法
		initConfig, // 初始化配置项 方法
		updateFormatSettingObject, // 更新格式化 方法
		id, //  'printDom' 字符串
	} = props;
	const [drag, setDrag] = useState({
		objX: '0px',
		objY: '0px',
		mouseY: 0,
		mouseX: 0,
	});
	const [isDownObj, setIsDownObj] = useState({});
钟是志's avatar
钟是志 committed
30

31
	const configInfo = Object.keys(formatSettingObject);
钟是志's avatar
钟是志 committed
32

33 34 35 36 37 38 39
	function initInfoThis() {
		ModalConfirm('您确定初始化打印录取通知书格式吗?', {
			onOk: () => {
				initConfig();
			},
		});
	}
钟是志's avatar
钟是志 committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	function handleOnMouseDown(e) {
		// 鼠标按钮 按下
		const id = e.target.id;
		const div = document.getElementById(id);
		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;
		setDrag(newDrag);
		setIsDownObj(isDownObj);
	}
钟是志's avatar
钟是志 committed
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
	function handleOnMouseUp(e) {
		// 鼠标 按钮 收起 更新数据到props
		const id = e.target.id;
		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;
			updateFormatSettingObject(id, styleLeft, styleTop); // 更新 偏移量 left  top 和 元素
			setDrag({
				mouseX,
				mouseY,
				objX,
				objY,
			});
			setIsDownObj({});
		}
	}
钟是志's avatar
钟是志 committed
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	function handleOnMouseMove(e) {
		// 鼠标 移动
		if (new Date().getTime() - time1 < 17) {
			// 每17毫秒进行一次移动的计算.
			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

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
		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');
			// console.log('styleLeft', styleLeft, 'dropW', dropZone.width);
			// console.log('styleTop', styleTop, dropZone.height);
			if (
				// 阻止拖拽到图片外部
				styleLeft > dropZone.width - 50 ||
				styleLeft < 15 ||
				styleTop > dropZone.height - 50 ||
				styleTop < 15
			) {
				console.error('拖拽到了图片区域外部,不能进行拖拽');
				return false;
			}
			div.style.left = styleLeft + 'px';
			div.style.top = styleTop + 'px';
		}
	}
	return (
		<Fragment>
			<Shell
				styleShell={{
					marginTop: '0',
					marginBottom: '20px',
				}}>
				<div
					style={{
						height: '54px',
						padding: '12px 0 12px 12px',
					}}>
					<ButtonDiy name="保存" className="primary" handleClick={saveConfig} />
					<ButtonDiy name={'初始化格式'} handleClick={initInfoThis} />
				</div>
			</Shell>
			<div className={styles.outSideDiv} onMouseMove={handleOnMouseMove}>
				<div>
					<img
						src={bgImage}
						id={'dropZone'}
						draggable={false}
						className={styles.bgimage}
						alt={'背景图'}
					/>
					{configInfo.map((x) => {
						return (
							<div
								className={styles.inSideItem}
								key={x}
								onMouseDown={handleOnMouseDown}
								onMouseUp={handleOnMouseUp}
								id={x}
								style={formatSettingObject[x].style}>
								{formatSettingObject[x].title}
							</div>
						);
					})}
				</div>
			</div>
		</Fragment>
	);
钟是志's avatar
钟是志 committed
167
}