|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +const SelectionHandler = ({phrase, onSelectionChange}) => { |
| 4 | + const [cssProperties, setCssProperties] = useState({}); |
| 5 | + // let position = {} |
| 6 | + |
| 7 | + useEffect(() => { |
| 8 | + document.addEventListener("selectstart", (e) => { |
| 9 | + // console.log(e); |
| 10 | + // console.log("selection starts"); |
| 11 | + document.addEventListener("mouseup", selectPhrase); |
| 12 | + }); |
| 13 | + |
| 14 | + const selectPhrase = (e) => { |
| 15 | + const selection = document.getSelection(); |
| 16 | + if (selection.toString() !== "") { |
| 17 | + onSelectionChange(selection) |
| 18 | + const oRange = selection.getRangeAt(0); //get the text range |
| 19 | + const oRect = oRange.getBoundingClientRect(); |
| 20 | + setCssProperties({ |
| 21 | + left: oRect.left, |
| 22 | + top: oRect.top, |
| 23 | + // height: oRect.height, |
| 24 | + // width: oRect.width, |
| 25 | + }); |
| 26 | + } |
| 27 | + document.removeEventListener("mouseup", selectPhrase); |
| 28 | + }; |
| 29 | + }, [setCssProperties, phrase, onSelectionChange]); |
| 30 | + |
| 31 | + return ( |
| 32 | + <></> |
| 33 | + // <div style={{ position: "absolute", ...cssProperties, zIndex: 10 }}> |
| 34 | + // <div aria-expanded="true" aria-haspopup="dialog" aria-controls="rpv-core__popver-body-77"> |
| 35 | + // <div |
| 36 | + // aria-describedby="rpv-core__popover-body-inner-373" |
| 37 | + // class="rpv-core__popover-body" |
| 38 | + // id="rpv-core__popover-body-373" |
| 39 | + // role="dialog" |
| 40 | + // tabindex="-1" |
| 41 | + // style={{...cssProperties}} |
| 42 | + // > |
| 43 | + // <div class="rpv-core__arrow rpv-core__arrow--bc rpv-core__popover-body-arrow"></div> |
| 44 | + // <div id="rpv-core__popover-body-inner-373"> |
| 45 | + // <div style={{padding: "0.5rem", width: "12rem"}}>{phrase}</div> |
| 46 | + // </div> |
| 47 | + // </div> |
| 48 | + // </div> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default SelectionHandler; |
0 commit comments