index.js
112 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { useCallback, useState } from '@wordpress/element'; |
| 5 | import { registerFormatType, getActiveFormat } from '@wordpress/rich-text'; |
| 6 | import { |
| 7 | RichTextToolbarButton, |
| 8 | RichTextShortcut, |
| 9 | } from '@wordpress/block-editor'; |
| 10 | import { Icon } from '@wordpress/components'; |
| 11 | |
| 12 | /** |
| 13 | * Internal dependencies |
| 14 | */ |
| 15 | import { ReactComponent as IconSVG } from './icon.svg'; |
| 16 | import hex2rgba from '@vkblocks/utils/hex-to-rgba'; |
| 17 | import { default as InlineColorUI } from './inline'; |
| 18 | import { |
| 19 | name, |
| 20 | alpha, |
| 21 | highlighColor as settings, |
| 22 | highlighterOnApply, |
| 23 | } from './common'; |
| 24 | |
| 25 | function HighlighterEdit({ |
| 26 | value, |
| 27 | onChange, |
| 28 | isActive, |
| 29 | activeAttributes, |
| 30 | contentRef, |
| 31 | }) { |
| 32 | const shortcutType = 'primary'; |
| 33 | const shortcutChar = 'h'; |
| 34 | |
| 35 | let heightlightColor; |
| 36 | if (isActive) { |
| 37 | const activeFormat = getActiveFormat(value, name); |
| 38 | heightlightColor = activeFormat.attributes.data; |
| 39 | } |
| 40 | let iconStyle = {}; |
| 41 | if (heightlightColor) { |
| 42 | const rgbaHeightlightColor = hex2rgba(heightlightColor, alpha); |
| 43 | iconStyle = { |
| 44 | ...iconStyle, |
| 45 | backgroundImage: `linear-gradient(transparent 60%, ${rgbaHeightlightColor} 0)`, |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | const [isAddingColor, setIsAddingColor] = useState(false); |
| 50 | |
| 51 | const enableIsAddingColor = useCallback( |
| 52 | () => setIsAddingColor(true), |
| 53 | [setIsAddingColor] |
| 54 | ); |
| 55 | const disableIsAddingColor = useCallback( |
| 56 | () => setIsAddingColor(false), |
| 57 | [setIsAddingColor] |
| 58 | ); |
| 59 | |
| 60 | return ( |
| 61 | <> |
| 62 | <RichTextShortcut |
| 63 | type={shortcutType} |
| 64 | character={shortcutChar} |
| 65 | onUse={() => setIsAddingColor(true)} |
| 66 | /> |
| 67 | <RichTextToolbarButton |
| 68 | title={settings.title} |
| 69 | onClick={() => { |
| 70 | if (heightlightColor === undefined) { |
| 71 | // set default color on initial |
| 72 | highlighterOnApply({ |
| 73 | heightlightColor, |
| 74 | value, |
| 75 | onChange, |
| 76 | }); |
| 77 | } |
| 78 | setIsAddingColor(true); |
| 79 | enableIsAddingColor(true); |
| 80 | }} |
| 81 | shortcutType={shortcutType} |
| 82 | shortcutCharacter={shortcutChar} |
| 83 | className="format-library-text-color-button" |
| 84 | isActive={isActive} |
| 85 | icon={ |
| 86 | <> |
| 87 | <Icon icon={IconSVG} style={iconStyle} /> |
| 88 | </> |
| 89 | } |
| 90 | /> |
| 91 | {isAddingColor && ( |
| 92 | <InlineColorUI |
| 93 | name={name} |
| 94 | onClose={disableIsAddingColor} |
| 95 | activeAttributes={activeAttributes} |
| 96 | value={value} |
| 97 | onChange={onChange} |
| 98 | contentRef={contentRef} |
| 99 | setIsAddingColor={setIsAddingColor} |
| 100 | /> |
| 101 | )} |
| 102 | </> |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | const highlighColor = { |
| 107 | ...settings, |
| 108 | edit: HighlighterEdit, |
| 109 | }; |
| 110 | |
| 111 | registerFormatType(name, highlighColor); |
| 112 |