2018-10-17 11:38:25 +02:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2018-2020 The Matrix.org Foundation C.I.C.
|
2018-10-17 11:38:25 +02:00
|
|
|
|
2025-01-06 11:18:54 +00: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.
|
2018-10-17 11:38:25 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { throttle } from "lodash";
|
2020-10-13 16:11:58 +01:00
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import type FixedDistributor from "./distributors/fixed";
|
|
|
|
|
import type ResizeItem from "./item";
|
|
|
|
|
import type Sizer from "./sizer";
|
2020-10-12 16:47:04 +01:00
|
|
|
|
|
|
|
|
interface IClassNames {
|
2018-10-16 15:16:10 +02:00
|
|
|
// class on resize-handle
|
2020-10-12 16:47:04 +01:00
|
|
|
handle?: string;
|
2018-10-16 15:16:10 +02:00
|
|
|
// class on resize-handle
|
2020-10-12 16:47:04 +01:00
|
|
|
reverse?: string;
|
2018-10-16 15:16:10 +02:00
|
|
|
// class on resize-handle
|
2020-10-12 16:47:04 +01:00
|
|
|
vertical?: string;
|
2018-10-16 15:16:10 +02:00
|
|
|
// class on container
|
2020-10-12 16:47:04 +01:00
|
|
|
resizing?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IConfig {
|
|
|
|
|
onResizeStart?(): void;
|
|
|
|
|
onResizeStop?(): void;
|
2023-02-24 15:28:40 +00:00
|
|
|
onResized?(size: number | null, id: string | null, element: HTMLElement): void;
|
2021-08-24 13:05:46 +02:00
|
|
|
handler?: HTMLDivElement;
|
2020-10-12 16:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-07 13:37:26 +01:00
|
|
|
export default class Resizer<C extends IConfig, I extends ResizeItem<C> = ResizeItem<C>> {
|
2020-10-12 16:47:04 +01:00
|
|
|
private classNames: IClassNames;
|
2018-12-19 23:51:19 +01:00
|
|
|
|
|
|
|
|
// TODO move vertical/horizontal to config option/container class
|
|
|
|
|
// as it doesn't make sense to mix them within one container/Resizer
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(
|
2023-04-17 08:31:58 +01:00
|
|
|
public container: HTMLElement | null,
|
2020-10-12 16:47:04 +01:00
|
|
|
private readonly distributorCtor: {
|
2023-07-07 13:37:26 +01:00
|
|
|
new (item: I): FixedDistributor<C, I>;
|
|
|
|
|
createItem(resizeHandle: HTMLDivElement, resizer: Resizer<C, I>, sizer: Sizer, container?: HTMLElement): I;
|
2023-04-17 08:31:58 +01:00
|
|
|
createSizer(containerElement: HTMLElement | null, vertical: boolean, reverse: boolean): Sizer;
|
2020-10-12 16:47:04 +01:00
|
|
|
},
|
|
|
|
|
public readonly config?: C,
|
|
|
|
|
) {
|
2018-10-16 15:16:10 +02:00
|
|
|
this.classNames = {
|
|
|
|
|
handle: "resizer-handle",
|
|
|
|
|
reverse: "resizer-reverse",
|
|
|
|
|
vertical: "resizer-vertical",
|
|
|
|
|
resizing: "resizer-resizing",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public setClassNames(classNames: IClassNames): void {
|
2018-10-16 15:16:10 +02:00
|
|
|
this.classNames = classNames;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public attach(): void {
|
2021-08-24 16:06:10 +02:00
|
|
|
const attachment = this?.config?.handler?.parentElement ?? this.container;
|
2023-04-17 08:31:58 +01:00
|
|
|
attachment?.addEventListener("mousedown", this.onMouseDown, false);
|
2020-10-13 16:11:58 +01:00
|
|
|
window.addEventListener("resize", this.onResize);
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public detach(): void {
|
2021-08-24 18:22:06 +02:00
|
|
|
const attachment = this?.config?.handler?.parentElement ?? this.container;
|
2023-04-17 08:31:58 +01:00
|
|
|
attachment?.removeEventListener("mousedown", this.onMouseDown, false);
|
2020-10-13 16:11:58 +01:00
|
|
|
window.removeEventListener("resize", this.onResize);
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 13:45:56 +02:00
|
|
|
/**
|
|
|
|
|
Gives the distributor for a specific resize handle, as if you would have started
|
|
|
|
|
to drag that handle. Can be used to manipulate the size of an item programmatically.
|
|
|
|
|
@param {number} handleIndex the index of the resize handle in the container
|
2020-10-20 11:03:03 +01:00
|
|
|
@return {FixedDistributor} a new distributor for the given handle
|
2018-10-17 13:45:56 +02:00
|
|
|
*/
|
2023-07-07 13:37:26 +01:00
|
|
|
public forHandleAt(handleIndex: number): FixedDistributor<C, I> | undefined {
|
2020-10-12 16:47:04 +01:00
|
|
|
const handles = this.getResizeHandles();
|
2018-10-16 17:22:12 +02:00
|
|
|
const handle = handles[handleIndex];
|
2018-11-26 16:46:27 +01:00
|
|
|
if (handle) {
|
2021-06-29 13:11:58 +01:00
|
|
|
const { distributor } = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
2018-11-26 16:46:27 +01:00
|
|
|
return distributor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 13:37:26 +01:00
|
|
|
public forHandleWithId(id: string): FixedDistributor<C, I> | undefined {
|
2020-10-12 16:47:04 +01:00
|
|
|
const handles = this.getResizeHandles();
|
2018-11-26 16:46:27 +01:00
|
|
|
const handle = handles.find((h) => h.getAttribute("data-id") === id);
|
|
|
|
|
if (handle) {
|
2021-06-29 13:11:58 +01:00
|
|
|
const { distributor } = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
2018-11-26 16:46:27 +01:00
|
|
|
return distributor;
|
|
|
|
|
}
|
2018-10-16 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 16:47:04 +01:00
|
|
|
public isReverseResizeHandle(el: HTMLElement): boolean {
|
2023-02-13 17:01:43 +00:00
|
|
|
return el.classList.contains(this.classNames.reverse!);
|
2018-12-19 23:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 16:47:04 +01:00
|
|
|
public isResizeHandle(el: HTMLElement): boolean {
|
2023-02-13 17:01:43 +00:00
|
|
|
return el.classList.contains(this.classNames.handle!);
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onMouseDown = (event: MouseEvent): void => {
|
2023-04-11 11:43:11 +05:30
|
|
|
const LEFT_MOUSE_BUTTON = 0;
|
|
|
|
|
if (event.button !== LEFT_MOUSE_BUTTON) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-18 14:27:10 +01:00
|
|
|
// use closest in case the resize handle contains
|
|
|
|
|
// child dom nodes that can be the target
|
2020-10-20 11:03:03 +01:00
|
|
|
const resizeHandle = event.target && (<HTMLDivElement>event.target).closest(`.${this.classNames.handle}`);
|
2021-08-24 13:05:46 +02:00
|
|
|
const hasHandler = this?.config?.handler;
|
2021-11-02 12:48:50 +01:00
|
|
|
// prevent that stacked resizer's are both activated with one mouse event
|
|
|
|
|
// (this is possible because the mouse events are connected to the containers not the handles)
|
|
|
|
|
if (
|
|
|
|
|
!resizeHandle || // if no resizeHandle exist / mouse event hit the container not the handle
|
|
|
|
|
(!hasHandler && resizeHandle.parentElement !== this.container) || // no handler from config -> check if the containers match
|
|
|
|
|
(hasHandler && resizeHandle !== hasHandler)
|
|
|
|
|
) {
|
|
|
|
|
// handler from config -> check if the handlers match
|
2018-10-16 15:16:10 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2021-11-02 12:48:50 +01:00
|
|
|
|
2018-10-16 15:16:10 +02:00
|
|
|
// prevent starting a drag operation
|
|
|
|
|
event.preventDefault();
|
2018-11-26 16:46:27 +01:00
|
|
|
|
2018-10-16 15:16:10 +02:00
|
|
|
// mark as currently resizing
|
|
|
|
|
if (this.classNames.resizing) {
|
2021-09-22 08:29:04 +02:00
|
|
|
this.container?.classList?.add(this.classNames.resizing);
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|
2023-02-13 17:01:43 +00:00
|
|
|
this.config?.onResizeStart?.();
|
2018-10-16 15:16:10 +02:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
const { sizer, distributor } = this.createSizerAndDistributor(<HTMLDivElement>resizeHandle);
|
2019-01-11 17:17:58 +01:00
|
|
|
distributor.start();
|
2018-10-16 15:16:10 +02:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const onMouseMove = (event: MouseEvent): void => {
|
2018-10-16 15:16:10 +02:00
|
|
|
const offset = sizer.offsetFromEvent(event);
|
2018-11-26 16:42:58 +01:00
|
|
|
distributor.resizeFromContainerOffset(offset);
|
2018-10-16 15:16:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const body = document.body;
|
2023-01-12 13:25:14 +00:00
|
|
|
const finishResize = (): void => {
|
2018-10-16 15:16:10 +02:00
|
|
|
if (this.classNames.resizing) {
|
2021-09-22 08:29:04 +02:00
|
|
|
this.container?.classList?.remove(this.classNames.resizing);
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|
2020-10-13 12:28:23 +01:00
|
|
|
distributor.finish();
|
2023-02-13 17:01:43 +00:00
|
|
|
this.config?.onResizeStop?.();
|
2019-01-11 18:20:34 +01:00
|
|
|
body.removeEventListener("mouseup", finishResize, false);
|
|
|
|
|
document.removeEventListener("mouseleave", finishResize, false);
|
2018-10-16 15:16:10 +02:00
|
|
|
body.removeEventListener("mousemove", onMouseMove, false);
|
|
|
|
|
};
|
2019-01-11 18:20:34 +01:00
|
|
|
body.addEventListener("mouseup", finishResize, false);
|
|
|
|
|
document.addEventListener("mouseleave", finishResize, false);
|
2018-10-16 15:16:10 +02:00
|
|
|
body.addEventListener("mousemove", onMouseMove, false);
|
2020-10-12 16:47:04 +01:00
|
|
|
};
|
2018-10-16 16:25:00 +02:00
|
|
|
|
2020-10-13 16:11:58 +01:00
|
|
|
private onResize = throttle(
|
|
|
|
|
() => {
|
2020-10-15 11:14:48 +01:00
|
|
|
const distributors = this.getDistributors();
|
2020-10-13 16:11:58 +01:00
|
|
|
|
|
|
|
|
// relax all items if they had any overconstrained flexboxes
|
|
|
|
|
distributors.forEach((d) => d.start());
|
|
|
|
|
distributors.forEach((d) => d.finish());
|
2021-06-29 13:11:58 +01:00
|
|
|
},
|
|
|
|
|
100,
|
|
|
|
|
{ trailing: true, leading: true },
|
|
|
|
|
);
|
2020-10-13 16:11:58 +01:00
|
|
|
|
2023-07-07 13:37:26 +01:00
|
|
|
public getDistributors = (): FixedDistributor<C, I>[] => {
|
2020-10-15 11:14:48 +01:00
|
|
|
return this.getResizeHandles().map((handle) => {
|
2021-06-29 13:11:58 +01:00
|
|
|
const { distributor } = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
2020-10-15 11:14:48 +01:00
|
|
|
return distributor;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-12 16:47:04 +01:00
|
|
|
private createSizerAndDistributor(resizeHandle: HTMLDivElement): {
|
|
|
|
|
sizer: Sizer;
|
2023-07-07 13:37:26 +01:00
|
|
|
distributor: FixedDistributor<C, I>;
|
2020-10-12 16:47:04 +01:00
|
|
|
} {
|
2023-02-13 17:01:43 +00:00
|
|
|
const vertical = resizeHandle.classList.contains(this.classNames.vertical!);
|
2018-12-19 23:51:19 +01:00
|
|
|
const reverse = this.isReverseResizeHandle(resizeHandle);
|
2019-01-14 20:24:54 +01:00
|
|
|
const Distributor = this.distributorCtor;
|
2023-02-13 17:01:43 +00:00
|
|
|
const useItemContainer = this.config?.handler ? this.container : undefined;
|
2019-01-14 20:24:54 +01:00
|
|
|
const sizer = Distributor.createSizer(this.container, vertical, reverse);
|
2023-04-17 08:31:58 +01:00
|
|
|
const item = Distributor.createItem(resizeHandle, this, sizer, useItemContainer ?? undefined);
|
2020-10-12 16:47:04 +01:00
|
|
|
const distributor = new Distributor(item);
|
2021-06-29 13:11:58 +01:00
|
|
|
return { sizer, distributor };
|
2018-10-16 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 22:04:37 +01:00
|
|
|
private getResizeHandles(): HTMLElement[] {
|
2021-08-24 13:05:46 +02:00
|
|
|
if (this?.config?.handler) {
|
|
|
|
|
return [this.config.handler];
|
|
|
|
|
}
|
2021-09-22 08:29:04 +02:00
|
|
|
if (!this.container?.children) return [];
|
2022-05-03 22:04:37 +01:00
|
|
|
return Array.from(this.container.querySelectorAll(`.${this.classNames.handle}`));
|
2018-10-16 17:22:12 +02:00
|
|
|
}
|
2018-10-16 15:16:10 +02:00
|
|
|
}
|