@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 5.23 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/gallery/order-images.js"],
"sourcesContent": ["/**\n * Returns the value an image block is ordered by, or `undefined` when it has no\n * resolved attachment record to take it from.\n *\n * @param {Object} block A `core/image` inner block.\n * @param {Array} media Attachment records for the gallery's images.\n * @param {string} orderby `date` or `title`.\n * @return {?string} The sort key.\n */\nfunction getSortKey( block, media, orderby ) {\n\tconst { id } = block.attributes;\n\tif ( id === undefined ) {\n\t\treturn undefined;\n\t}\n\tconst record = media.find( ( item ) => item.id === id );\n\tif ( ! record ) {\n\t\treturn undefined;\n\t}\n\tif ( orderby === 'title' ) {\n\t\t// The media is fetched in the default (view) context, so `rendered`\n\t\t// is the usual source; `raw` is only present with the edit context.\n\t\treturn record.title?.raw ?? record.title?.rendered ?? '';\n\t}\n\treturn record.date ?? '';\n}\n\n/**\n * Whether there's anything to sort: at least two image blocks with a resolved\n * attachment record. With fewer, sorting is a no-op, so the sort options are\n * disabled.\n *\n * @param {Array} blocks The gallery's `core/image` inner blocks.\n * @param {Array} media Attachment records for the gallery's images.\n * @return {boolean} Whether the images can be sorted.\n */\nexport function hasSortableImages( blocks, media ) {\n\t// Runs on every render, so avoid scanning `media` once per block.\n\tconst mediaIds = new Set( media.map( ( item ) => item.id ) );\n\tlet sortableCount = 0;\n\tfor ( const block of blocks ) {\n\t\tif ( ! mediaIds.has( block.attributes.id ) ) {\n\t\t\tcontinue;\n\t\t}\n\t\tsortableCount++;\n\t\tif ( sortableCount > 1 ) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\n/**\n * Compares two sort keys for the given field. Titles use a natural,\n * case-insensitive comparison so `IMG_2` sorts before `IMG_10`; dates are ISO\n * 8601 strings, so a plain string comparison orders them chronologically.\n *\n * @param {string} a First key.\n * @param {string} b Second key.\n * @param {string} orderby `date` or `title`.\n * @return {number} Negative when `a` sorts first, positive when `b` does.\n */\nfunction compareKeys( a, b, orderby ) {\n\tif ( orderby === 'title' ) {\n\t\treturn a.localeCompare( b, undefined, {\n\t\t\tnumeric: true,\n\t\t\tsensitivity: 'base',\n\t\t} );\n\t}\n\tif ( a === b ) {\n\t\treturn 0;\n\t}\n\treturn a < b ? -1 : 1;\n}\n\n/**\n * Sorts a static gallery's image blocks by an attachment field.\n *\n * Blocks without an `id`, or whose attachment record isn't in `media` (still\n * loading, or deleted from the library), can't be placed, so they keep their\n * relative order and move to the end. Blocks with equal keys likewise keep\n * their relative order (`Array.prototype.sort` is stable), so re-applying the\n * current order is a no-op.\n *\n * @param {Array} blocks The gallery's `core/image` inner blocks.\n * @param {Array} media Attachment records for the gallery's images.\n * @param {Object} order The order to apply.\n * @param {string} order.orderby `date` or `title`.\n * @param {string} order.order `asc` or `desc`.\n * @return {Array} A new array holding the same block objects, sorted.\n */\nexport function sortImageBlocks( blocks, media, { orderby, order } ) {\n\tconst direction = order === 'desc' ? -1 : 1;\n\tconst keys = new Map(\n\t\tblocks.map( ( block ) => [\n\t\t\tblock,\n\t\t\tgetSortKey( block, media, orderby ),\n\t\t] )\n\t);\n\n\treturn [ ...blocks ].sort( ( a, b ) => {\n\t\tconst keyA = keys.get( a );\n\t\tconst keyB = keys.get( b );\n\t\tif ( keyA === undefined || keyB === undefined ) {\n\t\t\tif ( keyA === keyB ) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\treturn keyA === undefined ? 1 : -1;\n\t\t}\n\t\treturn compareKeys( keyA, keyB, orderby ) * direction;\n\t} );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,SAAS,WAAY,OAAO,OAAO,SAAU;AAC5C,QAAM,EAAE,GAAG,IAAI,MAAM;AACrB,MAAK,OAAO,QAAY;AACvB,WAAO;AAAA,EACR;AACA,QAAM,SAAS,MAAM,KAAM,CAAE,SAAU,KAAK,OAAO,EAAG;AACtD,MAAK,CAAE,QAAS;AACf,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