@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 4.74 kB
Source Map (JSON)
{"version":3,"file":"order-images.cjs","names":[],"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 {Map} mediaById Attachment records for the gallery's images, by id.\n * @param {string} orderby `date` or `title`.\n * @return {?string} The sort key.\n */\nfunction getSortKey( block, mediaById, orderby ) {\n\tconst record = mediaById.get( block.attributes.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 {Map} mediaById Attachment records for the gallery's images, by id.\n * @return {boolean} Whether the images can be sorted.\n */\nexport function hasSortableImages( blocks, mediaById ) {\n\tlet sortableCount = 0;\n\tfor ( const block of blocks ) {\n\t\tif ( ! mediaById.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 `mediaById` (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 {Map} mediaById Attachment records for the gallery's images, by id.\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, mediaById, { 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, mediaById, orderby ),\n\t\t] )\n\t);\n\n\treturn blocks.toSorted( ( 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":";;;;;;;;;;;;;;;AASA,SAAS,WAAY,OAAO,WAAW,SAAU;CAChD,MAAM,SAAS,UAAU,IAAK,MAAM,WAAW,EAAG;CAClD,IAAK,CAAE,QACN;CAED,IAAK,YAAY,SAGhB,OAAO,OAAO,OAAO,OAAO,OAAO,OAAO,YAAY;CAEvD,OAAO,OAAO,QAAQ;AACvB;;;;;;;;;;AAWA,SAAgB,kBAAmB,QAAQ,WAAY;CACtD,IAAI,gBAAgB;CACpB,KAAM,MAAM,SAAS,QAAS;EAC7B,IAAK,CAAE,UAAU,IAAK,MAAM,WA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