@dile/ui 2.1.19 → 2.1.20

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,3 @@
1
+ import { DileDropFile } from "./src/DileDropFile";
2
+
3
+ customElements.define('dile-drop-file', DileDropFile);
@@ -0,0 +1,2 @@
1
+ import { DileFilePreview } from "./src/DileFilePreview";
2
+ customElements.define('dile-file-preview', DileFilePreview);
@@ -0,0 +1,2 @@
1
+ export { DileDropFile } from "./src/DileDropFile";
2
+ export { DileFilePreview } from './src/DileFilePreview'
@@ -0,0 +1,211 @@
1
+ import { LitElement, html, css } from 'lit';
2
+ import '../file-preview.js';
3
+ import '../../button/button.js';
4
+ import { messageStyles } from '../../input/src/message-styles.js';
5
+
6
+ export class DileDropFile extends LitElement {
7
+ static styles = [
8
+ messageStyles,
9
+ css`
10
+ * { box-sizing: border-box; }
11
+ :host {
12
+ margin-bottom: 10px;
13
+ }
14
+ #dropZone {
15
+ width: 100%;
16
+ min-height: var(--dile-drop-zone-min-height, auto);
17
+ border: 2px dashed #ccc;
18
+ display: flex;
19
+ flex-direction: column;
20
+ align-items: center;
21
+ justify-content: center;
22
+ color: #ccc;
23
+ font-size: 16px;
24
+ transition: border-color 0.3s, color 0.3s;
25
+ cursor: pointer;
26
+ padding: 1rem;
27
+ }
28
+
29
+ p {
30
+ margin: 0.5rem;
31
+ }
32
+ #dropZone.hover {
33
+ border-color: #333;
34
+ color: #333;
35
+ }
36
+
37
+ input[type="file"] {
38
+ display: none;
39
+ }
40
+
41
+ label {
42
+ display: block;
43
+ margin-bottom: var(--dile-input-label-margin-bottom, 4px);
44
+ font-size: var(--dile-input-label-font-size, 1em);
45
+ color: var(--dile-input-label-color, #59e);
46
+ font-weight: var(--dile-input-label-font-weight, normal);
47
+ }
48
+ main {
49
+ width: var(--dile-input-section-width, 100%);
50
+ }
51
+ `
52
+ ];
53
+
54
+ static get properties() {
55
+ return {
56
+ label: { type: String },
57
+ message: { type: String },
58
+ errored: { type: Boolean },
59
+ dropLabel: { type: String },
60
+ buttonLabel: { type: String },
61
+ fileName: { type: String },
62
+ selectedFileLabel: { type: String },
63
+ allowedExtensions: { type: Array },
64
+ extensionErrorMessage: { type: String},
65
+ };
66
+ }
67
+
68
+ constructor() {
69
+ super();
70
+ this.label = '';
71
+ this.fileName = '';
72
+ this.message = '';
73
+ this.dropLabel = "Drop here your file";
74
+ this.buttonLabel = "Select file";
75
+ this.selectedFileLabel = "Selected file";
76
+ this.extensionErrorMessage = "Only this file extensions are allowed: "
77
+ this.allowedExtensions = [];
78
+ }
79
+
80
+ firstUpdated() {
81
+ this.firstMessage = this.message;
82
+ }
83
+
84
+ render() {
85
+ return html`
86
+ <main>
87
+ ${this.label
88
+ ? html`<label for="fileInput">${this.label}</label>`
89
+ : ""
90
+ }
91
+ </main>
92
+ <div
93
+ id="dropZone"
94
+ @click=${this.openFileDialog}
95
+ @dragenter=${this.dragHover}
96
+ @dragover=${this.dragHover}
97
+ @dragleave=${this.dragLeave}
98
+ @drop=${this.dragHandle}
99
+ >
100
+ ${this.dropLabelTemplate}
101
+ </div>
102
+ ${this.messageTemplate}
103
+ <input type="file" id="fileInput" @change=${this._handleFileInput}>
104
+ <dile-file-preview
105
+ fileName="${this.fileName}"
106
+ selectedFileLabel="${this.selectedFileLabel}"
107
+ @dile-file-clear=${this.clearInput}
108
+ ></dile-file-preview>
109
+ `;
110
+ }
111
+
112
+ get messageTemplate() {
113
+ return html`
114
+ ${this.message
115
+ ? html`<div class="message ${this.errored ? 'errored-msg' : ''}"><span>${this.message}</span></div>`
116
+ : ''
117
+ }
118
+ `
119
+ }
120
+
121
+ get dropLabelTemplate() {
122
+ return html`
123
+ <p>${this.dropLabel}</p>
124
+ <p><dile-button>${this.buttonLabel}</dile-button></p>
125
+ `
126
+ }
127
+
128
+ get fileInput() {
129
+ return this.renderRoot.querySelector('#fileInput');
130
+ }
131
+
132
+ dragHover(e) {
133
+ this._preventDefaults(e);
134
+ this._toggleHover(true);
135
+ }
136
+
137
+ dragLeave(e) {
138
+ this._preventDefaults(e);
139
+ this._toggleHover(false);
140
+ }
141
+
142
+ dragHandle(e) {
143
+ this._handleDrop(e);
144
+ this._toggleHover(false);
145
+ }
146
+
147
+ _preventDefaults(e) {
148
+ e.preventDefault();
149
+ e.stopPropagation();
150
+ }
151
+
152
+ _handleDrop(e) {
153
+ this._preventDefaults(e);
154
+ const dt = e.dataTransfer;
155
+ const files = dt.files;
156
+ this.fileInput.files = files;
157
+ this._processFile(files);
158
+ }
159
+
160
+ _processFile(files) {
161
+ if (files.length > 0) {
162
+ console.log("Mostramos el nombre del archivo",files);
163
+ this.fileName = files[0].name;
164
+ if(this._isValidExtension(this.fileName)) {
165
+ this.message = this.firstMessage || '';
166
+ this.errored = false;
167
+ } else {
168
+ this.clearInput();
169
+ this.message = `${this.extensionErrorMessage} ${this.allowedExtensions.join(', ')}`;
170
+ this.errored = true;
171
+ }
172
+ }
173
+ }
174
+
175
+ _isValidExtension(fileName) {
176
+ if(this.allowedExtensions.length === 0) {
177
+ return true;
178
+ }
179
+ const extension = fileName.split('.').pop().toLowerCase();
180
+ if(this.allowedExtensions.includes(extension)) {
181
+ return true;
182
+ }
183
+ return false;
184
+ }
185
+
186
+ _handleFileInput(e) {
187
+ this._processFile(this.fileInput.files);
188
+ }
189
+
190
+ _toggleHover(isHovering) {
191
+ const dropZone = this.renderRoot.querySelector('#dropZone');
192
+ if (isHovering) {
193
+ dropZone.classList.add('hover');
194
+ } else {
195
+ dropZone.classList.remove('hover');
196
+ }
197
+ }
198
+
199
+ openFileDialog() {
200
+ this.fileInput.click();
201
+ }
202
+
203
+ clearInput() {
204
+ this.fileInput.value = "";
205
+ this.fileName = "";
206
+ }
207
+
208
+ getFiles() {
209
+ return this.fileInput.files;
210
+ }
211
+ }
@@ -0,0 +1,111 @@
1
+ import { LitElement, html, css } from 'lit';
2
+ import { clearIcon, imageIcon, pdfIcon, videocamIcon, draftIcon, folderZipIcon } from '@dile/icons';
3
+ import '../../icon/icon.js';
4
+
5
+ export class DileFilePreview extends LitElement {
6
+ static styles = [
7
+ css`
8
+ * { box-sizing: border-box }
9
+ :host {
10
+ display: block;
11
+ margin-top: var(--dile-file-preview-margin-top, 7px);
12
+ }
13
+ div {
14
+ display: flex;
15
+ align-items: center;
16
+ width: 100%;
17
+ background-color: var(--dile-file-preview-background-color, #eee);
18
+ color: var(--dile-file-preview-color, #303030);
19
+ padding: var(--dile-file-preview-padding, 0.5rem 1rem);
20
+ border-radius: var(--dile-file-preview-border-radius, 1rem);
21
+ font-size: var(--dile-file-preview-font-size, 0.8rem);
22
+ }
23
+ div span {
24
+ flex-grow: 1;
25
+ margin: 0 0.75rem;
26
+ }
27
+ .clearicon {
28
+ --dile-icon-color: var(--dile-file-preview-clear-icon-color, #39f);
29
+ }
30
+ .fileicon {
31
+ --dile-icon-color: var(--dile-file-preview-file-icon-color, #303030);
32
+ }
33
+ `
34
+ ];
35
+
36
+ static get properties() {
37
+ return {
38
+ fileName: { type: String },
39
+ fileExtension: { type: String },
40
+ fileIcon: { type: Object },
41
+ selectedFileLabel: { type: String },
42
+ };
43
+ }
44
+
45
+ constructor() {
46
+ super();
47
+ this.fileIcon = draftIcon;
48
+ this.availableExtensions = [
49
+ {
50
+ extensions: ['png', 'jpg', 'jpeg', 'gif'],
51
+ icon: imageIcon,
52
+ },
53
+ {
54
+ extensions: ['pdf'],
55
+ icon: pdfIcon,
56
+ },
57
+ {
58
+ extensions: ['mp4', 'avi', 'mkv', 'mov'],
59
+ icon: videocamIcon,
60
+ },
61
+ {
62
+ extensions: ['zip', 'gz', 'rar'],
63
+ icon: folderZipIcon,
64
+ },
65
+ ]
66
+ }
67
+
68
+ render() {
69
+ return html`
70
+ ${this.fileName
71
+ ? html`
72
+ <div>
73
+ <dile-icon class="fileicon" .icon=${this.fileIcon}></dile-icon>
74
+ <span>${this.selectedFileLabel}: ${this.fileName}</span>
75
+ <a href="#" @click=${this.dispatchClear}><dile-icon class="clearicon" .icon="${clearIcon}"></dile-icon></a>
76
+ </div>
77
+ `
78
+ : ''
79
+ }
80
+ `;
81
+ }
82
+
83
+ updated(changedProperties) {
84
+ if(changedProperties.has('fileName')) {
85
+ this.fileExtension = this._getFileExtension(this.fileName);
86
+ this.fileIcon = this._getIconFromExtension(this.fileExtension);
87
+ }
88
+ }
89
+
90
+ _getFileExtension(fileName) {
91
+ return fileName.split('.').pop().toLowerCase();
92
+ }
93
+
94
+ _getIconFromExtension(fileExtension) {
95
+ const extensionMatch = this.availableExtensions.find(item =>
96
+ item.extensions.includes(fileExtension)
97
+ );
98
+ return extensionMatch ? extensionMatch.icon : draftIcon;
99
+ }
100
+
101
+ dispatchClear(e) {
102
+ e.preventDefault();
103
+ this.dispatchEvent(new CustomEvent('dile-file-clear', {
104
+ bubbles: true,
105
+ composed: true,
106
+ detail: {
107
+ fileName: this.fileName
108
+ }
109
+ }));
110
+ }
111
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "2.1.19",
3
+ "version": "2.1.20",
4
4
  "description": "UI Core components from dile-components.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,11 +20,11 @@
20
20
  },
21
21
  "homepage": "https://dile-components.polydile.com/",
22
22
  "dependencies": {
23
- "@dile/icons": "^2.0.11",
23
+ "@dile/icons": "^2.0.12",
24
24
  "lit": "^2.7.0 || ^3.0.0"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  },
29
- "gitHead": "6f6fd44a9d3244f6f73e29d50a00a4eb8277286b"
29
+ "gitHead": "a80e638ff7147109adf302b8d86e0c0d7d2e7a32"
30
30
  }