import PropTypes from 'prop-types'; import React from 'react'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import classNames from 'classnames'; import { withTranslation } from 'react-i18next'; import * as IDEActions from '../actions/ide'; import * as FileActions from '../actions/files'; import DownArrowIcon from '../../../images/down-filled-triangle.svg'; import FolderRightIcon from '../../../images/triangle-arrow-right.svg'; import FolderDownIcon from '../../../images/triangle-arrow-down.svg'; import FileIcon from '../../../images/file.svg'; function parseFileName(name) { const nameArray = name.split('.'); if (nameArray.length > 1) { const extension = `.${nameArray[nameArray.length - 1]}`; const baseName = nameArray.slice(0, -1).join(''); const firstLetter = baseName[0]; const lastLetter = baseName[baseName.length - 1]; const middleText = baseName.slice(1, -1); return { baseName, firstLetter, lastLetter, middleText, extension }; } const firstLetter = name[0]; const lastLetter = name[name.length - 1]; const middleText = name.slice(1, -1); return { baseName: name, firstLetter, lastLetter, middleText }; } function FileName({ name }) { const { baseName, firstLetter, lastLetter, middleText, extension } = parseFileName(name); return ( {firstLetter} {baseName.length > 2 && {middleText} } {baseName.length > 1 && {lastLetter} } {extension && {extension} } ); } FileName.propTypes = { name: PropTypes.string.isRequired }; class FileNode extends React.Component { constructor(props) { super(props); this.state = { isOptionsOpen: false, isEditingName: false, isFocused: false, isDeleting: false, updatedName: this.props.name }; } onFocusComponent = () => { this.setState({ isFocused: true }); } onBlurComponent = () => { this.setState({ isFocused: false }); setTimeout(() => { if (!this.state.isFocused) { this.hideFileOptions(); } }, 200); } setUpdatedName = (updatedName) => { this.setState({ updatedName }); } saveUpdatedFileName = () => { const { updatedName } = this.state; const { name, updateFileName, id } = this.props; if (updatedName !== name) { updateFileName(id, updatedName); } } handleFileClick = (event) => { event.stopPropagation(); const { isDeleting } = this.state; const { id, setSelectedFile, name, onClickFile } = this.props; if (name !== 'root' && !isDeleting) { setSelectedFile(id); } // debugger; // eslint-disable-line if (onClickFile) { onClickFile(); } } handleFileNameChange = (event) => { const newName = event.target.value; this.setUpdatedName(newName); } handleFileNameBlur = () => { this.validateFileName(); this.hideEditFileName(); } handleClickRename = () => { this.setUpdatedName(this.props.name); this.showEditFileName(); setTimeout(() => this.fileNameInput.focus(), 0); setTimeout(() => this.hideFileOptions(), 0); } handleClickAddFile = () => { this.props.newFile(this.props.id); setTimeout(() => this.hideFileOptions(), 0); } handleClickAddFolder = () => { this.props.newFolder(this.props.id); setTimeout(() => this.hideFileOptions(), 0); } handleClickUploadFile = () => { this.props.openUploadFileModal(this.props.id); setTimeout(this.hideFileOptions, 0); } handleClickDelete = () => { const prompt = this.props.t('Common.DeleteConfirmation', { name: this.props.name }); if (window.confirm(prompt)) { this.setState({ isDeleting: true }); this.props.resetSelectedFile(this.props.id); setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 100); } } handleKeyPress = (event) => { if (event.key === 'Enter') { this.hideEditFileName(); } } validateFileName = () => { const currentName = this.props.name; const { updatedName } = this.state; const oldFileExtension = currentName.match(/\.[0-9a-z]+$/i); const newFileExtension = updatedName.match(/\.[0-9a-z]+$/i); const hasPeriod = updatedName.match(/\.+/); const hasNoExtension = oldFileExtension && !newFileExtension; const hasExtensionIfFolder = this.props.fileType === 'folder' && hasPeriod; const notSameExtension = oldFileExtension && newFileExtension && oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase(); const hasEmptyFilename = updatedName.trim() === ''; const hasOnlyExtension = newFileExtension && updatedName.trim() === newFileExtension[0]; if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) { this.setUpdatedName(currentName); } else { this.saveUpdatedFileName(); } } toggleFileOptions = (event) => { event.preventDefault(); if (!this.props.canEdit) { return; } if (this.state.isOptionsOpen) { this.setState({ isOptionsOpen: false }); } else { this[`fileOptions-${this.props.id}`].focus(); this.setState({ isOptionsOpen: true }); } } hideFileOptions = () => { this.setState({ isOptionsOpen: false }); } showEditFileName = () => { this.setState({ isEditingName: true }); } hideEditFileName = () => { this.setState({ isEditingName: false }); } showFolderChildren = () => { this.props.showFolderChildren(this.props.id); } hideFolderChildren = () => { this.props.hideFolderChildren(this.props.id); } renderChild = childId => (