style.js
127 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { addFilter } from '@wordpress/hooks'; |
| 5 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 6 | import { InspectorControls } from '@wordpress/block-editor'; |
| 7 | import { PanelBody, SelectControl } from '@wordpress/components'; |
| 8 | import { __ } from '@wordpress/i18n'; |
| 9 | |
| 10 | /** |
| 11 | * Internal dependencies |
| 12 | */ |
| 13 | import { VkPanelIcon } from '@vkblocks/components/vk-icon'; |
| 14 | |
| 15 | /** |
| 16 | * Query Block Extension - Add Modified Date Order Option |
| 17 | * |
| 18 | * This extension adds a "Modified Date" order option to the WordPress core Query block. |
| 19 | * It extends the core Query block's orderBy options without duplicating existing ones. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Add modified date order option to Query block |
| 24 | */ |
| 25 | const withQueryOrderExtension = createHigherOrderComponent( |
| 26 | (BlockEdit) => (props) => { |
| 27 | const { name, attributes, setAttributes } = props; |
| 28 | |
| 29 | // Only apply to core/query block |
| 30 | if (name !== 'core/query') { |
| 31 | return <BlockEdit {...props} />; |
| 32 | } |
| 33 | |
| 34 | const { query } = attributes; |
| 35 | const { orderBy = 'date', order = 'desc' } = query || {}; |
| 36 | |
| 37 | // セレクトボックスの値を取得 |
| 38 | const getSelectValue = () => { |
| 39 | if (orderBy === 'modified') { |
| 40 | return order === 'desc' ? 'modified_desc' : 'modified_asc'; |
| 41 | } |
| 42 | return ''; |
| 43 | }; |
| 44 | |
| 45 | return ( |
| 46 | <> |
| 47 | <BlockEdit {...props} /> |
| 48 | <InspectorControls> |
| 49 | <PanelBody |
| 50 | title={__('VK Query Extension', 'vk-blocks')} |
| 51 | icon={<VkPanelIcon isActive={orderBy === 'modified'} />} |
| 52 | initialOpen={false} |
| 53 | > |
| 54 | <SelectControl |
| 55 | label={__('Order by', 'vk-blocks')} |
| 56 | help={__( |
| 57 | 'This setting will override the standard order setting.', |
| 58 | 'vk-blocks' |
| 59 | )} |
| 60 | value={getSelectValue()} |
| 61 | onChange={(value) => { |
| 62 | if (value === 'modified_desc') { |
| 63 | setAttributes({ |
| 64 | query: { |
| 65 | ...query, |
| 66 | orderBy: 'modified', |
| 67 | order: 'desc', |
| 68 | }, |
| 69 | }); |
| 70 | } else if (value === 'modified_asc') { |
| 71 | setAttributes({ |
| 72 | query: { |
| 73 | ...query, |
| 74 | orderBy: 'modified', |
| 75 | order: 'asc', |
| 76 | }, |
| 77 | }); |
| 78 | } else if (orderBy === 'modified') { |
| 79 | // Reset to default if modified was selected and user wants to use core options |
| 80 | setAttributes({ |
| 81 | query: { |
| 82 | ...query, |
| 83 | orderBy: 'date', |
| 84 | order: 'desc', |
| 85 | }, |
| 86 | }); |
| 87 | } |
| 88 | }} |
| 89 | options={[ |
| 90 | { |
| 91 | value: '', |
| 92 | label: __( |
| 93 | 'Use standard options', |
| 94 | 'vk-blocks' |
| 95 | ), |
| 96 | }, |
| 97 | { |
| 98 | value: 'modified_desc', |
| 99 | label: __( |
| 100 | 'Modified (Newest to oldest)', |
| 101 | 'vk-blocks' |
| 102 | ), |
| 103 | }, |
| 104 | { |
| 105 | value: 'modified_asc', |
| 106 | label: __( |
| 107 | 'Modified (Oldest to newest)', |
| 108 | 'vk-blocks' |
| 109 | ), |
| 110 | }, |
| 111 | ]} |
| 112 | /> |
| 113 | </PanelBody> |
| 114 | </InspectorControls> |
| 115 | </> |
| 116 | ); |
| 117 | }, |
| 118 | 'withQueryOrderExtension' |
| 119 | ); |
| 120 | |
| 121 | // Apply the extension to the Query block |
| 122 | addFilter( |
| 123 | 'editor.BlockEdit', |
| 124 | 'vk-blocks/query-order-extension', |
| 125 | withQueryOrderExtension |
| 126 | ); |
| 127 |