react-photoswipe-gallery
Version:
React component wrapper around PhotoSwipe
47 lines • 1.65 kB
JavaScript
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;
};
import itemToSlide from "./item-to-slide.js";
import { NoSourceIdError } from "../no-source-id-error.js";
const getSlidesAndIndexFromDataSource = (dataSource, items, targetRef, targetId, itemIndex) => {
const itemsWithRefsMap = Array.from(items.current).reduce((acc, [ref, {
sourceId
}]) => {
if (sourceId === undefined) {
throw new NoSourceIdError('sourceId is missed on Item component');
}
acc.set(sourceId, ref);
return acc;
}, new Map());
const {
slides,
index
} = dataSource.reduce((acc, dataSourceItem, i) => {
const {
sourceId
} = dataSourceItem,
rest = __rest(dataSourceItem, ["sourceId"]);
if (sourceId === undefined) {
throw new NoSourceIdError('sourceId is missed in dataSource item');
}
const elementRef = itemsWithRefsMap.has(sourceId) ? itemsWithRefsMap.get(sourceId) : undefined;
if (targetRef === elementRef || rest.id !== undefined && String(rest.id) === targetId) {
acc.index = i;
}
acc.slides.push(itemToSlide(rest, elementRef));
return acc;
}, {
slides: [],
index: itemIndex || null
});
return {
slides,
index
};
};
export default getSlidesAndIndexFromDataSource;