inline.js
151 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { |
| 5 | applyFormat, |
| 6 | removeFormat, |
| 7 | getActiveFormat, |
| 8 | useAnchor, |
| 9 | useAnchorRef, |
| 10 | } from '@wordpress/rich-text'; |
| 11 | import { useCachedTruthy } from '@wordpress/block-editor'; |
| 12 | import { Popover, FontSizePicker, Button } from '@wordpress/components'; |
| 13 | import { __ } from '@wordpress/i18n'; |
| 14 | import { useMemo } from '@wordpress/element'; |
| 15 | |
| 16 | /** |
| 17 | * Internal dependencies |
| 18 | */ |
| 19 | import { inlineFontSize as settings } from './index'; |
| 20 | |
| 21 | const fontSizes = [ |
| 22 | { |
| 23 | name: __('Small', 'vk-blocks'), |
| 24 | slug: 'small', |
| 25 | size: '12px', |
| 26 | }, |
| 27 | { |
| 28 | name: __('Normal', 'vk-blocks'), |
| 29 | slug: 'normal', |
| 30 | size: '16px', |
| 31 | }, |
| 32 | { |
| 33 | name: __('Big', 'vk-blocks'), |
| 34 | slug: 'big', |
| 35 | size: '18px', |
| 36 | }, |
| 37 | { |
| 38 | name: __('Extra big', 'vk-blocks'), |
| 39 | slug: 'extra-big', |
| 40 | size: '21px', |
| 41 | }, |
| 42 | ]; |
| 43 | |
| 44 | function parseFontSize(fontSize = '') { |
| 45 | return fontSize.replace('font-size:', '').replace(/;/g, '').trim(); |
| 46 | } |
| 47 | |
| 48 | export function getActiveInlineFontSize(value, name) { |
| 49 | const activeInlineFontSizeFormat = getActiveFormat(value, name); |
| 50 | |
| 51 | if (!activeInlineFontSizeFormat) { |
| 52 | return undefined; |
| 53 | } |
| 54 | |
| 55 | if (activeInlineFontSizeFormat.attributes.data !== undefined) { |
| 56 | return activeInlineFontSizeFormat.attributes.data; |
| 57 | } |
| 58 | |
| 59 | return parseFontSize(activeInlineFontSizeFormat.attributes.style); |
| 60 | } |
| 61 | |
| 62 | function InlineFontSizePicker({ name, value, onChange, setIsSettingFontSize }) { |
| 63 | const pickerStyle = { |
| 64 | width: '200px', |
| 65 | }; |
| 66 | const buttonStyle = { |
| 67 | marginTop: '16px', |
| 68 | padding: '0 16px', |
| 69 | height: '30px', |
| 70 | }; |
| 71 | |
| 72 | const onInlineFontSizeChange = (newFontSize) => { |
| 73 | if (!newFontSize) { |
| 74 | // reset font size |
| 75 | onChange(removeFormat(value, name)); |
| 76 | } else if (newFontSize.match(/\d/)) { |
| 77 | // Applies only Font Size has a numeric value. |
| 78 | onChange( |
| 79 | applyFormat(value, { |
| 80 | type: name, |
| 81 | attributes: { |
| 82 | data: `${newFontSize}`, |
| 83 | style: `font-size: ${newFontSize};`, |
| 84 | }, |
| 85 | }) |
| 86 | ); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | const activeFontSize = getActiveInlineFontSize(value, name); |
| 91 | |
| 92 | return ( |
| 93 | <div style={pickerStyle}> |
| 94 | <FontSizePicker |
| 95 | __nextHasNoMarginBottom |
| 96 | fontSizes={fontSizes} |
| 97 | value={activeFontSize} |
| 98 | onChange={onInlineFontSizeChange} |
| 99 | /> |
| 100 | <Button |
| 101 | onClick={() => { |
| 102 | setIsSettingFontSize(false); |
| 103 | }} |
| 104 | isSmall |
| 105 | variant="secondary" |
| 106 | style={buttonStyle} |
| 107 | > |
| 108 | {__('Apply', 'vk-blocks')} |
| 109 | </Button> |
| 110 | </div> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | export default function InlineFontSizeUI({ |
| 115 | name, |
| 116 | value, |
| 117 | onChange, |
| 118 | onClose, |
| 119 | contentRef, |
| 120 | setIsSettingFontSize, |
| 121 | }) { |
| 122 | // NOTE: useAnchorRefが非推奨になったのでフォールバック WP6.0以下をサポートしなくなったら削除すること #1456 |
| 123 | const existsUseAnchor = typeof useAnchor === 'function'; |
| 124 | const _useAnchor = existsUseAnchor ? useAnchor : useAnchorRef; |
| 125 | const useAnchorObj = existsUseAnchor |
| 126 | ? { editableContentElement: contentRef.current, value, settings } |
| 127 | : { ref: contentRef, value }; |
| 128 | const popoverAnchor = useCachedTruthy(_useAnchor(useAnchorObj)); |
| 129 | |
| 130 | const rect = useMemo(() => popoverAnchor.getBoundingClientRect(), []); |
| 131 | if (!!popoverAnchor?.ownerDocument) { |
| 132 | popoverAnchor.getBoundingClientRect = () => rect; |
| 133 | } |
| 134 | |
| 135 | return ( |
| 136 | <Popover |
| 137 | onClose={onClose} |
| 138 | className="vk-blocks-format-popover components-inline-color-popover" |
| 139 | anchor={existsUseAnchor ? popoverAnchor : undefined} |
| 140 | anchorRef={existsUseAnchor ? undefined : popoverAnchor} |
| 141 | > |
| 142 | <InlineFontSizePicker |
| 143 | name={name} |
| 144 | value={value} |
| 145 | onChange={onChange} |
| 146 | setIsSettingFontSize={setIsSettingFontSize} |
| 147 | /> |
| 148 | </Popover> |
| 149 | ); |
| 150 | } |
| 151 |