ew-viewer 0.0.0-stage → 0.0.1
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/CHANGELOG.md +32 -0
- package/CHANGELOG.zh-CN.md +32 -0
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/README.zh-CN.md +124 -0
- package/dist/icons.d.ts +15 -0
- package/dist/index.cjs +33 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.iife.js +36 -0
- package/dist/index.js +559 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/template.d.ts +4 -0
- package/dist/internal/utils.d.ts +2 -0
- package/dist/style.css +172 -0
- package/dist/types.d.ts +64 -0
- package/dist/viewer.d.ts +113 -0
- package/package.json +61 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
var D = Object.defineProperty;
|
|
2
|
+
var A = (s, e, t) => e in s ? D(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var n = (s, e, t) => A(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { createConsoleLogger as V, isBoolean as F, isString as k, handleClassName as l, isBrowser as W, isValidNumber as x, isDom as O, isObject as q, isNumber as b, checkContainer as N, $ as g, on as $, addClass as R, removeClass as z, extend as E } from "@ew-color-picker/utils";
|
|
5
|
+
const d = V({
|
|
6
|
+
prefix: "ew-viewer",
|
|
7
|
+
timestamp: !1
|
|
8
|
+
}), Y = (s) => `<svg t="1697700092492" class="add-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="900" width="78" height="78"><path d="M560.064 149.952a48 48 0 0 0-96 0V464H150.016a48 48 0 0 0 0 96H464v313.984a48 48 0 0 0 96 0V560h314.048a48 48 0 0 0 0-96H560V149.952z" p-id="901" fill="#ffffff"></path></svg>`, Z = (s) => `<svg t="1697700199963" class="reduce-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1256" width="78" height="78"><path d="M922.048 512a48 48 0 0 0-48-48H149.952a48 48 0 1 0 0 96h724.096a48 48 0 0 0 48-48z" p-id="1257" fill="#ffffff"></path></svg>`, X = (s) => `<svg t="1690189203554" class="close-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2272" fill="currentColor"><path d="M504.224 470.288l207.84-207.84a16 16 0 0 1 22.608 0l11.328 11.328a16 16 0 0 1 0 22.624l-207.84 207.824 207.84 207.84a16 16 0 0 1 0 22.608l-11.328 11.328a16 16 0 0 1-22.624 0l-207.824-207.84-207.84 207.84a16 16 0 0 1-22.608 0l-11.328-11.328a16 16 0 0 1 0-22.624l207.84-207.824-207.84-207.84a16 16 0 0 1 0-22.608l11.328-11.328a16 16 0 0 1 22.624 0l207.824 207.84z" p-id="2273"></path></svg>`, U = (s) => `<svg t="1697700658302" class="one-to-one-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7084" width="78" height="78"><path d="M904.405333 149.333333v725.333334h-85.333333V250.346667l-66.24 33.152L714.666667 207.146667 830.336 149.333333h74.069333z m-608 0v725.333334h-85.333333V250.346667L144.853333 283.498667 106.666667 207.146667 222.336 149.333333h74.069333z m288 448v106.666667h-106.666666v-106.666667h106.666666z m0-277.333333v106.666667h-106.666666v-106.666667h106.666666z" fill="#ffffff" p-id="7085"></path></svg>`, j = (s) => `<svg t="1697712563708" class="arrow-up-down-icon${s}" viewBox="0 0 1088 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="20575" width="78" height="78"><path d="M513.344 253.504V768H323.968l244.416 255.424L825.344 768H640V253.504h185.344L566.848 6.72 319.424 253.504h193.92z" fill="#ffffff" p-id="20576"></path></svg>`, G = (s) => `<svg t="1697712627137" class="arrow-left-right-icon${s}" viewBox="0 0 1088 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="21609" width="78" height="78"><path d="M320 576h516.224v183.296l246.4-244.48-246.4-256.832L832 448H320l-7.296-190.08L65.92 516.48l246.784 247.424L320 576z" fill="#ffffff" p-id="21610"></path></svg>`, _ = (s) => `<svg t="1697712701928" class="flip-left-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="25120" width="78" height="78"><path d="M672 418H144c-17.7 0-32 14.3-32 32v414c0 17.7 14.3 32 32 32h528c17.7 0 32-14.3 32-32V450c0-17.7-14.3-32-32-32z m-44 402H188V494h440v326z" p-id="25121" fill="#ffffff"></path><path d="M819.3 328.5c-78.8-100.7-196-153.6-314.6-154.2l-0.2-64c0-6.5-7.6-10.1-12.6-6.1l-128 101c-4 3.1-3.9 9.1 0 12.3L492 318.6c5.1 4 12.7 0.4 12.6-6.1v-63.9c12.9 0.1 25.9 0.9 38.8 2.5 42.1 5.2 82.1 18.2 119 38.7 38.1 21.2 71.2 49.7 98.4 84.3 27.1 34.7 46.7 73.7 58.1 115.8 11 40.7 14 82.7 8.9 124.8-0.7 5.4-1.4 10.8-2.4 16.1h74.9c14.8-103.6-11.3-213-81-302.3z" p-id="25122" fill="#ffffff"></path></svg>`, J = (s) => `<svg t="1697712744122" class="flip-right-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="25323" width="78" height="78"><path d="M480.5 251.2c13-1.6 25.9-2.4 38.8-2.5v63.9c0 6.5 7.5 10.1 12.6 6.1L660 217.6c4-3.2 4-9.2 0-12.3l-128-101c-5.1-4-12.6-0.4-12.6 6.1l-0.2 64c-118.6 0.5-235.8 53.4-314.6 154.2-69.6 89.2-95.7 198.6-81.1 302.4h74.9c-0.9-5.3-1.7-10.7-2.4-16.1-5.1-42.1-2.1-84.1 8.9-124.8 11.4-42.2 31-81.1 58.1-115.8 27.2-34.7 60.3-63.2 98.4-84.3 37-20.6 76.9-33.6 119.1-38.8z" p-id="25324" fill="#ffffff"></path><path d="M880 418H352c-17.7 0-32 14.3-32 32v414c0 17.7 14.3 32 32 32h528c17.7 0 32-14.3 32-32V450c0-17.7-14.3-32-32-32z m-44 402H396V494h440v326z" p-id="25325" fill="#ffffff"></path></svg>`, K = (s) => `<svg t="1697787947316" class="arrow-left-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="900" width="78" height="78"><path d="M290.56 550.464a54.4 54.4 0 0 1 0-76.928l379.52-379.52a48 48 0 1 1 67.84 67.904L387.84 512l350.08 350.08a48 48 0 1 1-67.84 67.84L290.56 550.464z" p-id="901" fill="#ffffff"></path></svg>`, Q = (s) => `<svg t="1697772815002" class="arrow-right-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="900" width="78" height="78"><path d="M733.44 550.464a54.4 54.4 0 0 0 0-76.928l-379.52-379.52A48 48 0 0 0 286.08 161.92L636.16 512l-350.08 350.08a48 48 0 1 0 67.84 67.84l379.52-379.456z" p-id="901" fill="#ffffff"></path></svg>`, ee = (s) => `<svg t="1697773227169" class="reset-icon${s}" viewBox="0 0 1278 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2019" width="78" height="78"><path d="M1272.447024 472.907505l-150.052574 149.245613a32.021415 32.021415 0 0 1-44.681788 0L928.777033 472.906505c-17.717123-20.478986-5.895708-27.925618 6.45368-39.716035h136.525243A441.782135 441.782135 0 0 0 253.257466 304.732828L178.791151 275.257287a521.555187 521.555187 0 0 1 973.731808 157.934183h113.56338a26.653681 26.653681 0 0 1 6.360685 39.716035zM202.713967 440.946087l150.022575 149.556598a26.622682 26.622682 0 0 1-6.45468 39.717034H216.366292a442.247112 442.247112 0 0 0 789.701915 120.700026l75.213278 29.787526A521.555187 521.555187 0 0 1 134.513343 630.219719h-118.994111c-12.411386-11.791416-24.170804-19.238048-6.453681-39.717034l148.935629-149.556598a32.641385 32.641385 0 0 1 44.712787 0z" fill="#ffffff" p-id="2020"></path></svg>`, te = (s) => `<svg t="1697787617768" class="fullscreen-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3046" width="78" height="78"><path d="M213.333333 213.333333h213.333334V128H170.666667a42.666667 42.666667 0 0 0-42.666667 42.666667v256h85.333333V213.333333zM170.666667 896h256v-85.333333H213.333333v-213.333334H128v256a42.666667 42.666667 0 0 0 42.666667 42.666667z m725.333333-42.666667v-256h-85.333333v213.333334h-213.333334v85.333333h256a42.666667 42.666667 0 0 0 42.666667-42.666667zM597.333333 213.333333h213.333334v213.333334h85.333333V170.666667a42.666667 42.666667 0 0 0-42.666667-42.666667h-256v85.333333z" fill="#ffffff" p-id="3047"></path></svg>`, ie = (s) => `<svg t="1697800175999" class="play-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9820" width="78" height="78"><path d="M852.5 533.9L279 864.7c-11.9 6.9-27.2 2.8-34.1-9.1-2.2-3.8-3.3-8.1-3.3-12.5V181.5c0-13.8 11.2-24.9 24.9-24.9 4.4 0 8.7 1.2 12.5 3.3l573.4 330.8c11.9 6.9 16 22.1 9.1 34.1-2.1 3.8-5.2 6.9-9 9.1z" p-id="9821" fill="#ffffff"></path></svg>`, se = (s) => `<svg t="1697799289205" class="pause-icon${s}" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8597" width="78" height="78"><path d="M209.645253 863.934444l201.049992 0 0-703.866842L209.645253 160.067602 209.645253 863.934444zM611.804588 863.934444l201.113437 0 0-703.866842L611.804588 160.067602 611.804588 863.934444z" p-id="8598" fill="#ffffff"></path></svg>`;
|
|
9
|
+
function oe(s) {
|
|
10
|
+
const e = document.createElement("div");
|
|
11
|
+
return e.innerHTML = s, e.firstElementChild;
|
|
12
|
+
}
|
|
13
|
+
function u(s, e) {
|
|
14
|
+
return F(s) && s && k(e) && e ? e : "";
|
|
15
|
+
}
|
|
16
|
+
const ne = 450, ae = /* @__PURE__ */ new Set([
|
|
17
|
+
"mask",
|
|
18
|
+
"content",
|
|
19
|
+
"footer",
|
|
20
|
+
"title",
|
|
21
|
+
"toolbar",
|
|
22
|
+
"thumb",
|
|
23
|
+
"close"
|
|
24
|
+
]), re = {
|
|
25
|
+
isClickOutside: !0,
|
|
26
|
+
previewMode: "bg",
|
|
27
|
+
showCloseBtn: !0,
|
|
28
|
+
showCloseBtnIcon: X(l("ew-viewer-close-icon")),
|
|
29
|
+
closeTime: 200,
|
|
30
|
+
zoomOnWheeling: !0,
|
|
31
|
+
autoSize: !0,
|
|
32
|
+
minImageRatio: 0.1,
|
|
33
|
+
maxImageRatio: 1,
|
|
34
|
+
showToolBar: !0,
|
|
35
|
+
zoomInIcon: Y(l("ew-viewer-zoom-in-icon")),
|
|
36
|
+
zoomOutIcon: Z(l("ew-viewer-zoom-out-icon")),
|
|
37
|
+
oneToOneIcon: U(l("ew-viewer-one-to-one-icon")),
|
|
38
|
+
currentImageIndex: 0,
|
|
39
|
+
autoOpen: !1,
|
|
40
|
+
flipLeftIcon: G(l("ew-viewer-flip-left-icon")),
|
|
41
|
+
flipRightIcon: j(l("ew-viewer-flip-right-icon")),
|
|
42
|
+
rotateLeftIcon: _(l("ew-viewer-rotate-left-icon")),
|
|
43
|
+
rotateRightIcon: J(
|
|
44
|
+
l("ew-viewer-rotate-right-icon")
|
|
45
|
+
),
|
|
46
|
+
resetIcon: ee(l("ew-viewer-reset-icon")),
|
|
47
|
+
prevIcon: K(l("ew-viewer-prev-icon")),
|
|
48
|
+
nextIcon: Q(l("ew-viewer-next-icon")),
|
|
49
|
+
fullscreenIcon: te(l("ew-viewer-fullscreen-icon")),
|
|
50
|
+
playIcon: ie(l("ew-viewer-play-icon")),
|
|
51
|
+
pauseIcon: se(l("ew-viewer-pause-icon")),
|
|
52
|
+
minRotate: -1 / 0,
|
|
53
|
+
maxRotate: 1 / 0,
|
|
54
|
+
loop: !0,
|
|
55
|
+
zIndex: 2e3
|
|
56
|
+
};
|
|
57
|
+
function S(s, e) {
|
|
58
|
+
if (!e)
|
|
59
|
+
return E({}, s);
|
|
60
|
+
const t = e, i = {};
|
|
61
|
+
return Object.keys(t).forEach((o) => {
|
|
62
|
+
t[o] !== void 0 && (i[o] = t[o]);
|
|
63
|
+
}), E({}, s, i);
|
|
64
|
+
}
|
|
65
|
+
function le(s) {
|
|
66
|
+
if (k(s))
|
|
67
|
+
return s.trim() || null;
|
|
68
|
+
if (O(s)) {
|
|
69
|
+
const e = s;
|
|
70
|
+
return (e.getAttribute?.("src") ?? e.src) || null;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function ce(s) {
|
|
75
|
+
return new Promise((e) => {
|
|
76
|
+
const t = new Image();
|
|
77
|
+
t.onload = () => e({
|
|
78
|
+
naturalWidth: t.naturalWidth,
|
|
79
|
+
naturalHeight: t.naturalHeight,
|
|
80
|
+
scaleWidth: 0,
|
|
81
|
+
scaleHeight: 0,
|
|
82
|
+
scaleX: 0,
|
|
83
|
+
scaleY: 0,
|
|
84
|
+
rotate: 0
|
|
85
|
+
}), t.onerror = () => e(null), t.src = s;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function C(s) {
|
|
89
|
+
const e = Number(s);
|
|
90
|
+
return x(e) && e > 0 && e <= 1 ? e : 0.9;
|
|
91
|
+
}
|
|
92
|
+
function M(s) {
|
|
93
|
+
return s.fullscreenElement || s.webkitFullscreenElement || s.mozFullScreenElement || s.msFullscreenElement || null;
|
|
94
|
+
}
|
|
95
|
+
function P(s) {
|
|
96
|
+
return s?.closest?.("[data-ew-role]")?.getAttribute("data-ew-role") ?? null;
|
|
97
|
+
}
|
|
98
|
+
class me {
|
|
99
|
+
constructor(e) {
|
|
100
|
+
n(this, "config", S(re));
|
|
101
|
+
n(this, "options");
|
|
102
|
+
n(this, "imageDataList", []);
|
|
103
|
+
n(this, "imageStyle", []);
|
|
104
|
+
n(this, "currentImageIndex", 0);
|
|
105
|
+
n(this, "computedImageRatio", 0.9);
|
|
106
|
+
n(this, "wheeling", !1);
|
|
107
|
+
n(this, "isClickZoomIn", !1);
|
|
108
|
+
n(this, "isClickZoomOut", !1);
|
|
109
|
+
n(this, "isClickOneToOne", !1);
|
|
110
|
+
n(this, "fullScreenStatus", !1);
|
|
111
|
+
n(this, "isPlaying", !1);
|
|
112
|
+
n(this, "$dom", {});
|
|
113
|
+
n(this, "ac", null);
|
|
114
|
+
n(this, "timers", /* @__PURE__ */ new Set());
|
|
115
|
+
n(this, "carouselTimer", null);
|
|
116
|
+
n(this, "rafId", null);
|
|
117
|
+
n(this, "pendingOpen", null);
|
|
118
|
+
n(this, "seq", 0);
|
|
119
|
+
/** 轨道位移位置:0 为首图前置克隆位,1 ~ n 为真实图片,n + 1 为尾图后继克隆位 */
|
|
120
|
+
n(this, "slidePos", 1);
|
|
121
|
+
n(this, "handleRootClick", (e) => {
|
|
122
|
+
const t = P(e.target);
|
|
123
|
+
if (t !== "fullscreen") {
|
|
124
|
+
if (this.fullScreenStatus) {
|
|
125
|
+
this.exitFullscreen(), this.fullScreenStatus = !1;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.config.isClickOutside && t !== null && ae.has(t) && this.close();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
n(this, "handleResize", () => {
|
|
132
|
+
if (this.rafId === null) {
|
|
133
|
+
if (typeof requestAnimationFrame != "function") {
|
|
134
|
+
this.setImageStyle();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.rafId = requestAnimationFrame(() => {
|
|
138
|
+
this.rafId = null, this.setImageStyle();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
n(this, "handleWheel", (e) => {
|
|
143
|
+
e.preventDefault(), !this.wheeling && (this.wheeling = !0, this.setTimer(() => {
|
|
144
|
+
this.wheeling = !1;
|
|
145
|
+
}, 150), this.viewedImageHandler(e.deltaY < 0));
|
|
146
|
+
});
|
|
147
|
+
n(this, "handleThumbClick", (e) => {
|
|
148
|
+
e.stopPropagation();
|
|
149
|
+
const t = e.target;
|
|
150
|
+
if (!t?.className?.includes?.("ew-viewer-thumb-img"))
|
|
151
|
+
return;
|
|
152
|
+
const i = Number(t.getAttribute("data-index"));
|
|
153
|
+
x(i) && (this.currentImageIndex = i, this.changeThumbImage(), this.slideTo(i), this.setImageStyle());
|
|
154
|
+
});
|
|
155
|
+
n(this, "handleToolbarClick", (e) => {
|
|
156
|
+
const t = this.currentMeta();
|
|
157
|
+
if (!t)
|
|
158
|
+
return;
|
|
159
|
+
const { minRotate: i, maxRotate: o } = this.config;
|
|
160
|
+
switch (P(e.target)) {
|
|
161
|
+
case "zoom-in":
|
|
162
|
+
if (this.isClickZoomIn)
|
|
163
|
+
return;
|
|
164
|
+
this.isClickZoomIn = !0, this.setTimer(() => {
|
|
165
|
+
this.isClickZoomIn = !1;
|
|
166
|
+
}, 50), this.viewedImageHandler(!0);
|
|
167
|
+
break;
|
|
168
|
+
case "zoom-out":
|
|
169
|
+
if (this.isClickZoomOut)
|
|
170
|
+
return;
|
|
171
|
+
this.isClickZoomOut = !0, this.setTimer(() => {
|
|
172
|
+
this.isClickZoomOut = !1;
|
|
173
|
+
}, 50), this.viewedImageHandler(!1);
|
|
174
|
+
break;
|
|
175
|
+
case "one-to-one": {
|
|
176
|
+
if (this.isClickOneToOne)
|
|
177
|
+
return;
|
|
178
|
+
this.isClickOneToOne = !0, this.setTimer(() => {
|
|
179
|
+
this.isClickOneToOne = !1;
|
|
180
|
+
}, 50), this.viewedImageHandler(this.computedImageRatio === 1 ? 0.5 : 1);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case "rotate-left":
|
|
184
|
+
t.rotate -= 90, b(i) && i !== -1 / 0 && t.rotate <= i && (t.rotate = i), this.setImageStyle();
|
|
185
|
+
break;
|
|
186
|
+
case "rotate-right":
|
|
187
|
+
t.rotate += 90, b(o) && o !== 1 / 0 && t.rotate >= o && (t.rotate = o), this.setImageStyle();
|
|
188
|
+
break;
|
|
189
|
+
case "flip-horizontal":
|
|
190
|
+
t.scaleX = t.scaleX === -1 ? 0 : -1, this.setImageStyle();
|
|
191
|
+
break;
|
|
192
|
+
case "flip-vertical":
|
|
193
|
+
t.scaleY = t.scaleY === -1 ? 0 : -1, this.setImageStyle();
|
|
194
|
+
break;
|
|
195
|
+
case "reset":
|
|
196
|
+
this.resetImage();
|
|
197
|
+
break;
|
|
198
|
+
case "prev":
|
|
199
|
+
this.manualGo(-1);
|
|
200
|
+
break;
|
|
201
|
+
case "next":
|
|
202
|
+
this.manualGo(1);
|
|
203
|
+
break;
|
|
204
|
+
case "fullscreen":
|
|
205
|
+
this.fullScreenStatus = !this.fullScreenStatus, this.fullScreenStatus ? this.requestFullscreen() : this.exitFullscreen();
|
|
206
|
+
break;
|
|
207
|
+
case "play":
|
|
208
|
+
this.togglePlay();
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
if (!W) {
|
|
213
|
+
d.warn("EWViewer 只能在浏览器环境中使用");
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.computedImageRatio = C(this.config.imageRatio), this.reload(e);
|
|
217
|
+
}
|
|
218
|
+
/** 依次完成“解析入参 → 注入样式 → 渲染”,并用序号丢弃过期的异步结果 */
|
|
219
|
+
reload(e) {
|
|
220
|
+
const t = ++this.seq;
|
|
221
|
+
this.init(e).then((i) => {
|
|
222
|
+
t !== this.seq || !i || this.render();
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
async resolveImages(e) {
|
|
226
|
+
const t = e.map(le).filter((r) => !!r);
|
|
227
|
+
if (t.length === 0)
|
|
228
|
+
return d.warn("传入的图片地址不合法"), !1;
|
|
229
|
+
const i = await Promise.all(t.map(ce)), o = [], a = [];
|
|
230
|
+
if (i.forEach((r, w) => {
|
|
231
|
+
if (!r) {
|
|
232
|
+
d.warn(`图片加载失败,已忽略:${t[w]}`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
o.push(t[w]), a.push(r);
|
|
236
|
+
}), o.length === 0)
|
|
237
|
+
return !1;
|
|
238
|
+
this.imageDataList = o, this.imageStyle = a;
|
|
239
|
+
const h = this.config.currentImageIndex;
|
|
240
|
+
return this.currentImageIndex = x(h) && h >= 0 && h < o.length ? h : 0, !0;
|
|
241
|
+
}
|
|
242
|
+
/** 解析入参:字符串 / img 元素 / 数组 / 配置对象,返回是否拿到可用图片 */
|
|
243
|
+
async init(e) {
|
|
244
|
+
if (k(e) || O(e))
|
|
245
|
+
return this.resolveImages([e]);
|
|
246
|
+
if (Array.isArray(e))
|
|
247
|
+
return this.resolveImages(e);
|
|
248
|
+
if (q(e)) {
|
|
249
|
+
const { img: t, ...i } = e;
|
|
250
|
+
return this.config = S(this.config, i), this.validateRatio(), this.computedImageRatio = C(this.config.imageRatio), t == null ? (d.warn("未提供 img 参数"), !1) : this.resolveImages(Array.isArray(t) ? t : [t]);
|
|
251
|
+
}
|
|
252
|
+
return d.warn("传入的参数不合法"), !1;
|
|
253
|
+
}
|
|
254
|
+
validateRatio() {
|
|
255
|
+
const { minImageRatio: e, maxImageRatio: t } = this.config;
|
|
256
|
+
this.rangeRation(e) || (this.config.minImageRatio = 0.1, d.warn("minImageRatio 应在 0 到 1 之间")), this.rangeRation(t) || (this.config.maxImageRatio = 1, d.warn("maxImageRatio 应在 0 到 1 之间")), this.config.minImageRatio > this.config.maxImageRatio && d.warn("minImageRatio 不应大于 maxImageRatio");
|
|
257
|
+
}
|
|
258
|
+
rangeRation(e) {
|
|
259
|
+
return b(e) ? e >= 0 && e <= 1 : !1;
|
|
260
|
+
}
|
|
261
|
+
async render() {
|
|
262
|
+
this.teardown();
|
|
263
|
+
const e = N(this.config.container), t = this.currentMeta();
|
|
264
|
+
if (!e || !t)
|
|
265
|
+
return;
|
|
266
|
+
const {
|
|
267
|
+
previewMode: i,
|
|
268
|
+
showCloseBtn: o,
|
|
269
|
+
showCloseBtnIcon: a,
|
|
270
|
+
showToolBar: h
|
|
271
|
+
} = this.config, r = this.imageDataList[this.currentImageIndex] ?? "", w = `${Math.floor(this.computedImageRatio * 100)}%`, c = this.imageDataList.length > 1, m = `
|
|
272
|
+
<div class="ew-viewer-zoom-in" data-ew-role="zoom-in">${this.config.zoomInIcon}</div>
|
|
273
|
+
<div class="ew-viewer-zoom-out" data-ew-role="zoom-out">${this.config.zoomOutIcon}</div>
|
|
274
|
+
<div class="ew-viewer-one-to-one" data-ew-role="one-to-one">${this.config.oneToOneIcon}</div>
|
|
275
|
+
<div class="ew-viewer-reset" data-ew-role="reset">${this.config.resetIcon}</div>
|
|
276
|
+
<div class="ew-viewer-fullscreen" data-ew-role="fullscreen">${this.config.fullscreenIcon}</div>
|
|
277
|
+
${u(
|
|
278
|
+
c,
|
|
279
|
+
`<div class="ew-viewer-prev" data-ew-role="prev">${this.config.prevIcon}</div>`
|
|
280
|
+
)}
|
|
281
|
+
${u(
|
|
282
|
+
c,
|
|
283
|
+
`<div class="ew-viewer-play" data-ew-role="play">${this.isPlaying ? this.config.pauseIcon : this.config.playIcon}</div>`
|
|
284
|
+
)}
|
|
285
|
+
${u(
|
|
286
|
+
c,
|
|
287
|
+
`<div class="ew-viewer-next" data-ew-role="next">${this.config.nextIcon}</div>`
|
|
288
|
+
)}
|
|
289
|
+
<div class="ew-viewer-rotate-left" data-ew-role="rotate-left">${this.config.rotateLeftIcon}</div>
|
|
290
|
+
<div class="ew-viewer-rotate-right" data-ew-role="rotate-right">${this.config.rotateRightIcon}</div>
|
|
291
|
+
<div class="ew-viewer-flip-horizontal" data-ew-role="flip-horizontal">${this.config.flipLeftIcon}</div>
|
|
292
|
+
<div class="ew-viewer-flip-vertical" data-ew-role="flip-vertical">${this.config.flipRightIcon}</div>
|
|
293
|
+
`, p = (I, v, B = !1) => `
|
|
294
|
+
<div class="ew-viewer-img-box${l(
|
|
295
|
+
v === this.currentImageIndex ? "show" : ""
|
|
296
|
+
)}" data-index="${v}" data-ew-role="image"${B ? ' data-clone="true"' : ""}>
|
|
297
|
+
${i === "bg" ? `<div class="ew-viewer-bg-img" style="background-image:url(${I});"></div>` : `<img src="${I}" class="ew-viewer-img" />`}
|
|
298
|
+
</div>`, T = this.imageDataList.map((I, v) => p(I, v)).join(""), y = `<div class="ew-viewer-track">
|
|
299
|
+
${u(
|
|
300
|
+
c,
|
|
301
|
+
p(
|
|
302
|
+
this.imageDataList[this.imageDataList.length - 1],
|
|
303
|
+
this.imageDataList.length - 1,
|
|
304
|
+
!0
|
|
305
|
+
)
|
|
306
|
+
)}
|
|
307
|
+
${T}
|
|
308
|
+
${u(
|
|
309
|
+
c,
|
|
310
|
+
p(this.imageDataList[0], 0, !0)
|
|
311
|
+
)}
|
|
312
|
+
</div>`, H = this.imageDataList.map(
|
|
313
|
+
(I, v) => `<img src="${I}" class="ew-viewer-thumb-img${l(
|
|
314
|
+
v === this.currentImageIndex ? "active" : ""
|
|
315
|
+
)}" data-index="${v}"/>`
|
|
316
|
+
).join(""), f = oe(`<div class="ew-viewer hidden">
|
|
317
|
+
<div class="ew-viewer-mask" data-ew-role="mask">
|
|
318
|
+
<div class="ew-viewer-content" data-ew-role="content">${y}</div>
|
|
319
|
+
<div class="ew-viewer-tooltip-ratio hidden" data-ew-role="tooltip">${w}</div>
|
|
320
|
+
<div class="ew-viewer-footer" data-ew-role="footer">
|
|
321
|
+
<div class="ew-viewer-title" data-ew-role="title">${this.getImageNameFromURL(
|
|
322
|
+
r
|
|
323
|
+
)}(${t.naturalWidth} x ${t.naturalHeight})</div>
|
|
324
|
+
${u(
|
|
325
|
+
h,
|
|
326
|
+
`<div class="ew-viewer-button" data-ew-role="toolbar">${m}</div>`
|
|
327
|
+
)}
|
|
328
|
+
${u(
|
|
329
|
+
c,
|
|
330
|
+
`<div class="ew-viewer-thumb" data-ew-role="thumb">${H}</div>`
|
|
331
|
+
)}
|
|
332
|
+
</div>
|
|
333
|
+
${u(
|
|
334
|
+
o,
|
|
335
|
+
`<span class="ew-viewer-close-btn" data-ew-role="close">${a}</span>`
|
|
336
|
+
)}
|
|
337
|
+
</div>
|
|
338
|
+
</div>`);
|
|
339
|
+
if (!f)
|
|
340
|
+
return;
|
|
341
|
+
x(this.config.zIndex) && f.style.setProperty("--ew-viewer-z-index", `${this.config.zIndex}`), e.appendChild(f), this.$dom.$el = f, this.$dom.$viewerContentElement = g(".ew-viewer-content", f), this.$dom.$viewerTrackElement = g(".ew-viewer-track", f), this.$dom.$viewerRatioTooltipElement = g(".ew-viewer-tooltip-ratio", f), this.$dom.$viewerToolbarElement = g(".ew-viewer-button", f), this.$dom.$viewerThumbElement = g(".ew-viewer-thumb", f), this.$dom.$viewerTitleElement = g(".ew-viewer-title", f), this.slidePos = this.indexToPos(this.currentImageIndex), this.setTrackPosition(this.slidePos, !1), this.bindEvent();
|
|
342
|
+
const L = this.pendingOpen;
|
|
343
|
+
this.pendingOpen = null, L ? this.open(L.time) : this.config.autoOpen && this.open();
|
|
344
|
+
}
|
|
345
|
+
bindEvent() {
|
|
346
|
+
const e = this.$dom.$el;
|
|
347
|
+
if (!e)
|
|
348
|
+
return;
|
|
349
|
+
this.ac = new AbortController();
|
|
350
|
+
const { signal: t } = this.ac;
|
|
351
|
+
$(e, "click", this.handleRootClick, { signal: t }), this.config.autoSize && $(window, "resize", this.handleResize, { signal: t }), this.config.zoomOnWheeling && $(e, "wheel", this.handleWheel, { signal: t });
|
|
352
|
+
const i = this.$dom.$viewerToolbarElement;
|
|
353
|
+
i && $(i, "click", this.handleToolbarClick, { signal: t });
|
|
354
|
+
const o = this.$dom.$viewerThumbElement;
|
|
355
|
+
o && $(o, "click", this.handleThumbClick, { signal: t });
|
|
356
|
+
}
|
|
357
|
+
currentMeta() {
|
|
358
|
+
return this.imageStyle[this.currentImageIndex] ?? null;
|
|
359
|
+
}
|
|
360
|
+
get interval() {
|
|
361
|
+
const e = Number(this.config.interval);
|
|
362
|
+
return x(e) && e > 0 ? e : 5e3;
|
|
363
|
+
}
|
|
364
|
+
setTimer(e, t) {
|
|
365
|
+
const i = setTimeout(() => {
|
|
366
|
+
this.timers.delete(i), e();
|
|
367
|
+
}, t);
|
|
368
|
+
return this.timers.add(i), i;
|
|
369
|
+
}
|
|
370
|
+
clearTimers() {
|
|
371
|
+
this.timers.forEach((e) => clearTimeout(e)), this.timers.clear();
|
|
372
|
+
}
|
|
373
|
+
teardown() {
|
|
374
|
+
this.ac?.abort(), this.ac = null, this.clearTimers(), this.carouselTimer = null, this.rafId !== null && (typeof cancelAnimationFrame == "function" && cancelAnimationFrame(this.rafId), this.rafId = null), this.$dom.$el?.remove(), this.$dom = {};
|
|
375
|
+
}
|
|
376
|
+
stopCarousel() {
|
|
377
|
+
this.carouselTimer !== null && (clearTimeout(this.carouselTimer), this.timers.delete(this.carouselTimer), this.carouselTimer = null);
|
|
378
|
+
}
|
|
379
|
+
/** 真实下标对应的轨道位置:多图时前面有首图克隆位,单图没有克隆位 */
|
|
380
|
+
indexToPos(e) {
|
|
381
|
+
return this.imageDataList.length > 1 ? e + 1 : 0;
|
|
382
|
+
}
|
|
383
|
+
/** 把轨道整体平移到指定位置;animate 为 false 时无声跳转,用于首尾无缝衔接 */
|
|
384
|
+
setTrackPosition(e, t) {
|
|
385
|
+
const i = this.$dom.$viewerTrackElement;
|
|
386
|
+
if (!i)
|
|
387
|
+
return;
|
|
388
|
+
this.slidePos = e;
|
|
389
|
+
const a = `translate3d(${e === 0 ? "0" : `${-e * 100}%`}, 0, 0)`;
|
|
390
|
+
if (t) {
|
|
391
|
+
i.style.removeProperty("transition"), i.style.transform = a;
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
i.style.transition = "none", i.style.transform = a, i.offsetWidth, i.style.removeProperty("transition");
|
|
395
|
+
}
|
|
396
|
+
/** 滑到指定下标;delta 用于判断是否走了首尾循环,从而选择克隆位作为落点 */
|
|
397
|
+
slideTo(e, t) {
|
|
398
|
+
const i = this.imageDataList.length;
|
|
399
|
+
if (i <= 1)
|
|
400
|
+
return;
|
|
401
|
+
this.slidePos === 0 ? this.setTrackPosition(i, !1) : this.slidePos === i + 1 && this.setTrackPosition(1, !1);
|
|
402
|
+
let o = e + 1;
|
|
403
|
+
t !== void 0 && t > 0 && e === 0 ? o = i + 1 : t !== void 0 && t < 0 && e === i - 1 && (o = 0), this.setTrackPosition(o, !0), this.setTimer(() => {
|
|
404
|
+
this.slidePos === 0 ? this.setTrackPosition(i, !1) : this.slidePos === i + 1 && this.setTrackPosition(1, !1);
|
|
405
|
+
}, ne + 50);
|
|
406
|
+
}
|
|
407
|
+
startCarousel() {
|
|
408
|
+
this.stopCarousel();
|
|
409
|
+
const e = () => {
|
|
410
|
+
this.go(1), this.carouselTimer = this.setTimer(e, this.interval);
|
|
411
|
+
};
|
|
412
|
+
this.carouselTimer = this.setTimer(e, this.interval);
|
|
413
|
+
}
|
|
414
|
+
go(e) {
|
|
415
|
+
const t = this.imageDataList.length;
|
|
416
|
+
if (t === 0)
|
|
417
|
+
return;
|
|
418
|
+
const i = this.currentImageIndex;
|
|
419
|
+
let o = this.currentImageIndex + e;
|
|
420
|
+
const a = this.config.loop || this.isPlaying;
|
|
421
|
+
o < 0 && (o = a ? t - 1 : 0), o > t - 1 && (o = a ? 0 : t - 1), this.currentImageIndex = o, this.changeThumbImage(), this.setImageStyle(), o !== i && this.slideTo(o, e);
|
|
422
|
+
}
|
|
423
|
+
manualGo(e) {
|
|
424
|
+
this.go(e), this.isPlaying && this.startCarousel();
|
|
425
|
+
}
|
|
426
|
+
togglePlay() {
|
|
427
|
+
this.isPlaying = !this.isPlaying;
|
|
428
|
+
const e = this.$dom.$viewerToolbarElement, t = e ? g(".ew-viewer-play", e) : null;
|
|
429
|
+
t && (t.innerHTML = this.isPlaying ? this.config.pauseIcon : this.config.playIcon), this.isPlaying ? this.startCarousel() : this.stopCarousel();
|
|
430
|
+
}
|
|
431
|
+
/** 按当前缩放比例计算某张图片在预览区内的显示尺寸 */
|
|
432
|
+
imageSize(e, t) {
|
|
433
|
+
const { offsetWidth: i, offsetHeight: o } = t, a = e.naturalWidth / e.naturalHeight;
|
|
434
|
+
let h = i, r = o;
|
|
435
|
+
return o * a > i ? r = i / a : h = o * a, {
|
|
436
|
+
width: Math.min(
|
|
437
|
+
h * this.computedImageRatio,
|
|
438
|
+
e.naturalWidth
|
|
439
|
+
),
|
|
440
|
+
height: Math.min(
|
|
441
|
+
r * this.computedImageRatio,
|
|
442
|
+
e.naturalHeight
|
|
443
|
+
)
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
scaleImageCallback() {
|
|
447
|
+
const e = this.$dom.$viewerContentElement, t = this.currentMeta();
|
|
448
|
+
if (!e || !t)
|
|
449
|
+
return;
|
|
450
|
+
const { width: i, height: o } = this.imageSize(t, e);
|
|
451
|
+
t.scaleWidth = i, t.scaleHeight = o;
|
|
452
|
+
const a = this.$dom.$viewerRatioTooltipElement;
|
|
453
|
+
a && (a.textContent = `${Math.floor(this.computedImageRatio * 100)}%`);
|
|
454
|
+
}
|
|
455
|
+
async setImageStyle(e = !0) {
|
|
456
|
+
const t = this.$dom.$viewerContentElement, i = this.currentMeta();
|
|
457
|
+
if (!t || !i)
|
|
458
|
+
return;
|
|
459
|
+
e && this.scaleImageCallback();
|
|
460
|
+
const o = this.$dom.$viewerTrackElement;
|
|
461
|
+
(o ? [...o.children] : []).forEach((r) => {
|
|
462
|
+
const w = Number(r.getAttribute("data-index")), c = this.imageStyle[w], m = r.firstElementChild;
|
|
463
|
+
if (!c || !m)
|
|
464
|
+
return;
|
|
465
|
+
w === this.currentImageIndex ? R(r, "show") : z(r, "show");
|
|
466
|
+
const { width: p, height: T } = this.imageSize(c, t);
|
|
467
|
+
m.style.width = `${p}px`, m.style.height = `${T}px`, this.config.previewMode === "bg" && (m.style.backgroundSize = `${p}px ${T}px`), m.style.removeProperty("transform");
|
|
468
|
+
const y = `${c.rotate !== 0 ? `rotate(${c.rotate}deg)` : ""} ${c.scaleX !== 0 ? `scaleX(${c.scaleX})` : ""} ${c.scaleY !== 0 ? `scaleY(${c.scaleY})` : ""}`.trim();
|
|
469
|
+
y && (m.style.transform = y);
|
|
470
|
+
});
|
|
471
|
+
const h = this.$dom.$viewerTitleElement;
|
|
472
|
+
if (h) {
|
|
473
|
+
const r = this.imageDataList[this.currentImageIndex] ?? "";
|
|
474
|
+
h.innerHTML = `${this.getImageNameFromURL(r)}(${i.naturalWidth} x ${i.naturalHeight})`;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
getImageNameFromURL(e) {
|
|
478
|
+
return k(e) ? decodeURIComponent(e.replace(/^.*\//, "").replace(/[?&#].*$/, "")) : "";
|
|
479
|
+
}
|
|
480
|
+
computedImageRatioHandler(e) {
|
|
481
|
+
const { minImageRatio: t, maxImageRatio: i } = this.config, o = e ? 1 : -1, a = this.computedImageRatio + o * (Math.floor(Math.random() * 100) / 1e3);
|
|
482
|
+
this.computedImageRatio = Math.max(
|
|
483
|
+
t,
|
|
484
|
+
Math.min(a, i)
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
viewedImageHandler(e) {
|
|
488
|
+
F(e) ? this.computedImageRatioHandler(e) : this.computedImageRatio = e, this.toggleTooltip(!0), this.setTimer(() => this.toggleTooltip(!1), 1e3), this.setImageStyle();
|
|
489
|
+
}
|
|
490
|
+
changeThumbImage() {
|
|
491
|
+
const e = this.$dom.$viewerThumbElement;
|
|
492
|
+
if (!e)
|
|
493
|
+
return;
|
|
494
|
+
[...e.children].forEach((i) => {
|
|
495
|
+
Number(i.getAttribute("data-index")) === this.currentImageIndex ? R(i, "active") : z(i, "active");
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
getFullscreenTarget() {
|
|
499
|
+
const e = this.$dom.$viewerContentElement;
|
|
500
|
+
return e ? g(".ew-viewer-img-box.show", e) : null;
|
|
501
|
+
}
|
|
502
|
+
requestFullscreen() {
|
|
503
|
+
const t = this.getFullscreenTarget()?.ownerDocument ?? document;
|
|
504
|
+
if (M(t))
|
|
505
|
+
return;
|
|
506
|
+
const i = t.documentElement;
|
|
507
|
+
typeof i.requestFullscreen == "function" ? i.requestFullscreen() : typeof i.webkitRequestFullscreen == "function" ? i.webkitRequestFullscreen() : typeof i.mozRequestFullScreen == "function" ? i.mozRequestFullScreen() : typeof i.msRequestFullscreen == "function" && i.msRequestFullscreen();
|
|
508
|
+
}
|
|
509
|
+
exitFullscreen() {
|
|
510
|
+
const t = this.getFullscreenTarget()?.ownerDocument ?? document;
|
|
511
|
+
M(t) && (typeof t.exitFullscreen == "function" ? t.exitFullscreen() : typeof t.webkitExitFullscreen == "function" ? t.webkitExitFullscreen() : typeof t.mozCancelFullScreen == "function" ? t.mozCancelFullScreen() : typeof t.msExitFullscreen == "function" && t.msExitFullscreen());
|
|
512
|
+
}
|
|
513
|
+
resetImage() {
|
|
514
|
+
this.computedImageRatio = C(this.config.imageRatio);
|
|
515
|
+
const e = this.currentMeta();
|
|
516
|
+
e && (e.rotate = 0, e.scaleX = 0, e.scaleY = 0, this.setImageStyle());
|
|
517
|
+
}
|
|
518
|
+
toggleTooltip(e) {
|
|
519
|
+
const t = this.$dom.$viewerRatioTooltipElement;
|
|
520
|
+
t && this.setTimer(() => {
|
|
521
|
+
R(t, e ? "show" : "hidden"), z(t, e ? "hidden" : "show");
|
|
522
|
+
}, 100);
|
|
523
|
+
}
|
|
524
|
+
baseToggle(e, t) {
|
|
525
|
+
const { closeTime: i } = this.config, o = this.$dom.$el;
|
|
526
|
+
o && this.setTimer(
|
|
527
|
+
() => {
|
|
528
|
+
R(o, e ? "show" : "hidden"), z(o, e ? "hidden" : "show");
|
|
529
|
+
},
|
|
530
|
+
t || i
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
async close(e) {
|
|
534
|
+
this.resetImage(), this.stopCarousel(), this.isPlaying = !1, this.baseToggle(!1, e);
|
|
535
|
+
}
|
|
536
|
+
viewImage(e) {
|
|
537
|
+
b(e) && e >= 0 && e <= this.imageDataList.length - 1 && (this.currentImageIndex = e), this.open();
|
|
538
|
+
}
|
|
539
|
+
open(e) {
|
|
540
|
+
if (!this.$dom.$el) {
|
|
541
|
+
this.pendingOpen = { time: e };
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
this.changeThumbImage(), this.setTrackPosition(this.indexToPos(this.currentImageIndex), !1), this.setImageStyle(), this.baseToggle(!0, e);
|
|
545
|
+
}
|
|
546
|
+
/** 用最新入参重建整个预览层 */
|
|
547
|
+
update() {
|
|
548
|
+
this.options && this.reload(this.options);
|
|
549
|
+
}
|
|
550
|
+
/** 完整销毁:解绑监听、清空定时器、移除节点,可重复调用 */
|
|
551
|
+
destroy() {
|
|
552
|
+
this.teardown(), this.isPlaying = !1, this.fullScreenStatus = !1, this.pendingOpen = null;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
export {
|
|
556
|
+
me as EWViewer,
|
|
557
|
+
me as default
|
|
558
|
+
};
|
|
559
|
+
//# sourceMappingURL=index.js.map
|