@umbraco-ui/uui-file-preview 0.1.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 +21 -0
- package/README.md +31 -0
- package/custom-elements.json +26 -0
- package/lib/UUIFileSize.d.ts +13 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +227 -0
- package/lib/uui-file-preview.element.d.ts +67 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 uui-app
|
|
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/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# uui-file-preview
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Umbraco style file-preview component.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### ES imports
|
|
10
|
+
|
|
11
|
+
```zsh
|
|
12
|
+
npm i @umbraco-ui/uui-file-preview
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Import the registration of `<uui-file-preview>` via:
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import '@umbraco-ui/uui-file-preview';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
When looking to leverage the `UUIFilePreviewElement` base class as a type and/or for extension purposes, do so via:
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import { UUIFilePreviewElement } from '@umbraco-ui/uui-file-preview';
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<uui-file-preview></uui-file-preview>
|
|
31
|
+
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "experimental",
|
|
3
|
+
"tags": [
|
|
4
|
+
{
|
|
5
|
+
"name": "uui-file-preview",
|
|
6
|
+
"path": "./lib/uui-file-preview.element.ts",
|
|
7
|
+
"properties": [
|
|
8
|
+
{
|
|
9
|
+
"name": "styles",
|
|
10
|
+
"type": "CSSResult[]",
|
|
11
|
+
"default": "[null]"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "file",
|
|
15
|
+
"type": "File | undefined"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"slots": [
|
|
19
|
+
{
|
|
20
|
+
"name": "actions",
|
|
21
|
+
"description": "To display one or more actions that can be executed on the file."
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format bytes as human-readable text.
|
|
3
|
+
*
|
|
4
|
+
* @param bytes Number of bytes.
|
|
5
|
+
* @param si True to use metric (SI) units, aka powers of 1000. False to use
|
|
6
|
+
* binary (IEC), aka powers of 1024.
|
|
7
|
+
* @param dp Number of decimal places to display.
|
|
8
|
+
*
|
|
9
|
+
* @return Formatted string.
|
|
10
|
+
*/
|
|
11
|
+
export declare class UUIFileSize {
|
|
12
|
+
static humanFileSize(bytes: number, si?: boolean, decimalPlaces?: number): string;
|
|
13
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './uui-file-preview.element';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
|
|
2
|
+
import { state, property } from 'lit/decorators.js';
|
|
3
|
+
import { css, LitElement, html } from 'lit';
|
|
4
|
+
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
|
|
5
|
+
|
|
6
|
+
class UUIFileSize {
|
|
7
|
+
static humanFileSize(bytes, si = false, decimalPlaces = 1) {
|
|
8
|
+
const thresh = si ? 1e3 : 1024;
|
|
9
|
+
if (Math.abs(bytes) < thresh) {
|
|
10
|
+
return bytes + " B";
|
|
11
|
+
}
|
|
12
|
+
const units = si ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
|
13
|
+
let u = -1;
|
|
14
|
+
const r = 10 ** decimalPlaces;
|
|
15
|
+
do {
|
|
16
|
+
bytes /= thresh;
|
|
17
|
+
++u;
|
|
18
|
+
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
19
|
+
return bytes.toFixed(decimalPlaces) + " " + units[u];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var __defProp = Object.defineProperty;
|
|
24
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
25
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
26
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
27
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
28
|
+
if (decorator = decorators[i])
|
|
29
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
30
|
+
if (kind && result)
|
|
31
|
+
__defProp(target, key, result);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
let UUIFilePreviewElement = class extends LitElement {
|
|
35
|
+
constructor() {
|
|
36
|
+
super(...arguments);
|
|
37
|
+
this._name = "";
|
|
38
|
+
this._url = "";
|
|
39
|
+
this._extension = "";
|
|
40
|
+
this._src = "";
|
|
41
|
+
this._size = 0;
|
|
42
|
+
this._isDirectory = false;
|
|
43
|
+
}
|
|
44
|
+
get file() {
|
|
45
|
+
return this._file;
|
|
46
|
+
}
|
|
47
|
+
set file(newValue) {
|
|
48
|
+
const oldValue = this._file;
|
|
49
|
+
if (newValue instanceof File) {
|
|
50
|
+
this._name = newValue.name.split(".")[0];
|
|
51
|
+
this._extension = newValue.name.split(".")[1];
|
|
52
|
+
this._isDirectory = false;
|
|
53
|
+
this._size = newValue.size;
|
|
54
|
+
if (this._isFileAnImage(newValue)) {
|
|
55
|
+
this._isImage = true;
|
|
56
|
+
this._getThumbnail(newValue).then((result) => {
|
|
57
|
+
this._src = result;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
this.requestUpdate("file", oldValue);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
connectedCallback() {
|
|
64
|
+
super.connectedCallback();
|
|
65
|
+
demandCustomElement(this, "uui-symbol-file-thumbnail");
|
|
66
|
+
demandCustomElement(this, "uui-symbol-folder");
|
|
67
|
+
demandCustomElement(this, "uui-symbol-file");
|
|
68
|
+
}
|
|
69
|
+
_openSource() {
|
|
70
|
+
window.open(this._url, "_blank");
|
|
71
|
+
}
|
|
72
|
+
_fileTypeTemplate() {
|
|
73
|
+
if (this._isDirectory) {
|
|
74
|
+
return html`<uui-symbol-folder id="file-symbol"></uui-symbol-folder>`;
|
|
75
|
+
}
|
|
76
|
+
if (this._isImage) {
|
|
77
|
+
return html`<uui-symbol-file-thumbnail
|
|
78
|
+
.src=${this._src}
|
|
79
|
+
.alt=${this._name}
|
|
80
|
+
id="file-symbol"></uui-symbol-file-thumbnail>`;
|
|
81
|
+
}
|
|
82
|
+
return html`<uui-symbol-file
|
|
83
|
+
id="file-symbol"
|
|
84
|
+
.type=${this._extension}></uui-symbol-file>`;
|
|
85
|
+
}
|
|
86
|
+
_renderLongName() {
|
|
87
|
+
const endCharCount = 6;
|
|
88
|
+
const nameStart = this._name.substring(0, this._name.length - endCharCount);
|
|
89
|
+
const nameEnd = this._name.substring(this._name.length - endCharCount, this._name.length);
|
|
90
|
+
return html`
|
|
91
|
+
<span
|
|
92
|
+
id="file-name"
|
|
93
|
+
class=${this._url ? "has-source" : ""}
|
|
94
|
+
@click=${() => this._url ? this._openSource() : ""}
|
|
95
|
+
@keydown=${() => ""}>
|
|
96
|
+
<span id="file-name-start">${nameStart}</span>
|
|
97
|
+
<span id="file-name-end">${nameEnd}</span>
|
|
98
|
+
</span>
|
|
99
|
+
`;
|
|
100
|
+
}
|
|
101
|
+
_isFileAnImage(file) {
|
|
102
|
+
return file ? file["type"].split("/")[0] === "image" : false;
|
|
103
|
+
}
|
|
104
|
+
async _getThumbnail(file) {
|
|
105
|
+
return await new Promise((resolve) => {
|
|
106
|
+
const reader = new FileReader();
|
|
107
|
+
reader.readAsDataURL(file);
|
|
108
|
+
reader.onload = () => {
|
|
109
|
+
resolve(reader.result);
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
render() {
|
|
114
|
+
return html`
|
|
115
|
+
<slot id="actions" name="actions"></slot>
|
|
116
|
+
${this._fileTypeTemplate()}
|
|
117
|
+
<div id="file-info">
|
|
118
|
+
${this._renderLongName()}
|
|
119
|
+
<span id="file-size">
|
|
120
|
+
${this._size && !this._isDirectory ? html`${UUIFileSize.humanFileSize(this._size, true)}` : ""}
|
|
121
|
+
</span>
|
|
122
|
+
</div>
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
UUIFilePreviewElement.styles = [
|
|
127
|
+
css`
|
|
128
|
+
:host {
|
|
129
|
+
position: relative;
|
|
130
|
+
display: grid;
|
|
131
|
+
/* These have to be changed together */
|
|
132
|
+
grid-template-rows: 100px 50px;
|
|
133
|
+
width: 150px;
|
|
134
|
+
height: 150px;
|
|
135
|
+
/* --------------------------------- */
|
|
136
|
+
box-sizing: border-box;
|
|
137
|
+
aspect-ratio: 1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
:host(:hover) slot[name='actions']::slotted(*) {
|
|
141
|
+
opacity: 1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
slot[name='actions']::slotted(*) {
|
|
145
|
+
position: absolute;
|
|
146
|
+
top: 8px;
|
|
147
|
+
right: 8px;
|
|
148
|
+
max-width: 100%;
|
|
149
|
+
height: 32px;
|
|
150
|
+
font-size: 13px;
|
|
151
|
+
opacity: 0;
|
|
152
|
+
z-index: 1;
|
|
153
|
+
transition: opacity 150ms;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#file-symbol {
|
|
157
|
+
aspect-ratio: 1 / 1;
|
|
158
|
+
margin: auto;
|
|
159
|
+
max-width: 100%;
|
|
160
|
+
max-height: 100%;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#file-info {
|
|
164
|
+
min-width: 0;
|
|
165
|
+
display: flex;
|
|
166
|
+
text-align: center;
|
|
167
|
+
flex-direction: column;
|
|
168
|
+
font-size: 1rem;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#file-name {
|
|
172
|
+
display: flex;
|
|
173
|
+
justify-content: center;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#file-name-start {
|
|
177
|
+
overflow: hidden;
|
|
178
|
+
white-space: nowrap;
|
|
179
|
+
text-overflow: ellipsis;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
#file-name-end {
|
|
183
|
+
white-space: nowrap;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#file-size {
|
|
187
|
+
opacity: 0.6;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.has-source:hover {
|
|
191
|
+
text-decoration: underline;
|
|
192
|
+
cursor: pointer;
|
|
193
|
+
}
|
|
194
|
+
`
|
|
195
|
+
];
|
|
196
|
+
__decorateClass([
|
|
197
|
+
state()
|
|
198
|
+
], UUIFilePreviewElement.prototype, "_name", 2);
|
|
199
|
+
__decorateClass([
|
|
200
|
+
state()
|
|
201
|
+
], UUIFilePreviewElement.prototype, "_url", 2);
|
|
202
|
+
__decorateClass([
|
|
203
|
+
state()
|
|
204
|
+
], UUIFilePreviewElement.prototype, "_extension", 2);
|
|
205
|
+
__decorateClass([
|
|
206
|
+
state()
|
|
207
|
+
], UUIFilePreviewElement.prototype, "_src", 2);
|
|
208
|
+
__decorateClass([
|
|
209
|
+
state()
|
|
210
|
+
], UUIFilePreviewElement.prototype, "_size", 2);
|
|
211
|
+
__decorateClass([
|
|
212
|
+
state()
|
|
213
|
+
], UUIFilePreviewElement.prototype, "_isDirectory", 2);
|
|
214
|
+
__decorateClass([
|
|
215
|
+
state()
|
|
216
|
+
], UUIFilePreviewElement.prototype, "_file", 2);
|
|
217
|
+
__decorateClass([
|
|
218
|
+
state()
|
|
219
|
+
], UUIFilePreviewElement.prototype, "_isImage", 2);
|
|
220
|
+
__decorateClass([
|
|
221
|
+
property({ attribute: false })
|
|
222
|
+
], UUIFilePreviewElement.prototype, "file", 1);
|
|
223
|
+
UUIFilePreviewElement = __decorateClass([
|
|
224
|
+
defineElement("uui-file-preview")
|
|
225
|
+
], UUIFilePreviewElement);
|
|
226
|
+
|
|
227
|
+
export { UUIFilePreviewElement };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { LitElement } from 'lit';
|
|
2
|
+
/**
|
|
3
|
+
* @element uui-file-preview
|
|
4
|
+
* @slot actions - To display one or more actions that can be executed on the file.
|
|
5
|
+
* @description - A file preview with file size.
|
|
6
|
+
*/
|
|
7
|
+
export declare class UUIFilePreviewElement extends LitElement {
|
|
8
|
+
static styles: import("lit").CSSResult[];
|
|
9
|
+
/**
|
|
10
|
+
* Name of the file.
|
|
11
|
+
* @type {string}
|
|
12
|
+
* @attr
|
|
13
|
+
* @default ''
|
|
14
|
+
*/
|
|
15
|
+
private _name;
|
|
16
|
+
/**
|
|
17
|
+
* Link to the source of the file. Applied on the file name.
|
|
18
|
+
* @type {string}
|
|
19
|
+
* @attr
|
|
20
|
+
* @default ''
|
|
21
|
+
*/
|
|
22
|
+
private _url;
|
|
23
|
+
/**
|
|
24
|
+
* File extension. Will be shown in the square on the file symbol. If a thumbnail is provided, then that will show instead.
|
|
25
|
+
* @type {string}
|
|
26
|
+
* @attr
|
|
27
|
+
* @default ''
|
|
28
|
+
*/
|
|
29
|
+
private _extension;
|
|
30
|
+
/**
|
|
31
|
+
* Source of a thumbnail to be displayed as the file symbol. Often used for images and video thumbnails.
|
|
32
|
+
* @type {string}
|
|
33
|
+
* @attr
|
|
34
|
+
* @default ''
|
|
35
|
+
*/
|
|
36
|
+
private _src;
|
|
37
|
+
/**
|
|
38
|
+
* Size of the file in bytes. It will be formatted to a more readable format.
|
|
39
|
+
* @type {number}
|
|
40
|
+
* @attr
|
|
41
|
+
* @default 0
|
|
42
|
+
*/
|
|
43
|
+
private _size;
|
|
44
|
+
/**
|
|
45
|
+
* Dertermines if a folder symbol should be used instead of file symbol.
|
|
46
|
+
* @type {boolean}
|
|
47
|
+
* @attr
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
private _isDirectory;
|
|
51
|
+
private _file?;
|
|
52
|
+
private _isImage?;
|
|
53
|
+
get file(): File | undefined;
|
|
54
|
+
set file(newValue: File | undefined);
|
|
55
|
+
connectedCallback(): void;
|
|
56
|
+
private _openSource;
|
|
57
|
+
private _fileTypeTemplate;
|
|
58
|
+
private _renderLongName;
|
|
59
|
+
private _isFileAnImage;
|
|
60
|
+
private _getThumbnail;
|
|
61
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
62
|
+
}
|
|
63
|
+
declare global {
|
|
64
|
+
interface HTMLElementTagNameMap {
|
|
65
|
+
'uui-file-preview': UUIFilePreviewElement;
|
|
66
|
+
}
|
|
67
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@umbraco-ui/uui-file-preview",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Umbraco",
|
|
7
|
+
"Custom elements",
|
|
8
|
+
"Web components",
|
|
9
|
+
"UI",
|
|
10
|
+
"Lit",
|
|
11
|
+
"File Preview"
|
|
12
|
+
],
|
|
13
|
+
"description": "Umbraco UI file-preview component.",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/umbraco/Umbraco.UI.git",
|
|
17
|
+
"directory": "packages/uui-file-preview"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/umbraco/Umbraco.UI/issues"
|
|
21
|
+
},
|
|
22
|
+
"main": "./lib/index.js",
|
|
23
|
+
"module": "./lib/index.js",
|
|
24
|
+
"types": "./lib/index.d.ts",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"customElements": "custom-elements.json",
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/**/*.d.ts",
|
|
29
|
+
"lib/**/*.js",
|
|
30
|
+
"custom-elements.json"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@umbraco-ui/uui-base": "0.2.0",
|
|
34
|
+
"@umbraco-ui/uui-symbol-file": "0.2.0",
|
|
35
|
+
"@umbraco-ui/uui-symbol-file-thumbnail": "0.1.0",
|
|
36
|
+
"@umbraco-ui/uui-symbol-folder": "0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "npm run analyze && tsc --build --force && rollup -c rollup.config.js",
|
|
40
|
+
"clean": "tsc --build --clean && rimraf dist lib/*.js lib/**/*.js custom-elements.json",
|
|
41
|
+
"analyze": "web-component-analyzer **/*.element.ts --outFile custom-elements.json"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://uui.umbraco.com/?path=/story/uui-file-preview",
|
|
47
|
+
"gitHead": "5494b55e03c9fb3ba8f160e693d3ce59c02d21cd"
|
|
48
|
+
}
|