balloon
1 week ago
block-manager
2 months ago
block-style-manager
2 months ago
custom-block-style
2 months ago
custom-format
2 months ago
import-export
2 months ago
utils
2 months ago
block-category-position.js
2 months ago
breadcrumb.js
2 months ago
custom-css.js
2 months ago
index.js
2 months ago
license.js
2 months ago
load-separate.js
2 months ago
margin.js
2 months ago
new-faq.js
2 months ago
save-button.js
2 months ago
toc.js
2 months ago
toc.js
189 lines
| 1 | /*globals vkBlocksObject */ |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { useContext, useMemo, useCallback } from '@wordpress/element'; |
| 4 | import { AdminContext } from './index'; |
| 5 | import { |
| 6 | SelectControl, |
| 7 | CheckboxControl, |
| 8 | RadioControl, |
| 9 | } from '@wordpress/components'; |
| 10 | import { BlockIcon } from '@wordpress/block-editor'; |
| 11 | import { withSelect } from '@wordpress/data'; |
| 12 | import { useInstanceId } from '@wordpress/compose'; |
| 13 | import classnames from 'classnames'; |
| 14 | import { |
| 15 | generateHeadingLevels, |
| 16 | getCurrentMaxLevel, |
| 17 | } from '@vkblocks/utils/heading-level-utils'; |
| 18 | |
| 19 | function AdminToc({ borderBoxBlockType }) { |
| 20 | const instanceId = useInstanceId(AdminToc); |
| 21 | const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext); |
| 22 | |
| 23 | // blockJsonのtitleがあったらblockTypesのtitleがあれば |
| 24 | const getBlockTitle = useCallback((name, blockTypeTitle) => { |
| 25 | // vkBlocksObjectが利用可能な場合 |
| 26 | if ( |
| 27 | typeof vkBlocksObject !== 'undefined' && |
| 28 | vkBlocksObject.blockJsonLists |
| 29 | ) { |
| 30 | const blockJsonList = vkBlocksObject.blockJsonLists.find( |
| 31 | (item) => item.name === name |
| 32 | ); |
| 33 | return blockJsonList && blockJsonList.title |
| 34 | ? blockJsonList.title |
| 35 | : blockTypeTitle; |
| 36 | } |
| 37 | return blockTypeTitle; |
| 38 | }, []); |
| 39 | |
| 40 | // 枠線ブロックのタイトルをメモ化 |
| 41 | const borderBoxTitle = useMemo(() => { |
| 42 | return getBlockTitle( |
| 43 | 'vk-blocks/border-box', |
| 44 | __('Border Box', 'vk-blocks') |
| 45 | ); |
| 46 | }, [getBlockTitle]); |
| 47 | |
| 48 | // ブロックマネージャーと同じ構造のトグル関数 |
| 49 | const toggleBorderBoxInclusion = useCallback( |
| 50 | (nextIsChecked) => { |
| 51 | setVkBlocksOption({ |
| 52 | ...vkBlocksOption, |
| 53 | toc_include_border_box: nextIsChecked, |
| 54 | }); |
| 55 | }, |
| 56 | [vkBlocksOption, setVkBlocksOption] |
| 57 | ); |
| 58 | |
| 59 | const handleMaxLevelChange = (maxLevel) => { |
| 60 | const levels = generateHeadingLevels(maxLevel); |
| 61 | |
| 62 | setVkBlocksOption({ |
| 63 | ...vkBlocksOption, |
| 64 | toc_heading_levels: levels, |
| 65 | }); |
| 66 | }; |
| 67 | |
| 68 | const handleNumberingChange = (value) => { |
| 69 | setVkBlocksOption({ |
| 70 | ...vkBlocksOption, |
| 71 | toc_numbering: value, |
| 72 | }); |
| 73 | }; |
| 74 | |
| 75 | // 現在の最大レベルを取得 |
| 76 | const getCurrentMaxLevelForAdmin = () => { |
| 77 | return getCurrentMaxLevel(vkBlocksOption.toc_heading_levels); |
| 78 | }; |
| 79 | |
| 80 | return ( |
| 81 | <section className="vk_admin_page_section" id="toc-setting"> |
| 82 | <h3>{__('Table of Contents Setting', 'vk-blocks')}</h3> |
| 83 | <p> |
| 84 | {__( |
| 85 | 'Please specify a common heading level setting to be used across the site.', |
| 86 | 'vk-blocks' |
| 87 | )} |
| 88 | </p> |
| 89 | <SelectControl |
| 90 | name="vk_blocks_options[toc_heading_levels]" |
| 91 | className="vk_admin_selectControl" |
| 92 | value={getCurrentMaxLevelForAdmin()} |
| 93 | options={[ |
| 94 | { label: 'H2', value: 'h2' }, |
| 95 | { label: 'H3', value: 'h3' }, |
| 96 | { label: 'H4', value: 'h4' }, |
| 97 | { label: 'H5', value: 'h5' }, |
| 98 | { label: 'H6', value: 'h6' }, |
| 99 | ]} |
| 100 | onChange={(value) => handleMaxLevelChange(value)} |
| 101 | /> |
| 102 | <p className="description"> |
| 103 | {__( |
| 104 | 'Headings from H2 up to the selected level will be included.', |
| 105 | 'vk-blocks' |
| 106 | )} |
| 107 | </p> |
| 108 | <div style={{ marginTop: '1rem' }}> |
| 109 | <h4>{__('Include in Table of Contents', 'vk-blocks')}</h4> |
| 110 | <div |
| 111 | role="group" |
| 112 | aria-labelledby={`toc-block-integration-title-${instanceId}`} |
| 113 | className={classnames( |
| 114 | 'block-manager__category', |
| 115 | 'blockManagerList' |
| 116 | )} |
| 117 | > |
| 118 | <ul className="block-manager__checklist block-manager__checklist--no-parent"> |
| 119 | <li className="block-manager__checklist-item block-manager__checklist-item--no-parent"> |
| 120 | <CheckboxControl |
| 121 | __nextHasNoMarginBottom |
| 122 | label={ |
| 123 | <> |
| 124 | {borderBoxTitle} |
| 125 | {borderBoxBlockType && ( |
| 126 | <BlockIcon |
| 127 | icon={borderBoxBlockType.icon} |
| 128 | /> |
| 129 | )} |
| 130 | </> |
| 131 | } |
| 132 | checked={ |
| 133 | !!vkBlocksOption?.toc_include_border_box |
| 134 | } |
| 135 | onChange={toggleBorderBoxInclusion} |
| 136 | name="vk_blocks_options[toc_include_border_box]" |
| 137 | /> |
| 138 | </li> |
| 139 | </ul> |
| 140 | </div> |
| 141 | <p className="description"> |
| 142 | {__( |
| 143 | 'When enabled, headings within Border Box blocks will be included in the Table of Contents.', |
| 144 | 'vk-blocks' |
| 145 | )} |
| 146 | </p> |
| 147 | </div> |
| 148 | <div style={{ marginTop: '1rem' }}> |
| 149 | <h4>{__('Numbering', 'vk-blocks')}</h4> |
| 150 | <RadioControl |
| 151 | selected={vkBlocksOption?.toc_numbering || 'auto'} |
| 152 | options={[ |
| 153 | { |
| 154 | label: __( |
| 155 | 'Auto (hierarchical numbering)', |
| 156 | 'vk-blocks' |
| 157 | ), |
| 158 | value: 'auto', |
| 159 | }, |
| 160 | { |
| 161 | label: __('H2 only', 'vk-blocks'), |
| 162 | value: 'h2-only', |
| 163 | }, |
| 164 | { |
| 165 | label: __('None', 'vk-blocks'), |
| 166 | value: 'none', |
| 167 | }, |
| 168 | ]} |
| 169 | onChange={handleNumberingChange} |
| 170 | /> |
| 171 | <p className="description"> |
| 172 | {__( |
| 173 | 'Choose how numbers are displayed in the Table of Contents.', |
| 174 | 'vk-blocks' |
| 175 | )} |
| 176 | </p> |
| 177 | </div> |
| 178 | </section> |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | export default withSelect((select) => { |
| 183 | const { getBlockType } = select('core/blocks'); |
| 184 | |
| 185 | return { |
| 186 | borderBoxBlockType: getBlockType('vk-blocks/border-box'), |
| 187 | }; |
| 188 | })(AdminToc); |
| 189 |