UNPKG

6.92 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _DuoyunImagePreviewElement_instances, _DuoyunImagePreviewElement_status_get, _DuoyunImagePreviewElement_color_get, _DuoyunImagePreviewElement_icon_get;
13import { adoptedStyle, customElement, attribute, property, numattribute } from '@mantou/gem/lib/decorators';
14import { GemElement, html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css, styleMap } from '@mantou/gem/lib/utils';
16import { theme, getSemanticColor } from '../lib/theme';
17import { compressionImage } from '../lib/image';
18import { icons } from '../lib/icons';
19import { commonHandle } from '../lib/hotkeys';
20import { focusStyle } from '../lib/styles';
21import './use';
22const style = createCSSSheet(css `
23 :host {
24 --color: initial;
25 position: relative;
26 aspect-ratio: 1;
27 background: conic-gradient(
28 transparent 0.25turn,
29 #d3cfcf 0.25turn 0.5turn,
30 transparent 0.5turn 0.75turn,
31 #d3cfcf 0.75turn
32 )
33 top left / 1.2em 1.2em repeat;
34 }
35 :host,
36 .mask {
37 display: flex;
38 align-items: center;
39 justify-content: center;
40 }
41 :host(:not(:hover)) .mask:not(.status) {
42 display: none;
43 }
44 .preview,
45 .mask {
46 position: absolute;
47 inset: 0;
48 width: 100%;
49 height: 100%;
50 object-fit: contain;
51 }
52 .desc {
53 position: relative;
54 color: ${theme.backgroundColor};
55 background: var(--color);
56 border-radius: 10em;
57 padding: 0 0.5em;
58 line-height: 1.5;
59 }
60 .mask {
61 background-color: rgba(0, 0, 0, calc(${theme.maskAlpha} + 0.4));
62 }
63 .icon {
64 width: 1.5em;
65 color: ${theme.backgroundColor};
66 }
67 .mask:not(.status) .icon {
68 cursor: pointer;
69 }
70 .status .icon {
71 color: var(--color);
72 }
73`);
74/**
75 * @customElement dy-image-preview
76 */
77let DuoyunImagePreviewElement = class DuoyunImagePreviewElement extends GemElement {
78 constructor() {
79 super({ delegatesFocus: true });
80 _DuoyunImagePreviewElement_instances.add(this);
81 this.state = {
82 previewUrl: '',
83 };
84 this.mounted = () => {
85 const dimension = 64 * window.devicePixelRatio;
86 this.effect(async () => {
87 if (this.file) {
88 const previewUrl = await compressionImage(this.file, { dimension: { width: dimension, height: dimension } }, { type: 'url', aspectRatio: 1 });
89 this.setState({ previewUrl });
90 }
91 }, () => [this.file]);
92 };
93 this.render = () => {
94 if (!this.file)
95 return html ``;
96 const { previewUrl } = this.state;
97 const color = this.progress ? theme.informativeColor : __classPrivateFieldGet(this, _DuoyunImagePreviewElement_instances, "a", _DuoyunImagePreviewElement_color_get);
98 return html `
99 ${color
100 ? html `
101 <style>
102 :host {
103 --color: ${color} !important;
104 }
105 </style>
106 `
107 : ''}
108 ${previewUrl ? html `<img class="preview" alt=${this.file.name} src=${previewUrl}></img>` : ''}
109 ${this.progress
110 ? html `
111 <div
112 class="mask status"
113 style=${styleMap({
114 opacity: '0.4',
115 background: `linear-gradient(to top, ${color} ${this.progress}%, transparent ${this.progress}%);`,
116 })}
117 ></div>
118 <div class="desc" role="progressbar">${Math.floor(this.progress)}%</div>
119 `
120 : __classPrivateFieldGet(this, _DuoyunImagePreviewElement_instances, "a", _DuoyunImagePreviewElement_status_get) !== 'default'
121 ? html `
122 <div class="mask status">
123 <dy-use class="icon" .element=${__classPrivateFieldGet(this, _DuoyunImagePreviewElement_instances, "a", _DuoyunImagePreviewElement_icon_get)}></dy-use>
124 </div>
125 `
126 : this.actions
127 ? html `
128 <div class="mask">
129 ${this.actions.map(({ icon, handle }) => html `
130 <dy-use
131 class="icon"
132 .element=${icon}
133 role="button"
134 tabindex="0"
135 @keydown=${commonHandle}
136 @click=${() => handle()}
137 ></dy-use>
138 `)}
139 </div>
140 `
141 : ''}
142 `;
143 };
144 }
145};
146_DuoyunImagePreviewElement_instances = new WeakSet(), _DuoyunImagePreviewElement_status_get = function _DuoyunImagePreviewElement_status_get() {
147 return this.status || 'default';
148}, _DuoyunImagePreviewElement_color_get = function _DuoyunImagePreviewElement_color_get() {
149 return getSemanticColor(__classPrivateFieldGet(this, _DuoyunImagePreviewElement_instances, "a", _DuoyunImagePreviewElement_status_get));
150}, _DuoyunImagePreviewElement_icon_get = function _DuoyunImagePreviewElement_icon_get() {
151 switch (__classPrivateFieldGet(this, _DuoyunImagePreviewElement_instances, "a", _DuoyunImagePreviewElement_status_get)) {
152 case 'negative':
153 return icons.close;
154 case 'positive':
155 return icons.check;
156 }
157};
158__decorate([
159 attribute
160], DuoyunImagePreviewElement.prototype, "status", void 0);
161__decorate([
162 numattribute
163], DuoyunImagePreviewElement.prototype, "progress", void 0);
164__decorate([
165 property
166], DuoyunImagePreviewElement.prototype, "file", void 0);
167__decorate([
168 property
169], DuoyunImagePreviewElement.prototype, "actions", void 0);
170DuoyunImagePreviewElement = __decorate([
171 customElement('dy-image-preview'),
172 adoptedStyle(style),
173 adoptedStyle(focusStyle)
174], DuoyunImagePreviewElement);
175export { DuoyunImagePreviewElement };
176//# sourceMappingURL=image-preview.js.map
\No newline at end of file