UNPKG

@wordpress/block-library

Version:
84 lines (83 loc) • 3.23 kB
// packages/block-library/src/gallery/order-images.js /** * Returns the value an image block is ordered by, or `undefined` when it has no * resolved attachment record to take it from. * * @param {Object} block A `core/image` inner block. * @param {Map} mediaById Attachment records for the gallery's images, by id. * @param {string} orderby `date` or `title`. * @return {?string} The sort key. */ function getSortKey(block, mediaById, orderby) { const record = mediaById.get(block.attributes.id); if (!record) return; if (orderby === "title") return record.title?.raw ?? record.title?.rendered ?? ""; return record.date ?? ""; } /** * Whether there's anything to sort: at least two image blocks with a resolved * attachment record. With fewer, sorting is a no-op, so the sort options are * disabled. * * @param {Array} blocks The gallery's `core/image` inner blocks. * @param {Map} mediaById Attachment records for the gallery's images, by id. * @return {boolean} Whether the images can be sorted. */ function hasSortableImages(blocks, mediaById) { let sortableCount = 0; for (const block of blocks) { if (!mediaById.has(block.attributes.id)) continue; sortableCount++; if (sortableCount > 1) return true; } return false; } /** * Compares two sort keys for the given field. Titles use a natural, * case-insensitive comparison so `IMG_2` sorts before `IMG_10`; dates are ISO * 8601 strings, so a plain string comparison orders them chronologically. * * @param {string} a First key. * @param {string} b Second key. * @param {string} orderby `date` or `title`. * @return {number} Negative when `a` sorts first, positive when `b` does. */ function compareKeys(a, b, orderby) { if (orderby === "title") return a.localeCompare(b, void 0, { numeric: true, sensitivity: "base" }); if (a === b) return 0; return a < b ? -1 : 1; } /** * Sorts a static gallery's image blocks by an attachment field. * * Blocks without an `id`, or whose attachment record isn't in `mediaById` (still * loading, or deleted from the library), can't be placed, so they keep their * relative order and move to the end. Blocks with equal keys likewise keep * their relative order (`Array.prototype.sort` is stable), so re-applying the * current order is a no-op. * * @param {Array} blocks The gallery's `core/image` inner blocks. * @param {Map} mediaById Attachment records for the gallery's images, by id. * @param {Object} order The order to apply. * @param {string} order.orderby `date` or `title`. * @param {string} order.order `asc` or `desc`. * @return {Array} A new array holding the same block objects, sorted. */ function sortImageBlocks(blocks, mediaById, { orderby, order }) { const direction = order === "desc" ? -1 : 1; const keys = new Map(blocks.map((block) => [block, getSortKey(block, mediaById, orderby)])); return blocks.toSorted((a, b) => { const keyA = keys.get(a); const keyB = keys.get(b); if (keyA === void 0 || keyB === void 0) { if (keyA === keyB) return 0; return keyA === void 0 ? 1 : -1; } return compareKeys(keyA, keyB, orderby) * direction; }); } export { hasSortableImages, sortImageBlocks }; //# sourceMappingURL=order-images.mjs.map