UNPKG

vue3-photo-preview

Version:
42 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var vue_1 = require("vue"); function useItems(index) { var items = (0, vue_1.ref)([]); var getElementIndex = function (children, child) { return child ? Array.prototype.indexOf.call(children, child) : -1; }; var sortItems = function (items) { var _a, _b; var children = (_b = (_a = items[0].originRef) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.children; if (children && children.length) { items.sort(function (cur, next) { return getElementIndex(children, cur.originRef) - getElementIndex(children, next.originRef); }); } }; var updateItem = function (item) { var index = items.value.findIndex(function (_a) { var key = _a.key; return item.key === key; }); if (index > -1) { items.value.splice(index, 1, item); } else { items.value.push(item); sortItems(items.value); } }; var removeItem = function (key) { var nextItems = items.value.filter(function (item) { return item.key !== key; }); var nextEndIndex = nextItems.length - 1; items.value = nextItems; index.value = Math.max(Math.min(index.value, nextEndIndex), 0); }; return { items: items, updateItem: updateItem, removeItem: removeItem, }; } exports.default = useItems; //# sourceMappingURL=useItems.js.map