@wordpress/block-library
Version:
Block library for the WordPress editor.
89 lines (88 loc) • 3.32 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { SelectControl } from "@wordpress/ui";
import { ORDER_OPTIONS, RANDOM_OPTION, RANDOM_ORDER, parseOrderValue, toOrderValue } from "./order-options.mjs";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/gallery/order-controls.tsx
function getOrderHelp(isRandom) {
return isRandom ? __("Images are shown in a random order each time the page loads.") : void 0;
}
const SOURCE_ORDER_OPTIONS = [...ORDER_OPTIONS, RANDOM_OPTION];
/**
* Ordering control for a dynamic gallery, shown in the Settings panel. The
* chosen order is a *setting*: it's stored in the source's query and re-applied
* every time the source resolves. "Random" is stored separately (the
* `randomOrder` attribute) and overrides the query order on the front end, so
* while it's set the control shows it instead of the query order.
*/
function SourceOrderControl({ order, isRandom, onChange, onSelectRandom }) {
const value = isRandom ? RANDOM_ORDER : toOrderValue(order);
return /* @__PURE__ */ jsx(SelectControl, {
label: __("Order by"),
value: SOURCE_ORDER_OPTIONS.find((option) => option.value === value) ?? null,
items: SOURCE_ORDER_OPTIONS,
description: getOrderHelp(isRandom),
onValueChange: (item) => {
if (!item || item.value === null || item.value === value) return;
const nextValue = item.value;
if (nextValue === RANDOM_ORDER) {
onSelectRandom();
return;
}
onChange(parseOrderValue(nextValue));
}
});
}
const CUSTOM_ORDER = "custom";
/**
* Ordering control for a static gallery, shown in the Settings panel.
*
* The date and title orders are one-off *actions*: choosing one reorders the
* inner image blocks once, and the user can drag them into any other order
* afterwards. Nothing is stored for them, so the caller passes back the last
* sort it applied for as long as the images remain in that sequence, and
* `null` ("Custom") otherwise. "Custom" means the order arranged in the
* editor, and is choosable only while "Random" is on: picking it turns
* "Random" off without reordering anything.
*
* "Random" is the one stored value (the `randomOrder` attribute). It applies
* on the front end only, leaving the editor order as it is, and choosing any
* other option clears it.
*/
function SortImagesControl({ currentOrder, isRandom, canSort, onSort, onSelectCustom, onSelectRandom }) {
const options = [
{
label: __("Custom"),
value: CUSTOM_ORDER,
disabled: !isRandom
},
...ORDER_OPTIONS.map((option) => ({
...option,
disabled: !canSort
})),
RANDOM_OPTION
];
let value = CUSTOM_ORDER;
if (isRandom) value = RANDOM_ORDER;
else if (currentOrder) value = toOrderValue(currentOrder);
return /* @__PURE__ */ jsx(SelectControl, {
label: __("Order by"),
value: options.find((option) => option.value === value) ?? null,
items: options,
description: getOrderHelp(isRandom),
onValueChange: (item) => {
if (!item || item.value === null || item.value === value) return;
const nextValue = item.value;
if (nextValue === RANDOM_ORDER) {
onSelectRandom();
return;
}
if (nextValue === CUSTOM_ORDER) {
onSelectCustom();
return;
}
onSort(parseOrderValue(nextValue));
}
});
}
export { SortImagesControl, SourceOrderControl };
//# sourceMappingURL=order-controls.mjs.map