wx-svelte-uploader 1.3.0

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/license.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 XB Software Sp. z o.o.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "wx-svelte-uploader",
3
+ "version": "1.3.0",
4
+ "productTag": "uploader",
5
+ "productTrial": false,
6
+ "type": "module",
7
+ "scripts": {
8
+ "build": "vite build",
9
+ "build:dist": "vite build --mode dist",
10
+ "build:tests": "vite build --mode test",
11
+ "lint": "yarn eslint ./demos ./src --ext .svelte,.ts,.js",
12
+ "start": "vite --open=/demos/",
13
+ "start:tests": "vite --open=/tests/ --host 0.0.0.0 --port 5100 --mode test",
14
+ "test": "true",
15
+ "test:cypress": "cypress run -P ./ --config \"baseUrl=http://localhost:5100/tests\""
16
+ },
17
+ "svelte": "src/index.js",
18
+ "exports": {
19
+ ".": {
20
+ "svelte": "./src/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "wx-lib-dom": "^0.6.0"
27
+ },
28
+ "devDependencies": {
29
+ "wx-svelte-core": "^1.3.0"
30
+ },
31
+ "files": [
32
+ "src",
33
+ "readme.md",
34
+ "whatsnew.md",
35
+ "license.txt"
36
+ ]
37
+ }
package/readme.md ADDED
@@ -0,0 +1,38 @@
1
+ # WX Uploader
2
+
3
+ ## Usage
4
+
5
+ ```
6
+ <script>
7
+ import { Uploader } from 'wx-svelte-uploader';
8
+ </script>
9
+
10
+ <Uploader />
11
+ ```
12
+
13
+ ## Cli commands
14
+
15
+ ### Build
16
+
17
+ - `yarn dev` - start project in dev mode, rebuild on change
18
+ - `yarn build` - build production ready js file
19
+
20
+ the dev server will start at http://localhost:5100/tests.html
21
+
22
+ ### Tests
23
+
24
+ #### Run tests
25
+
26
+ ```
27
+ yarn dev:tests
28
+ cypress run -P ./ --config "baseUrl=http://localhost:5100"
29
+ ```
30
+
31
+ #### Open Cypress console
32
+
33
+ ```
34
+ yarn dev:tests
35
+ cypress open -P ./ --config "baseUrl=http://localhost:5100"
36
+ ```
37
+
38
+ dev:tests command will start dev server at http://localhost:5100/tests.html
@@ -0,0 +1,248 @@
1
+ <script>
2
+ import { uid } from "wx-lib-dom";
3
+ import { onMount, createEventDispatcher, setContext } from "svelte";
4
+ import { apiKey } from "../helpers/consts";
5
+
6
+ const dispatch = createEventDispatcher();
7
+
8
+ export let data = [];
9
+ export let accept = "";
10
+ export let multiple = true;
11
+ export let folder = false;
12
+ export let uploadURL = "";
13
+ export let apiOnly = false;
14
+ export let disabled = false;
15
+
16
+ export let ready = new Promise(() => ({}));
17
+
18
+ let input;
19
+ let drag;
20
+ let count = 0;
21
+ let lastCtx = {};
22
+
23
+ const api = {
24
+ open: ctx => open(ctx),
25
+ droparea: (node, ctx) => {
26
+ if (disabled) return;
27
+
28
+ ctx = ctx || {};
29
+ node.addEventListener("dragenter", () => {
30
+ if (ctx.dragEnter) ctx.dragEnter();
31
+ dragenter();
32
+ });
33
+ node.addEventListener("dragleave", () => {
34
+ if (ctx.dragEnter) ctx.dragLeave();
35
+ dragleave();
36
+ });
37
+
38
+ node.addEventListener("dragover", ev => ev.preventDefault(), true);
39
+ node.addEventListener(
40
+ "drop",
41
+ ev => {
42
+ ev.preventDefault();
43
+ lastCtx = ctx;
44
+ drop(ev);
45
+ if (ctx.dragEnter) ctx.dragLeave();
46
+ },
47
+ true
48
+ );
49
+ },
50
+ };
51
+
52
+ onMount(() => {
53
+ input.webkitdirectory = folder;
54
+ });
55
+
56
+ setContext(apiKey, api);
57
+
58
+ function add(ev) {
59
+ const files = Array.from(ev.target.files);
60
+ files.forEach(f => addFile(f));
61
+ }
62
+
63
+ function drop(ev) {
64
+ const items = Array.from(ev.dataTransfer.items);
65
+
66
+ items.forEach(item => {
67
+ const entry = item.webkitGetAsEntry();
68
+ if (entry) flatten(entry);
69
+ });
70
+
71
+ drag = false;
72
+ count = 0;
73
+ }
74
+
75
+ function flatten(item, path) {
76
+ path = path || "";
77
+ if (item.isFile) {
78
+ item.file(file => {
79
+ addFile(file);
80
+ });
81
+ } else if (item.isDirectory) {
82
+ const dir = item.createReader();
83
+ dir.readEntries(files => {
84
+ files.forEach(file => {
85
+ flatten(file, path + file.name + "/");
86
+ });
87
+ });
88
+ }
89
+ }
90
+
91
+ function addFile(file) {
92
+ const obj = {
93
+ ...lastCtx,
94
+ id: uid(),
95
+ status: "client",
96
+ name: file.name,
97
+ file,
98
+ };
99
+
100
+ if (obj.selected) obj.selected(obj);
101
+ dispatch("select", obj);
102
+
103
+ if (multiple) {
104
+ data = [...data, obj];
105
+ } else {
106
+ data = [obj];
107
+ }
108
+
109
+ upload(obj);
110
+ input.value = "";
111
+ }
112
+
113
+ function defaultUploader(obj) {
114
+ const formData = new FormData();
115
+ formData.append("upload", obj.file);
116
+
117
+ const config = {
118
+ method: "POST",
119
+ body: formData,
120
+ };
121
+
122
+ return fetch(uploadURL, config)
123
+ .then(res => res.json())
124
+ .then(
125
+ data => ({ id: obj.id, ...data }),
126
+ () => ({ id: obj.id, status: "error" })
127
+ )
128
+ .catch(error => console.log(error));
129
+ }
130
+
131
+ function upload(obj) {
132
+ if (!obj) return;
133
+
134
+ const request =
135
+ typeof uploadURL === "function"
136
+ ? uploadURL(obj)
137
+ : defaultUploader(obj);
138
+
139
+ ready = request
140
+ .then(r => {
141
+ r.status = r.status || "server";
142
+ updateData(obj.id, r);
143
+ })
144
+ .catch(error => {
145
+ updateData(obj.id, { status: "error", error });
146
+ });
147
+ }
148
+
149
+ function updateData(id, result) {
150
+ const ind = data.findIndex(i => i.id == id);
151
+ const temp = (data[ind] = { ...data[ind], ...result });
152
+ if (temp && temp.uploaded) temp.uploaded(temp);
153
+ dispatch("upload", data[ind]);
154
+
155
+ if (temp.temp) data = data.filter(i => i.id != id);
156
+ }
157
+
158
+ function dragenter() {
159
+ if (count === 0) drag = true;
160
+ count++;
161
+ }
162
+
163
+ function dragleave() {
164
+ count--;
165
+ if (count === 0) drag = false;
166
+ }
167
+
168
+ function open(ctx) {
169
+ lastCtx = ctx || {};
170
+ input.click();
171
+ }
172
+ </script>
173
+
174
+ {#if apiOnly}
175
+ <input
176
+ type="file"
177
+ class="input"
178
+ bind:this={input}
179
+ on:change={add}
180
+ {accept}
181
+ {multiple}
182
+ {disabled}
183
+ />
184
+ <slot />
185
+ {:else}
186
+ <div
187
+ class="label"
188
+ class:active={drag}
189
+ class:wx-disabled={disabled}
190
+ use:api.droparea
191
+ >
192
+ <input
193
+ type="file"
194
+ class="input"
195
+ bind:this={input}
196
+ on:change={add}
197
+ {accept}
198
+ {multiple}
199
+ {disabled}
200
+ />
201
+ <slot {open}>
202
+ <div class="dropzone">
203
+ <span>
204
+ Drop files here or
205
+ <span class="action" on:click={open}>select files</span>
206
+ </span>
207
+ </div>
208
+ </slot>
209
+ </div>
210
+ {/if}
211
+
212
+ <style>
213
+ .label {
214
+ display: flex;
215
+ align-items: center;
216
+ }
217
+
218
+ .label.active:not(.wx-disabled) .dropzone {
219
+ background-color: var(--wx-background-alt);
220
+ }
221
+ .input {
222
+ position: absolute;
223
+ width: 0;
224
+ height: 0;
225
+ opacity: 0;
226
+ }
227
+
228
+ .dropzone {
229
+ display: flex;
230
+ align-items: center;
231
+ justify-content: center;
232
+ padding: var(--wx-padding);
233
+ border: var(--wx-input-border);
234
+ border-style: dashed;
235
+ border-radius: var(--wx-input-border-radius);
236
+ background: var(--wx-uploader-background);
237
+ }
238
+ .label:not(.wx-disabled) .action {
239
+ cursor: pointer;
240
+ color: var(--wx-color-link);
241
+ text-decoration: underline;
242
+ }
243
+
244
+ .label.wx-disabled .dropzone {
245
+ background: var(--wx-color-disabled);
246
+ color: var(--wx-color-font-disabled);
247
+ }
248
+ </style>
@@ -0,0 +1,132 @@
1
+ <script>
2
+ export let data;
3
+
4
+ const fileSize = ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb"];
5
+
6
+ function removeAll() {
7
+ data = [];
8
+ }
9
+
10
+ function remove(id) {
11
+ data = data.filter(i => i.id !== id);
12
+ }
13
+
14
+ function formatSize(size) {
15
+ let index = 0;
16
+ while (size > 1024) {
17
+ index++;
18
+ size = size / 1024;
19
+ }
20
+ return Math.round(size * 100) / 100 + " " + fileSize[index];
21
+ }
22
+
23
+ </script>
24
+
25
+ {#if data.length}
26
+ <div class="layout">
27
+ <div class="header">
28
+ <i class="icon wxi-close" on:click={removeAll} />
29
+ </div>
30
+ <div class="list">
31
+ {#each data as obj (obj.id)}
32
+ <div class="row">
33
+ <div class="file-icon" />
34
+ <div class="name">{obj.name}</div>
35
+ {#if obj.file}
36
+ <div class="size">{formatSize(obj.file.size)}</div>
37
+ {/if}
38
+ <div class="controls">
39
+ {#if obj.status === 'client'}
40
+ <i class="icon wxi-spin wxi-loading" />
41
+ {:else if obj.status === 'error'}
42
+ <i class="icon wxi-alert" />
43
+ <i
44
+ class="icon wxi-close"
45
+ on:click={() => remove(obj.id)} />
46
+ {:else if !obj.status || obj.status === 'server'}
47
+ <i class="icon wxi-check" />
48
+ <i
49
+ class="icon wxi-close"
50
+ on:click={() => remove(obj.id)} />
51
+ {/if}
52
+ </div>
53
+ </div>
54
+ {/each}
55
+ </div>
56
+ </div>
57
+ {/if}
58
+
59
+ <style>
60
+ .layout {
61
+ display: flex;
62
+ flex-direction: column;
63
+ width: 100%;
64
+ }
65
+
66
+ .header {
67
+ display: flex;
68
+ align-items: center;
69
+ justify-content: flex-end;
70
+ padding: var(--wx-padding);
71
+ border-bottom: var(--wx-border);
72
+ }
73
+
74
+ .list {
75
+ overflow: auto;
76
+ }
77
+
78
+ .row {
79
+ display: flex;
80
+ align-items: center;
81
+ gap: var(--wx-padding);
82
+ padding: var(--wx-padding);
83
+ border-bottom: var(--wx-border);
84
+ }
85
+
86
+ .name {
87
+ flex: 1;
88
+ overflow: hidden;
89
+ white-space: nowrap;
90
+ text-overflow: ellipsis;
91
+ }
92
+
93
+ .controls {
94
+ }
95
+
96
+ .icon {
97
+ display: flex;
98
+ justify-content: center;
99
+ align-items: center;
100
+ width: var(--wx-icon-size);
101
+ height: var(--wx-icon-size);
102
+ font-size: var(--wx-icon-size);
103
+ line-height: 1;
104
+ border-radius: var(--wx-border-radius);
105
+ color: var(--wx-icon-color);
106
+ cursor: pointer;
107
+ }
108
+ .icon:before {
109
+ display: block;
110
+ }
111
+ .icon:hover {
112
+ background-color: var(--wx-background-hover);
113
+ }
114
+
115
+ .row:hover .wxi-close {
116
+ display: flex;
117
+ }
118
+ .row:hover .wxi-check,
119
+ .row:hover .wxi-alert {
120
+ display: none;
121
+ }
122
+
123
+ .row .wxi-close {
124
+ display: none;
125
+ }
126
+
127
+ .wxi-check,
128
+ .wxi-alert {
129
+ display: flex;
130
+ }
131
+
132
+ </style>
@@ -0,0 +1,2 @@
1
+ // context key for Uploader
2
+ export const apiKey = "wx-uploader-api";
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import Uploader from "./components/Uploader.svelte";
2
+ import UploaderList from "./components/UploaderList.svelte";
3
+ import { apiKey } from "./helpers/consts";
4
+
5
+ export { Uploader, UploaderList, apiKey };
package/whatsnew.md ADDED
@@ -0,0 +1,15 @@
1
+ ### 1.3.0
2
+
3
+ [dev] using core@1.3.0
4
+
5
+ ### 0.2.1
6
+
7
+ [fix] disabled state was ignored by drag-n-drop operations
8
+
9
+ ### 0.2.0
10
+
11
+ [add] disabled state
12
+
13
+ ### 0.1.0
14
+
15
+ Initial version