PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.4.1
Block Animations, Motion & Scroll Effects – Ghost Kit v3.4.1
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / settings / pages / typography.js

typography.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.4.1, at settings/pages/typography.js

267 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { debounce } from 'throttle-debounce';
2
3 import apiFetch from '@wordpress/api-fetch';
4 import { Button, Spinner } from '@wordpress/components';
5 import { compose } from '@wordpress/compose';
6 import { withDispatch, withSelect } from '@wordpress/data';
7 import { Component } from '@wordpress/element';
8 import { __ } from '@wordpress/i18n';
9
10 import Typography from '../../gutenberg/components/typography';
11 import {
12 getCustomTypographyList,
13 getInitialAdvancedState,
14 } from '../../gutenberg/plugins/typography';
15
16 class TypographySettings extends Component {
17 constructor(props) {
18 super(props);
19
20 this.state = {
21 customTypography: false,
22 advanced: {},
23 };
24
25 this.maybePrepareTypographyData =
26 this.maybePrepareTypographyData.bind(this);
27 this.getPlaceholders = this.getPlaceholders.bind(this);
28 this.updateTypography = this.updateTypography.bind(this);
29 this.updateTypographyDebounce = debounce(
30 1000,
31 this.updateTypographyDebounce.bind(this)
32 );
33 }
34
35 componentDidMount() {
36 this.maybePrepareTypographyData();
37 }
38
39 componentDidUpdate() {
40 this.maybePrepareTypographyData();
41 }
42
43 /**
44 * Function for setting the current state with a list of child typographies and a button status flag when a button is clicked.
45 *
46 * @param {int} key - Typography identifier.
47 */
48 onClickAdvanced(key) {
49 this.setState((prevState) => ({
50 advanced: {
51 ...prevState.advanced,
52 [key]: !prevState.advanced[key],
53 },
54 }));
55 }
56
57 /**
58 * The function returns a placeholder object.
59 *
60 * @param {string} key - Key of current typography.
61 * @param {boolean} isGlobal - Flag of global customization.
62 * @return {Object} - Placeholders Object.
63 */
64 getPlaceholders() {
65 const placeholders = {
66 'font-size': '-',
67 'line-height': '-',
68 'letter-spacing': '-',
69 };
70
71 return placeholders;
72 }
73
74 /**
75 * Function for get Child Render Typographies.
76 *
77 * @param {Object} typographyList - Typography List.
78 * @param {int} key - Typography identifier.
79 * @param {boolean} isGlobal - Flag of global customization.
80 * @return {Array} - Array with child typography objects.
81 */
82 getChildrenTypography(typographyList, key) {
83 const childTypographies = [];
84
85 Object.keys(typographyList).forEach((childKey) => {
86 if (typographyList[childKey].childOf === key) {
87 childTypographies.push(
88 <div key={childKey}>
89 {this.getTypographyComponent(typographyList, childKey)}
90 </div>
91 );
92 }
93 });
94
95 return childTypographies;
96 }
97
98 /**
99 * Function for get Typography Component.
100 *
101 * @param {Object} typographyList - Typography List.
102 * @param {int} key - Typography identifier.
103 * @param {boolean} isGlobal - Flag of global customization.
104 * @return {*} - Typography Object.
105 */
106 getTypographyComponent(typographyList, key) {
107 const placeholders = this.getPlaceholders();
108
109 return this.state.customTypography !== false ? (
110 <Typography
111 onChange={(opt) => {
112 this.updateTypography(opt, typographyList, key);
113 }}
114 fontFamily={typographyList[key].fontFamily}
115 fontFamilyCategory={typographyList[key].fontFamilyCategory}
116 fontWeight={typographyList[key].fontWeight}
117 fontSize={typographyList[key].fontSize}
118 lineHeight={typographyList[key].lineHeight}
119 letterSpacing={typographyList[key].letterSpacing}
120 label={typographyList[key].label}
121 placeholders={placeholders}
122 childOf={typographyList[key].childOf}
123 />
124 ) : (
125 <Spinner />
126 );
127 }
128
129 maybePrepareTypographyData() {
130 const { customTypography = {} } = this.props;
131
132 if (customTypography && this.state.customTypography === false) {
133 this.setState({
134 customTypography:
135 getCustomTypographyList(
136 customTypography.ghostkit_typography,
137 true
138 ) || '',
139 advanced: getInitialAdvancedState(
140 getCustomTypographyList(
141 customTypography.ghostkit_typography,
142 true
143 )
144 ),
145 });
146 }
147 }
148
149 updateTypography(opt, typographyList, key) {
150 this.setState(
151 {
152 customTypography: {
153 ...typographyList,
154 [key]: {
155 ...typographyList[key],
156 ...opt,
157 },
158 },
159 },
160 () => {
161 this.updateTypographyDebounce();
162 }
163 );
164 }
165
166 updateTypographyDebounce() {
167 this.props.updateTypography({
168 ghostkit_typography: this.state.customTypography,
169 });
170 }
171
172 render() {
173 const typographyList = getCustomTypographyList(
174 this.state.customTypography,
175 true
176 );
177
178 return (
179 <div className="ghostkit-settings-content-wrapper ghostkit-settings-typography">
180 {typographyList && Object.keys(typographyList).length ? (
181 <>
182 {Object.keys(typographyList).map((key) => {
183 const advancedData = this.state.advanced[key];
184 const advancedLabel =
185 advancedData === true
186 ? __('Hide Advanced', 'ghostkit')
187 : __('Show Advanced', 'ghostkit');
188
189 if (typographyList[key].childOf === '') {
190 return (
191 <div
192 className="ghostkit-typography-container"
193 key={key}
194 >
195 {this.getTypographyComponent(
196 typographyList,
197 key
198 )}
199 {typeof advancedData !== 'undefined' ? (
200 <div className="ghostkit-typography-advanced">
201 <Button
202 variant="secondary"
203 onClick={() =>
204 this.onClickAdvanced(
205 key
206 )
207 }
208 className="ghostkit-typography-advanced-button"
209 >
210 {advancedLabel}
211 </Button>
212 </div>
213 ) : null}
214 {advancedData
215 ? this.getChildrenTypography(
216 typographyList,
217 key
218 )
219 : ''}
220 </div>
221 );
222 }
223
224 return '';
225 })}
226 </>
227 ) : null}
228 </div>
229 );
230 }
231 }
232
233 export default compose([
234 withSelect((select) => {
235 const customTypography = select(
236 'ghostkit/plugins/typography'
237 ).getCustomTypography();
238
239 try {
240 customTypography.ghostkit_typography = JSON.parse(
241 customTypography.ghostkit_typography
242 );
243 } catch (e) {}
244
245 return {
246 customTypography,
247 };
248 }),
249 withDispatch((dispatch) => ({
250 updateTypography(value) {
251 value = {
252 ghostkit_typography: JSON.stringify(value.ghostkit_typography),
253 };
254
255 dispatch('ghostkit/plugins/typography').setCustomTypography(value);
256
257 apiFetch({
258 path: '/ghostkit/v1/update_custom_typography',
259 method: 'POST',
260 data: {
261 data: value,
262 },
263 });
264 },
265 })),
266 ])(TypographySettings);
267