PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / gutenberg / src / components / typography / fontloader.js

fontloader.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.0, at modules/gutenberg/src/components/typography/fontloader.js

101 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 if ( googlefonts === undefined ) {
2 var googlefonts = []; // eslint-disable-line no-var
3 }
4 import PropTypes from 'prop-types';
5 import WebFont from 'webfontloader';
6 import { useState, useEffect } from '@wordpress/element';
7 const statuses = {
8 inactive: 'inactive',
9 active: 'active',
10 loading: 'loading',
11 };
12 const noop = () => {};
13
14 const WebfontLoader = ( props ) => {
15 const [ value, setValue ] = useState( [] );
16
17 const status = undefined;
18
19 useEffect( () => {
20 loadFonts();
21 }, [] );
22
23 useEffect( () => {
24 const { onStatus, config } = props;
25
26 if ( status !== value.status ) {
27 onStatus( value.status );
28 }
29 if ( config !== value.config ) {
30 loadFonts();
31 }
32 }, [ props ] );
33
34 const handleLoading = () => {
35 setValue( { status: statuses.loading } );
36 };
37
38 const addFont = ( font ) => {
39 if ( ! googlefonts.includes( font ) ) {
40 googlefonts.push( font );
41 }
42 };
43
44 const handleActive = () => {
45 setValue( { status: statuses.active } );
46 };
47
48 const handleInactive = () => {
49 setValue( { status: statuses.inactive } );
50 };
51
52 const loadFonts = () => {
53 if ( ! googlefonts.includes( props.config.google.families[ 0 ] ) ) {
54 WebFont.load( {
55 ...props.config,
56 loading: handleLoading,
57 active: handleActive,
58 inactive: handleInactive,
59 } );
60 addFont( props.config.google.families[ 0 ] );
61 }
62
63 const tabletPreview = document.getElementsByClassName( 'is-tablet-preview' );
64 const mobilePreview = document.getElementsByClassName( 'is-mobile-preview' );
65
66 if ( 0 !== tabletPreview.length || 0 !== mobilePreview.length ) {
67
68 const preview = tabletPreview[0] || mobilePreview[0];
69
70 const iframe = preview.getElementsByTagName( 'iframe' )[0];
71
72 if ( iframe ) {
73
74 WebFont.load( {
75 ...props.config,
76 loading: handleLoading,
77 active: handleActive,
78 inactive: handleInactive,
79 context: iframe?.contentWindow
80 } );
81 addFont( props.config.google.families[ 0 ] );
82 }
83 }
84 };
85
86 const { children } = props;
87 return children || null;
88 };
89
90 WebfontLoader.propTypes = {
91 config: PropTypes.object?.isRequired,
92 children: PropTypes.element,
93 onStatus: PropTypes.func?.isRequired,
94 };
95
96 WebfontLoader.defaultProps = {
97 onStatus: noop,
98 };
99
100 export default WebfontLoader;
101