UNPKG

@wordpress/block-library

Version:
95 lines (94 loc) • 3.86 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_ui = require("@wordpress/ui"); let __order_options_ts = require("./order-options.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/gallery/order-controls.tsx function getOrderHelp(isRandom) { return isRandom ? (0, _wordpress_i18n.__)("Images are shown in a random order each time the page loads.") : void 0; } const SOURCE_ORDER_OPTIONS = [...__order_options_ts.ORDER_OPTIONS, __order_options_ts.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 ? __order_options_ts.RANDOM_ORDER : (0, __order_options_ts.toOrderValue)(order); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, { label: (0, _wordpress_i18n.__)("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 === __order_options_ts.RANDOM_ORDER) { onSelectRandom(); return; } onChange((0, __order_options_ts.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: (0, _wordpress_i18n.__)("Custom"), value: CUSTOM_ORDER, disabled: !isRandom }, ...__order_options_ts.ORDER_OPTIONS.map((option) => ({ ...option, disabled: !canSort })), __order_options_ts.RANDOM_OPTION ]; let value = CUSTOM_ORDER; if (isRandom) value = __order_options_ts.RANDOM_ORDER; else if (currentOrder) value = (0, __order_options_ts.toOrderValue)(currentOrder); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, { label: (0, _wordpress_i18n.__)("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 === __order_options_ts.RANDOM_ORDER) { onSelectRandom(); return; } if (nextValue === CUSTOM_ORDER) { onSelectCustom(); return; } onSort((0, __order_options_ts.parseOrderValue)(nextValue)); } }); } exports.SortImagesControl = SortImagesControl; exports.SourceOrderControl = SourceOrderControl; //# sourceMappingURL=order-controls.cjs.map