Files
ThreadNet-Web/src/components/views/elements/IRCTimelineProfileResizer.tsx
T

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

90 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-05-13 02:16:43 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-05-13 02:16:43 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
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.
2020-05-13 02:16:43 +01:00
*/
import React from "react";
2021-10-22 17:23:32 -05:00
import SettingsStore from "../../../settings/SettingsStore";
2021-06-29 13:11:58 +01:00
import Draggable, { ILocationState } from "./Draggable";
import { SettingLevel } from "../../../settings/SettingLevel";
2020-05-13 02:16:43 +01:00
interface IProps {
// Current room
roomId: string | null;
2020-06-18 14:32:43 +01:00
minWidth: number;
maxWidth: number;
}
2020-05-13 02:16:43 +01:00
interface IState {
2020-06-18 14:32:43 +01:00
width: number;
IRCLayoutRoot: HTMLElement | null;
2020-06-18 14:32:43 +01:00
}
2020-05-13 02:16:43 +01:00
export default class IRCTimelineProfileResizer extends React.Component<IProps, IState> {
public constructor(props: IProps) {
2020-05-13 02:16:43 +01:00
super(props);
this.state = {
width: SettingsStore.getValue("ircDisplayNameWidth", this.props.roomId),
IRCLayoutRoot: null,
2020-06-18 14:32:43 +01:00
};
}
2020-05-13 02:16:43 +01:00
public componentDidMount(): void {
2020-05-13 02:16:43 +01:00
this.setState(
{
IRCLayoutRoot: document.querySelector(".mx_IRCLayout"),
2020-06-18 14:32:43 +01:00
},
() => this.updateCSSWidth(this.state.width),
);
2020-05-13 02:16:43 +01:00
}
private dragFunc = (location: ILocationState, event: MouseEvent): ILocationState => {
2020-05-13 02:16:43 +01:00
const offset = event.clientX - location.currentX;
const newWidth = this.state.width + offset;
// If we're trying to go smaller than min width, don't.
2020-05-13 14:04:46 +01:00
if (newWidth < this.props.minWidth) {
2020-05-13 02:16:43 +01:00
return location;
}
2020-05-13 14:04:46 +01:00
if (newWidth > this.props.maxWidth) {
2020-05-13 02:16:43 +01:00
return location;
}
this.setState({
width: newWidth,
});
this.updateCSSWidth.bind(this)(newWidth);
return {
currentX: event.clientX,
currentY: location.currentY,
2020-06-18 14:32:43 +01:00
};
};
2020-05-13 02:16:43 +01:00
private updateCSSWidth(newWidth: number): void {
this.state.IRCLayoutRoot?.style.setProperty("--name-width", newWidth + "px");
2020-05-13 02:16:43 +01:00
}
private onMoueUp = (): void => {
2020-05-18 16:37:10 +01:00
if (this.props.roomId) {
SettingsStore.setValue(
"ircDisplayNameWidth",
this.props.roomId,
SettingLevel.ROOM_DEVICE,
this.state.width,
);
2020-05-18 16:37:10 +01:00
}
};
2020-05-13 02:16:43 +01:00
public render(): React.ReactNode {
2020-06-18 14:32:43 +01:00
return <Draggable className="mx_ProfileResizer" dragFunc={this.dragFunc} onMouseUp={this.onMoueUp} />;
2020-05-13 02:16:43 +01:00
}
2020-06-18 14:32:43 +01:00
}