image-viewer2.js 15 KB

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