Skip to content

Commit a37569a

Browse files
committed
feat(rating): migrate from individual repo
1 parent 8f00721 commit a37569a

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*/
44
import './marker';
55
import './infotip';
6+
import './rating';

src/rating/editor.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* The following styles get applied inside the editor only.
3+
*
4+
* Replace them with your own styles or remove the file completely.
5+
*/
6+
.block-editor-format-toolbar__lubus-badge-popover > div {
7+
padding: 12px;
8+
}

src/rating/index.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
} );

src/rating/style.scss

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The following styles get applied both on the front of your site
3+
* and in the editor.
4+
*
5+
* Replace them with your own styles or remove the file completely.
6+
*/
7+
8+
.has-rating {
9+
display: inline-block;
10+
11+
&:before {
12+
margin-right: 5px;
13+
color: #f69e0b;
14+
}
15+
}
16+
17+
.has-1-star:before {
18+
content: "★☆☆☆☆";
19+
}
20+
21+
.has-2-star:before {
22+
content: "★★☆☆☆";
23+
}
24+
25+
.has-3-star:before {
26+
content: "★★★☆☆";
27+
}
28+
29+
.has-4-star:before {
30+
content: "★★★★☆";
31+
}
32+
33+
.has-5-star:before {
34+
content: "★★★★★";
35+
}

0 commit comments

Comments
 (0)