1 | var __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 | };
|
7 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
8 | if (kind === "m") throw new TypeError("Private method is not writable");
|
9 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
10 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
11 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
12 | };
|
13 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
14 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
15 | 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");
|
16 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
17 | };
|
18 | var _DuoyunToastElement_over, _DuoyunToastElement_getIcon;
|
19 | import { adoptedStyle, customElement, property } from '@mantou/gem/lib/decorators';
|
20 | import { GemElement, html, TemplateResult } from '@mantou/gem/lib/element';
|
21 | import { createCSSSheet, css, classMap, isArrayChange } from '@mantou/gem/lib/utils';
|
22 | import { icons } from '../lib/icons';
|
23 | import { theme } from '../lib/theme';
|
24 | import './use';
|
25 | const style = createCSSSheet(css `
|
26 | :host(:where(:not([hidden]))) {
|
27 | z-index: ${theme.popupZIndex};
|
28 | position: fixed;
|
29 | display: flex;
|
30 | align-items: center;
|
31 | flex-direction: column;
|
32 | gap: 1em;
|
33 | top: calc(1em + env(titlebar-area-height, var(--titlebar-area-height, 0px)));
|
34 | left: 50%;
|
35 | transform: translateX(-50%);
|
36 | max-width: 90%;
|
37 | font-size: 0.875em;
|
38 | }
|
39 | .item {
|
40 | display: flex;
|
41 | align-items: center;
|
42 | gap: 0.5em;
|
43 | color: white;
|
44 | background: ${theme.informativeColor};
|
45 | border-radius: ${theme.normalRound};
|
46 | padding: 0.6em 0.8em;
|
47 | line-height: 1;
|
48 | max-width: 100%;
|
49 | }
|
50 | .success {
|
51 | background: ${theme.positiveColor};
|
52 | }
|
53 | .warning {
|
54 | background: ${theme.noticeColor};
|
55 | }
|
56 | .error {
|
57 | background: ${theme.negativeColor};
|
58 | }
|
59 | .icon {
|
60 | width: 1.2em;
|
61 | }
|
62 | .body {
|
63 | overflow: hidden;
|
64 | text-overflow: ellipsis;
|
65 | white-space: nowrap;
|
66 | }
|
67 | .body::first-letter {
|
68 | text-transform: capitalize;
|
69 | }
|
70 | `);
|
71 | const itemMap = new Map();
|
72 |
|
73 |
|
74 |
|
75 | let DuoyunToastElement = class DuoyunToastElement extends GemElement {
|
76 | constructor() {
|
77 | super();
|
78 | _DuoyunToastElement_over.set(this, Promise.resolve());
|
79 | _DuoyunToastElement_getIcon.set(this, (type) => {
|
80 | switch (type) {
|
81 | case 'success':
|
82 | return icons.success;
|
83 | case 'warning':
|
84 | return icons.warning;
|
85 | case 'error':
|
86 | return icons.error;
|
87 | default:
|
88 | return icons.info;
|
89 | }
|
90 | });
|
91 | this.mounted = () => {
|
92 | Toast.instance = this;
|
93 | return () => (Toast.instance = undefined);
|
94 | };
|
95 | this.render = () => {
|
96 | var _a;
|
97 | return html `
|
98 | ${(_a = this.data) === null || _a === void 0 ? void 0 : _a.map(({ type, content }) => html `
|
99 | <div class=${classMap({ item: true, [type]: true })}>
|
100 | <dy-use class="icon" .element=${__classPrivateFieldGet(this, _DuoyunToastElement_getIcon, "f").call(this, type)}></dy-use>
|
101 | <span class="body">${content}</span>
|
102 | </div>
|
103 | `)}
|
104 | `;
|
105 | };
|
106 | this.internals.role = 'alert';
|
107 | this.internals.ariaLive = 'polite';
|
108 | if (Toast.instance)
|
109 | throw new Error('Single instance component');
|
110 | document.body.append(this);
|
111 | this.addEventListener('mouseover', () => {
|
112 | __classPrivateFieldSet(this, _DuoyunToastElement_over, new Promise((res) => {
|
113 | this.addEventListener('mouseout', () => res(), { once: true });
|
114 | }), "f");
|
115 | });
|
116 | }
|
117 | static open(type, content, { debug, duration = 3000 } = {}) {
|
118 | var _a;
|
119 | const toast = Toast.instance || new Toast();
|
120 | const item = ((_a = toast.data) === null || _a === void 0 ? void 0 : _a.find((e) => {
|
121 | if (e.type !== type)
|
122 | return false;
|
123 |
|
124 | return e.content instanceof TemplateResult && content instanceof TemplateResult
|
125 | ? content.strings.join() === e.content.strings.join() &&
|
126 | !isArrayChange(content.values, e.content.values)
|
127 | : e.content === content;
|
128 | })) || { type, content };
|
129 | clearTimeout(itemMap.get(item));
|
130 | toast.data = [...(toast.data || []).filter((e) => e !== item), item];
|
131 | itemMap.set(item, window.setTimeout(async () => {
|
132 | await __classPrivateFieldGet(toast, _DuoyunToastElement_over, "f");
|
133 | if (!toast.data)
|
134 | return;
|
135 | itemMap.delete(item);
|
136 | toast.data = toast.data.filter((e) => e !== item);
|
137 | if (toast.data.length === 0)
|
138 | toast.remove();
|
139 | }, debug ? 1000000 : duration));
|
140 | }
|
141 | };
|
142 | _DuoyunToastElement_over = new WeakMap(), _DuoyunToastElement_getIcon = new WeakMap();
|
143 | __decorate([
|
144 | property
|
145 | ], DuoyunToastElement.prototype, "data", void 0);
|
146 | DuoyunToastElement = __decorate([
|
147 | customElement('dy-toast'),
|
148 | adoptedStyle(style)
|
149 | ], DuoyunToastElement);
|
150 | export { DuoyunToastElement };
|
151 | export const Toast = DuoyunToastElement;
|
152 |
|
\ | No newline at end of file |