2018-10-30 18:12:21 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-10-08 15:43:57 +01:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2018 New Vector Ltd
|
2018-10-30 18:12:21 +01:00
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2018-10-30 18:12:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-03-08 13:28:07 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2021-09-12 08:40:56 +02:00
|
|
|
import { NumberSize, Resizable } from "re-resizable";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { Direction } from "re-resizable/lib/resizer";
|
|
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
import ResizeNotifier from "../../utils/ResizeNotifier";
|
|
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
resizeNotifier: ResizeNotifier;
|
|
|
|
|
collapsedRhs?: boolean;
|
2021-09-16 11:22:55 +01:00
|
|
|
panel?: JSX.Element;
|
2023-03-08 13:28:07 +00:00
|
|
|
children: ReactNode;
|
2023-06-15 12:57:58 +01:00
|
|
|
/**
|
|
|
|
|
* A unique identifier for this panel split.
|
|
|
|
|
*
|
|
|
|
|
* This is appended to the key used to store the panel size in localStorage, allowing the widths of different
|
|
|
|
|
* panels to be stored.
|
|
|
|
|
*/
|
|
|
|
|
sizeKey?: string;
|
|
|
|
|
/**
|
|
|
|
|
* The size to use for the panel component if one isn't persisted in storage. Defaults to 350.
|
|
|
|
|
*/
|
|
|
|
|
defaultSize: number;
|
2021-09-12 08:40:56 +02:00
|
|
|
}
|
2018-10-30 18:12:21 +01:00
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
export default class MainSplit extends React.Component<IProps> {
|
2023-06-15 12:57:58 +01:00
|
|
|
public static defaultProps = {
|
|
|
|
|
defaultSize: 350,
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
private onResizeStart = (): void => {
|
2020-09-02 12:00:35 +01:00
|
|
|
this.props.resizeNotifier.startResizing();
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
private onResize = (): void => {
|
2020-09-02 12:00:35 +01:00
|
|
|
this.props.resizeNotifier.notifyRightHandleResized();
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-15 12:57:58 +01:00
|
|
|
private get sizeSettingStorageKey(): string {
|
|
|
|
|
let key = "mx_rhs_size";
|
|
|
|
|
if (!!this.props.sizeKey) {
|
|
|
|
|
key += `_${this.props.sizeKey}`;
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
private onResizeStop = (
|
|
|
|
|
event: MouseEvent | TouchEvent,
|
|
|
|
|
direction: Direction,
|
|
|
|
|
elementRef: HTMLElement,
|
|
|
|
|
delta: NumberSize,
|
|
|
|
|
): void => {
|
2020-09-02 12:00:35 +01:00
|
|
|
this.props.resizeNotifier.stopResizing();
|
2023-06-15 12:57:58 +01:00
|
|
|
window.localStorage.setItem(
|
|
|
|
|
this.sizeSettingStorageKey,
|
|
|
|
|
(this.loadSidePanelSize().width + delta.width).toString(),
|
|
|
|
|
);
|
2020-09-02 12:00:35 +01:00
|
|
|
};
|
2018-12-03 10:43:35 +01:00
|
|
|
|
2021-09-12 08:40:56 +02:00
|
|
|
private loadSidePanelSize(): { height: string | number; width: number } {
|
2023-06-15 12:57:58 +01:00
|
|
|
let rhsSize = parseInt(window.localStorage.getItem(this.sizeSettingStorageKey)!, 10);
|
2018-10-30 18:12:21 +01:00
|
|
|
|
2020-07-16 02:08:24 +01:00
|
|
|
if (isNaN(rhsSize)) {
|
2023-06-15 12:57:58 +01:00
|
|
|
rhsSize = this.props.defaultSize;
|
2018-10-31 12:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-16 02:08:24 +01:00
|
|
|
return {
|
|
|
|
|
height: "100%",
|
|
|
|
|
width: rhsSize,
|
|
|
|
|
};
|
2019-12-23 19:26:59 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2018-10-30 18:12:21 +01:00
|
|
|
const bodyView = React.Children.only(this.props.children);
|
|
|
|
|
const panelView = this.props.panel;
|
|
|
|
|
|
2020-03-10 20:09:40 +00:00
|
|
|
const hasResizer = !this.props.collapsedRhs && panelView;
|
|
|
|
|
|
|
|
|
|
let children;
|
|
|
|
|
if (hasResizer) {
|
2020-07-16 02:08:24 +01:00
|
|
|
children = (
|
|
|
|
|
<Resizable
|
2023-06-15 12:57:58 +01:00
|
|
|
key={this.props.sizeKey}
|
2021-09-12 08:40:56 +02:00
|
|
|
defaultSize={this.loadSidePanelSize()}
|
2020-07-16 02:08:24 +01:00
|
|
|
minWidth={264}
|
|
|
|
|
maxWidth="50%"
|
|
|
|
|
enable={{
|
|
|
|
|
top: false,
|
|
|
|
|
right: false,
|
|
|
|
|
bottom: false,
|
|
|
|
|
left: true,
|
|
|
|
|
topRight: false,
|
|
|
|
|
bottomRight: false,
|
|
|
|
|
bottomLeft: false,
|
|
|
|
|
topLeft: false,
|
|
|
|
|
}}
|
2021-09-12 08:40:56 +02:00
|
|
|
onResizeStart={this.onResizeStart}
|
|
|
|
|
onResize={this.onResize}
|
|
|
|
|
onResizeStop={this.onResizeStop}
|
2020-07-16 02:08:24 +01:00
|
|
|
className="mx_RightPanel_ResizeWrapper"
|
2023-05-04 15:19:55 +00:00
|
|
|
handleClasses={{ left: "mx_ResizeHandle--horizontal" }}
|
2020-07-16 02:08:24 +01:00
|
|
|
>
|
2018-10-30 18:12:21 +01:00
|
|
|
{panelView}
|
2020-07-16 02:08:24 +01:00
|
|
|
</Resizable>
|
|
|
|
|
);
|
2018-10-30 18:12:21 +01:00
|
|
|
}
|
2020-03-10 20:09:40 +00:00
|
|
|
|
2020-07-16 02:08:24 +01:00
|
|
|
return (
|
|
|
|
|
<div className="mx_MainSplit">
|
2020-03-10 20:09:40 +00:00
|
|
|
{bodyView}
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2018-10-30 18:12:21 +01:00
|
|
|
}
|
2018-10-31 12:20:10 +01:00
|
|
|
}
|