|
| 1 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 2 | +import { debounce } from "../util"; |
| 3 | +import React from "react"; |
| 4 | +import { css, Global } from "@emotion/react"; |
| 5 | +import { useGlobalStateContext } from "../globalState"; |
| 6 | +import { pinboard, pinMetal } from "../../colours"; |
| 7 | +import { textSans } from "../../fontNormaliser"; |
| 8 | +import { space } from "@guardian/source-foundations"; |
| 9 | +import ReactDOM from "react-dom"; |
| 10 | +import PinIcon from "../../icons/pin-icon.svg"; |
| 11 | +import root from "react-shadow/emotion"; |
| 12 | +import { boxShadow } from "../styling"; |
| 13 | + |
| 14 | +const SELECTION_TARGET_DATA_ATTR = "[data-pinboard-selection-target]"; |
| 15 | + |
| 16 | +interface ButtonPosition { |
| 17 | + top: number; |
| 18 | + left: number; |
| 19 | + unRoundedCorner: "bottom-left" | "top-right" | "top-left" | "bottom-right"; |
| 20 | +} |
| 21 | + |
| 22 | +export const NewswiresIntegration = () => { |
| 23 | + const { setPayloadToBeSent, setIsExpanded } = useGlobalStateContext(); |
| 24 | + |
| 25 | + const [state, setState] = useState<{ |
| 26 | + selectedHTML: string; |
| 27 | + containerElement: HTMLElement; |
| 28 | + firstButtonPosition: ButtonPosition; |
| 29 | + lastButtonPosition: ButtonPosition; |
| 30 | + } | null>(null); |
| 31 | + |
| 32 | + const handleSelectionChange = () => { |
| 33 | + const selection = window.getSelection(); |
| 34 | + const maybeOriginalTargetEl: HTMLElement | null = document.querySelector( |
| 35 | + SELECTION_TARGET_DATA_ATTR |
| 36 | + ); |
| 37 | + if ( |
| 38 | + selection && |
| 39 | + selection.rangeCount > 0 && |
| 40 | + selection.toString().length > 0 && |
| 41 | + maybeOriginalTargetEl |
| 42 | + ) { |
| 43 | + const clonedContents = selection.getRangeAt(0).cloneContents(); |
| 44 | + const maybeClonedTargetEl = clonedContents.querySelector( |
| 45 | + SELECTION_TARGET_DATA_ATTR |
| 46 | + ); |
| 47 | + const parentRect = maybeOriginalTargetEl.getBoundingClientRect(); |
| 48 | + const selectionRects = Array.from( |
| 49 | + selection.getRangeAt(0).getClientRects() |
| 50 | + ); |
| 51 | + const firstRect = selectionRects[0]; |
| 52 | + const lastRect = selectionRects[selectionRects.length - 1]; |
| 53 | + const firstButtonCoords = { |
| 54 | + top: firstRect.y - parentRect.y, |
| 55 | + left: firstRect.x - parentRect.x, |
| 56 | + }; |
| 57 | + const firstButtonPosition: ButtonPosition = { |
| 58 | + ...firstButtonCoords, |
| 59 | + unRoundedCorner: `bottom-${ |
| 60 | + firstButtonCoords.left > parentRect.width / 2 ? "right" : "left" |
| 61 | + }`, |
| 62 | + }; |
| 63 | + const lastButtonCoords = { |
| 64 | + top: lastRect.y - parentRect.y + lastRect.height, |
| 65 | + left: lastRect.x - parentRect.x + lastRect.width - 1, |
| 66 | + }; |
| 67 | + const lastButtonPosition: ButtonPosition = { |
| 68 | + ...lastButtonCoords, |
| 69 | + unRoundedCorner: `top-${ |
| 70 | + lastButtonCoords.left > parentRect.width / 2 ? "right" : "left" |
| 71 | + }`, |
| 72 | + }; |
| 73 | + |
| 74 | + if (maybeClonedTargetEl) { |
| 75 | + console.log( |
| 76 | + "selection contains whole target element; contents:", |
| 77 | + maybeClonedTargetEl.innerHTML |
| 78 | + ); |
| 79 | + setState({ |
| 80 | + selectedHTML: maybeClonedTargetEl.innerHTML, |
| 81 | + containerElement: maybeOriginalTargetEl, |
| 82 | + firstButtonPosition, |
| 83 | + lastButtonPosition, |
| 84 | + }); |
| 85 | + } else if ( |
| 86 | + maybeOriginalTargetEl?.contains(selection.anchorNode) && |
| 87 | + maybeOriginalTargetEl?.contains(selection.focusNode) |
| 88 | + ) { |
| 89 | + const tempEl = document.createElement("div"); |
| 90 | + tempEl.appendChild(clonedContents); |
| 91 | + console.log( |
| 92 | + "selection is within target element; contents:", |
| 93 | + tempEl.innerHTML |
| 94 | + ); |
| 95 | + setState({ |
| 96 | + selectedHTML: tempEl.innerHTML, |
| 97 | + containerElement: maybeOriginalTargetEl, |
| 98 | + firstButtonPosition, |
| 99 | + lastButtonPosition, |
| 100 | + }); |
| 101 | + //TODO might need to clean up tempEl to avoid memory leak? |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const debouncedSelectionHandler = useMemo( |
| 107 | + () => () => { |
| 108 | + setState(null); // clear selection to hide buttons |
| 109 | + debounce(handleSelectionChange, 500)(); |
| 110 | + }, |
| 111 | + [handleSelectionChange] |
| 112 | + ); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + document.addEventListener("selectionchange", debouncedSelectionHandler); |
| 116 | + /** |
| 117 | + * todos: |
| 118 | + * [ ] limit to newswires domain |
| 119 | + * [x] add selection listener -- addEventListener("selectionchange", (event) => {}); |
| 120 | + * [x] debounce handler |
| 121 | + * [x] check parent node of selection is newswires body text el (maybe add data attribute to body text el) |
| 122 | + * - (find first shared parent of anchorNode and focusNode, make sure we're not sharing bits of text outside of the target) |
| 123 | + * [x] extract HTML from selection (see chat thread) |
| 124 | + * [x] render button when there's a selection |
| 125 | + * [x] do things with pinboard |
| 126 | + */ |
| 127 | + return () => |
| 128 | + document.removeEventListener( |
| 129 | + "selectionchange", |
| 130 | + debouncedSelectionHandler |
| 131 | + ); |
| 132 | + }, []); |
| 133 | + |
| 134 | + const addSelectionToPinboard = useCallback(() => { |
| 135 | + if (state) { |
| 136 | + setPayloadToBeSent({ |
| 137 | + type: "newswires-snippet", |
| 138 | + payload: { |
| 139 | + embeddableHtml: state.selectedHTML, |
| 140 | + embeddableUrl: window.location.href, |
| 141 | + maybeUsageNote: state.containerElement.dataset.usageNote, |
| 142 | + }, |
| 143 | + }); |
| 144 | + setIsExpanded(true); |
| 145 | + } |
| 146 | + }, [state, setPayloadToBeSent]); |
| 147 | + |
| 148 | + return ( |
| 149 | + <> |
| 150 | + <Global |
| 151 | + styles={css` |
| 152 | + ${SELECTION_TARGET_DATA_ATTR} { |
| 153 | + position: relative; |
| 154 | + } |
| 155 | + ${SELECTION_TARGET_DATA_ATTR}::selection, ${SELECTION_TARGET_DATA_ATTR} ::selection { |
| 156 | + background-color: ${pinboard[500]}; |
| 157 | + color: ${pinMetal}; |
| 158 | + } |
| 159 | + `} |
| 160 | + /> |
| 161 | + {state && |
| 162 | + ReactDOM.createPortal( |
| 163 | + <root.div> |
| 164 | + {[state.firstButtonPosition, state.lastButtonPosition].map( |
| 165 | + (buttonCoords, index) => ( |
| 166 | + <button |
| 167 | + key={index} |
| 168 | + css={css` |
| 169 | + position: absolute; |
| 170 | + top: ${buttonCoords.top}px; |
| 171 | + left: ${buttonCoords.left}px; |
| 172 | + transform: translate( |
| 173 | + ${ |
| 174 | + buttonCoords.unRoundedCorner.includes("left") |
| 175 | + ? "0" |
| 176 | + : "-100%" |
| 177 | + },${ |
| 178 | + buttonCoords.unRoundedCorner.includes("bottom") |
| 179 | + ? "-100%" |
| 180 | + : "0" |
| 181 | + } |
| 182 | + ); |
| 183 | + display: flex; |
| 184 | + align-items: center; |
| 185 | + background-color: ${pinboard[500]}; |
| 186 | + ${textSans.xsmall({ fontWeight: "bold" })}; |
| 187 | + box-shadow: ${boxShadow}; |
| 188 | + border: none; |
| 189 | + border-radius: 100px; |
| 190 | + border-${buttonCoords.unRoundedCorner}-radius: 0; |
| 191 | + padding: 0 ${space[2]}px 0 ${space[3]}px; |
| 192 | + line-height: 2; |
| 193 | + cursor: pointer; |
| 194 | + color: ${pinMetal}; |
| 195 | + text-wrap: nowrap; |
| 196 | + `} |
| 197 | + onClick={addSelectionToPinboard} |
| 198 | + > |
| 199 | + Add selection to{" "} |
| 200 | + <PinIcon |
| 201 | + css={css` |
| 202 | + height: 18px; |
| 203 | + margin-left: ${space[1]}px; |
| 204 | + path { |
| 205 | + stroke: ${pinMetal}; |
| 206 | + stroke-width: 1px; |
| 207 | + } |
| 208 | + `} |
| 209 | + /> |
| 210 | + </button> |
| 211 | + ) |
| 212 | + )} |
| 213 | + </root.div>, |
| 214 | + state.containerElement |
| 215 | + )} |
| 216 | + </> |
| 217 | + ); |
| 218 | +}; |
0 commit comments