/** * Copyright (c) 2021 OpenLens Authors * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import "./drop-file-input.scss"; import React from "react"; import { autobind, cssNames, IClassName } from "../../utils"; import { observable } from "mobx"; import { observer } from "mobx-react"; import logger from "../../../main/logger"; export interface DropFileInputProps extends React.DOMAttributes { className?: IClassName; disabled?: boolean; onDropFiles(files: File[], meta: DropFileMeta): void; } export interface DropFileMeta { evt: React.DragEvent; } @observer export class DropFileInput extends React.Component { @observable dropAreaActive = false; dragCounter = 0; // Counter preventing firing onDragLeave() too early (https://stackoverflow.com/questions/7110353/html5-dragleave-fired-when-hovering-a-child-element) @autobind() onDragEnter() { this.dragCounter++; this.dropAreaActive = true; } @autobind() onDragLeave() { this.dragCounter--; if (this.dragCounter == 0) { this.dropAreaActive = false; } } @autobind() onDragOver(evt: React.DragEvent) { if (this.props.onDragOver) { this.props.onDragOver(evt); } evt.preventDefault(); // enable onDrop()-callback evt.dataTransfer.dropEffect = "move"; } @autobind() onDrop(evt: React.DragEvent) { if (this.props.onDrop) { this.props.onDrop(evt); } this.dropAreaActive = false; const files = Array.from(evt.dataTransfer.files); if (files.length > 0) { this.props.onDropFiles(files, { evt }); } } render() { const { onDragEnter, onDragLeave, onDragOver, onDrop } = this; const { disabled, className } = this.props; try { const contentElem = React.Children.only(this.props.children) as React.ReactElement>; if (disabled) { return contentElem; } if (React.isValidElement(contentElem)) { const contentElemProps: React.HTMLProps = { className: cssNames("DropFileInput", contentElem.props.className, className, { droppable: this.dropAreaActive, }), onDragEnter, onDragLeave, onDragOver, onDrop, }; return React.cloneElement(contentElem, contentElemProps); } return null; } catch (err) { logger.error(`Error: must contain only single child element`); return this.props.children; } } }