add-item.js
2 months ago
delete-item-button.js
2 months ago
index.js
2 months ago
preview.js
2 months ago
add-item.js
172 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { useContext, useState } from '@wordpress/element'; |
| 6 | import { |
| 7 | Button, |
| 8 | Modal, |
| 9 | TextControl, |
| 10 | Flex, |
| 11 | FlexItem, |
| 12 | } from '@wordpress/components'; |
| 13 | import { plusCircle } from '@wordpress/icons'; |
| 14 | |
| 15 | /** |
| 16 | * Internal dependencies |
| 17 | */ |
| 18 | import { AdminContext } from '@vkblocks/admin/index'; |
| 19 | /*globals vkBlocksObject */ |
| 20 | |
| 21 | // 書式設定 初期値 |
| 22 | const CUSTOM_FORMAT_DEFAULT_OBJ = { |
| 23 | title: '', |
| 24 | font_weight_bold: false, |
| 25 | font_italic: false, |
| 26 | font_strikethrough: false, |
| 27 | color: '', |
| 28 | background_color: '', |
| 29 | is_active_highlighter: false, |
| 30 | highlighter: vkBlocksObject.highlighterColor, |
| 31 | font_size: '', |
| 32 | nowrap: false, |
| 33 | class_name: '', |
| 34 | }; |
| 35 | |
| 36 | export const AddItemButton = () => { |
| 37 | const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext); |
| 38 | const [isModalOpen, setIsModalOpen] = useState(false); |
| 39 | const [title, setTitle] = useState(''); |
| 40 | const [className, setClassName] = useState(''); |
| 41 | const [isDisableAdd, setIsDisableAdd] = useState(false); |
| 42 | const [errorMessage, setErrorMessage] = useState(''); |
| 43 | |
| 44 | const openModal = () => setIsModalOpen(true); |
| 45 | // モーダルをクローズしたらstateを初期値に戻す |
| 46 | const closeModal = () => { |
| 47 | setIsModalOpen(false); |
| 48 | setTitle(''); |
| 49 | setClassName(''); |
| 50 | setIsDisableAdd(false); |
| 51 | setErrorMessage(''); |
| 52 | }; |
| 53 | |
| 54 | const addItem = () => { |
| 55 | vkBlocksOption.custom_format_lists.push({ |
| 56 | ...CUSTOM_FORMAT_DEFAULT_OBJ, |
| 57 | ...{ |
| 58 | class_name: className, |
| 59 | title, |
| 60 | }, |
| 61 | }); |
| 62 | setVkBlocksOption({ ...vkBlocksOption }); |
| 63 | }; |
| 64 | |
| 65 | const validateClassName = (value) => { |
| 66 | let bool = true; |
| 67 | let message; |
| 68 | if (typeof value !== 'string') { |
| 69 | bool = false; |
| 70 | message = __('Please enter a string', 'vk-blocks'); |
| 71 | } |
| 72 | if (!/^[a-z][a-z0-9-]*$/.test(value)) { |
| 73 | bool = false; |
| 74 | message = __( |
| 75 | 'Must begin with an alphabetic character and only alphanumeric characters and hyphens may be used.', |
| 76 | 'vk-blocks' |
| 77 | ); |
| 78 | } |
| 79 | if (value === '') { |
| 80 | bool = false; |
| 81 | message = __('Class name is required', 'vk-blocks'); |
| 82 | } |
| 83 | // クラス名が既に登録されているか |
| 84 | vkBlocksOption.custom_format_lists.forEach((option) => { |
| 85 | if (option.class_name === value) { |
| 86 | bool = false; |
| 87 | message = __('Already registered', 'vk-blocks'); |
| 88 | } |
| 89 | }); |
| 90 | setIsDisableAdd(bool); |
| 91 | setErrorMessage(message); |
| 92 | }; |
| 93 | |
| 94 | return ( |
| 95 | <div className="custom_format_lists_item_add"> |
| 96 | <Button |
| 97 | className="add-item-button" |
| 98 | icon={plusCircle} |
| 99 | iconSize={18} |
| 100 | variant="secondary" |
| 101 | onClick={openModal} |
| 102 | > |
| 103 | {__('Add Custom Format', 'vk-blocks')} |
| 104 | </Button> |
| 105 | {isModalOpen && ( |
| 106 | <Modal |
| 107 | title={__('Add Custom Format', 'vk-blocks')} |
| 108 | onRequestClose={closeModal} |
| 109 | isDismissible={false} |
| 110 | > |
| 111 | <div className="custom_format_add_modal"> |
| 112 | <TextControl |
| 113 | className="custom_format_item_class_name" |
| 114 | label={__( |
| 115 | 'CSS class/unique ID (Required/Unchangeable)', |
| 116 | 'vk-blocks' |
| 117 | )} |
| 118 | placeholder={__('(e.g.) vk-format-1', 'vk-blocks')} |
| 119 | onChange={(value) => { |
| 120 | value = value.trim(); |
| 121 | setClassName(value); |
| 122 | validateClassName(value); |
| 123 | }} |
| 124 | value={className ? className : ''} |
| 125 | /> |
| 126 | {!isDisableAdd && ( |
| 127 | <p className="custom_format_item_name_error"> |
| 128 | {errorMessage} |
| 129 | </p> |
| 130 | )} |
| 131 | <TextControl |
| 132 | className="custom_format_item_title" |
| 133 | label={__( |
| 134 | 'Toolbar title (Changeable)', |
| 135 | 'vk-blocks' |
| 136 | )} |
| 137 | onChange={(value) => { |
| 138 | setTitle(value); |
| 139 | }} |
| 140 | value={title} |
| 141 | /> |
| 142 | <div className="custom_format_add_modal_button_area"> |
| 143 | <Flex justify="flex-end"> |
| 144 | <FlexItem> |
| 145 | <Button |
| 146 | variant="secondary" |
| 147 | onClick={closeModal} |
| 148 | > |
| 149 | {__('Cancel')} |
| 150 | </Button> |
| 151 | </FlexItem> |
| 152 | <FlexItem> |
| 153 | <Button |
| 154 | onClick={() => { |
| 155 | addItem(); |
| 156 | closeModal(); |
| 157 | }} |
| 158 | variant="primary" |
| 159 | disabled={!isDisableAdd} |
| 160 | > |
| 161 | {__('Add', 'vk-blocks')} |
| 162 | </Button> |
| 163 | </FlexItem> |
| 164 | </Flex> |
| 165 | </div> |
| 166 | </div> |
| 167 | </Modal> |
| 168 | )} |
| 169 | </div> |
| 170 | ); |
| 171 | }; |
| 172 |