summaryrefslogtreecommitdiff
path: root/demo/components/file-picker/index.tsx
blob: b88aad46c1d8ef1025128471690d2bff4a5de319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React, { CSSProperties, FC, HTMLAttributes, InputHTMLAttributes, useEffect, useRef, useState } from 'react';

const supportsInputDirs = 'webkitdirectory' in HTMLInputElement.prototype;
const supportsRelativePath = 'webkitRelativePath' in File.prototype;
const supportsDirs = typeof DataTransferItem != 'undefined' && 'webkitGetAsEntry' in DataTransferItem.prototype;

const readRecurse = (dir: FileSystemDirectoryEntry, onComplete: (files: File[]) => void, onError: (err: Error) => void) => {
  let files: File[] = [];
  let total = 0;
  let errored = false;
  let reachedEnd = false;
  const onErr = (err: Error) => {
    if (!errored) {
      errored = true;
      onError(err);
    }
  };
  const onDone = (f: File[]) => {
    files = files.concat(f);
    if (!--total && reachedEnd) onComplete(files);
  };
  const reader = dir.createReader();
  const onRead = (entries: FileSystemEntry[]) => {
    if (!entries.length && !errored) {
      if (!total) onComplete(files);
      else reachedEnd = true;
    } else reader.readEntries(onRead, onError);
    for (const entry of entries) {
      ++total;
      if (entry.isFile) entry.file(f => onDone([
        new File([f], entry.fullPath.slice(1), f)
      ]), onErr);
      else readRecurse(entry as FileSystemDirectoryEntry, onDone, onErr);
    }
  };
  reader.readEntries(onRead, onError);
}

const FilePicker: FC<{
  onFiles(files: File[] | null): void;
  onDrag(on: boolean): void;
  onError(err: string | Error): void;
  allowDirs: boolean;
} & Omit<HTMLAttributes<HTMLDivElement>, 'onError'>
> = ({ onFiles, onDrag, onError, style, allowDirs, children, ...props }) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const dirInputRef = useRef<HTMLInputElement>(null);
  const dragRef = useRef(0);
  const [inputHover, setInputHover] = useState(false);
  const [dirInputHover, setDirInputHover] = useState(false);
  const [isHovering, setIsHovering] = useState(false);
  useEffect(() => {
    // only init'd when support dirs
    if (dirInputRef.current) {
      dirInputRef.current.setAttribute('webkitdirectory', '');
    }
  }, []);
  const rootProps: HTMLAttributes<HTMLDivElement> = {
    onDrop(ev) {
      ev.preventDefault();
      const tf = ev.dataTransfer;
      if (!tf.files.length) onError('Please drop some files in');
      else {
        onFiles(null);
        if (supportsDirs && allowDirs) {
          let outFiles: File[] = [];
          let lft = tf.items.length;
          let errored = false;
          const onErr = (err: Error) => {
            if (!errored) {
              errored = true;
              onError(err);
            }
          }
          const onDone = (f: File[]) => {
            outFiles = outFiles.concat(f);
            if (!--lft && !errored) onFiles(outFiles);
          };
          for (let i = 0; i < tf.items.length; ++i) {
            const entry = tf.items[i].webkitGetAsEntry();
            if (entry.isFile) entry.file(f => onDone([f]), onErr);
            else readRecurse(entry as FileSystemDirectoryEntry, onDone, onErr);
          }
        } else onFiles(Array.prototype.slice.call(tf.files));
      }
      setIsHovering(false);
    },
    onDragEnter() {
      ++dragRef.current;
      onDrag(true);
      setIsHovering(true);
    },
    onDragOver(ev) {
      ev.preventDefault();
    },
    onDragLeave() {
      if (!--dragRef.current) {
        onDrag(false);  
        setIsHovering(false);
      }
    },
    style: {
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center',
      ...style
    }
  };
  const inputProps: InputHTMLAttributes<HTMLInputElement> = {
    onInput(ev) {
      const t = ev.currentTarget, files = t.files!;
      if (supportsRelativePath) {
        const outFiles: File[] = Array(files.length);
        for (let i = 0; i < files.length; ++i) {
          const file = files[i];
          outFiles[i] = new File([file], file.webkitRelativePath || file.name, file);
        }
        onFiles(outFiles);
      } else onFiles(Array.prototype.slice.call(files));
      t.value = '';
    },
    style: { display: 'none' },
    multiple: true
  };
  const buttonStyles: CSSProperties = {
    cursor: 'default',
    minWidth: '8vw',
    height: '6vh',
    margin: '1vmin',
    padding: '1vmin',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    boxShadow: '0 1px 2px 1px rgba(0, 0, 0, 0.2), 0 2px 4px 2px rgba(0, 0, 0, 0.15), 0 4px 8px 4px rgba(0, 0, 0, 0.12)',
    border: '1px solid black',
    borderRadius: '6px',
    transition: 'background-color 300ms ease-in-out',
    WebkitTouchCallout: 'none',
    WebkitUserSelect: 'none',
    msUserSelect: 'none',
    MozUserSelect: 'none',
    userSelect: 'none'
  };
  return (
    <div {...props} {...rootProps}>
      {children}
      <div style={{
        transition: 'transform ' + (isHovering ? 300 : 50) + 'ms ease-in-out',
        transform: isHovering ? 'scale(1.5)' : 'none'
      }}>Drag and Drop</div>
      <div style={{
        borderBottom: '1px solid gray',
        margin: '1.5vh',
        color: 'gray',
        lineHeight: 0,
        paddingTop: '1.5vh',
        marginBottom: '3vh',
        width: '100%',
      }}>
        <span style={{ background: 'white', padding: '0.25em' }}>OR</span>
      </div>
      <div style={{
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
        width: '100%'
      }}>
        <input type="file" ref={inputRef} {...inputProps} />
        <div onClick={() => inputRef.current!.click()} onMouseOver={() => setInputHover(true)} onMouseOut={() => setInputHover(false)} style={{
          ...buttonStyles,
          backgroundColor: inputHover ? 'rgba(0, 0, 0, 0.14)' : 'white'
        }}>Select Files</div>
        {supportsInputDirs && allowDirs &&
          <>
            <div style={{ boxShadow: '1px 0 black', height: '100%' }}><span /></div>
            <input type="file" ref={dirInputRef} {...inputProps} />
            <div onClick={() => dirInputRef.current!.click()} onMouseOver={() => setDirInputHover(true)} onMouseOut={() => setDirInputHover(false)} style={{
              ...buttonStyles,
              marginLeft: '8vmin',
              backgroundColor: dirInputHover ? 'rgba(0, 0, 0, 0.14)' : 'white'
            }}>Select Folders</div>
          </>
        }
      </div>
    </div>
  );
}

export default FilePicker;