|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { useState } from '@wordpress/element'; |
| 5 | +import { starEmpty } from '@wordpress/icons'; |
| 6 | +import { Popover, MenuItem, MenuItemsChoice } from '@wordpress/components'; |
| 7 | +import { |
| 8 | + applyFormat, |
| 9 | + removeFormat, |
| 10 | + useAnchorRef, |
| 11 | + registerFormatType, |
| 12 | +} from '@wordpress/rich-text'; |
| 13 | +import { RichTextToolbarButton } from '@wordpress/block-editor'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Internal dependencies |
| 17 | + */ |
| 18 | +import './editor.scss'; |
| 19 | +import './style.scss'; |
| 20 | + |
| 21 | +const name = 'lubus/rating'; |
| 22 | +const title = 'Rating'; |
| 23 | +const ratings = [ |
| 24 | + { |
| 25 | + class: 'has-1-star', |
| 26 | + text: '1/5', |
| 27 | + }, |
| 28 | + { |
| 29 | + class: 'has-2-star', |
| 30 | + text: '2/5', |
| 31 | + }, |
| 32 | + { |
| 33 | + class: 'has-3-star', |
| 34 | + text: '3/5', |
| 35 | + }, |
| 36 | + { |
| 37 | + class: 'has-4-star', |
| 38 | + text: '4/5', |
| 39 | + }, |
| 40 | + { |
| 41 | + class: 'has-5-star', |
| 42 | + text: '5/5', |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +/** |
| 47 | + * InlineUI |
| 48 | + */ |
| 49 | +function InlineUI( { |
| 50 | + value, |
| 51 | + onChange, |
| 52 | + onClose, |
| 53 | + activeAttributes, |
| 54 | + contentRef, |
| 55 | +} ) { |
| 56 | + const anchorRef = useAnchorRef( { |
| 57 | + ref: contentRef, |
| 58 | + value, |
| 59 | + settings: { |
| 60 | + title, |
| 61 | + }, |
| 62 | + } ); |
| 63 | + |
| 64 | + const ratingChoices = ratings.map( ( rating ) => { |
| 65 | + const choice = { |
| 66 | + value: rating.class, |
| 67 | + label: ( |
| 68 | + <span className={ `has-rating ${ rating.class }` }> |
| 69 | + { rating.text } |
| 70 | + </span> |
| 71 | + ), |
| 72 | + }; |
| 73 | + |
| 74 | + return choice; |
| 75 | + } ); |
| 76 | + |
| 77 | + function onSetRating( rating ) { |
| 78 | + if ( 'none' === rating ) { |
| 79 | + onChange( removeFormat( value, name ) ); |
| 80 | + } else { |
| 81 | + onChange( |
| 82 | + applyFormat( value, { |
| 83 | + type: name, |
| 84 | + attributes: { |
| 85 | + class: rating, |
| 86 | + }, |
| 87 | + } ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + onClose(); // Close InlineUI |
| 92 | + } |
| 93 | + |
| 94 | + return ( |
| 95 | + <Popover |
| 96 | + position="bottom center" |
| 97 | + anchorRef={ anchorRef } |
| 98 | + className="block-editor-format-toolbar__lubus-badge-popover" |
| 99 | + onClose={ onClose } |
| 100 | + > |
| 101 | + <MenuItem onClick={ () => onSetRating( 'none' ) }> |
| 102 | + <span className="has-rating">none</span> |
| 103 | + </MenuItem> |
| 104 | + <MenuItemsChoice |
| 105 | + choices={ ratingChoices } |
| 106 | + value={ activeAttributes.class } |
| 107 | + onSelect={ ( rating ) => onSetRating( rating ) } |
| 108 | + /> |
| 109 | + </Popover> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Format edit |
| 115 | + */ |
| 116 | +function EditButton( props ) { |
| 117 | + const { value, onChange, onFocus, isActive, contentRef, activeAttributes } = |
| 118 | + props; |
| 119 | + |
| 120 | + const [ isSettingOpen, setIsSettingOpen ] = useState( false ); |
| 121 | + |
| 122 | + function openSettings() { |
| 123 | + setIsSettingOpen( true ); |
| 124 | + } |
| 125 | + |
| 126 | + function closeSettings() { |
| 127 | + setIsSettingOpen( false ); |
| 128 | + } |
| 129 | + |
| 130 | + return ( |
| 131 | + <> |
| 132 | + <RichTextToolbarButton |
| 133 | + icon={ starEmpty } |
| 134 | + title={ title } |
| 135 | + onClick={ openSettings } |
| 136 | + isActive={ isActive } |
| 137 | + /> |
| 138 | + |
| 139 | + { isSettingOpen && ( |
| 140 | + <InlineUI |
| 141 | + value={ value } |
| 142 | + onChange={ onChange } |
| 143 | + onClose={ closeSettings } |
| 144 | + activeAttributes={ activeAttributes } |
| 145 | + contentRef={ contentRef } |
| 146 | + /> |
| 147 | + ) } |
| 148 | + </> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Register Richtext Color Format. |
| 154 | + */ |
| 155 | +registerFormatType( name, { |
| 156 | + title, |
| 157 | + tagName: 'span', |
| 158 | + className: 'has-rating', |
| 159 | + edit: EditButton, |
| 160 | +} ); |
0 commit comments