[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var tslib_1 = require("tslib");
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | var useMountedState_1 = tslib_1.__importDefault(require("./useMountedState"));
|
---|
| 6 | var util_1 = require("./misc/util");
|
---|
| 7 | /*
|
---|
| 8 | const defaultState: DropAreaState = {
|
---|
| 9 | over: false,
|
---|
| 10 | };
|
---|
| 11 | */
|
---|
| 12 | var createProcess = function (options, mounted) { return function (dataTransfer, event) {
|
---|
| 13 | var uri = dataTransfer.getData('text/uri-list');
|
---|
| 14 | if (uri) {
|
---|
| 15 | (options.onUri || util_1.noop)(uri, event);
|
---|
| 16 | return;
|
---|
| 17 | }
|
---|
| 18 | if (dataTransfer.files && dataTransfer.files.length) {
|
---|
| 19 | (options.onFiles || util_1.noop)(Array.from(dataTransfer.files), event);
|
---|
| 20 | return;
|
---|
| 21 | }
|
---|
| 22 | if (dataTransfer.items && dataTransfer.items.length) {
|
---|
| 23 | dataTransfer.items[0].getAsString(function (text) {
|
---|
| 24 | if (mounted) {
|
---|
| 25 | (options.onText || util_1.noop)(text, event);
|
---|
| 26 | }
|
---|
| 27 | });
|
---|
| 28 | }
|
---|
| 29 | }; };
|
---|
| 30 | var createBond = function (process, setOver) { return ({
|
---|
| 31 | onDragOver: function (event) {
|
---|
| 32 | event.preventDefault();
|
---|
| 33 | },
|
---|
| 34 | onDragEnter: function (event) {
|
---|
| 35 | event.preventDefault();
|
---|
| 36 | setOver(true);
|
---|
| 37 | },
|
---|
| 38 | onDragLeave: function () {
|
---|
| 39 | setOver(false);
|
---|
| 40 | },
|
---|
| 41 | onDrop: function (event) {
|
---|
| 42 | event.preventDefault();
|
---|
| 43 | event.persist();
|
---|
| 44 | setOver(false);
|
---|
| 45 | process(event.dataTransfer, event);
|
---|
| 46 | },
|
---|
| 47 | onPaste: function (event) {
|
---|
| 48 | event.persist();
|
---|
| 49 | process(event.clipboardData, event);
|
---|
| 50 | },
|
---|
| 51 | }); };
|
---|
| 52 | var useDropArea = function (options) {
|
---|
| 53 | if (options === void 0) { options = {}; }
|
---|
| 54 | var onFiles = options.onFiles, onText = options.onText, onUri = options.onUri;
|
---|
| 55 | var isMounted = useMountedState_1.default();
|
---|
| 56 | var _a = react_1.useState(false), over = _a[0], setOver = _a[1];
|
---|
| 57 | var process = react_1.useMemo(function () { return createProcess(options, isMounted()); }, [onFiles, onText, onUri]);
|
---|
| 58 | var bond = react_1.useMemo(function () { return createBond(process, setOver); }, [process, setOver]);
|
---|
| 59 | return [bond, { over: over }];
|
---|
| 60 | };
|
---|
| 61 | exports.default = useDropArea;
|
---|