image-viewer.js 17 KB

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