2021-05-25 11:49:15 +03:00
|
|
|
/*
|
2021-05-25 13:48:45 +03:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2021-05-25 11:49:15 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EventEmitter from "events";
|
2021-07-21 18:39:41 +03:00
|
|
|
// XXX: resize-observer-polyfill has types that now conflict with typescript's
|
|
|
|
// own DOM types: https://github.com/que-etc/resize-observer-polyfill/issues/80
|
|
|
|
// Using require here rather than import is a horrenous workaround. We should
|
|
|
|
// be able to remove the polyfill once Safari 14 is released.
|
|
|
|
const ResizeObserverPolyfill = require('resize-observer-polyfill'); // eslint-disable-line @typescript-eslint/no-var-requires
|
2021-05-25 12:25:36 +03:00
|
|
|
import ResizeObserverEntry from 'resize-observer-polyfill/src/ResizeObserverEntry';
|
2021-05-25 11:49:15 +03:00
|
|
|
|
|
|
|
export enum UI_EVENTS {
|
|
|
|
Resize = "resize"
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ResizeObserverCallbackFunction = (entries: ResizeObserverEntry[]) => void;
|
|
|
|
|
|
|
|
export default class UIStore extends EventEmitter {
|
|
|
|
private static _instance: UIStore = null;
|
|
|
|
|
|
|
|
private resizeObserver: ResizeObserver;
|
|
|
|
|
2021-05-27 14:36:16 +03:00
|
|
|
private uiElementDimensions = new Map<string, DOMRectReadOnly>();
|
|
|
|
private trackedUiElements = new Map<Element, string>();
|
|
|
|
|
2021-05-25 12:25:36 +03:00
|
|
|
public windowWidth: number;
|
2021-05-25 11:49:15 +03:00
|
|
|
public windowHeight: number;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2021-05-25 12:25:36 +03:00
|
|
|
// eslint-disable-next-line no-restricted-properties
|
|
|
|
this.windowWidth = window.innerWidth;
|
|
|
|
// eslint-disable-next-line no-restricted-properties
|
2021-05-25 11:49:15 +03:00
|
|
|
this.windowHeight = window.innerHeight;
|
|
|
|
|
2021-07-21 18:39:41 +03:00
|
|
|
this.resizeObserver = new ResizeObserverPolyfill(this.resizeObserverCallback);
|
2021-05-25 11:49:15 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 14:36:16 +03:00
|
|
|
public getElementDimensions(name: string): DOMRectReadOnly {
|
|
|
|
return this.uiElementDimensions.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public trackElementDimensions(name: string, element: Element): void {
|
|
|
|
this.trackedUiElements.set(element, name);
|
|
|
|
this.resizeObserver.observe(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public stopTrackingElementDimensions(name: string): void {
|
|
|
|
let trackedElement: Element;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:49:15 +03:00
|
|
|
private resizeObserverCallback = (entries: ResizeObserverEntry[]) => {
|
2021-05-27 14:36:16 +03:00
|
|
|
const windowEntry = entries.find(entry => entry.target === document.body);
|
2021-05-25 11:49:15 +03:00
|
|
|
|
2021-05-27 14:36:16 +03:00
|
|
|
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 11:49:15 +03:00
|
|
|
|
|
|
|
this.emit(UI_EVENTS.Resize, entries);
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2021-05-25 11:49:15 +03:00
|
|
|
}
|
2021-05-27 14:36:16 +03:00
|
|
|
|
|
|
|
window.mxUIStore = UIStore.instance;
|