提交 b8cc9b37 authored 作者: 钟是志's avatar 钟是志

拖到打印修改

上级 035a30b2
...@@ -4,73 +4,111 @@ import styles from './index.less'; ...@@ -4,73 +4,111 @@ import styles from './index.less';
export default class Index extends Component { export default class Index extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {}; this.time1 = new Date().getTime();
this.state = {
isDownObj: {},
drag: {
objX: '0px',
objY: '0px',
mouseY: 0,
mouseX: 0,
},
};
}
componentDidMount() {
} }
dragEventList = () => { handleOnMouseDown = (e, id) => {
const { updateConfig } = this.props; const div = document.getElementById(id);
let draggedRef = null; const { isDownObj } = this.state;
document.addEventListener( const newDrag = {
'dragstart', objX: div.style.left,
function(event) { objY: div.style.top,
// 保存拖动元素的引用(ref.) mouseX: e.clientX,
draggedRef = event.target; mouseY: e.clientY,
// 使其半透明 };
event.target.style.opacity = 0.5; for (let item in isDownObj) {
}, isDownObj[item] = false;
false, }
); isDownObj[id] = true;
document.addEventListener(
'dragend',
function(event) {
// 重置透明度
event.target.style.opacity = '';
},
false,
);
/* 放下目标节点时触发事件 */ this.setState({
document.addEventListener( drag: newDrag,
'dragover', isDownObj,
function(event) { });
// 阻止默认动作 };
event.preventDefault();
console.log('drop-x = ', event.offsetX); handleOnMouseMove = (e) => {
console.log('drop-y = ', event.offsety); const { drag, isDownObj } = this.state;
}, if (new Date().getTime() - this.time1 < 17) {
false, this.time1 = new Date().getTime();
); return false;
document.addEventListener( }
'drop', let id = '';
function(event) { for (let item in isDownObj) {
// 阻止默认动作(如打开一些元素的链接) if (isDownObj[item]) {
event.preventDefault(); id = item;
// 将拖动的元素到所选择的放置目标节点中 }
if (event.target.id === 'dropZone') { }
const left = event.offsetX; if (!id) {
if (left > event.target.width - draggedRef.offsetWidth - 10) { return false;
}
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('拖拽到了图片区域外部,不能进行拖拽'); console.error('拖拽到了图片区域外部,不能进行拖拽');
return false; return false;
} }
const top = event.offsetY; div.style.left = styleLeft + 'px';
console.log('drop-x = ', event.offsetX); div.style.top = styleTop + 'px';
console.log('drop-y = ', event.offsety);
updateConfig({
id: draggedRef.id.replace('dragKey-', ''),
x: left,
y: top,
});
draggedRef.style.left = left; // `${left}px`;
draggedRef.style.top = top; // `${top}px`;
} }
},
false,
);
}; };
componentDidMount() { handleOnMouseUp = (e, id) => {
this.dragEventList(); 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: {},
});
} }
};
render() { render() {
const { const {
...@@ -80,6 +118,9 @@ export default class Index extends Component { ...@@ -80,6 +118,9 @@ export default class Index extends Component {
return ( return (
<div className={styles.outSideDiv}> <div className={styles.outSideDiv}>
<div <div
onMouseMove={(e) => {
this.handleOnMouseMove(e);
}}
style={{ style={{
padding: '12px', padding: '12px',
}}> }}>
...@@ -91,17 +132,26 @@ export default class Index extends Component { ...@@ -91,17 +132,26 @@ export default class Index extends Component {
alt={'背景图'} alt={'背景图'}
/> />
{config.map((item, index) => { {config.map((item, index) => {
const domId = `dragKey-${item.id}`;
return ( return (
<div <div
draggable={true} draggable={false}
className={styles.inSideItem} className={styles.inSideItem}
key={item.id} key={item.id}
id={`dragKey-${item.id}`} onMouseDown={(e) => {
this.handleOnMouseDown(e, domId);
}}
onMouseUp={(e) => {
this.handleOnMouseUp(e, domId);
}}
id={domId}
style={{ style={{
top: `${item.y || 20 + index * 40}px`, top: `${item.y || 20 + index * 40}px`,
left: `${item.x || 20}px`, left: `${item.x || 20}px`,
}}> }}>
<span style={{ marginLeft: '10px', marginRight: '10px' }}>
{item.title} {item.title}
</span>
</div> </div>
); );
})} })}
......
...@@ -23,6 +23,9 @@ export default class ViewPrint extends Component { ...@@ -23,6 +23,9 @@ export default class ViewPrint extends Component {
} }
countImageHeight = () => { countImageHeight = () => {
if(!this.state.configAll){
return false;
}
const { const {
configAll: { backgroundUrl }, configAll: { backgroundUrl },
} = this.state; } = this.state;
......
...@@ -60,7 +60,7 @@ export default class Index extends Component { ...@@ -60,7 +60,7 @@ export default class Index extends Component {
if (!configAll || !configAll.backgroundUrl) { if (!configAll || !configAll.backgroundUrl) {
return null; return null;
} }
console.log(configAll); // console.log(configAll);
return ( return (
<Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}> <Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}>
<div style={{ height: '54px', padding: '12px 0 24px 12px' }}> <div style={{ height: '54px', padding: '12px 0 24px 12px' }}>
......
...@@ -12,10 +12,11 @@ ...@@ -12,10 +12,11 @@
cursor: move; cursor: move;
z-index: 10; z-index: 10;
color: #000; color: #000;
padding-left: 10px; user-select: none;
padding-right: 10px; // padding-left: 10px;
// padding-right: 10px;
font-size: 14px; font-size: 14px;
line-height: 28px; // line-height: 28px;
border: 1px solid gray; border: 1px solid gray;
background-color: #CCC; background-color: #CCC;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论