Files
ThreadNet-Web/src/stores/UIStore.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-05-25 09:49:15 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-05-25 11:48:45 +01:00
Copyright 2021 The Matrix.org Foundation C.I.C.
2021-05-25 09:49:15 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-05-25 09:49:15 +01:00
*/
2022-02-18 13:51:27 +00:00
import EventEmitter from "events";
2021-05-25 09:49:15 +01:00
export enum UI_EVENTS {
Resize = "resize",
}
export default class UIStore extends EventEmitter {
private static _instance: UIStore | null = null;
2021-05-25 09:49:15 +01:00
private resizeObserver: ResizeObserver;
2021-05-27 12:36:16 +01:00
private uiElementDimensions = new Map<string, DOMRectReadOnly>();
private trackedUiElements = new Map<Element, string>();
public windowWidth: number;
2021-05-25 09:49:15 +01:00
public windowHeight: number;
public constructor() {
2021-05-25 09:49:15 +01:00
super();
// eslint-disable-next-line no-restricted-properties
this.windowWidth = window.innerWidth;
// eslint-disable-next-line no-restricted-properties
2021-05-25 09:49:15 +01:00
this.windowHeight = window.innerHeight;
2022-02-18 13:51:27 +00:00
this.resizeObserver = new ResizeObserver(this.resizeObserverCallback);
2021-05-25 09:49:15 +01:00
this.resizeObserver.observe(document.body);
}
public static get instance(): UIStore {
if (!UIStore._instance) {
UIStore._instance = new UIStore();
}
return UIStore._instance;
}
public static destroy(): void {
if (UIStore._instance) {
UIStore._instance.resizeObserver.disconnect();
UIStore._instance.removeAllListeners();
UIStore._instance = null;
}
}
public getElementDimensions(name: string): DOMRectReadOnly | undefined {
2021-05-27 12:36:16 +01:00
return this.uiElementDimensions.get(name);
}
2021-05-25 09:49:15 +01:00
2021-05-27 12:36:16 +01:00
public trackElementDimensions(name: string, element: Element): void {
this.trackedUiElements.set(element, name);
this.resizeObserver.observe(element);
}
public stopTrackingElementDimensions(name: string): void {
let trackedElement: Element | undefined;
2021-05-27 12:36:16 +01:00
this.trackedUiElements.forEach((trackedElementName, element) => {
if (trackedElementName === name) {
trackedElement = element;
}
});
if (trackedElement) {
this.resizeObserver.unobserve(trackedElement);
this.uiElementDimensions.delete(name);
this.trackedUiElements.delete(trackedElement);
}
}
public isTrackingElementDimensions(name: string): boolean {
return this.uiElementDimensions.has(name);
}
private resizeObserverCallback = (entries: ResizeObserverEntry[]): void => {
2021-05-27 12:36:16 +01:00
const windowEntry = entries.find((entry) => entry.target === document.body);
if (windowEntry) {
this.windowWidth = windowEntry.contentRect.width;
this.windowHeight = windowEntry.contentRect.height;
}
entries.forEach((entry) => {
const trackedElementName = this.trackedUiElements.get(entry.target);
if (trackedElementName) {
this.uiElementDimensions.set(trackedElementName, entry.contentRect);
this.emit(trackedElementName, UI_EVENTS.Resize, entry);
}
});
2021-05-25 09:49:15 +01:00
this.emit(UI_EVENTS.Resize, entries);
2021-06-29 13:11:58 +01:00
};
2021-05-25 09:49:15 +01:00
}
2021-05-27 12:36:16 +01:00
window.mxUIStore = UIStore.instance;