Move maze utils to a submodule, move tooltip out

This commit is contained in:
Ajay
2023-06-30 02:46:27 -04:00
parent e2a01bb744
commit e1982f265e
8 changed files with 12 additions and 192 deletions

View File

@@ -1,154 +1,7 @@
import * as React from "react";
import { createRoot, Root } from 'react-dom/client';
import { ButtonListener } from "../types";
import { isFirefoxOrSafari } from "@ajayyy/maze-utils";
import { isSafari } from "@ajayyy/maze-utils/lib/config";
import { GenericTooltip, TooltipProps } from "../../maze-utils/src/components/Tooltip";
export interface TooltipProps {
text?: string;
link?: string;
linkOnClick?: () => void;
referenceNode: HTMLElement;
prependElement?: HTMLElement; // Element to append before
bottomOffset?: string;
topOffset?: string;
leftOffset?: string;
rightOffset?: string;
timeout?: number;
opacity?: number;
displayTriangle?: boolean;
extraClass?: string;
showLogo?: boolean;
showGotIt?: boolean;
center?: boolean;
positionRealtive?: boolean;
containerAbsolute?: boolean;
buttons?: ButtonListener[];
}
export class Tooltip {
text?: string;
container: HTMLDivElement;
timer: NodeJS.Timeout;
root: Root;
export class Tooltip extends GenericTooltip {
constructor(props: TooltipProps) {
props.bottomOffset ??= "70px";
props.topOffset ??= "inherit";
props.leftOffset ??= "inherit";
props.rightOffset ??= "inherit";
props.opacity ??= 0.7;
props.displayTriangle ??= true;
props.extraClass ??= "";
props.showLogo ??= true;
props.showGotIt ??= true;
props.positionRealtive ??= true;
props.containerAbsolute ??= false;
props.center ??= false;
this.text = props.text;
this.container = document.createElement('div');
this.container.id = "sponsorTooltip" + props.text;
if (props.positionRealtive) this.container.style.position = "relative";
if (props.containerAbsolute) this.container.style.position = "absolute";
if (props.center) {
if (isFirefoxOrSafari() && !isSafari()) {
this.container.style.width = "-moz-available";
} else {
this.container.style.width = "-webkit-fill-available";
}
}
if (props.prependElement) {
props.referenceNode.insertBefore(this.container, props.prependElement);
} else {
props.referenceNode.appendChild(this.container);
}
if (props.timeout) {
this.timer = setTimeout(() => this.close(), props.timeout * 1000);
}
const backgroundColor = `rgba(28, 28, 28, ${props.opacity})`;
this.root = createRoot(this.container);
this.root.render(
<div style={{
bottom: props.bottomOffset,
top: props.topOffset,
left: props.leftOffset,
right: props.rightOffset,
backgroundColor,
margin: props.center ? "auto" : null
}}
className={"sponsorBlockTooltip" +
(props.displayTriangle ? " sbTriangle" : "") +
` ${props.extraClass}`}>
<div>
{props.showLogo ?
<img className="sponsorSkipLogo sponsorSkipObject"
src={chrome.extension.getURL("icons/IconSponsorBlocker256px.png")}>
</img>
: null}
{this.text ?
<span className={`sponsorSkipObject${!props.showLogo ? ` sponsorSkipObjectFirst` : ``}`}>
{this.text + (props.link ? ". " : "")}
{props.link ?
<a style={{textDecoration: "underline"}}
target="_blank"
rel="noopener noreferrer"
href={props.link}>
{chrome.i18n.getMessage("LearnMore")}
</a>
: (props.linkOnClick ?
<a style={{textDecoration: "underline", marginLeft: "5px", cursor: "pointer"}}
onClick={props.linkOnClick}>
{chrome.i18n.getMessage("LearnMore")}
</a>
: null)}
</span>
: null}
{this.getButtons(props.buttons)}
</div>
{props.showGotIt ?
<button className="sponsorSkipObject sponsorSkipNoticeButton"
style ={{float: "right" }}
onClick={() => this.close()}>
{chrome.i18n.getMessage("GotIt")}
</button>
: null}
</div>
)
}
getButtons(buttons?: ButtonListener[]): JSX.Element[] {
if (buttons) {
const result: JSX.Element[] = [];
for (const button of buttons) {
result.push(
<button className="sponsorSkipObject sponsorSkipNoticeButton sponsorSkipNoticeRightButton"
key={button.name}
onClick={(e) => button.listener(e)}>
{button.name}
</button>
)
}
return result;
} else {
return null;
}
}
close(): void {
this.root.unmount();
this.container.remove();
if (this.timer) clearTimeout(this.timer);
super(props, "icons/IconSponsorBlocker256px.png")
}
}