@umbraco-ui/uui-file-dropzone 1.2.1 → 1.3.0-rc.1

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.
@@ -7,9 +7,8 @@
7
7
  "attributes": [
8
8
  {
9
9
  "name": "accept",
10
- "description": "Accepted filetypes. Will allow all types if empty.",
11
- "type": "string",
12
- "default": "\"false\""
10
+ "description": "Comma-separated list of accepted mime types or file extensions (denoted with a `.`).\nIf this is left empty, it will allow all types.",
11
+ "type": "string"
13
12
  },
14
13
  {
15
14
  "name": "multiple",
@@ -32,9 +31,8 @@
32
31
  {
33
32
  "name": "accept",
34
33
  "attribute": "accept",
35
- "description": "Accepted filetypes. Will allow all types if empty.",
36
- "type": "string",
37
- "default": "\"false\""
34
+ "description": "Comma-separated list of accepted mime types or file extensions (denoted with a `.`).\nIf this is left empty, it will allow all types.",
35
+ "type": "string"
38
36
  },
39
37
  {
40
38
  "name": "multiple",
@@ -52,7 +50,7 @@
52
50
  ],
53
51
  "events": [
54
52
  {
55
- "name": "file-change",
53
+ "name": "change",
56
54
  "description": "fires when the a file has been selected."
57
55
  }
58
56
  ],
@@ -3,5 +3,6 @@ import { UUIFileDropzoneElement } from './uui-file-dropzone.element';
3
3
  export declare class UUIFileDropzoneEvent extends UUIEvent<{
4
4
  files: File[];
5
5
  }, UUIFileDropzoneElement> {
6
- static readonly FILE_CHANGE: string;
6
+ static readonly CHANGE: string;
7
+ constructor(evName: string, eventInit?: any | null);
7
8
  }
package/lib/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './uui-file-dropzone.element';
2
+ export * from './UUIFileDropzoneEvent';
package/lib/index.js CHANGED
@@ -5,9 +5,28 @@ import { UUIEvent } from '@umbraco-ui/uui-base/lib/events';
5
5
  import { LabelMixin } from '@umbraco-ui/uui-base/lib/mixins';
6
6
  import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
7
7
 
8
+ var __defProp$1 = Object.defineProperty;
9
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
8
24
  class UUIFileDropzoneEvent extends UUIEvent {
25
+ constructor(evName, eventInit = {}) {
26
+ super(evName, __spreadValues(__spreadValues({}, { bubbles: true }), eventInit));
27
+ }
9
28
  }
10
- UUIFileDropzoneEvent.FILE_CHANGE = "file-change";
29
+ UUIFileDropzoneEvent.CHANGE = "change";
11
30
 
12
31
  var __defProp = Object.defineProperty;
13
32
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -23,13 +42,37 @@ var __decorateClass = (decorators, target, key, kind) => {
23
42
  let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
24
43
  constructor() {
25
44
  super();
26
- this.accept = "";
45
+ this._acceptedFileExtensions = [];
46
+ this._acceptedMimeTypes = [];
27
47
  this.multiple = false;
28
48
  this.addEventListener("dragenter", this._onDragEnter, false);
29
49
  this.addEventListener("dragleave", this._onDragLeave, false);
30
50
  this.addEventListener("dragover", this._onDragOver, false);
31
51
  this.addEventListener("drop", this._onDrop, false);
32
52
  }
53
+ set accept(value) {
54
+ if (value) {
55
+ const mimetypes = [];
56
+ const fileextensions = [];
57
+ value.split(",").forEach((item) => {
58
+ item = item.trim().toLowerCase();
59
+ if (new RegExp("[a-z]+\\/[a-z*]", "s").test(item)) {
60
+ mimetypes.push(item);
61
+ } else {
62
+ fileextensions.push(item.replace(/^\./, ""));
63
+ }
64
+ });
65
+ this._acceptedMimeTypes = mimetypes;
66
+ this._acceptedFileExtensions = fileextensions;
67
+ } else {
68
+ this._acceptedMimeTypes = [];
69
+ this._acceptedFileExtensions = [];
70
+ }
71
+ }
72
+ /**
73
+ * Opens the native file picker to select a file.
74
+ * @method browse
75
+ */
33
76
  browse() {
34
77
  this._input.click();
35
78
  }
@@ -37,55 +80,31 @@ let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
37
80
  super.connectedCallback();
38
81
  demandCustomElement(this, "uui-symbol-file-dropzone");
39
82
  }
40
- _checkIsItDirectory(dtItem) {
41
- return !dtItem.type ? dtItem.webkitGetAsEntry().isDirectory : false;
42
- }
43
83
  async _getAllFileEntries(dataTransferItemList) {
44
84
  const fileEntries = [];
45
- const queue = [];
46
- for (let i = 0; i < dataTransferItemList.length; i++) {
47
- queue.push(dataTransferItemList[i].webkitGetAsEntry());
48
- }
49
- if (this.accept) {
50
- const acceptList = [];
51
- const wildcards = [];
52
- this.accept.split(",").forEach((item) => {
53
- if (item.includes("*")) {
54
- wildcards.push(item.split("*")[0].trim().toLowerCase());
55
- } else {
56
- acceptList.push(item.trim().toLowerCase());
57
- }
58
- });
59
- while (queue.length > 0) {
60
- const entry = queue.shift();
61
- if (entry.isFile && await this._isAccepted(acceptList, wildcards, entry)) {
62
- fileEntries.push(entry);
63
- } else if (entry.isDirectory) {
64
- fileEntries.push(entry);
65
- queue.push(
66
- ...await this._readAllDirectoryEntries(
67
- entry.createReader()
68
- )
69
- );
70
- }
71
- }
72
- } else {
73
- while (queue.length > 0) {
74
- const entry = queue.shift();
75
- if (entry.isFile) {
76
- fileEntries.push(entry);
77
- } else if (entry.isDirectory) {
78
- fileEntries.push(entry);
79
- queue.push(
80
- ...await this._readAllDirectoryEntries(
81
- entry.createReader()
82
- )
83
- );
85
+ const queue = [...dataTransferItemList];
86
+ while (queue.length > 0) {
87
+ const entry = queue.shift();
88
+ if (entry.kind === "file") {
89
+ const file = entry.getAsFile();
90
+ if (!file)
91
+ continue;
92
+ if (this._isAccepted(file)) {
93
+ fileEntries.push(file);
84
94
  }
95
+ } else if (entry.kind === "directory") {
96
+ if ("webkitGetAsEntry" in entry === false)
97
+ continue;
98
+ const directory = entry.webkitGetAsEntry();
99
+ queue.push(
100
+ ...await this._readAllDirectoryEntries(directory.createReader())
101
+ );
85
102
  }
86
103
  }
87
104
  return fileEntries;
88
105
  }
106
+ // Get all the entries (files or sub-directories) in a directory
107
+ // by calling readEntries until it returns empty array
89
108
  async _readAllDirectoryEntries(directoryReader) {
90
109
  const entries = [];
91
110
  let readEntries = await this._readEntriesPromise(directoryReader);
@@ -104,23 +123,21 @@ let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
104
123
  console.log(err);
105
124
  }
106
125
  }
107
- async _getFile(fileEntry) {
108
- return await new Promise(
109
- (resolve, reject) => fileEntry.file(resolve, reject)
110
- );
111
- }
112
- async _isAccepted(acceptList, wildcards, entry) {
113
- const file = await this._getFile(entry);
114
- const fileType = file.type.toLowerCase();
115
- const fileExtension = "." + file.name.split(".")[1].toLowerCase();
116
- if (acceptList.includes(fileExtension)) {
126
+ _isAccepted(file) {
127
+ if (this._acceptedFileExtensions.length === 0 && this._acceptedMimeTypes.length === 0) {
117
128
  return true;
118
129
  }
119
- if (acceptList.includes(fileType)) {
130
+ const fileType = file.type.toLowerCase();
131
+ const fileExtension = file.name.split(".").pop();
132
+ if (fileExtension && this._acceptedFileExtensions.includes(fileExtension.toLowerCase())) {
120
133
  return true;
121
134
  }
122
- if (wildcards.some((wildcard) => fileType.startsWith(wildcard))) {
123
- return true;
135
+ for (const mimeType in this._acceptedMimeTypes) {
136
+ if (fileType === mimeType) {
137
+ return true;
138
+ } else if (mimeType.endsWith("/*") && fileType.startsWith(mimeType.replace("/*", ""))) {
139
+ return true;
140
+ }
124
141
  }
125
142
  return false;
126
143
  }
@@ -131,11 +148,11 @@ let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
131
148
  const items = (_a = e.dataTransfer) == null ? void 0 : _a.items;
132
149
  if (items) {
133
150
  let result = await this._getAllFileEntries(items);
134
- if (this.multiple === false && result.length > 0) {
151
+ if (this.multiple === false && result.length) {
135
152
  result = [result[0]];
136
153
  }
137
154
  this.dispatchEvent(
138
- new UUIFileDropzoneEvent(UUIFileDropzoneEvent.FILE_CHANGE, {
155
+ new UUIFileDropzoneEvent(UUIFileDropzoneEvent.CHANGE, {
139
156
  detail: { files: result }
140
157
  })
141
158
  );
@@ -155,7 +172,7 @@ let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
155
172
  _onFileInputChange() {
156
173
  const files = this._input.files ? Array.from(this._input.files) : [];
157
174
  this.dispatchEvent(
158
- new UUIFileDropzoneEvent(UUIFileDropzoneEvent.FILE_CHANGE, {
175
+ new UUIFileDropzoneEvent(UUIFileDropzoneEvent.CHANGE, {
159
176
  detail: { files }
160
177
  })
161
178
  );
@@ -173,6 +190,7 @@ let UUIFileDropzoneElement = class extends LabelMixin("", LitElement) {
173
190
  ?multiple=${this.multiple}
174
191
  @change=${this._onFileInputChange}
175
192
  aria-label="${this.label}" />
193
+ <slot></slot>
176
194
  </div>
177
195
  `;
178
196
  }
@@ -226,7 +244,7 @@ __decorateClass([
226
244
  ], UUIFileDropzoneElement.prototype, "_dropzone", 2);
227
245
  __decorateClass([
228
246
  property({ type: String })
229
- ], UUIFileDropzoneElement.prototype, "accept", 2);
247
+ ], UUIFileDropzoneElement.prototype, "accept", 1);
230
248
  __decorateClass([
231
249
  property({ type: Boolean })
232
250
  ], UUIFileDropzoneElement.prototype, "multiple", 2);
@@ -234,4 +252,4 @@ UUIFileDropzoneElement = __decorateClass([
234
252
  defineElement("uui-file-dropzone")
235
253
  ], UUIFileDropzoneElement);
236
254
 
237
- export { UUIFileDropzoneElement };
255
+ export { UUIFileDropzoneElement, UUIFileDropzoneEvent };
@@ -2,7 +2,7 @@ import { LitElement } from 'lit';
2
2
  declare const UUIFileDropzoneElement_base: (new (...args: any[]) => import("@umbraco-ui/uui-base/lib/mixins").LabelMixinInterface) & typeof LitElement;
3
3
  /**
4
4
  * @element uui-file-dropzone
5
- * @fires {UUIFileDropzoneEvent} file-change - fires when the a file has been selected.
5
+ * @fires {UUIFileDropzoneEvent} change - fires when the a file has been selected.
6
6
  * @slot - For the content of the dropzone
7
7
  * @description - Dropzone for file upload. Supports native browsing and drag n drop.
8
8
  */
@@ -10,13 +10,20 @@ export declare class UUIFileDropzoneElement extends UUIFileDropzoneElement_base
10
10
  static styles: import("lit").CSSResult[];
11
11
  private _input;
12
12
  private _dropzone;
13
+ private _acceptedFileExtensions;
14
+ private _acceptedMimeTypes;
13
15
  /**
14
- * Accepted filetypes. Will allow all types if empty.
16
+ * Comma-separated list of accepted mime types or file extensions (denoted with a `.`).
17
+ * If this is left empty, it will allow all types.
18
+ *
15
19
  * @type {string}
16
20
  * @attr
17
- * @default false
21
+ * @examples [
22
+ * "image/*,application/pdf",
23
+ * ".gif,.png,.jpg,.jpeg,.pdf",
24
+ * ]
18
25
  */
19
- accept: string;
26
+ set accept(value: string);
20
27
  /**
21
28
  * Allows for multiple files to be selected.
22
29
  * @type {boolean}
@@ -31,11 +38,9 @@ export declare class UUIFileDropzoneElement extends UUIFileDropzoneElement_base
31
38
  browse(): void;
32
39
  constructor();
33
40
  connectedCallback(): void;
34
- protected _checkIsItDirectory(dtItem: DataTransferItem): boolean;
35
41
  private _getAllFileEntries;
36
42
  private _readAllDirectoryEntries;
37
43
  private _readEntriesPromise;
38
- private _getFile;
39
44
  private _isAccepted;
40
45
  private _onDrop;
41
46
  private _onDragOver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umbraco-ui/uui-file-dropzone",
3
- "version": "1.2.1",
3
+ "version": "1.3.0-rc.1",
4
4
  "license": "MIT",
5
5
  "keywords": [
6
6
  "Umbraco",
@@ -30,17 +30,17 @@
30
30
  "custom-elements.json"
31
31
  ],
32
32
  "dependencies": {
33
- "@umbraco-ui/uui-base": "1.2.1",
34
- "@umbraco-ui/uui-symbol-file-dropzone": "1.2.1"
33
+ "@umbraco-ui/uui-base": "1.3.0-rc.1",
34
+ "@umbraco-ui/uui-symbol-file-dropzone": "1.3.0-rc.1"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "npm run analyze && tsc --build --force && rollup -c rollup.config.js",
38
- "clean": "tsc --build --clean && rimraf dist lib/*.js lib/**/*.js custom-elements.json",
38
+ "clean": "tsc --build --clean && rimraf -g dist lib/*.js lib/**/*.js *.tgz lib/**/*.d.ts custom-elements.json",
39
39
  "analyze": "web-component-analyzer **/*.element.ts --outFile custom-elements.json"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"
43
43
  },
44
44
  "homepage": "https://uui.umbraco.com/?path=/story/uui-file-dropzone",
45
- "gitHead": "94eb22bee5ff21bac6fadbd78653671279bebe36"
45
+ "gitHead": "0c517175884931aa0bc0d8f05974852a7704626e"
46
46
  }