From a758039fe4d857363e1cb2bf80cef63325bfad72 Mon Sep 17 00:00:00 2001 From: fluks Date: Sun, 20 Oct 2019 14:50:12 +0300 Subject: [PATCH] Make content panel movable (#183) Let the user move the content panel. It can be moved by grabbing the blue bar on top. --- src/content/components/TranslatePanel.js | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/content/components/TranslatePanel.js b/src/content/components/TranslatePanel.js index 45809c4..a2b47c6 100644 --- a/src/content/components/TranslatePanel.js +++ b/src/content/components/TranslatePanel.js @@ -20,8 +20,42 @@ export default class TranslatePanel extends Component { isOverflow: false }; this.isFirst = true; + document.addEventListener("dragstart", this.handleDragStart); + document.addEventListener("dragover", this.handleDragOver); + document.addEventListener("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; + } + const rect = document.querySelector(".simple-translate-panel").getBoundingClientRect(); + const offsets = { + x: e.clientX - rect.left, + y: e.clientY - rect.top + }; + e.dataTransfer.setData("text/plain", JSON.stringify(offsets)); + }; + + handleDragOver = e => { + e.preventDefault(); + }; + + handleDrop = e => { + e.preventDefault(); + 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`; + }; + calcPosition = () => { const maxWidth = parseInt(getSettings("width")); const maxHeight = parseInt(getSettings("height")); @@ -141,6 +175,11 @@ 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)}