UNPKG

@wordpress/block-library

Version:
1 lines • 6.3 kB
{"version":3,"file":"order-controls.mjs","names":[],"sources":["../../src/gallery/order-controls.tsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { SelectControl } from '@wordpress/ui';\nimport {\n\tORDER_OPTIONS,\n\tRANDOM_OPTION,\n\tRANDOM_ORDER,\n\tparseOrderValue,\n\ttoOrderValue,\n} from './order-options';\nimport type { Order } from './order-options';\n\nfunction getOrderHelp( isRandom: boolean ): string | undefined {\n\treturn isRandom\n\t\t? __( 'Images are shown in a random order each time the page loads.' )\n\t\t: undefined;\n}\n\nconst SOURCE_ORDER_OPTIONS = [ ...ORDER_OPTIONS, RANDOM_OPTION ];\n\ntype SourceOrderControlProps = {\n\t/** The stored query order. */\n\torder: Order;\n\t/** Whether the gallery's `randomOrder` attribute is set. */\n\tisRandom: boolean;\n\t/**\n\t * Called with `{ orderby, order }` to update the query. Choosing an order\n\t * also turns \"Random\" off; the handler owns that so both writes land in one\n\t * undo level.\n\t */\n\tonChange: ( order: Order ) => void;\n\t/** Called when \"Random\" is chosen. */\n\tonSelectRandom: () => void;\n};\n\n/**\n * Ordering control for a dynamic gallery, shown in the Settings panel. The\n * chosen order is a *setting*: it's stored in the source's query and re-applied\n * every time the source resolves. \"Random\" is stored separately (the\n * `randomOrder` attribute) and overrides the query order on the front end, so\n * while it's set the control shows it instead of the query order.\n */\nexport function SourceOrderControl( {\n\torder,\n\tisRandom,\n\tonChange,\n\tonSelectRandom,\n}: SourceOrderControlProps ) {\n\tconst value = isRandom ? RANDOM_ORDER : toOrderValue( order );\n\n\treturn (\n\t\t<SelectControl\n\t\t\tlabel={ __( 'Order by' ) }\n\t\t\tvalue={\n\t\t\t\tSOURCE_ORDER_OPTIONS.find(\n\t\t\t\t\t( option ) => option.value === value\n\t\t\t\t) ?? null\n\t\t\t}\n\t\t\titems={ SOURCE_ORDER_OPTIONS }\n\t\t\tdescription={ getOrderHelp( isRandom ) }\n\t\t\tonValueChange={ ( item ) => {\n\t\t\t\tif ( ! item || item.value === null || item.value === value ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst nextValue = item.value;\n\t\t\t\tif ( nextValue === RANDOM_ORDER ) {\n\t\t\t\t\tonSelectRandom();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tonChange( parseOrderValue( nextValue ) );\n\t\t\t} }\n\t\t/>\n\t);\n}\n\nconst CUSTOM_ORDER = 'custom';\n\ntype SortImagesControlProps = {\n\t/** The last sort applied, while it still holds, or `null` for a custom order. */\n\tcurrentOrder: Order | null;\n\t/** Whether the gallery's `randomOrder` attribute is set. */\n\tisRandom: boolean;\n\t/** Whether the sort orders can be applied (false while media resolves). */\n\tcanSort: boolean;\n\t/**\n\t * Called with `{ orderby, order }` to sort the images. Sorting also turns\n\t * \"Random\" off; the handler owns that so both writes land in one undo level.\n\t */\n\tonSort: ( order: Order ) => void;\n\t/** Called when \"Custom\" is chosen, which turns \"Random\" off. */\n\tonSelectCustom: () => void;\n\t/** Called when \"Random\" is chosen. */\n\tonSelectRandom: () => void;\n};\n\n/**\n * Ordering control for a static gallery, shown in the Settings panel.\n *\n * The date and title orders are one-off *actions*: choosing one reorders the\n * inner image blocks once, and the user can drag them into any other order\n * afterwards. Nothing is stored for them, so the caller passes back the last\n * sort it applied for as long as the images remain in that sequence, and\n * `null` (\"Custom\") otherwise. \"Custom\" means the order arranged in the\n * editor, and is choosable only while \"Random\" is on: picking it turns\n * \"Random\" off without reordering anything.\n *\n * \"Random\" is the one stored value (the `randomOrder` attribute). It applies\n * on the front end only, leaving the editor order as it is, and choosing any\n * other option clears it.\n */\nexport function SortImagesControl( {\n\tcurrentOrder,\n\tisRandom,\n\tcanSort,\n\tonSort,\n\tonSelectCustom,\n\tonSelectRandom,\n}: SortImagesControlProps ) {\n\tconst options = [\n\t\t{ label: __( 'Custom' ), value: CUSTOM_ORDER, disabled: ! isRandom },\n\t\t...ORDER_OPTIONS.map( ( option ) => ( {\n\t\t\t...option,\n\t\t\tdisabled: ! canSort,\n\t\t} ) ),\n\t\tRANDOM_OPTION,\n\t];\n\n\tlet value = CUSTOM_ORDER;\n\tif ( isRandom ) {\n\t\tvalue = RANDOM_ORDER;\n\t} else if ( currentOrder ) {\n\t\tvalue = toOrderValue( currentOrder );\n\t}\n\n\treturn (\n\t\t<SelectControl\n\t\t\tlabel={ __( 'Order by' ) }\n\t\t\tvalue={\n\t\t\t\toptions.find( ( option ) => option.value === value ) ?? null\n\t\t\t}\n\t\t\titems={ options }\n\t\t\tdescription={ getOrderHelp( isRandom ) }\n\t\t\tonValueChange={ ( item ) => {\n\t\t\t\tif ( ! item || item.value === null || item.value === value ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst nextValue = item.value;\n\t\t\t\tif ( nextValue === RANDOM_ORDER ) {\n\t\t\t\t\tonSelectRandom();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif ( nextValue === CUSTOM_ORDER ) {\n\t\t\t\t\tonSelectCustom();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tonSort( parseOrderValue( nextValue ) );\n\t\t\t} }\n\t\t/>\n\t);\n}\n"],"mappings":";;;;;AAWA,SAAS,aAAc,UAAwC;CAC9D,OAAO,WACJ,GAAI,8DAA+D,IACnE,KAAA;AACJ;AAEA,MAAM,uBAAuB,CAAE,GAAG,eAAe,aAAc;;;;;;;;AAwB/D,SAAgB,mBAAoB,EACnC,OACA,UACA,UACA,kBAC4B;CAC5B,MAAM,QAAQ,WAAW,eAAe,aAAc,KAAM;CAE5D,OACC,oBAAC,eAAD;EACC,OAAQ,GAAI,UAAW;EACvB,OACC,qBAAqB,MAClB,WAAY,OAAO,UAAU,KAChC,KAAK;EAEN,OAAQ;EACR,aAAc,aAAc,QAAS;EACrC,gBAAkB,SAAU;GAC3B,IAAK,CAAE,QAAQ,KAAK,UAAU,QAAQ,KAAK,UAAU,OACpD;GAED,MAAM,YAAY,KAAK;GACvB,I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