@wordpress/block-library
Version:
Block library for the WordPress editor.
56 lines (55 loc) • 2.04 kB
JavaScript
// packages/block-library/src/gallery/dynamic-source.js
import { __ } from "@wordpress/i18n";
var ATTACHED_MEDIA = "core/attached-media";
var DEFAULT_ORDERBY = "date";
var DEFAULT_ORDER = "desc";
var DYNAMIC_SOURCES = {
[]: {
// Short label for the entry affordance / future source chooser. Mirrors
// the "Attached images" media inserter category name.
title: __("Use attached images"),
// Help text shown beneath the Source controls.
description: __("Images attached to the post."),
// Empty-state copy for the canvas preview.
emptyMessage: __("Images attached to the post will appear here.")
}
};
function getDynamicSource(source) {
return DYNAMIC_SOURCES[source];
}
var MAX_IMAGES = 100;
function getSourceQuery(dynamicContent, { postId }) {
const { source, args = {} } = dynamicContent ?? {};
switch (source) {
case ATTACHED_MEDIA:
if (!postId) {
return null;
}
return {
parent: postId,
per_page: MAX_IMAGES,
// The gallery only accepts images, so constrain the source to
// image media (matching the server resolver). This keeps the
// editor preview in step with the rendered output for posts
// that also have non-image attachments.
media_type: "image",
// Map the camelCase `args` to the REST-named media collection
// params. Unexpected values (only reachable via hand-edited
// markup) are coerced back to the defaults — mirroring the server
// resolver's allow list — so the editor preview stays in step with
// the frontend instead of issuing an invalid REST query.
orderby: args.orderBy === "date" || args.orderBy === "title" ? args.orderBy : DEFAULT_ORDERBY,
order: args.order === "asc" || args.order === "desc" ? args.order : DEFAULT_ORDER
};
}
return null;
}
export {
ATTACHED_MEDIA,
DEFAULT_ORDER,
DEFAULT_ORDERBY,
MAX_IMAGES,
getDynamicSource,
getSourceQuery
};
//# sourceMappingURL=dynamic-source.mjs.map