UNPKG

8.01 kBJavaScriptView Raw
1var __create = Object.create;
2var __defProp = Object.defineProperty;
3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4var __getOwnPropNames = Object.getOwnPropertyNames;
5var __getProtoOf = Object.getPrototypeOf;
6var __hasOwnProp = Object.prototype.hasOwnProperty;
7var __export = (target, all) => {
8 for (var name2 in all)
9 __defProp(target, name2, { get: all[name2], enumerable: true });
10};
11var __copyProps = (to, from, except, desc) => {
12 if (from && typeof from === "object" || typeof from === "function") {
13 for (let key of __getOwnPropNames(from))
14 if (!__hasOwnProp.call(to, key) && key !== except)
15 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16 }
17 return to;
18};
19var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20 // If the importer is in node compatibility mode or this is not an ESM
21 // file that has been converted to a CommonJS file using a Babel-
22 // compatible transform (i.e. "__esModule" has not been set), then set
23 // "default" to the CommonJS "module.exports" for node compatibility.
24 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25 mod
26));
27var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28var stdin_exports = {};
29__export(stdin_exports, {
30 default: () => stdin_default,
31 imagePreviewProps: () => imagePreviewProps
32});
33module.exports = __toCommonJS(stdin_exports);
34var import_vue = require("vue");
35var import_utils = require("../utils");
36var import_use = require("@vant/use");
37var import_use_expose = require("../composables/use-expose");
38var import_icon = require("../icon");
39var import_swipe = require("../swipe");
40var import_popup = require("../popup");
41var import_ImagePreviewItem = __toESM(require("./ImagePreviewItem"));
42const [name, bem] = (0, import_utils.createNamespace)("image-preview");
43const popupProps = ["show", "teleport", "transition", "overlayStyle", "closeOnPopstate"];
44const imagePreviewProps = {
45 show: Boolean,
46 loop: import_utils.truthProp,
47 images: (0, import_utils.makeArrayProp)(),
48 minZoom: (0, import_utils.makeNumericProp)(1 / 3),
49 maxZoom: (0, import_utils.makeNumericProp)(3),
50 overlay: import_utils.truthProp,
51 vertical: Boolean,
52 closeable: Boolean,
53 showIndex: import_utils.truthProp,
54 className: import_utils.unknownProp,
55 closeIcon: (0, import_utils.makeStringProp)("clear"),
56 transition: String,
57 beforeClose: Function,
58 doubleScale: import_utils.truthProp,
59 overlayClass: import_utils.unknownProp,
60 overlayStyle: Object,
61 swipeDuration: (0, import_utils.makeNumericProp)(300),
62 startPosition: (0, import_utils.makeNumericProp)(0),
63 showIndicators: Boolean,
64 closeOnPopstate: import_utils.truthProp,
65 closeOnClickImage: import_utils.truthProp,
66 closeOnClickOverlay: import_utils.truthProp,
67 closeIconPosition: (0, import_utils.makeStringProp)("top-right"),
68 teleport: [String, Object]
69};
70var stdin_default = (0, import_vue.defineComponent)({
71 name,
72 props: imagePreviewProps,
73 emits: ["scale", "close", "closed", "change", "longPress", "update:show"],
74 setup(props, {
75 emit,
76 slots
77 }) {
78 const swipeRef = (0, import_vue.ref)();
79 const activedPreviewItemRef = (0, import_vue.ref)();
80 const state = (0, import_vue.reactive)({
81 active: 0,
82 rootWidth: 0,
83 rootHeight: 0,
84 disableZoom: false
85 });
86 const resize = () => {
87 if (swipeRef.value) {
88 const rect = (0, import_use.useRect)(swipeRef.value.$el);
89 state.rootWidth = rect.width;
90 state.rootHeight = rect.height;
91 swipeRef.value.resize();
92 }
93 };
94 const emitScale = (args) => emit("scale", args);
95 const updateShow = (show) => emit("update:show", show);
96 const emitClose = () => {
97 (0, import_utils.callInterceptor)(props.beforeClose, {
98 args: [state.active],
99 done: () => updateShow(false)
100 });
101 };
102 const setActive = (active) => {
103 if (active !== state.active) {
104 state.active = active;
105 emit("change", active);
106 }
107 };
108 const renderIndex = () => {
109 if (props.showIndex) {
110 return (0, import_vue.createVNode)("div", {
111 "class": bem("index")
112 }, [slots.index ? slots.index({
113 index: state.active
114 }) : `${state.active + 1} / ${props.images.length}`]);
115 }
116 };
117 const renderCover = () => {
118 if (slots.cover) {
119 return (0, import_vue.createVNode)("div", {
120 "class": bem("cover")
121 }, [slots.cover()]);
122 }
123 };
124 const onDragStart = () => {
125 state.disableZoom = true;
126 };
127 const onDragEnd = () => {
128 state.disableZoom = false;
129 };
130 const renderImages = () => (0, import_vue.createVNode)(import_swipe.Swipe, {
131 "ref": swipeRef,
132 "lazyRender": true,
133 "loop": props.loop,
134 "class": bem("swipe"),
135 "vertical": props.vertical,
136 "duration": props.swipeDuration,
137 "initialSwipe": props.startPosition,
138 "showIndicators": props.showIndicators,
139 "indicatorColor": "white",
140 "onChange": setActive,
141 "onDragEnd": onDragEnd,
142 "onDragStart": onDragStart
143 }, {
144 default: () => [props.images.map((image, index) => (0, import_vue.createVNode)(import_ImagePreviewItem.default, {
145 "ref": (item) => {
146 if (index === state.active) {
147 activedPreviewItemRef.value = item;
148 }
149 },
150 "src": image,
151 "show": props.show,
152 "active": state.active,
153 "maxZoom": props.maxZoom,
154 "minZoom": props.minZoom,
155 "rootWidth": state.rootWidth,
156 "rootHeight": state.rootHeight,
157 "disableZoom": state.disableZoom,
158 "doubleScale": props.doubleScale,
159 "closeOnClickImage": props.closeOnClickImage,
160 "closeOnClickOverlay": props.closeOnClickOverlay,
161 "vertical": props.vertical,
162 "onScale": emitScale,
163 "onClose": emitClose,
164 "onLongPress": () => emit("longPress", {
165 index
166 })
167 }, {
168 image: slots.image
169 }))]
170 });
171 const renderClose = () => {
172 if (props.closeable) {
173 return (0, import_vue.createVNode)(import_icon.Icon, {
174 "role": "button",
175 "name": props.closeIcon,
176 "class": [bem("close-icon", props.closeIconPosition), import_utils.HAPTICS_FEEDBACK],
177 "onClick": emitClose
178 }, null);
179 }
180 };
181 const onClosed = () => emit("closed");
182 const swipeTo = (index, options) => {
183 var _a;
184 return (_a = swipeRef.value) == null ? void 0 : _a.swipeTo(index, options);
185 };
186 (0, import_use_expose.useExpose)({
187 resetScale: () => {
188 var _a;
189 (_a = activedPreviewItemRef.value) == null ? void 0 : _a.resetScale();
190 },
191 swipeTo
192 });
193 (0, import_vue.onMounted)(resize);
194 (0, import_vue.watch)([import_utils.windowWidth, import_utils.windowHeight], resize);
195 (0, import_vue.watch)(() => props.startPosition, (value) => setActive(+value));
196 (0, import_vue.watch)(() => props.show, (value) => {
197 const {
198 images,
199 startPosition
200 } = props;
201 if (value) {
202 setActive(+startPosition);
203 (0, import_vue.nextTick)(() => {
204 resize();
205 swipeTo(+startPosition, {
206 immediate: true
207 });
208 });
209 } else {
210 emit("close", {
211 index: state.active,
212 url: images[state.active]
213 });
214 }
215 });
216 return () => (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
217 "class": [bem(), props.className],
218 "overlayClass": [bem("overlay"), props.overlayClass],
219 "onClosed": onClosed,
220 "onUpdate:show": updateShow
221 }, (0, import_utils.pick)(props, popupProps)), {
222 default: () => [renderClose(), renderImages(), renderIndex(), renderCover()]
223 });
224 }
225});