d815884631efa1619cbea2bb5a0a3ef2428ee4c82aa782472c5b1015024382f518962722a308680d5a301de44f814da3721597c3f628fd1f722ccd67450cdf 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var lodashUnified = require('lodash-unified');
  6. var focusTrap = require('../../focus-trap/src/focus-trap.js');
  7. var index$3 = require('../../teleport/index.js');
  8. var index$4 = require('../../icon/index.js');
  9. var iconsVue = require('@element-plus/icons-vue');
  10. var imageViewer = require('./image-viewer2.js');
  11. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  12. var index = require('../../../hooks/use-locale/index.js');
  13. var index$1 = require('../../../hooks/use-namespace/index.js');
  14. var index$2 = require('../../../hooks/use-z-index/index.js');
  15. var aria = require('../../../constants/aria.js');
  16. var objects = require('../../../utils/objects.js');
  17. const __default__ = vue.defineComponent({
  18. name: "ElImageViewer"
  19. });
  20. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  21. ...__default__,
  22. props: imageViewer.imageViewerProps,
  23. emits: imageViewer.imageViewerEmits,
  24. setup(__props, { expose, emit }) {
  25. var _a;
  26. const props = __props;
  27. const modes = {
  28. CONTAIN: {
  29. name: "contain",
  30. icon: vue.markRaw(iconsVue.FullScreen)
  31. },
  32. ORIGINAL: {
  33. name: "original",
  34. icon: vue.markRaw(iconsVue.ScaleToOriginal)
  35. }
  36. };
  37. let stopWheelListener;
  38. let prevOverflow = "";
  39. const { t } = index.useLocale();
  40. const ns = index$1.useNamespace("image-viewer");
  41. const { nextZIndex } = index$2.useZIndex();
  42. const wrapper = vue.ref();
  43. const imgRef = vue.ref();
  44. const scopeEventListener = vue.effectScope();
  45. const scaleClamped = vue.computed(() => {
  46. const { scale, minScale, maxScale } = props;
  47. return core.clamp(scale, minScale, maxScale);
  48. });
  49. const loading = vue.ref(true);
  50. const loadError = vue.ref(false);
  51. const activeIndex = vue.ref(props.initialIndex);
  52. const mode = vue.shallowRef(modes.CONTAIN);
  53. const transform = vue.ref({
  54. scale: scaleClamped.value,
  55. deg: 0,
  56. offsetX: 0,
  57. offsetY: 0,
  58. enableTransition: false
  59. });
  60. const zIndex = vue.ref((_a = props.zIndex) != null ? _a : nextZIndex());
  61. const isSingle = vue.computed(() => {
  62. const { urlList } = props;
  63. return urlList.length <= 1;
  64. });
  65. const isFirst = vue.computed(() => activeIndex.value === 0);
  66. const isLast = vue.computed(() => activeIndex.value === props.urlList.length - 1);
  67. const currentImg = vue.computed(() => props.urlList[activeIndex.value]);
  68. const arrowPrevKls = vue.computed(() => [
  69. ns.e("btn"),
  70. ns.e("prev"),
  71. ns.is("disabled", !props.infinite && isFirst.value)
  72. ]);
  73. const arrowNextKls = vue.computed(() => [
  74. ns.e("btn"),
  75. ns.e("next"),
  76. ns.is("disabled", !props.infinite && isLast.value)
  77. ]);
  78. const imgStyle = vue.computed(() => {
  79. const { scale, deg, offsetX, offsetY, enableTransition } = transform.value;
  80. let translateX = offsetX / scale;
  81. let translateY = offsetY / scale;
  82. const radian = deg * Math.PI / 180;
  83. const cosRadian = Math.cos(radian);
  84. const sinRadian = Math.sin(radian);
  85. translateX = translateX * cosRadian + translateY * sinRadian;
  86. translateY = translateY * cosRadian - offsetX / scale * sinRadian;
  87. const style = {
  88. transform: `scale(${scale}) rotate(${deg}deg) translate(${translateX}px, ${translateY}px)`,
  89. transition: enableTransition ? "transform .3s" : ""
  90. };
  91. if (mode.value.name === modes.CONTAIN.name) {
  92. style.maxWidth = style.maxHeight = "100%";
  93. }
  94. return style;
  95. });
  96. const progress = vue.computed(() => `${activeIndex.value + 1} / ${props.urlList.length}`);
  97. function hide() {
  98. unregisterEventListener();
  99. stopWheelListener == null ? void 0 : stopWheelListener();
  100. document.body.style.overflow = prevOverflow;
  101. emit("close");
  102. }
  103. function registerEventListener() {
  104. const keydownHandler = lodashUnified.throttle((e) => {
  105. switch (e.code) {
  106. case aria.EVENT_CODE.esc:
  107. props.closeOnPressEscape && hide();
  108. break;
  109. case aria.EVENT_CODE.space:
  110. toggleMode();
  111. break;
  112. case aria.EVENT_CODE.left:
  113. prev();
  114. break;
  115. case aria.EVENT_CODE.up:
  116. handleActions("zoomIn");
  117. break;
  118. case aria.EVENT_CODE.right:
  119. next();
  120. break;
  121. case aria.EVENT_CODE.down:
  122. handleActions("zoomOut");
  123. break;
  124. }
  125. });
  126. const mousewheelHandler = lodashUnified.throttle((e) => {
  127. const delta = e.deltaY || e.deltaX;
  128. handleActions(delta < 0 ? "zoomIn" : "zoomOut", {
  129. zoomRate: props.zoomRate,
  130. enableTransition: false
  131. });
  132. });
  133. scopeEventListener.run(() => {
  134. core.useEventListener(document, "keydown", keydownHandler);
  135. core.useEventListener(document, "wheel", mousewheelHandler);
  136. });
  137. }
  138. function unregisterEventListener() {
  139. scopeEventListener.stop();
  140. }
  141. function handleImgLoad() {
  142. loading.value = false;
  143. }
  144. function handleImgError(e) {
  145. loadError.value = true;
  146. loading.value = false;
  147. emit("error", e);
  148. e.target.alt = t("el.image.error");
  149. }
  150. function handleMouseDown(e) {
  151. if (loading.value || e.button !== 0 || !wrapper.value)
  152. return;
  153. transform.value.enableTransition = false;
  154. const { offsetX, offsetY } = transform.value;
  155. const startX = e.pageX;
  156. const startY = e.pageY;
  157. const dragHandler = lodashUnified.throttle((ev) => {
  158. transform.value = {
  159. ...transform.value,
  160. offsetX: offsetX + ev.pageX - startX,
  161. offsetY: offsetY + ev.pageY - startY
  162. };
  163. });
  164. const removeMousemove = core.useEventListener(document, "mousemove", dragHandler);
  165. core.useEventListener(document, "mouseup", () => {
  166. removeMousemove();
  167. });
  168. e.preventDefault();
  169. }
  170. function reset() {
  171. transform.value = {
  172. scale: scaleClamped.value,
  173. deg: 0,
  174. offsetX: 0,
  175. offsetY: 0,
  176. enableTransition: false
  177. };
  178. }
  179. function toggleMode() {
  180. if (loading.value || loadError.value)
  181. return;
  182. const modeNames = objects.keysOf(modes);
  183. const modeValues = Object.values(modes);
  184. const currentMode = mode.value.name;
  185. const index = modeValues.findIndex((i) => i.name === currentMode);
  186. const nextIndex = (index + 1) % modeNames.length;
  187. mode.value = modes[modeNames[nextIndex]];
  188. reset();
  189. }
  190. function setActiveItem(index) {
  191. loadError.value = false;
  192. const len = props.urlList.length;
  193. activeIndex.value = (index + len) % len;
  194. }
  195. function prev() {
  196. if (isFirst.value && !props.infinite)
  197. return;
  198. setActiveItem(activeIndex.value - 1);
  199. }
  200. function next() {
  201. if (isLast.value && !props.infinite)
  202. return;
  203. setActiveItem(activeIndex.value + 1);
  204. }
  205. function handleActions(action, options = {}) {
  206. if (loading.value || loadError.value)
  207. return;
  208. const { minScale, maxScale } = props;
  209. const { zoomRate, rotateDeg, enableTransition } = {
  210. zoomRate: props.zoomRate,
  211. rotateDeg: 90,
  212. enableTransition: true,
  213. ...options
  214. };
  215. switch (action) {
  216. case "zoomOut":
  217. if (transform.value.scale > minScale) {
  218. transform.value.scale = Number.parseFloat((transform.value.scale / zoomRate).toFixed(3));
  219. }
  220. break;
  221. case "zoomIn":
  222. if (transform.value.scale < maxScale) {
  223. transform.value.scale = Number.parseFloat((transform.value.scale * zoomRate).toFixed(3));
  224. }
  225. break;
  226. case "clockwise":
  227. transform.value.deg += rotateDeg;
  228. emit("rotate", transform.value.deg);
  229. break;
  230. case "anticlockwise":
  231. transform.value.deg -= rotateDeg;
  232. emit("rotate", transform.value.deg);
  233. break;
  234. }
  235. transform.value.enableTransition = enableTransition;
  236. }
  237. function onFocusoutPrevented(event) {
  238. var _a2;
  239. if (((_a2 = event.detail) == null ? void 0 : _a2.focusReason) === "pointer") {
  240. event.preventDefault();
  241. }
  242. }
  243. function onCloseRequested() {
  244. if (props.closeOnPressEscape) {
  245. hide();
  246. }
  247. }
  248. function wheelHandler(e) {
  249. if (!e.ctrlKey)
  250. return;
  251. if (e.deltaY < 0) {
  252. e.preventDefault();
  253. return false;
  254. } else if (e.deltaY > 0) {
  255. e.preventDefault();
  256. return false;
  257. }
  258. }
  259. vue.watch(() => scaleClamped.value, (val) => {
  260. transform.value.scale = val;
  261. });
  262. vue.watch(currentImg, () => {
  263. vue.nextTick(() => {
  264. const $img = imgRef.value;
  265. if (!($img == null ? void 0 : $img.complete)) {
  266. loading.value = true;
  267. }
  268. });
  269. });
  270. vue.watch(activeIndex, (val) => {
  271. reset();
  272. emit("switch", val);
  273. });
  274. vue.onMounted(() => {
  275. registerEventListener();
  276. stopWheelListener = core.useEventListener("wheel", wheelHandler, {
  277. passive: false
  278. });
  279. prevOverflow = document.body.style.overflow;
  280. document.body.style.overflow = "hidden";
  281. });
  282. expose({
  283. setActiveItem
  284. });
  285. return (_ctx, _cache) => {
  286. return vue.openBlock(), vue.createBlock(vue.unref(index$3.ElTeleport), {
  287. to: "body",
  288. disabled: !_ctx.teleported
  289. }, {
  290. default: vue.withCtx(() => [
  291. vue.createVNode(vue.Transition, {
  292. name: "viewer-fade",
  293. appear: ""
  294. }, {
  295. default: vue.withCtx(() => [
  296. vue.createElementVNode("div", {
  297. ref_key: "wrapper",
  298. ref: wrapper,
  299. tabindex: -1,
  300. class: vue.normalizeClass(vue.unref(ns).e("wrapper")),
  301. style: vue.normalizeStyle({ zIndex: zIndex.value })
  302. }, [
  303. vue.createVNode(vue.unref(focusTrap["default"]), {
  304. loop: "",
  305. trapped: "",
  306. "focus-trap-el": wrapper.value,
  307. "focus-start-el": "container",
  308. onFocusoutPrevented,
  309. onReleaseRequested: onCloseRequested
  310. }, {
  311. default: vue.withCtx(() => [
  312. vue.createElementVNode("div", {
  313. class: vue.normalizeClass(vue.unref(ns).e("mask")),
  314. onClick: vue.withModifiers(($event) => _ctx.hideOnClickModal && hide(), ["self"])
  315. }, null, 10, ["onClick"]),
  316. vue.createCommentVNode(" CLOSE "),
  317. vue.createElementVNode("span", {
  318. class: vue.normalizeClass([vue.unref(ns).e("btn"), vue.unref(ns).e("close")]),
  319. onClick: hide
  320. }, [
  321. vue.createVNode(vue.unref(index$4.ElIcon), null, {
  322. default: vue.withCtx(() => [
  323. vue.createVNode(vue.unref(iconsVue.Close))
  324. ]),
  325. _: 1
  326. })
  327. ], 2),
  328. vue.createCommentVNode(" ARROW "),
  329. !vue.unref(isSingle) ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 0 }, [
  330. vue.createElementVNode("span", {
  331. class: vue.normalizeClass(vue.unref(arrowPrevKls)),
  332. onClick: prev
  333. }, [
  334. vue.createVNode(vue.unref(index$4.ElIcon), null, {
  335. default: vue.withCtx(() => [
  336. vue.createVNode(vue.unref(iconsVue.ArrowLeft))
  337. ]),
  338. _: 1
  339. })
  340. ], 2),
  341. vue.createElementVNode("span", {
  342. class: vue.normalizeClass(vue.unref(arrowNextKls)),
  343. onClick: next
  344. }, [
  345. vue.createVNode(vue.unref(index$4.ElIcon), null, {
  346. default: vue.withCtx(() => [
  347. vue.createVNode(vue.unref(iconsVue.ArrowRight))
  348. ]),
  349. _: 1
  350. })
  351. ], 2)
  352. ], 64)) : vue.createCommentVNode("v-if", true),
  353. _ctx.$slots.progress || _ctx.showProgress ? (vue.openBlock(), vue.createElementBlock("div", {
  354. key: 1,
  355. class: vue.normalizeClass([vue.unref(ns).e("btn"), vue.unref(ns).e("progress")])
  356. }, [
  357. vue.renderSlot(_ctx.$slots, "progress", {
  358. activeIndex: activeIndex.value,
  359. total: _ctx.urlList.length
  360. }, () => [
  361. vue.createTextVNode(vue.toDisplayString(vue.unref(progress)), 1)
  362. ])
  363. ], 2)) : vue.createCommentVNode("v-if", true),
  364. vue.createCommentVNode(" ACTIONS "),
  365. vue.createElementVNode("div", {
  366. class: vue.normalizeClass([vue.unref(ns).e("btn"), vue.unref(ns).e("actions")])
  367. }, [
  368. vue.createElementVNode("div", {
  369. class: vue.normalizeClass(vue.unref(ns).e("actions__inner"))
  370. }, [
  371. vue.renderSlot(_ctx.$slots, "toolbar", {
  372. actions: handleActions,
  373. prev,
  374. next,
  375. reset: toggleMode,
  376. activeIndex: activeIndex.value,
  377. setActiveItem
  378. }, () => [
  379. vue.createVNode(vue.unref(index$4.ElIcon), {
  380. onClick: ($event) => handleActions("zoomOut")
  381. }, {
  382. default: vue.withCtx(() => [
  383. vue.createVNode(vue.unref(iconsVue.ZoomOut))
  384. ]),
  385. _: 1
  386. }, 8, ["onClick"]),
  387. vue.createVNode(vue.unref(index$4.ElIcon), {
  388. onClick: ($event) => handleActions("zoomIn")
  389. }, {
  390. default: vue.withCtx(() => [
  391. vue.createVNode(vue.unref(iconsVue.ZoomIn))
  392. ]),
  393. _: 1
  394. }, 8, ["onClick"]),
  395. vue.createElementVNode("i", {
  396. class: vue.normalizeClass(vue.unref(ns).e("actions__divider"))
  397. }, null, 2),
  398. vue.createVNode(vue.unref(index$4.ElIcon), { onClick: toggleMode }, {
  399. default: vue.withCtx(() => [
  400. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(mode).icon)))
  401. ]),
  402. _: 1
  403. }),
  404. vue.createElementVNode("i", {
  405. class: vue.normalizeClass(vue.unref(ns).e("actions__divider"))
  406. }, null, 2),
  407. vue.createVNode(vue.unref(index$4.ElIcon), {
  408. onClick: ($event) => handleActions("anticlockwise")
  409. }, {
  410. default: vue.withCtx(() => [
  411. vue.createVNode(vue.unref(iconsVue.RefreshLeft))
  412. ]),
  413. _: 1
  414. }, 8, ["onClick"]),
  415. vue.createVNode(vue.unref(index$4.ElIcon), {
  416. onClick: ($event) => handleActions("clockwise")
  417. }, {
  418. default: vue.withCtx(() => [
  419. vue.createVNode(vue.unref(iconsVue.RefreshRight))
  420. ]),
  421. _: 1
  422. }, 8, ["onClick"])
  423. ])
  424. ], 2)
  425. ], 2),
  426. vue.createCommentVNode(" CANVAS "),
  427. vue.createElementVNode("div", {
  428. class: vue.normalizeClass(vue.unref(ns).e("canvas"))
  429. }, [
  430. loadError.value && _ctx.$slots["viewer-error"] ? vue.renderSlot(_ctx.$slots, "viewer-error", {
  431. key: 0,
  432. activeIndex: activeIndex.value,
  433. src: vue.unref(currentImg)
  434. }) : (vue.openBlock(), vue.createElementBlock("img", {
  435. ref_key: "imgRef",
  436. ref: imgRef,
  437. key: vue.unref(currentImg),
  438. src: vue.unref(currentImg),
  439. style: vue.normalizeStyle(vue.unref(imgStyle)),
  440. class: vue.normalizeClass(vue.unref(ns).e("img")),
  441. crossorigin: _ctx.crossorigin,
  442. onLoad: handleImgLoad,
  443. onError: handleImgError,
  444. onMousedown: handleMouseDown
  445. }, null, 46, ["src", "crossorigin"]))
  446. ], 2),
  447. vue.renderSlot(_ctx.$slots, "default")
  448. ]),
  449. _: 3
  450. }, 8, ["focus-trap-el"])
  451. ], 6)
  452. ]),
  453. _: 3
  454. })
  455. ]),
  456. _: 3
  457. }, 8, ["disabled"]);
  458. };
  459. }
  460. });
  461. var ImageViewer = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "image-viewer.vue"]]);
  462. exports["default"] = ImageViewer;
  463. //# sourceMappingURL=image-viewer.js.map