@chayns-components/core
Version:
A set of beautiful React components for developing your own applications with chayns.
157 lines (156 loc) • 7.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _chaynsApi = require("chayns-api");
var _react = _interopRequireWildcard(require("react"));
var _file = require("../../utils/file");
var _fileDialog = require("../../utils/fileDialog");
var _useKeyboardFocusHighlighting = require("../../hooks/useKeyboardFocusHighlighting");
var _Icon = _interopRequireDefault(require("../icon/Icon"));
var _FileSelect = require("./FileSelect.styles");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
var DialogView = /*#__PURE__*/function (DialogView) {
DialogView["PIXABAY"] = "pixabay";
DialogView["EDITOR"] = "editor";
return DialogView;
}(DialogView || {});
const FileSelect = ({
fileSelectionIcons = ['fa fa-upload'],
imageSelectIcons = ['ts-image'],
fileTypes,
isDisabled,
maxFileSizeInMB,
onAdd,
fileSelectionPlaceholder = 'Dateien hochladen',
imageSelectPlaceholder,
shouldPreventImageUpload = false,
shouldEnableKeyboardHighlighting
}) => {
const shouldShowKeyboardHighlighting = (0, _useKeyboardFocusHighlighting.useKeyboardFocusHighlighting)(shouldEnableKeyboardHighlighting && !isDisabled);
const handleAddImages = (0, _react.useCallback)(images => {
if (typeof onAdd === 'function') {
onAdd(images);
}
}, [onAdd]);
const handleAddFiles = (0, _react.useCallback)(newFiles => {
if (typeof onAdd === 'function') {
onAdd(newFiles);
}
}, [onAdd]);
const handleOpenEditor = (0, _react.useCallback)(async file => {
const {
buttonType,
result
} = await (0, _chaynsApi.createDialog)({
dialogInput: {
upload: true,
initialView: DialogView.EDITOR,
imageArrayBuffer: file
},
type: _chaynsApi.DialogType.MODULE,
system: {
url: 'https://tapp.chayns-static.space/api/dialog-image-editor/v1/remoteEntry.js',
scope: 'dialog_image_editor',
module: './ImageEditorEntry'
},
buttons: []
}).open();
if (buttonType === 1 && result !== null && result !== void 0 && result.url) handleAddImages([{
url: result.url,
size: file.size,
name: file.name
}]);
}, [handleAddImages]);
const handleImageSelectionClick = (0, _react.useCallback)(async () => {
if (isDisabled) return;
const {
buttonType,
result
} = await (0, _chaynsApi.createDialog)({
dialogInput: {
upload: true,
initialView: DialogView.PIXABAY
},
type: _chaynsApi.DialogType.MODULE,
system: {
url: 'https://tapp.chayns-static.space/api/dialog-image-editor/v1/remoteEntry.js',
scope: 'dialog_image_editor',
module: './ImageEditorEntry'
},
buttons: []
}).open();
if (buttonType === 1 && result !== null && result !== void 0 && result.url) handleAddImages([{
url: result.url
}]);
}, [handleAddImages, isDisabled]);
const handleFileSelectionClick = (0, _react.useCallback)(async () => {
if (isDisabled) return;
const newFiles = await (0, _fileDialog.selectFiles)({
multiple: true,
type: fileTypes,
maxFileSizeInMB
});
if (newFiles.length === 1 && newFiles[0] && newFiles[0].type.startsWith('image') && !shouldPreventImageUpload) {
void handleOpenEditor(newFiles[0]);
return;
}
handleAddFiles(newFiles);
}, [fileTypes, handleAddFiles, handleOpenEditor, isDisabled, maxFileSizeInMB, shouldPreventImageUpload]);
const handleDrop = (0, _react.useCallback)(e => {
e.preventDefault();
const draggedFiles = Array.from(e.dataTransfer.files).filter(file => {
if (fileTypes && !(0, _file.isValidFileType)({
file,
types: fileTypes
})) {
return false;
}
return !(maxFileSizeInMB && file.size > maxFileSizeInMB * 1024 * 1024);
});
if (draggedFiles.length === 1 && draggedFiles[0] && draggedFiles[0].type.startsWith('image') && !shouldPreventImageUpload) {
void handleOpenEditor(draggedFiles[0]);
return;
}
handleAddFiles(draggedFiles);
}, [shouldPreventImageUpload, handleAddFiles, fileTypes, maxFileSizeInMB, handleOpenEditor]);
const handleFileSelectionKeyDown = (0, _react.useCallback)(event => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
void handleFileSelectionClick();
}
}, [handleFileSelectionClick]);
const handleImageSelectionKeyDown = (0, _react.useCallback)(event => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
void handleImageSelectionClick();
}
}, [handleImageSelectionClick]);
return (0, _react.useMemo)(() => /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelect, null, /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelectWrapper, {
$isDisabled: isDisabled
}, /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelectContainer, {
$shouldShowKeyboardHighlighting: shouldShowKeyboardHighlighting,
onClick: () => void handleFileSelectionClick(),
onKeyDown: handleFileSelectionKeyDown,
onDragOver: e => e.preventDefault(),
onDrop: handleDrop,
role: "button",
tabIndex: isDisabled ? -1 : 0
}, /*#__PURE__*/_react.default.createElement(_Icon.default, {
icons: fileSelectionIcons
}), /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelectText, null, fileSelectionPlaceholder)), imageSelectPlaceholder && /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelectContainer, {
$isImageSelection: true,
$shouldShowKeyboardHighlighting: shouldShowKeyboardHighlighting,
onClick: () => void handleImageSelectionClick(),
onKeyDown: handleImageSelectionKeyDown,
role: "button",
tabIndex: isDisabled ? -1 : 0
}, /*#__PURE__*/_react.default.createElement(_Icon.default, {
icons: imageSelectIcons
}), /*#__PURE__*/_react.default.createElement(_FileSelect.StyledFileSelectText, null, imageSelectPlaceholder)))), [isDisabled, fileSelectionIcons, fileSelectionPlaceholder, imageSelectPlaceholder, imageSelectIcons, shouldShowKeyboardHighlighting, handleFileSelectionClick, handleDrop, handleFileSelectionKeyDown, handleImageSelectionClick, handleImageSelectionKeyDown]);
};
FileSelect.displayName = 'FileSelect';
var _default = exports.default = FileSelect;
//# sourceMappingURL=FileSelect.js.map