UNPKG

react-photoswipe-gallery

Version:

React component wrapper around PhotoSwipe

178 lines 7.49 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomGallery = void 0; var photoswipe_1 = __importDefault(require("photoswipe")); var react_1 = __importStar(require("react")); var prop_types_1 = __importDefault(require("prop-types")); var helpers_1 = require("./helpers"); var context_1 = require("./context"); var gallery_base_1 = require("./gallery-base"); /** * Gallery component with ability to use specific UI and Layout */ var CustomGallery = function (_a) { var children = _a.children, layoutRef = _a.layoutRef, ui = _a.ui, options = _a.options, galleryUID = _a.id, onOpen = _a.onOpen; var items = react_1.useRef(new Map()); var openWhenReadyPid = react_1.useRef(null); var open = react_1.useCallback(function (targetRef, targetId, itemIndex) { var index = itemIndex || null; var normalized = []; var entries = Array.from(items.current); var prepare = function (entry, i) { var _a = __read(entry, 2), ref = _a[0], _b = _a[1], width = _b.width, height = _b.height, title = _b.title, original = _b.original, thumbnail = _b.thumbnail, pid = _b.id, rest = __rest(_b, ["width", "height", "title", "original", "thumbnail", "id"]); if (targetRef === ref || (pid !== undefined && String(pid) === targetId)) { index = i; } normalized.push(__assign(__assign(__assign(__assign({}, (title ? { title: title } : {})), { w: Number(width), h: Number(height), src: original, msrc: thumbnail, el: ref.current }), (pid !== undefined ? { pid: pid } : {})), rest)); }; if (items.current.size > 1) { entries .sort(function (_a, _b) { var _c = __read(_a, 1), a = _c[0].current; var _d = __read(_b, 1), b = _d[0].current; return helpers_1.sortNodes(a, b); }) .forEach(prepare); } else { entries.forEach(prepare); } var layoutEl = layoutRef.current; if (layoutEl) { var instance = new photoswipe_1.default(layoutEl, ui, normalized, __assign(__assign({ index: index === null ? parseInt(targetId, 10) - 1 : index, getThumbBoundsFn: function (thumbIndex) { var el = normalized[thumbIndex].el; return el ? helpers_1.getElBounds(el) : { x: 0, y: 0, w: 0 }; }, history: false }, (galleryUID !== undefined ? { galleryUID: galleryUID, history: true } : {})), (options || {}))); instance.init(); if (onOpen !== undefined && typeof onOpen === 'function') { onOpen(instance); } } }, [options, galleryUID, onOpen]); react_1.useEffect(function () { if (galleryUID === undefined) { return; } var hash = window.location.hash.substring(1); var params = {}; if (hash.length < 5) { return; } var vars = hash.split('&'); for (var i = 0; i < vars.length; i++) { if (vars[i]) { var _a = __read(vars[i].split('='), 2), key = _a[0], value = _a[1]; if (key && value) { params[key] = value; } } } var pid = params.pid, gid = params.gid; if (items.current.size === 0) { openWhenReadyPid.current = pid; return; } if (pid && gid === String(galleryUID)) { open(null, pid); } }, [open, galleryUID]); var remove = react_1.useCallback(function (ref) { items.current.delete(ref); }, []); var set = react_1.useCallback(function (ref, data) { var id = data.id; items.current.set(ref, data); if (!openWhenReadyPid.current) return; if (id === openWhenReadyPid.current) { open(ref); openWhenReadyPid.current = null; } else if (!id) { var index = parseInt(openWhenReadyPid.current, 10) - 1; var refToOpen = Array.from(items.current.keys())[index]; if (refToOpen) { open(refToOpen); openWhenReadyPid.current = null; } } }, [open]); var openAt = react_1.useCallback(function (index) { open(null, null, index); }, [open]); return (react_1.default.createElement(context_1.Context.Provider, { value: { remove: remove, set: set, handleClick: open, open: openAt } }, children)); }; exports.CustomGallery = CustomGallery; exports.CustomGallery.propTypes = __assign(__assign({}, gallery_base_1.baseGalleryPropTypes), { // @ts-ignore layoutRef: prop_types_1.default.shape({ current: prop_types_1.default.instanceOf(typeof Element === 'undefined' ? /** @class */ (function () { function Element() { } return Element; }()) : Element), }).isRequired, ui: prop_types_1.default.any.isRequired }); //# sourceMappingURL=gallery-custom.js.map