@functionalcms/svelte-components 4.8.4 → 4.8.6

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.
@@ -0,0 +1,376 @@
1
+ <script lang="ts">
2
+ // import { fromEvent } from 'file-selector';
3
+ import {
4
+ fileAccepted,
5
+ fileMatchSize,
6
+ isEvtWithFiles,
7
+ isIeOrEdge,
8
+ isPropagationStopped,
9
+ TOO_MANY_FILES_REJECTION
10
+ } from './dropzone.ts';
11
+ import { onMount, onDestroy, createEventDispatcher } from 'svelte';
12
+
13
+ interface Props {
14
+ accept: string | Array<string>; // = undefined;
15
+ disabled: boolean; // = false;
16
+ maxSize: number; // = Infinity;
17
+ minSize: number; // = 0;
18
+ multiple: boolean; // = true;
19
+ preventDropOnDocument: boolean; // = true;
20
+ noClick: boolean; // = false;
21
+ noKeyboard: boolean; // = false;
22
+ noDrag: boolean; // = false;
23
+ noDragEventsBubbling: boolean; // = false;
24
+ containerClasses: string; // = '';
25
+ containerStyles: string; // = '';
26
+ disableDefaultStyles: boolean; // = false;
27
+ name: string; // = '';
28
+ // inputElement?: boolean; // = undefined;
29
+ required: boolean; // = false;
30
+ }
31
+
32
+ let {
33
+ accept = undefined,
34
+ disabled = false,
35
+ maxSize = Number.MAX_VALUE,
36
+ minSize = 0,
37
+ multiple = true,
38
+ preventDropOnDocument = true,
39
+ noClick = false,
40
+ noKeyboard = false,
41
+ noDrag = false,
42
+ noDragEventsBubbling = false,
43
+ containerClasses = '',
44
+ containerStyles = '',
45
+ disableDefaultStyles = false,
46
+ name = '',
47
+ // inputElement = undefined,
48
+ required = false
49
+ }: Partial<Props> = $props();
50
+
51
+ let inputElement: HTMLInputElement | undefined;
52
+
53
+ let state = $state({
54
+ isFocused: false,
55
+ isFileDialogActive: false,
56
+ isDragActive: false,
57
+ isDragAccept: false,
58
+ isDragReject: false,
59
+ draggedFiles: [],
60
+ acceptedFiles: [],
61
+ fileRejections: []
62
+ });
63
+
64
+ let rootRef: any;
65
+
66
+ function resetState() {
67
+ state.isFileDialogActive = false;
68
+ state.isDragActive = false;
69
+ state.draggedFiles = [];
70
+ state.acceptedFiles = [];
71
+ state.fileRejections = [];
72
+ }
73
+
74
+ // Fn for opening the file dialog programmatically
75
+ function openFileDialog() {
76
+ if (inputElement) {
77
+ inputElement.value = null; // TODO check if null needs to be set
78
+ state.isFileDialogActive = true;
79
+ inputElement.click();
80
+ }
81
+ }
82
+
83
+ // Cb to open the file dialog when SPACE/ENTER occurs on the dropzone
84
+ function onKeyDownCb(event) {
85
+ // Ignore keyboard events bubbling up the DOM tree
86
+ if (!rootRef || !rootRef.isEqualNode(event.target)) {
87
+ return;
88
+ }
89
+
90
+ if (event.keyCode === 32 || event.keyCode === 13) {
91
+ event.preventDefault();
92
+ openFileDialog();
93
+ }
94
+ }
95
+
96
+ // Update focus state for the dropzone
97
+ function onFocusCb() {
98
+ state.isFocused = true;
99
+ }
100
+ function onBlurCb() {
101
+ state.isFocused = false;
102
+ }
103
+
104
+ // Cb to open the file dialog when click occurs on the dropzone
105
+ function onClickCb() {
106
+ if (noClick) {
107
+ return;
108
+ }
109
+
110
+ // In IE11/Edge the file-browser dialog is blocking, therefore, use setTimeout()
111
+ // to ensure React can handle state changes
112
+ // See: https://github.com/react-dropzone/react-dropzone/issues/450
113
+ if (isIeOrEdge()) {
114
+ setTimeout(openFileDialog, 0);
115
+ } else {
116
+ openFileDialog();
117
+ }
118
+ }
119
+
120
+ function onDragEnterCb(event) {
121
+ event.preventDefault();
122
+ stopPropagation(event);
123
+
124
+ dragTargetsRef = [...dragTargetsRef, event.target];
125
+
126
+ if (isEvtWithFiles(event)) {
127
+ Promise.resolve(getFilesFromEvent(event)).then((draggedFiles) => {
128
+ if (isPropagationStopped(event) && !noDragEventsBubbling) {
129
+ return;
130
+ }
131
+
132
+ state.draggedFiles = draggedFiles;
133
+ state.isDragActive = true;
134
+
135
+ dispatch('dragenter', {
136
+ dragEvent: event
137
+ });
138
+ });
139
+ }
140
+ }
141
+
142
+ function onDragOverCb(event) {
143
+ event.preventDefault();
144
+ stopPropagation(event);
145
+
146
+ if (event.dataTransfer) {
147
+ try {
148
+ event.dataTransfer.dropEffect = 'copy';
149
+ } catch {} /* eslint-disable-line no-empty */
150
+ }
151
+
152
+ if (isEvtWithFiles(event)) {
153
+ dispatch('dragover', {
154
+ dragEvent: event
155
+ });
156
+ }
157
+
158
+ return false;
159
+ }
160
+
161
+ function onDragLeaveCb(event) {
162
+ event.preventDefault();
163
+ stopPropagation(event);
164
+
165
+ // Only deactivate once the dropzone and all children have been left
166
+ const targets = dragTargetsRef.filter((target) => rootRef && rootRef.contains(target));
167
+ // Make sure to remove a target present multiple times only once
168
+ // (Firefox may fire dragenter/dragleave multiple times on the same element)
169
+ const targetIdx = targets.indexOf(event.target);
170
+ if (targetIdx !== -1) {
171
+ targets.splice(targetIdx, 1);
172
+ }
173
+ dragTargetsRef = targets;
174
+ if (targets.length > 0) {
175
+ return;
176
+ }
177
+
178
+ state.isDragActive = false;
179
+ state.draggedFiles = [];
180
+
181
+ if (isEvtWithFiles(event)) {
182
+ dispatch('dragleave', {
183
+ dragEvent: event
184
+ });
185
+ }
186
+ }
187
+
188
+ function onDropCb(event) {
189
+ event.preventDefault();
190
+ stopPropagation(event);
191
+
192
+ dragTargetsRef = [];
193
+
194
+ if (isEvtWithFiles(event)) {
195
+ dispatch('filedropped', {
196
+ event
197
+ });
198
+ Promise.resolve(getFilesFromEvent(event)).then((files) => {
199
+ if (isPropagationStopped(event) && !noDragEventsBubbling) {
200
+ return;
201
+ }
202
+
203
+ const acceptedFiles = [];
204
+ const fileRejections = [];
205
+
206
+ files.forEach((file) => {
207
+ const [accepted, acceptError] = fileAccepted(file, accept);
208
+ const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
209
+ if (accepted && sizeMatch) {
210
+ acceptedFiles.push(file);
211
+ } else {
212
+ const errors = [acceptError, sizeError].filter((e) => e);
213
+ fileRejections.push({ file, errors });
214
+ }
215
+ });
216
+
217
+ if (!multiple && acceptedFiles.length > 1) {
218
+ // Reject everything and empty accepted files
219
+ acceptedFiles.forEach((file) => {
220
+ fileRejections.push({ file, errors: [TOO_MANY_FILES_REJECTION] });
221
+ });
222
+ acceptedFiles.splice(0);
223
+ }
224
+
225
+ // Files dropped keep input in sync
226
+ if (event.dataTransfer) {
227
+ inputElement.files = event.dataTransfer.files;
228
+ }
229
+
230
+ state.acceptedFiles = acceptedFiles;
231
+ state.fileRejections = fileRejections;
232
+
233
+ dispatch('drop', {
234
+ acceptedFiles,
235
+ fileRejections,
236
+ event
237
+ });
238
+
239
+ if (fileRejections.length > 0) {
240
+ dispatch('droprejected', {
241
+ fileRejections,
242
+ event
243
+ });
244
+ }
245
+
246
+ if (acceptedFiles.length > 0) {
247
+ dispatch('dropaccepted', {
248
+ acceptedFiles,
249
+ event
250
+ });
251
+ }
252
+ });
253
+ }
254
+ resetState();
255
+ }
256
+
257
+ $: composeHandler = (fn) => (disabled ? null : fn);
258
+
259
+ $: composeKeyboardHandler = (fn) => (noKeyboard ? null : composeHandler(fn));
260
+
261
+ $: composeDragHandler = (fn) => (noDrag ? null : composeHandler(fn));
262
+
263
+ $: defaultPlaceholderString = multiple
264
+ ? "Drag 'n' drop some files here, or click to select files"
265
+ : "Drag 'n' drop a file here, or click to select a file";
266
+
267
+ function stopPropagation(event) {
268
+ if (noDragEventsBubbling) {
269
+ event.stopPropagation();
270
+ }
271
+ }
272
+
273
+ // allow the entire document to be a drag target
274
+ function onDocumentDragOver(event) {
275
+ if (preventDropOnDocument) {
276
+ event.preventDefault();
277
+ }
278
+ }
279
+
280
+ let dragTargetsRef = [];
281
+ function onDocumentDrop(event) {
282
+ if (!preventDropOnDocument) {
283
+ return;
284
+ }
285
+ if (rootRef && rootRef.contains(event.target)) {
286
+ // If we intercepted an event for our instance, let it propagate down to the instance's onDrop handler
287
+ return;
288
+ }
289
+ event.preventDefault();
290
+ dragTargetsRef = [];
291
+ }
292
+
293
+ // Update file dialog active state when the window is focused on
294
+ function onWindowFocus() {
295
+ // Execute the timeout only if the file dialog is opened in the browser
296
+ if (state.isFileDialogActive) {
297
+ setTimeout(() => {
298
+ if (inputElement) {
299
+ const { files } = inputElement;
300
+
301
+ if (!files.length) {
302
+ state.isFileDialogActive = false;
303
+ dispatch('filedialogcancel');
304
+ }
305
+ }
306
+ }, 300);
307
+ }
308
+ }
309
+
310
+ onDestroy(() => {
311
+ // This is critical for canceling the timeout behaviour on `onWindowFocus()`
312
+ inputElement = null;
313
+ });
314
+
315
+ function onInputElementClick(event) {
316
+ event.stopPropagation();
317
+ }
318
+ </script>
319
+
320
+ <svelte:window on:focus={onWindowFocus} on:dragover={onDocumentDragOver} on:drop={onDocumentDrop} />
321
+
322
+ <div
323
+ bind:this={rootRef}
324
+ tabindex="0"
325
+ role="button"
326
+ class="{disableDefaultStyles ? '' : 'dropzone'}
327
+ {containerClasses}"
328
+ style={containerStyles}
329
+ on:keydown={composeKeyboardHandler(onKeyDownCb)}
330
+ on:focus={composeKeyboardHandler(onFocusCb)}
331
+ on:blur={composeKeyboardHandler(onBlurCb)}
332
+ on:click={composeHandler(onClickCb)}
333
+ on:dragenter={composeDragHandler(onDragEnterCb)}
334
+ on:dragover={composeDragHandler(onDragOverCb)}
335
+ on:dragleave={composeDragHandler(onDragLeaveCb)}
336
+ on:drop={composeDragHandler(onDropCb)}
337
+ {...$$restProps}
338
+ >
339
+ <input
340
+ accept={accept?.toString()}
341
+ {multiple}
342
+ {required}
343
+ type="file"
344
+ {name}
345
+ autocomplete="off"
346
+ tabindex="-1"
347
+ on:change={onDropCb}
348
+ on:click={onInputElementClick}
349
+ bind:this={inputElement}
350
+ style="display: none;"
351
+ />
352
+ <slot>
353
+ <p>{defaultPlaceholderString}</p>
354
+ </slot>
355
+ </div>
356
+
357
+ <style>
358
+ .dropzone {
359
+ flex: 1;
360
+ display: flex;
361
+ flex-direction: column;
362
+ align-items: center;
363
+ padding: 20px;
364
+ border-width: 2px;
365
+ border-radius: 2px;
366
+ border-color: #eeeeee;
367
+ border-style: dashed;
368
+ background-color: #fafafa;
369
+ color: #bdbdbd;
370
+ outline: none;
371
+ transition: border 0.24s ease-in-out;
372
+ }
373
+ .dropzone:focus {
374
+ border-color: #2196f3;
375
+ }
376
+ </style>
@@ -0,0 +1,43 @@
1
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
2
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
3
+ $$bindings?: Bindings;
4
+ } & Exports;
5
+ (internal: unknown, props: Props & {
6
+ $$events?: Events;
7
+ $$slots?: Slots;
8
+ }): Exports & {
9
+ $set?: any;
10
+ $on?: any;
11
+ };
12
+ z_$$bindings?: Bindings;
13
+ }
14
+ type $$__sveltets_2_PropsWithChildren<Props, Slots> = Props & (Slots extends {
15
+ default: any;
16
+ } ? Props extends Record<string, never> ? any : {
17
+ children?: any;
18
+ } : {});
19
+ declare const Dropzone: $$__sveltets_2_IsomorphicComponent<$$__sveltets_2_PropsWithChildren<Partial<{
20
+ accept: string | Array<string>;
21
+ disabled: boolean;
22
+ maxSize: number;
23
+ minSize: number;
24
+ multiple: boolean;
25
+ preventDropOnDocument: boolean;
26
+ noClick: boolean;
27
+ noKeyboard: boolean;
28
+ noDrag: boolean;
29
+ noDragEventsBubbling: boolean;
30
+ containerClasses: string;
31
+ containerStyles: string;
32
+ disableDefaultStyles: boolean;
33
+ name: string;
34
+ required: boolean;
35
+ }>, {
36
+ default: {};
37
+ }>, {
38
+ [evt: string]: CustomEvent<any>;
39
+ }, {
40
+ default: {};
41
+ }, {}, "">;
42
+ type Dropzone = InstanceType<typeof Dropzone>;
43
+ export default Dropzone;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Check if the provided file type should be accepted by the input with accept attribute.
3
+ * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-accept
4
+ *
5
+ * Inspired by https://github.com/enyo/dropzone
6
+ *
7
+ * @param file {File} https://developer.mozilla.org/en-US/docs/Web/API/File
8
+ * @param acceptedFiles {string}
9
+ * @returns {boolean}
10
+ */
11
+ export default function (file: any, acceptedFiles: any): any;
12
+ export declare const FILE_INVALID_TYPE = "file-invalid-type";
13
+ export declare const FILE_TOO_LARGE = "file-too-large";
14
+ export declare const FILE_TOO_SMALL = "file-too-small";
15
+ export declare const TOO_MANY_FILES = "too-many-files";
16
+ export declare const getInvalidTypeRejectionErr: (accept: any) => {
17
+ code: string;
18
+ message: string;
19
+ };
20
+ export declare const getTooLargeRejectionErr: (maxSize: any) => {
21
+ code: string;
22
+ message: string;
23
+ };
24
+ export declare const getTooSmallRejectionErr: (minSize: any) => {
25
+ code: string;
26
+ message: string;
27
+ };
28
+ export declare const TOO_MANY_FILES_REJECTION: {
29
+ code: string;
30
+ message: string;
31
+ };
32
+ export declare function fileAccepted(file: any, accept: any): any[];
33
+ export declare function fileMatchSize(file: any, minSize: any, maxSize: any): (boolean | {
34
+ code: string;
35
+ message: string;
36
+ })[] | (boolean | null)[];
37
+ export declare function allFilesAccepted({ files, accept, minSize, maxSize, multiple, }: {
38
+ files: any;
39
+ accept: any;
40
+ minSize: any;
41
+ maxSize: any;
42
+ multiple: any;
43
+ }): any;
44
+ export declare function isPropagationStopped(event: any): any;
45
+ export declare function isEvtWithFiles(event: any): boolean;
46
+ export declare function isKindFile(item: any): boolean;
47
+ export declare function isIeOrEdge(userAgent?: string): boolean;
48
+ /**
49
+ * This is intended to be used to compose event handlers
50
+ * They are executed in order until one of them calls `event.isPropagationStopped()`.
51
+ * Note that the check is done on the first invoke too,
52
+ * meaning that if propagation was stopped before invoking the fns,
53
+ * no handlers will be executed.
54
+ *
55
+ * @param {Function} fns the event hanlder functions
56
+ * @return {Function} the event handler to add to an element
57
+ */
58
+ export declare function composeEventHandlers(...fns: any[]): (event: any, ...args: any[]) => boolean;
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Check if the provided file type should be accepted by the input with accept attribute.
3
+ * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-accept
4
+ *
5
+ * Inspired by https://github.com/enyo/dropzone
6
+ *
7
+ * @param file {File} https://developer.mozilla.org/en-US/docs/Web/API/File
8
+ * @param acceptedFiles {string}
9
+ * @returns {boolean}
10
+ */
11
+ export default function (file, acceptedFiles) {
12
+ if (file && acceptedFiles) {
13
+ const acceptedFilesArray = Array.isArray(acceptedFiles)
14
+ ? acceptedFiles
15
+ : acceptedFiles.split(",");
16
+ const fileName = file.name || "";
17
+ const mimeType = (file.type || "").toLowerCase();
18
+ const baseMimeType = mimeType.replace(/\/.*$/, "");
19
+ return acceptedFilesArray.some((type) => {
20
+ const validType = type.trim().toLowerCase();
21
+ if (validType.charAt(0) === ".") {
22
+ return fileName.toLowerCase().endsWith(validType);
23
+ }
24
+ else if (validType.endsWith("/*")) {
25
+ // This is something like a image/* mime type
26
+ return baseMimeType === validType.replace(/\/.*$/, "");
27
+ }
28
+ return mimeType === validType;
29
+ });
30
+ }
31
+ return true;
32
+ }
33
+ // Error codes
34
+ export const FILE_INVALID_TYPE = "file-invalid-type";
35
+ export const FILE_TOO_LARGE = "file-too-large";
36
+ export const FILE_TOO_SMALL = "file-too-small";
37
+ export const TOO_MANY_FILES = "too-many-files";
38
+ // File Errors
39
+ export const getInvalidTypeRejectionErr = (accept) => {
40
+ accept = Array.isArray(accept) && accept.length === 1 ? accept[0] : accept;
41
+ const messageSuffix = Array.isArray(accept)
42
+ ? `one of ${accept.join(", ")}`
43
+ : accept;
44
+ return {
45
+ code: FILE_INVALID_TYPE,
46
+ message: `File type must be ${messageSuffix}`,
47
+ };
48
+ };
49
+ export const getTooLargeRejectionErr = (maxSize) => {
50
+ return {
51
+ code: FILE_TOO_LARGE,
52
+ message: `File is larger than ${maxSize} bytes`,
53
+ };
54
+ };
55
+ export const getTooSmallRejectionErr = (minSize) => {
56
+ return {
57
+ code: FILE_TOO_SMALL,
58
+ message: `File is smaller than ${minSize} bytes`,
59
+ };
60
+ };
61
+ export const TOO_MANY_FILES_REJECTION = {
62
+ code: TOO_MANY_FILES,
63
+ message: "Too many files",
64
+ };
65
+ // Firefox versions prior to 53 return a bogus MIME type for every file drag, so dragovers with
66
+ // that MIME type will always be accepted
67
+ export function fileAccepted(file, accept) {
68
+ const isAcceptable = file.type === "application/x-moz-file" || accepts(file, accept);
69
+ return [
70
+ isAcceptable,
71
+ isAcceptable ? null : getInvalidTypeRejectionErr(accept),
72
+ ];
73
+ }
74
+ export function fileMatchSize(file, minSize, maxSize) {
75
+ if (isDefined(file.size)) {
76
+ if (isDefined(minSize) && isDefined(maxSize)) {
77
+ if (file.size > maxSize)
78
+ return [false, getTooLargeRejectionErr(maxSize)];
79
+ if (file.size < minSize)
80
+ return [false, getTooSmallRejectionErr(minSize)];
81
+ }
82
+ else if (isDefined(minSize) && file.size < minSize)
83
+ return [false, getTooSmallRejectionErr(minSize)];
84
+ else if (isDefined(maxSize) && file.size > maxSize)
85
+ return [false, getTooLargeRejectionErr(maxSize)];
86
+ }
87
+ return [true, null];
88
+ }
89
+ function isDefined(value) {
90
+ return value !== undefined && value !== null;
91
+ }
92
+ export function allFilesAccepted({ files, accept, minSize, maxSize, multiple, }) {
93
+ if (!multiple && files.length > 1) {
94
+ return false;
95
+ }
96
+ return files.every((file) => {
97
+ const [accepted] = fileAccepted(file, accept);
98
+ const [sizeMatch] = fileMatchSize(file, minSize, maxSize);
99
+ return accepted && sizeMatch;
100
+ });
101
+ }
102
+ // React's synthetic events has event.isPropagationStopped,
103
+ // but to remain compatibility with other libs (Preact) fall back
104
+ // to check event.cancelBubble
105
+ export function isPropagationStopped(event) {
106
+ if (typeof event.isPropagationStopped === "function") {
107
+ return event.isPropagationStopped();
108
+ }
109
+ else if (typeof event.cancelBubble !== "undefined") {
110
+ return event.cancelBubble;
111
+ }
112
+ return false;
113
+ }
114
+ export function isEvtWithFiles(event) {
115
+ if (!event.dataTransfer) {
116
+ return !!event.target && !!event.target.files;
117
+ }
118
+ // https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/types
119
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types#file
120
+ return Array.prototype.some.call(event.dataTransfer.types, (type) => type === "Files" || type === "application/x-moz-file");
121
+ }
122
+ export function isKindFile(item) {
123
+ return typeof item === "object" && item !== null && item.kind === "file";
124
+ }
125
+ function isIe(userAgent) {
126
+ return (userAgent.indexOf("MSIE") !== -1 || userAgent.indexOf("Trident/") !== -1);
127
+ }
128
+ function isEdge(userAgent) {
129
+ return userAgent.indexOf("Edge/") !== -1;
130
+ }
131
+ export function isIeOrEdge(userAgent = window.navigator.userAgent) {
132
+ return isIe(userAgent) || isEdge(userAgent);
133
+ }
134
+ /**
135
+ * This is intended to be used to compose event handlers
136
+ * They are executed in order until one of them calls `event.isPropagationStopped()`.
137
+ * Note that the check is done on the first invoke too,
138
+ * meaning that if propagation was stopped before invoking the fns,
139
+ * no handlers will be executed.
140
+ *
141
+ * @param {Function} fns the event hanlder functions
142
+ * @return {Function} the event handler to add to an element
143
+ */
144
+ export function composeEventHandlers(...fns) {
145
+ return (event, ...args) => fns.some((fn) => {
146
+ if (!isPropagationStopped(event) && fn) {
147
+ fn(event, ...args);
148
+ }
149
+ return isPropagationStopped(event);
150
+ });
151
+ }
@@ -0,0 +1,7 @@
1
+ export declare const COMMON_MIME_TYPES: Map<string, string>;
2
+ export declare function toFileWithPath(file: FileWithPath, path?: string, h?: FileSystemHandle): FileWithPath;
3
+ export interface FileWithPath extends File {
4
+ readonly path?: string;
5
+ readonly handle?: FileSystemFileHandle;
6
+ readonly relativePath?: string;
7
+ }