block.json
3 months ago
edit.js
3 months ago
icon.svg
3 months ago
index.js
3 months ago
index.php
3 months ago
style.scss
3 months ago
edit.js
157 lines
| 1 | // import WordPress Scripts |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { |
| 4 | PanelBody, |
| 5 | SelectControl, |
| 6 | ToggleControl, |
| 7 | TextControl, |
| 8 | } from '@wordpress/components'; |
| 9 | import ServerSideRender from '@wordpress/server-side-render'; |
| 10 | import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 11 | import { useEffect } from '@wordpress/element'; |
| 12 | |
| 13 | // 正規表現を定数として定義 |
| 14 | const TITLE_TAG_REGEX = /^(h[2-6]).*/; |
| 15 | const CLASS_NAME_REGEX = /[^a-zA-Z0-9-_ ]/g; |
| 16 | |
| 17 | export default function AncestorPageListEdit(props) { |
| 18 | const { attributes, setAttributes } = props; |
| 19 | const { |
| 20 | ancestorTitleDisplay, |
| 21 | ancestorTitleTagName, |
| 22 | ancestorTitleClassName, |
| 23 | ancestorTitleLink, |
| 24 | displayHasChildOnly, |
| 25 | hiddenGrandChild, |
| 26 | } = attributes; |
| 27 | |
| 28 | const blockProps = useBlockProps(); |
| 29 | |
| 30 | // タイトルタグのバリデーション関数 |
| 31 | const validateTitleTag = (value) => { |
| 32 | const matches = value?.match(TITLE_TAG_REGEX); |
| 33 | return matches ? matches[1] : 'h4'; // デフォルト値を設定 |
| 34 | }; |
| 35 | |
| 36 | // クラス名のサニタイズ関数 |
| 37 | const sanitizeClassName = (value) => { |
| 38 | return value?.replace(CLASS_NAME_REGEX, '') || ''; |
| 39 | }; |
| 40 | |
| 41 | // useEffectを使ってレンダリング後にstateを更新 |
| 42 | useEffect(() => { |
| 43 | const validatedTag = validateTitleTag(ancestorTitleTagName); |
| 44 | const sanitizedValue = sanitizeClassName(ancestorTitleClassName); |
| 45 | const updates = {}; |
| 46 | if (validatedTag !== ancestorTitleTagName) { |
| 47 | updates.ancestorTitleTagName = validatedTag; |
| 48 | } |
| 49 | if (sanitizedValue !== ancestorTitleClassName) { |
| 50 | updates.ancestorTitleClassName = sanitizedValue; |
| 51 | } |
| 52 | if (Object.keys(updates).length > 0) { |
| 53 | setAttributes(updates); |
| 54 | } |
| 55 | }, [ancestorTitleTagName, ancestorTitleClassName]); |
| 56 | |
| 57 | return ( |
| 58 | <> |
| 59 | <InspectorControls> |
| 60 | <PanelBody |
| 61 | title={__('Ancestor Page List Setting', 'vk-blocks')} |
| 62 | > |
| 63 | <ToggleControl |
| 64 | label={__('Display Ancestor Page Title', 'vk-blocks')} |
| 65 | checked={ancestorTitleDisplay} |
| 66 | onChange={(checked) => |
| 67 | setAttributes({ ancestorTitleDisplay: checked }) |
| 68 | } |
| 69 | /> |
| 70 | <SelectControl |
| 71 | label={__('Ancestor title tag', 'vk-blocks')} |
| 72 | value={ancestorTitleTagName} |
| 73 | onChange={(value) => { |
| 74 | setAttributes({ |
| 75 | ancestorTitleTagName: validateTitleTag(value), |
| 76 | }); |
| 77 | }} |
| 78 | options={[ |
| 79 | { |
| 80 | value: 'h2', |
| 81 | label: __('h2', 'vk-blocks'), |
| 82 | }, |
| 83 | { |
| 84 | value: 'h3', |
| 85 | label: __('h3', 'vk-blocks'), |
| 86 | }, |
| 87 | { |
| 88 | value: 'h4', |
| 89 | label: __('h4', 'vk-blocks'), |
| 90 | }, |
| 91 | { |
| 92 | value: 'h5', |
| 93 | label: __('h5', 'vk-blocks'), |
| 94 | }, |
| 95 | { |
| 96 | value: 'h6', |
| 97 | label: __('h6', 'vk-blocks'), |
| 98 | }, |
| 99 | ]} |
| 100 | /> |
| 101 | <TextControl |
| 102 | label={__( |
| 103 | 'Ancestor page title class name', |
| 104 | 'vk-blocks' |
| 105 | )} |
| 106 | value={ancestorTitleClassName || ''} |
| 107 | className={`mt-0 mb-3`} |
| 108 | onChange={(value) => { |
| 109 | setAttributes({ |
| 110 | ancestorTitleClassName: |
| 111 | sanitizeClassName(value), |
| 112 | }); |
| 113 | }} |
| 114 | /> |
| 115 | <ToggleControl |
| 116 | label={__( |
| 117 | 'Add link to ancestor page title', |
| 118 | 'vk-blocks' |
| 119 | )} |
| 120 | checked={ancestorTitleLink} |
| 121 | onChange={(checked) => |
| 122 | setAttributes({ ancestorTitleLink: checked }) |
| 123 | } |
| 124 | /> |
| 125 | <hr /> |
| 126 | <ToggleControl |
| 127 | label={__( |
| 128 | 'If there is no child page, the block itself is not displayed', |
| 129 | 'vk-blocks' |
| 130 | )} |
| 131 | checked={displayHasChildOnly} |
| 132 | onChange={(checked) => |
| 133 | setAttributes({ displayHasChildOnly: checked }) |
| 134 | } |
| 135 | /> |
| 136 | <ToggleControl |
| 137 | label={__( |
| 138 | "Don't display inactive grand child pages", |
| 139 | 'vk-blocks' |
| 140 | )} |
| 141 | checked={hiddenGrandChild} |
| 142 | onChange={(checked) => |
| 143 | setAttributes({ hiddenGrandChild: checked }) |
| 144 | } |
| 145 | /> |
| 146 | </PanelBody> |
| 147 | </InspectorControls> |
| 148 | <div {...blockProps}> |
| 149 | <ServerSideRender |
| 150 | block="vk-blocks/ancestor-page-list" |
| 151 | attributes={attributes} |
| 152 | /> |
| 153 | </div> |
| 154 | </> |
| 155 | ); |
| 156 | } |
| 157 |