Refactor content panel dragging

This commit is contained in:
sienori 2019-10-20 21:50:14 +09:00
parent a758039fe4
commit 4f36b77890
2 changed files with 74 additions and 60 deletions

View file

@ -19,41 +19,51 @@ export default class TranslatePanel extends Component {
shouldResize: true, shouldResize: true,
isOverflow: false isOverflow: false
}; };
this.isFirst = true;
this.dragOffsets = { x: 0, y: 0 };
this.isDragging = false;
}
componentDidMount = () => {
document.addEventListener("dragstart", this.handleDragStart); document.addEventListener("dragstart", this.handleDragStart);
document.addEventListener("dragover", this.handleDragOver); document.addEventListener("dragover", this.handleDragOver);
document.addEventListener("drop", this.handleDrop); document.addEventListener("drop", this.handleDrop);
} };
componentWillUnmount = () => {
document.removeEventListener("dragstart", this.handleDragStart);
document.removeEventListener("dragover", this.handleDragOver);
document.removeEventListener("drop", this.handleDrop);
};
handleDragStart = e => { handleDragStart = e => {
// Firefox. if (e.target.className !== "simple-translate-move") return;
if (e.explicitOriginalTarget) { this.isDragging = true;
if (e.explicitOriginalTarget.className !== "simple-translate-move") {
return;
}
}
// Chrome.
else if (e.target.className !== "simple-translate-move") {
return;
}
const rect = document.querySelector(".simple-translate-panel").getBoundingClientRect(); const rect = document.querySelector(".simple-translate-panel").getBoundingClientRect();
const offsets = { this.dragOffsets = {
x: e.clientX - rect.left, x: e.clientX - rect.left,
y: e.clientY - rect.top y: e.clientY - rect.top
}; };
e.dataTransfer.setData("text/plain", JSON.stringify(offsets)); e.dataTransfer.setData("text/plain", "");
}; };
handleDragOver = e => { handleDragOver = e => {
if (!this.isDragging) return;
e.preventDefault(); e.preventDefault();
const panel = document.querySelector(".simple-translate-panel");
panel.style.top = `${e.clientY - this.dragOffsets.y}px`;
panel.style.left = `${e.clientX - this.dragOffsets.x}px`;
}; };
handleDrop = e => { handleDrop = e => {
if (!this.isDragging) return;
e.preventDefault(); e.preventDefault();
this.isDragging = false;
const panel = document.querySelector(".simple-translate-panel"); const panel = document.querySelector(".simple-translate-panel");
const offsets = JSON.parse(e.dataTransfer.getData("text/plain")); panel.style.top = `${e.clientY - this.dragOffsets.y}px`;
panel.style.top = `${e.clientY - offsets.y}px`; panel.style.left = `${e.clientX - this.dragOffsets.x}px`;
panel.style.left = `${e.clientX - offsets.x}px`;
}; };
calcPosition = () => { calcPosition = () => {
@ -175,11 +185,6 @@ export default class TranslatePanel extends Component {
const candidateStyles = { const candidateStyles = {
color: getSettings("candidateFontColor") color: getSettings("candidateFontColor")
}; };
const moveStyles = {
backgroundColor: "rgb(0, 145, 247)",
cursor: "grab",
height: "15px"
};
return ( return (
<div <div
@ -187,8 +192,9 @@ export default class TranslatePanel extends Component {
ref="panel" ref="panel"
style={panelStyles} style={panelStyles}
> >
<div className="simple-translate-move" draggable="true" style={moveStyles}></div>
<div className="simple-translate-result-wrapper" ref="wrapper" style={wrapperStyles}> <div className="simple-translate-result-wrapper" ref="wrapper" style={wrapperStyles}>
<div className="simple-translate-move" draggable="true" ref="move"></div>
<div className="simple-translate-result-contents">
<p className="simple-translate-result" style={resultStyles}> <p className="simple-translate-result" style={resultStyles}>
{splitLine(resultText)} {splitLine(resultText)}
</p> </p>
@ -202,6 +208,7 @@ export default class TranslatePanel extends Component {
)} )}
</div> </div>
</div> </div>
</div>
); );
}; };
} }

View file

@ -23,7 +23,6 @@
font-size: inherit; font-size: inherit;
visibility: inherit; visibility: inherit;
opacity: inherit; opacity: inherit;
padding: 10px 18px;
box-sizing: border-box; box-sizing: border-box;
width: max-content; width: max-content;
height: max-content; height: max-content;
@ -33,6 +32,13 @@
max-height: 100%; max-height: 100%;
overflow: auto; overflow: auto;
.simple-translate-move {
height: 15px;
cursor: grab;
}
.simple-translate-result-contents {
padding: 0px 18px 15px;
p { p {
all: initial; all: initial;
display: block; display: block;
@ -64,4 +70,5 @@
} }
} }
} }
}
} }