UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 5.88 kB
{ "version": 3, "sources": ["../../src/gallery/dynamic-source.js"], "sourcesContent": ["import { __ } from '@wordpress/i18n';\n\n/**\n * Source discriminator for \"images attached to the post\" — the only dynamic\n * source implemented so far. Kept as a named constant since it's referenced from\n * several call sites (the query builder, the descriptor, and the entry points\n * that switch a gallery into dynamic mode).\n */\nexport const ATTACHED_MEDIA = 'core/attached-media';\n\n/**\n * Default ordering for a dynamic source. `menu_order` (the manual media-library\n * order) is intentionally not used: it isn't a valid `orderby` value on the\n * media REST endpoint, so the editor preview couldn't reproduce it. Both the\n * editor query and the server resolver default to the same REST-supported order\n * so the preview matches the frontend.\n */\nexport const DEFAULT_ORDERBY = 'date';\nexport const DEFAULT_ORDER = 'desc';\n\n/**\n * Per-source copy, keyed by the `source` discriminator in a gallery's\n * `dynamicContent`. Adding a dynamic source means adding an entry here; the\n * editor components read these strings instead of hardcoding source-specific\n * wording. The fields also map onto a future \"Choose source\" control: `title`\n * becomes an option label, `description` its help text, and `emptyMessage` the\n * canvas preview when the source resolves to nothing.\n */\nconst DYNAMIC_SOURCES = {\n\t[ ATTACHED_MEDIA ]: {\n\t\t// Short label for the entry affordance / future source chooser. Mirrors\n\t\t// the \"Attached images\" media inserter category name.\n\t\ttitle: __( 'Use attached images' ),\n\t\t// Help text shown beneath the Source controls.\n\t\tdescription: __( 'Images attached to the post.' ),\n\t\t// Empty-state copy for the canvas preview.\n\t\temptyMessage: __( 'Images attached to the post will appear here.' ),\n\t},\n};\n\n/**\n * Returns the descriptor for a dynamic source, or `undefined` for an unknown or\n * not-yet-implemented source.\n *\n * @param {?string} source The `dynamicContent.source` discriminator.\n * @return {?Object} The source descriptor (`title`, `description`, `emptyMessage`).\n */\nexport function getDynamicSource( source ) {\n\treturn DYNAMIC_SOURCES[ source ];\n}\n\n/**\n * Upper bound on the number of images a dynamic source resolves, until the\n * gallery supports pagination. Kept in sync with the `posts_per_page` cap in\n * `block_core_gallery_resolve_dynamic_source()`.\n */\nexport const MAX_IMAGES = 100;\n\n/**\n * Maps a gallery's `dynamicContent` attribute to a query for the `attachment`\n * entity (i.e. `/wp/v2/media` collection params), used to resolve the source to\n * a list of media in the editor.\n *\n * The `source` key is the dispatch discriminator and `args` holds the\n * source's parameters. This `{ source, args }` shape mirrors the Block\n * Bindings metadata shape (`metadata.bindings.<key> = { source, args }`) so\n * dynamic mode can migrate to an `innerBlocks` binding with minimal change.\n * `args` keys are camelCase (the block-attribute convention, as used by the\n * Query block's `query` attribute); each source's resolver maps them to the\n * REST/transport names it needs. `core/attached-media` is a context-relative anchor\n * resolved here to the REST `parent` param. The server-side counterpart is\n * `block_core_gallery_resolve_dynamic_source()` in `index.php`.\n *\n * @param {Object} dynamicContent The gallery's `dynamicContent` attribute.\n * @param {Object} context Resolution context.\n * @param {number} context.postId The current post ID.\n * @return {Object|null} A `getEntityRecords` query, or `null` when the source\n * cannot be resolved (unknown source or missing context).\n */\nexport function getSourceQuery( dynamicContent, { postId } ) {\n\tconst { source, args = {} } = dynamicContent ?? {};\n\n\tswitch ( source ) {\n\t\tcase ATTACHED_MEDIA:\n\t\t\tif ( ! postId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tparent: postId,\n\t\t\t\tper_page: MAX_IMAGES,\n\t\t\t\t// The gallery only accepts images, so constrain the source to\n\t\t\t\t// image media (matching the server resolver). This keeps the\n\t\t\t\t// editor preview in step with the rendered output for posts\n\t\t\t\t// that also have non-image attachments.\n\t\t\t\tmedia_type: 'image',\n\t\t\t\t// Map the camelCase `args` to the REST-named media collection\n\t\t\t\t// params. Unexpected values (only reachable via hand-edited\n\t\t\t\t// markup) are coerced back to the defaults — mirroring the server\n\t\t\t\t// resolver's allow list — so the editor preview stays in step with\n\t\t\t\t// the frontend instead of issuing an invalid REST query.\n\t\t\t\torderby:\n\t\t\t\t\targs.orderBy === 'date' || args.orderBy === 'title'\n\t\t\t\t\t\t? args.orderBy\n\t\t\t\t\t\t: DEFAULT_ORDERBY,\n\t\t\t\torder:\n\t\t\t\t\targs.order === 'asc' || args.order === 'desc'\n\t\t\t\t\t\t? args.order\n\t\t\t\t\t\t: DEFAULT_ORDER,\n\t\t\t};\n\t}\n\n\t// Unknown or not-yet-implemented source.\n\treturn null;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAmB;AAQZ,IAAM,iBAAiB;AASvB,IAAM,kBAAkB;AACxB,IAAM,gBAAgB;AAU7B,IAAM,kBAAkB;AAAA,EACvB,CAAE,cAAe,GAAG;AAAA;AAAA;AAAA,IAGnB,WAAO,gBAAI,qBAAsB;AAAA;AAAA,IAEjC,iBAAa,gBAAI,8BAA+B;AAAA;AAAA,IAEhD,kBAAc,gBAAI,+CAAgD;AAAA,EACnE;AACD;AASO,SAAS,iBAAkB,QAAS;AAC1C,SAAO,gBAAiB,MAAO;AAChC;AAOO,IAAM,aAAa;AAuBnB,SAAS,eAAgB,gBAAgB,EAAE,OAAO,GAAI;AAC5D,QAAM,EAAE,QAAQ,OAAO,CAAC,EAAE,IAAI,kBAAkB,CAAC;AAEjD,UAAS,QAAS;AAAA,IACjB,KAAK;AACJ,UAAK,CAAE,QAAS;AACf,eAAO;AAAA,MACR;AACA,aAAO;AAAA,QACN,QAAQ;AAAA,QACR,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA,QAKV,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAMZ,SACC,KAAK,YAAY,UAAU,KAAK,YAAY,UACzC,KAAK,UACL;AAAA,QACJ,OACC,KAAK,UAAU,SAAS,KAAK,UAAU,SACpC,KAAK,QACL;AAAA,MACL;AAAA,EACF;AAGA,SAAO;AACR;", "names": [] }