From 8f0442876cc0e41863bcac59b5f5bd77d8d04a7e Mon Sep 17 00:00:00 2001 From: Luc Date: Mon, 1 Nov 2021 11:09:35 +0100 Subject: [PATCH] add action menu --- src/components/fileBrowser.jsx | 55 ++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/src/components/fileBrowser.jsx b/src/components/fileBrowser.jsx index 7149955..f6d0d16 100644 --- a/src/components/fileBrowser.jsx +++ b/src/components/fileBrowser.jsx @@ -35,6 +35,8 @@ import ZIP from '../assets/fileTypes/zip.svg' import CaretDown from '../assets/icons/caretDown.svg' import bytesToString from '../utils/bytestring.js' +import { Button } from '../styled.js' +import assert from 'assert' const FBContainer = styled.div` display: flex; @@ -204,7 +206,11 @@ class FileBrowser extends Component { orderAscending: true, files: [], prevPath: "", - transitionFiles: 0 + transitionFiles: 0, + renderMenu: false, + cursorX: 0, + cursorY: 0, + clicked: "" } this.backListener = undefined @@ -220,6 +226,9 @@ class FileBrowser extends Component { // if the component was just created set the path if (this.state.prevPath === "" && this.props.currentPath !== "") return this.setState({ prevPath: this.props.currentPath, files: this.props.files }) + // add click event listener for closing menu + window.addEventListener('click', () => this.setState({ renderMenu: false })) + // the path changed if (this.props.currentPath !== this.state.prevPath) { let direction = 1 @@ -327,13 +336,44 @@ class FileBrowser extends Component { } } + doAction = a => this.props.action(a, this.state.clicked) + openMenu = (e) => { e.preventDefault() - // get position of the cursor - // somehow know the file that was clicked - // open menu on the position of the cursor - // display different options for dir and files? + assert( + typeof e.pageX === "number" + && typeof e.pageY === "number" + && typeof e.target.innerHTML === "string" + && e.target.innerHTML.length > 0 + ) + + this.setState({ + cursorX: e.pageX, + cursorY: e.pageY, + renderMenu: true, + clicked: e.target.innerHTML + }) + } + + renderMenu = () => { + if (this.state.renderMenu) return ( +
this.setState({ renderMenu: false })} + style={{ + position: "fixed", + top: this.state.cursorY - 3, + left: this.state.cursorX + 3, + backgroundColor: "var(--button-color)", + zIndex: 900, + borderRadius: "3px" + }} + > + + + +
+ ) } /** @@ -366,7 +406,7 @@ class FileBrowser extends Component { v.IsDir ? this.updatePath(v.IsDir, v.Name)} onContextMenu={this.openMenu}> { v.Name } : - { v.Name } + { v.Name } } {/* { v.modified.toLocaleString() } */} { !v.IsDir ? bytesToString(v.Size, {}) : "" } @@ -387,6 +427,9 @@ class FileBrowser extends Component { return ( + { + this.renderMenu() + }