Merge branch 'develop' into fix/2163
This commit is contained in:
commit
fbaabc2633
4 changed files with 92 additions and 49 deletions
|
@ -1,4 +1,4 @@
|
|||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { createRef } from 'react';
|
||||
import styled from 'styled-components/macro';
|
||||
import tw from 'twin.macro';
|
||||
import Fade from '@/components/elements/Fade';
|
||||
|
@ -17,64 +17,91 @@ export const DropdownButtonRow = styled.button<{ danger?: boolean }>`
|
|||
}
|
||||
`;
|
||||
|
||||
const DropdownMenu = ({ renderToggle, children }: Props) => {
|
||||
const menu = useRef<HTMLDivElement>(null);
|
||||
const [ posX, setPosX ] = useState(0);
|
||||
const [ visible, setVisible ] = useState(false);
|
||||
interface State {
|
||||
posX: number;
|
||||
visible: boolean;
|
||||
}
|
||||
|
||||
const onClickHandler = (e: React.MouseEvent<any, MouseEvent>) => {
|
||||
e.preventDefault();
|
||||
class DropdownMenu extends React.PureComponent<Props, State> {
|
||||
menu = createRef<HTMLDivElement>();
|
||||
|
||||
!visible && setPosX(e.clientX);
|
||||
setVisible(s => !s);
|
||||
state: State = {
|
||||
posX: 0,
|
||||
visible: false,
|
||||
};
|
||||
|
||||
const windowListener = (e: MouseEvent) => {
|
||||
if (e.button === 2 || !visible || !menu.current) {
|
||||
return;
|
||||
componentWillUnmount () {
|
||||
this.removeListeners();
|
||||
}
|
||||
|
||||
if (e.target === menu.current || menu.current.contains(e.target as Node)) {
|
||||
return;
|
||||
}
|
||||
componentDidUpdate (prevProps: Readonly<Props>, prevState: Readonly<State>) {
|
||||
const menu = this.menu.current;
|
||||
|
||||
if (e.target !== menu.current && !menu.current.contains(e.target as Node)) {
|
||||
setVisible(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!visible || !menu.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
document.addEventListener('click', windowListener);
|
||||
menu.current.setAttribute(
|
||||
'style', `left: ${Math.round(posX - menu.current.clientWidth)}px`,
|
||||
if (this.state.visible && !prevState.visible && menu) {
|
||||
document.addEventListener('click', this.windowListener);
|
||||
document.addEventListener('contextmenu', this.contextMenuListener);
|
||||
menu.setAttribute(
|
||||
'style', `left: ${Math.round(this.state.posX - menu.clientWidth)}px`,
|
||||
);
|
||||
}
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('click', windowListener);
|
||||
if (!this.state.visible && prevState.visible) {
|
||||
this.removeListeners();
|
||||
}
|
||||
}
|
||||
|
||||
removeListeners = () => {
|
||||
document.removeEventListener('click', this.windowListener);
|
||||
document.removeEventListener('contextmenu', this.contextMenuListener);
|
||||
};
|
||||
}, [ visible ]);
|
||||
|
||||
onClickHandler = (e: React.MouseEvent<any, MouseEvent>) => {
|
||||
e.preventDefault();
|
||||
this.triggerMenu(e.clientX);
|
||||
};
|
||||
|
||||
contextMenuListener = () => this.setState({ visible: false });
|
||||
|
||||
windowListener = (e: MouseEvent) => {
|
||||
const menu = this.menu.current;
|
||||
|
||||
if (e.button === 2 || !this.state.visible || !menu) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.target === menu || menu.contains(e.target as Node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.target !== menu && !menu.contains(e.target as Node)) {
|
||||
this.setState({ visible: false });
|
||||
}
|
||||
};
|
||||
|
||||
triggerMenu = (posX: number) => this.setState(s => ({
|
||||
posX: !s.visible ? posX : s.posX,
|
||||
visible: !s.visible,
|
||||
}));
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
{renderToggle(onClickHandler)}
|
||||
<Fade timeout={150} in={visible} unmountOnExit>
|
||||
{this.props.renderToggle(this.onClickHandler)}
|
||||
<Fade timeout={150} in={this.state.visible} unmountOnExit>
|
||||
<div
|
||||
ref={menu}
|
||||
ref={this.menu}
|
||||
onClick={e => {
|
||||
e.stopPropagation();
|
||||
setVisible(false);
|
||||
this.setState({ visible: false });
|
||||
}}
|
||||
css={tw`absolute bg-white p-2 rounded border border-neutral-700 shadow-lg text-neutral-500 min-w-48`}
|
||||
>
|
||||
{children}
|
||||
{this.props.children}
|
||||
</div>
|
||||
</Fade>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default DropdownMenu;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useState } from 'react';
|
||||
import React, { useRef, useState } from 'react';
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import {
|
||||
faCopy,
|
||||
|
@ -24,6 +24,7 @@ import { FileObject } from '@/api/server/files/loadDirectory';
|
|||
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
||||
import DropdownMenu from '@/components/elements/DropdownMenu';
|
||||
import styled from 'styled-components/macro';
|
||||
import useEventListener from '@/plugins/useEventListener';
|
||||
|
||||
type ModalType = 'rename' | 'move';
|
||||
|
||||
|
@ -46,15 +47,21 @@ const Row = ({ icon, title, ...props }: RowProps) => (
|
|||
);
|
||||
|
||||
export default ({ file }: { file: FileObject }) => {
|
||||
const onClickRef = useRef<DropdownMenu>(null);
|
||||
const [ showSpinner, setShowSpinner ] = useState(false);
|
||||
const [ modal, setModal ] = useState<ModalType | null>(null);
|
||||
|
||||
const { uuid } = useServer();
|
||||
const { mutate } = useFileManagerSwr();
|
||||
const { clearAndAddHttpError, clearFlashes } = useFlash();
|
||||
|
||||
const directory = ServerContext.useStoreState(state => state.files.directory);
|
||||
|
||||
useEventListener(`pterodactyl:files:ctx:${file.uuid}`, (e: CustomEvent) => {
|
||||
if (onClickRef.current) {
|
||||
onClickRef.current.triggerMenu(e.detail);
|
||||
}
|
||||
});
|
||||
|
||||
const doDeletion = () => {
|
||||
clearFlashes('files');
|
||||
|
||||
|
@ -95,6 +102,7 @@ export default ({ file }: { file: FileObject }) => {
|
|||
|
||||
return (
|
||||
<DropdownMenu
|
||||
ref={onClickRef}
|
||||
renderToggle={onClick => (
|
||||
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
|
||||
<FontAwesomeIcon icon={faEllipsisH}/>
|
||||
|
@ -112,9 +120,11 @@ export default ({ file }: { file: FileObject }) => {
|
|||
<Row onClick={() => setModal('rename')} icon={faPencilAlt} title={'Rename'}/>
|
||||
<Row onClick={() => setModal('move')} icon={faLevelUpAlt} title={'Move'}/>
|
||||
</Can>
|
||||
{file.isFile &&
|
||||
<Can action={'file.create'}>
|
||||
<Row onClick={doCopy} icon={faCopy} title={'Copy'}/>
|
||||
</Can>
|
||||
}
|
||||
<Row onClick={doDownload} icon={faFileDownload} title={'Download'}/>
|
||||
<Can action={'file.delete'}>
|
||||
<Row onClick={doDeletion} icon={faTrashAlt} title={'Delete'} $danger/>
|
||||
|
|
|
@ -37,7 +37,13 @@ const FileObjectRow = ({ file }: { file: FileObject }) => {
|
|||
};
|
||||
|
||||
return (
|
||||
<Row key={file.name}>
|
||||
<Row
|
||||
key={file.name}
|
||||
onContextMenu={e => {
|
||||
e.preventDefault();
|
||||
window.dispatchEvent(new CustomEvent(`pterodactyl:files:ctx:${file.uuid}`, { detail: e.clientX }));
|
||||
}}
|
||||
>
|
||||
<NavLink
|
||||
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
|
||||
css={tw`flex flex-1 text-neutral-300 no-underline p-3`}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { useEffect, useRef } from 'react';
|
||||
|
||||
export default (eventName: string, handler: any, element: any = window) => {
|
||||
export default (eventName: string, handler: (e: Event | CustomEvent | UIEvent | any) => void, element: any = window) => {
|
||||
const savedHandler = useRef<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
Loading…
Reference in a new issue