2024-04-24 15:56:11 +00:00
|
|
|
|
import { Root, createRoot } from "react-dom/client";
|
|
|
|
|
import OpenQuizButton from "./OpenQuizButton";
|
2024-04-28 13:25:59 +00:00
|
|
|
|
import { createPortal } from "react-dom";
|
2024-05-04 16:18:56 +00:00
|
|
|
|
import { pollForSelector } from "../shared/pollForSelector";
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ButtonWidget {
|
|
|
|
|
root: Root | undefined;
|
|
|
|
|
element = document.createElement("div");
|
|
|
|
|
|
2024-04-28 13:25:59 +00:00
|
|
|
|
constructor({ quizId, selector, selectorPollingTimeLimit = 60 }: {
|
|
|
|
|
quizId: string;
|
|
|
|
|
selector: string;
|
|
|
|
|
/**
|
|
|
|
|
* In seconds, null - polling disabled
|
|
|
|
|
*/
|
|
|
|
|
selectorPollingTimeLimit?: number | null;
|
|
|
|
|
}) {
|
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
|
if (element) {
|
|
|
|
|
this.root = createRoot(element);
|
|
|
|
|
this.root.render(<OpenQuizButton quizId={quizId} />);
|
2024-04-24 15:56:11 +00:00
|
|
|
|
|
2024-04-28 13:25:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!selectorPollingTimeLimit) {
|
|
|
|
|
console.error(`Не удалось найти элемент ${selector} для вставки виджета`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pollForSelector(selector, selectorPollingTimeLimit, (element) => {
|
|
|
|
|
this.root = createRoot(element);
|
|
|
|
|
this.root.render(<OpenQuizButton quizId={quizId} />);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
if (this.root) this.root.unmount();
|
|
|
|
|
this.element.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ButtonWidgetFixed {
|
|
|
|
|
root: Root | undefined;
|
|
|
|
|
element = document.createElement("div");
|
|
|
|
|
|
|
|
|
|
constructor({ quizId, side }: {
|
|
|
|
|
quizId: string;
|
|
|
|
|
side: "left" | "right";
|
|
|
|
|
}) {
|
2024-04-24 15:56:11 +00:00
|
|
|
|
this.element.style.setProperty("display", "none");
|
|
|
|
|
document.body.appendChild(this.element);
|
|
|
|
|
|
|
|
|
|
this.root = createRoot(this.element);
|
|
|
|
|
|
2024-04-28 13:25:59 +00:00
|
|
|
|
this.root.render(createPortal(
|
2024-04-24 15:56:11 +00:00
|
|
|
|
<OpenQuizButton
|
2024-04-28 13:25:59 +00:00
|
|
|
|
fixedSide={side}
|
2024-04-24 15:56:11 +00:00
|
|
|
|
quizId={quizId}
|
2024-04-28 13:25:59 +00:00
|
|
|
|
/>,
|
|
|
|
|
document.body
|
|
|
|
|
));
|
2024-04-24 15:56:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
if (this.root) this.root.unmount();
|
|
|
|
|
this.element.remove();
|
|
|
|
|
}
|
2024-04-28 13:25:59 +00:00
|
|
|
|
}
|