@functionalcms/svelte-components 4.8.10 → 4.8.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/form/Dropzone.svelte +175 -334
- package/dist/components/form/Dropzone.svelte.d.ts +41 -41
- package/package.json +4 -4
- package/dist/components/form/file.d.ts +0 -7
- package/dist/components/form/file.js +0 -1255
- package/dist/components/form/from-event.d.ts +0 -12
- package/dist/components/form/from-event.js +0 -175
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type FileWithPath } from './file.ts';
|
|
2
|
-
/**
|
|
3
|
-
* Convert a DragEvent's DataTrasfer object to a list of File objects
|
|
4
|
-
* NOTE: If some of the items are folders,
|
|
5
|
-
* everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
|
|
6
|
-
*
|
|
7
|
-
* EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
|
|
8
|
-
* and a list of File objects will be returned.
|
|
9
|
-
*
|
|
10
|
-
* @param evt
|
|
11
|
-
*/
|
|
12
|
-
export declare function fromEvent(evt: Event | any): Promise<(FileWithPath | DataTransferItem)[]>;
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { toFileWithPath } from './file.ts';
|
|
2
|
-
const FILES_TO_IGNORE = [
|
|
3
|
-
// Thumbnail cache files for macOS and Windows
|
|
4
|
-
'.DS_Store', // macOs
|
|
5
|
-
'Thumbs.db' // Windows
|
|
6
|
-
];
|
|
7
|
-
/**
|
|
8
|
-
* Convert a DragEvent's DataTrasfer object to a list of File objects
|
|
9
|
-
* NOTE: If some of the items are folders,
|
|
10
|
-
* everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
|
|
11
|
-
*
|
|
12
|
-
* EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
|
|
13
|
-
* and a list of File objects will be returned.
|
|
14
|
-
*
|
|
15
|
-
* @param evt
|
|
16
|
-
*/
|
|
17
|
-
export async function fromEvent(evt) {
|
|
18
|
-
if (isObject(evt) && isDataTransfer(evt.dataTransfer)) {
|
|
19
|
-
return getDataTransferFiles(evt.dataTransfer, evt.type);
|
|
20
|
-
}
|
|
21
|
-
else if (isChangeEvt(evt)) {
|
|
22
|
-
return getInputFiles(evt);
|
|
23
|
-
}
|
|
24
|
-
else if (Array.isArray(evt) && evt.every(item => 'getFile' in item && typeof item.getFile === 'function')) {
|
|
25
|
-
return getFsHandleFiles(evt);
|
|
26
|
-
}
|
|
27
|
-
return [];
|
|
28
|
-
}
|
|
29
|
-
function isDataTransfer(value) {
|
|
30
|
-
return isObject(value);
|
|
31
|
-
}
|
|
32
|
-
function isChangeEvt(value) {
|
|
33
|
-
return isObject(value) && isObject(value.target);
|
|
34
|
-
}
|
|
35
|
-
function isObject(v) {
|
|
36
|
-
return typeof v === 'object' && v !== null;
|
|
37
|
-
}
|
|
38
|
-
function getInputFiles(evt) {
|
|
39
|
-
return fromList(evt.target.files).map(file => toFileWithPath(file));
|
|
40
|
-
}
|
|
41
|
-
// Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
|
|
42
|
-
async function getFsHandleFiles(handles) {
|
|
43
|
-
const files = await Promise.all(handles.map(h => h.getFile()));
|
|
44
|
-
return files.map(file => toFileWithPath(file));
|
|
45
|
-
}
|
|
46
|
-
async function getDataTransferFiles(dt, type) {
|
|
47
|
-
// IE11 does not support dataTransfer.items
|
|
48
|
-
// See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items#Browser_compatibility
|
|
49
|
-
if (dt.items) {
|
|
50
|
-
const items = fromList(dt.items)
|
|
51
|
-
.filter(item => item.kind === 'file');
|
|
52
|
-
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
|
|
53
|
-
// only 'dragstart' and 'drop' has access to the data (source node)
|
|
54
|
-
if (type !== 'drop') {
|
|
55
|
-
return items;
|
|
56
|
-
}
|
|
57
|
-
const files = await Promise.all(items.map(toFilePromises));
|
|
58
|
-
return noIgnoredFiles(flatten(files));
|
|
59
|
-
}
|
|
60
|
-
return noIgnoredFiles(fromList(dt.files)
|
|
61
|
-
.map(file => toFileWithPath(file)));
|
|
62
|
-
}
|
|
63
|
-
function noIgnoredFiles(files) {
|
|
64
|
-
return files.filter(file => FILES_TO_IGNORE.indexOf(file.name) === -1);
|
|
65
|
-
}
|
|
66
|
-
// IE11 does not support Array.from()
|
|
67
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
|
|
68
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileList
|
|
69
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
|
|
70
|
-
function fromList(items) {
|
|
71
|
-
if (items === null) {
|
|
72
|
-
return [];
|
|
73
|
-
}
|
|
74
|
-
const files = [];
|
|
75
|
-
// tslint:disable: prefer-for-of
|
|
76
|
-
for (let i = 0; i < items.length; i++) {
|
|
77
|
-
const file = items[i];
|
|
78
|
-
files.push(file);
|
|
79
|
-
}
|
|
80
|
-
return files;
|
|
81
|
-
}
|
|
82
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
|
|
83
|
-
function toFilePromises(item) {
|
|
84
|
-
if (typeof item.webkitGetAsEntry !== 'function') {
|
|
85
|
-
return fromDataTransferItem(item);
|
|
86
|
-
}
|
|
87
|
-
const entry = item.webkitGetAsEntry();
|
|
88
|
-
// Safari supports dropping an image node from a different window and can be retrieved using
|
|
89
|
-
// the DataTransferItem.getAsFile() API
|
|
90
|
-
// NOTE: FileSystemEntry.file() throws if trying to get the file
|
|
91
|
-
if (entry && entry.isDirectory) {
|
|
92
|
-
return fromDirEntry(entry);
|
|
93
|
-
}
|
|
94
|
-
return fromDataTransferItem(item, entry);
|
|
95
|
-
}
|
|
96
|
-
function flatten(items) {
|
|
97
|
-
return items.reduce((acc, files) => [
|
|
98
|
-
...acc,
|
|
99
|
-
...(Array.isArray(files) ? flatten(files) : [files])
|
|
100
|
-
], []);
|
|
101
|
-
}
|
|
102
|
-
async function fromDataTransferItem(item, entry) {
|
|
103
|
-
// Check if we're in a secure context; due to a bug in Chrome (as far as we know)
|
|
104
|
-
// the browser crashes when calling this API (yet to be confirmed as a consistent behaviour).
|
|
105
|
-
//
|
|
106
|
-
// See:
|
|
107
|
-
// - https://issues.chromium.org/issues/40186242
|
|
108
|
-
// - https://github.com/react-dropzone/react-dropzone/issues/1397
|
|
109
|
-
if (globalThis.isSecureContext && typeof item.getAsFileSystemHandle === 'function') {
|
|
110
|
-
const h = await item.getAsFileSystemHandle();
|
|
111
|
-
if (h === null) {
|
|
112
|
-
throw new Error(`${item} is not a File`);
|
|
113
|
-
}
|
|
114
|
-
// It seems that the handle can be `undefined` (see https://github.com/react-dropzone/file-selector/issues/120),
|
|
115
|
-
// so we check if it isn't; if it is, the code path continues to the next API (`getAsFile`).
|
|
116
|
-
if (h !== undefined) {
|
|
117
|
-
const file = await h.getFile();
|
|
118
|
-
file.handle = h;
|
|
119
|
-
return toFileWithPath(file);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
const file = item.getAsFile();
|
|
123
|
-
if (!file) {
|
|
124
|
-
throw new Error(`${item} is not a File`);
|
|
125
|
-
}
|
|
126
|
-
const fwp = toFileWithPath(file, entry?.fullPath ?? undefined);
|
|
127
|
-
return fwp;
|
|
128
|
-
}
|
|
129
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry
|
|
130
|
-
async function fromEntry(entry) {
|
|
131
|
-
return entry.isDirectory ? fromDirEntry(entry) : fromFileEntry(entry);
|
|
132
|
-
}
|
|
133
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry
|
|
134
|
-
function fromDirEntry(entry) {
|
|
135
|
-
const reader = entry.createReader();
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
const entries = [];
|
|
138
|
-
function readEntries() {
|
|
139
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader
|
|
140
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries
|
|
141
|
-
reader.readEntries(async (batch) => {
|
|
142
|
-
if (!batch.length) {
|
|
143
|
-
// Done reading directory
|
|
144
|
-
try {
|
|
145
|
-
const files = await Promise.all(entries);
|
|
146
|
-
resolve(files);
|
|
147
|
-
}
|
|
148
|
-
catch (err) {
|
|
149
|
-
reject(err);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
const items = Promise.all(batch.map(fromEntry));
|
|
154
|
-
entries.push(items);
|
|
155
|
-
// Continue reading
|
|
156
|
-
readEntries();
|
|
157
|
-
}
|
|
158
|
-
}, (err) => {
|
|
159
|
-
reject(err);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
readEntries();
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry
|
|
166
|
-
async function fromFileEntry(entry) {
|
|
167
|
-
return new Promise((resolve, reject) => {
|
|
168
|
-
entry.file((file) => {
|
|
169
|
-
const fwp = toFileWithPath(file, entry.fullPath);
|
|
170
|
-
resolve(fwp);
|
|
171
|
-
}, (err) => {
|
|
172
|
-
reject(err);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
}
|