1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var react_1 = require("react");
|
---|
5 | var util_1 = require("./misc/util");
|
---|
6 | var createProcess = function (options) { return function (dataTransfer, event) {
|
---|
7 | var uri = dataTransfer.getData('text/uri-list');
|
---|
8 | if (uri) {
|
---|
9 | (options.onUri || util_1.noop)(uri, event);
|
---|
10 | return;
|
---|
11 | }
|
---|
12 | if (dataTransfer.files && dataTransfer.files.length) {
|
---|
13 | (options.onFiles || util_1.noop)(Array.from(dataTransfer.files), event);
|
---|
14 | return;
|
---|
15 | }
|
---|
16 | if (event.clipboardData) {
|
---|
17 | var text = event.clipboardData.getData('text');
|
---|
18 | (options.onText || util_1.noop)(text, event);
|
---|
19 | return;
|
---|
20 | }
|
---|
21 | }; };
|
---|
22 | var useDrop = function (options, args) {
|
---|
23 | if (options === void 0) { options = {}; }
|
---|
24 | if (args === void 0) { args = []; }
|
---|
25 | var onFiles = options.onFiles, onText = options.onText, onUri = options.onUri;
|
---|
26 | var _a = react_1.useState(false), over = _a[0], setOverRaw = _a[1];
|
---|
27 | var setOver = react_1.useCallback(setOverRaw, []);
|
---|
28 | var process = react_1.useMemo(function () { return createProcess(options); }, [onFiles, onText, onUri]);
|
---|
29 | react_1.useEffect(function () {
|
---|
30 | var onDragOver = function (event) {
|
---|
31 | event.preventDefault();
|
---|
32 | setOver(true);
|
---|
33 | };
|
---|
34 | var onDragEnter = function (event) {
|
---|
35 | event.preventDefault();
|
---|
36 | setOver(true);
|
---|
37 | };
|
---|
38 | var onDragLeave = function () {
|
---|
39 | setOver(false);
|
---|
40 | };
|
---|
41 | var onDragExit = function () {
|
---|
42 | setOver(false);
|
---|
43 | };
|
---|
44 | var onDrop = function (event) {
|
---|
45 | event.preventDefault();
|
---|
46 | setOver(false);
|
---|
47 | process(event.dataTransfer, event);
|
---|
48 | };
|
---|
49 | var onPaste = function (event) {
|
---|
50 | process(event.clipboardData, event);
|
---|
51 | };
|
---|
52 | util_1.on(document, 'dragover', onDragOver);
|
---|
53 | util_1.on(document, 'dragenter', onDragEnter);
|
---|
54 | util_1.on(document, 'dragleave', onDragLeave);
|
---|
55 | util_1.on(document, 'dragexit', onDragExit);
|
---|
56 | util_1.on(document, 'drop', onDrop);
|
---|
57 | if (onText) {
|
---|
58 | util_1.on(document, 'paste', onPaste);
|
---|
59 | }
|
---|
60 | return function () {
|
---|
61 | util_1.off(document, 'dragover', onDragOver);
|
---|
62 | util_1.off(document, 'dragenter', onDragEnter);
|
---|
63 | util_1.off(document, 'dragleave', onDragLeave);
|
---|
64 | util_1.off(document, 'dragexit', onDragExit);
|
---|
65 | util_1.off(document, 'drop', onDrop);
|
---|
66 | util_1.off(document, 'paste', onPaste);
|
---|
67 | };
|
---|
68 | }, tslib_1.__spreadArrays([process], args));
|
---|
69 | return { over: over };
|
---|
70 | };
|
---|
71 | exports.default = useDrop;
|
---|