diff --git a/src/content/components/TranslatePanel.js b/src/content/components/TranslatePanel.js index a2b47c6..74a1c7a 100644 --- a/src/content/components/TranslatePanel.js +++ b/src/content/components/TranslatePanel.js @@ -19,41 +19,51 @@ export default class TranslatePanel extends Component { shouldResize: true, isOverflow: false }; - this.isFirst = true; + + this.dragOffsets = { x: 0, y: 0 }; + this.isDragging = false; + } + + componentDidMount = () => { document.addEventListener("dragstart", this.handleDragStart); document.addEventListener("dragover", this.handleDragOver); document.addEventListener("drop", this.handleDrop); - } + }; + + componentWillUnmount = () => { + document.removeEventListener("dragstart", this.handleDragStart); + document.removeEventListener("dragover", this.handleDragOver); + document.removeEventListener("drop", this.handleDrop); + }; handleDragStart = e => { - // Firefox. - if (e.explicitOriginalTarget) { - if (e.explicitOriginalTarget.className !== "simple-translate-move") { - return; - } - } - // Chrome. - else if (e.target.className !== "simple-translate-move") { - return; - } + if (e.target.className !== "simple-translate-move") return; + this.isDragging = true; + const rect = document.querySelector(".simple-translate-panel").getBoundingClientRect(); - const offsets = { - x: e.clientX - rect.left, - y: e.clientY - rect.top + this.dragOffsets = { + x: e.clientX - rect.left, + y: e.clientY - rect.top }; - e.dataTransfer.setData("text/plain", JSON.stringify(offsets)); + e.dataTransfer.setData("text/plain", ""); }; handleDragOver = e => { + if (!this.isDragging) return; 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 => { + if (!this.isDragging) return; e.preventDefault(); + this.isDragging = false; + const panel = document.querySelector(".simple-translate-panel"); - const offsets = JSON.parse(e.dataTransfer.getData("text/plain")); - panel.style.top = `${e.clientY - offsets.y}px`; - panel.style.left = `${e.clientX - offsets.x}px`; + panel.style.top = `${e.clientY - this.dragOffsets.y}px`; + panel.style.left = `${e.clientX - this.dragOffsets.x}px`; }; calcPosition = () => { @@ -175,11 +185,6 @@ export default class TranslatePanel extends Component { const candidateStyles = { color: getSettings("candidateFontColor") }; - const moveStyles = { - backgroundColor: "rgb(0, 145, 247)", - cursor: "grab", - height: "15px" - }; return (
- {splitLine(resultText)} -
-- {splitLine(candidateText)} -
- {isError && ( -- {getErrorMessage(statusText)} +
++ {splitLine(resultText)}
- )} ++ {splitLine(candidateText)} +
+ {isError && ( ++ {getErrorMessage(statusText)} +
+ )} +