import * as React from "react"; import { createPortal } from "react-dom"; import { Rect } from "../Rect"; import { CLASSES } from "../Types"; /** @internal */ export interface IFloatingWindowProps { title: string; id: string; url: string; rect: Rect | null; onCloseWindow: (id: string) => void; onSetWindow: (id: string, window: Window) => void; } interface IStyleSheet { href: string | null; type: string; rules: string[] | null; } /** @internal */ export const FloatingWindow = (props: React.PropsWithChildren) => { const { title, id, url, rect, onCloseWindow, onSetWindow, children } = props; const popoutWindow = React.useRef(null); const timerId = React.useRef(null); const [content, setContent] = React.useState(undefined); const styles = Array.from(window.document.styleSheets).reduce((result, styleSheet) => { let rules: CSSRuleList | undefined = undefined; try { rules = styleSheet.cssRules; } catch (e) { // styleSheet.cssRules can throw security exception } try { return [ ...result, { href: styleSheet.href, type: styleSheet.type, rules: rules ? Array.from(rules).map(rule => rule.cssText) : null, } ]; } catch (e) { return result; } }, [] as IStyleSheet[]); React.useLayoutEffect(() => { if (timerId.current) { clearTimeout(timerId.current); } let isMounted = true; const r = rect || new Rect(0, 0, 100, 100); // Make a local copy of the styles from the current window which will be passed into // the floating window. window.document.styleSheets is mutable and we can't guarantee // the styles will exist when 'popoutWindow.load' is called below. // let test = false; let left, top, width, height; let X, Y, W, H; const storedPosition = localStorage.getItem(`popoutWindowPosition_${id}`); const storedPositionObj = storedPosition ? JSON.parse(storedPosition) : null; if (storedPositionObj) { X = storedPositionObj.x; Y = storedPositionObj.y; W = storedPositionObj.width; H = storedPositionObj.height; } else { X = r.x; Y = r.y; W = r.width; H = r.height; } popoutWindow.current = window.open(url, id, `left=${X},top=${Y},width=${W},height=${H}`); // if(test){ // popoutWindow.current = window.open(url, id, `left=${r.x},top=${r.y},width=${r.width},height=${r.height}`); // } else { // popoutWindow.current = window.open(url, id, `left=${X},top=${Y},width=${W},height=${H}`); // } if (popoutWindow.current !== null) { onSetWindow(id, popoutWindow.current); window.addEventListener("beforeunload", () => { if (popoutWindow.current) { // test = true; left = popoutWindow.current.screenX; top = popoutWindow.current.screenY; width = popoutWindow.current.innerWidth; height = popoutWindow.current.innerHeight; // Store the window position in localStorage localStorage.setItem(`popoutWindowPosition_${id}`, JSON.stringify({ x: left, y: top, width, height })); popoutWindow.current.close(); popoutWindow.current = null; } }); popoutWindow.current.addEventListener("load", () => { if (isMounted) { const popoutDocument = popoutWindow.current!.document; popoutDocument.title = title; const popoutContent = popoutDocument.createElement("div"); popoutContent.className = CLASSES.FLEXLAYOUT__FLOATING_WINDOW_CONTENT; popoutDocument.body.appendChild(popoutContent); copyStyles(popoutDocument, styles).then(() => { setContent(popoutContent); }); // listen for popout unloading (needs to be after load for safari) popoutWindow.current!.addEventListener("beforeunload", () => { onCloseWindow(id); }); } }); } else { console.warn(`Unable to open window ${url}`); onCloseWindow(id); } return () => { isMounted = false; // delay so refresh will close window timerId.current = setTimeout(() => { if (popoutWindow.current) { // popoutWindow.current.close(); // popoutWindow.current = null; } }, 0); }; }, []); React.useEffect(() => { copyStyles(popoutWindow.current!.document, styles).then(() => { }); const handleMutations = (mutationsList: any) => { for (const mutation of mutationsList) { if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { // Copy the new styles to the floating window copyStyles(popoutWindow.current!.document, [{ href: null, type: 'text/css', rules: [mutation.addedNodes[0].textContent], // Assuming the added node is a style element }]); } } }; const observer = new MutationObserver(handleMutations); const observerConfig = { childList: true, subtree: true, }; observer.observe(document.head, observerConfig); return () => { observer.disconnect(); // Disconnect the observer when the component unmounts if (popoutWindow.current) { popoutWindow.current.removeEventListener("beforeunload", () => { onCloseWindow(id); }); } }; // eslint-disable-next-line react-hooks/exhaustive-deps } ,[]); if (content !== undefined) { return createPortal(children, content!); } else { return null; } }; /** @internal */ function copyStyles(doc: Document, styleSheets: IStyleSheet[]): Promise { const head = doc.head; const promises: Promise[] = []; for (const styleSheet of styleSheets) { if (styleSheet.href) { // prefer links since they will keep paths to images etc const styleElement = doc.createElement("link"); styleElement.type = styleSheet.type; styleElement.rel = "stylesheet"; styleElement.href = styleSheet.href!; head.appendChild(styleElement); promises.push( new Promise((resolve) => { styleElement.onload = () => resolve(true); }) ); } else { if (styleSheet.rules) { const style = doc.createElement("style"); for (const rule of styleSheet.rules) { style.appendChild(doc.createTextNode(rule)); } head.appendChild(style); } } } return Promise.all(promises); }