-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTypographyControl.js
More file actions
141 lines (128 loc) · 3.54 KB
/
Copy pathTypographyControl.js
File metadata and controls
141 lines (128 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* global tiobDash */
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import classnames from 'classnames';
import SVG from '../../utils/svg';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
const FALLBACK_FONT = 'Arial, Helvetica, sans-serif';
const getFontPairs = ( importData ) => {
if ( importData && importData.font_pairs && Object.keys( importData.font_pairs ).length ) {
return importData.font_pairs;
}
if ( tiobDash && tiobDash.fontParings ) {
return tiobDash.fontParings;
}
return null;
};
const getDefaultFonts = ( importDataDefault ) => {
const themeMods = importDataDefault?.theme_mods;
return {
body: themeMods?.neve_body_font_family || FALLBACK_FONT,
heading: themeMods?.neve_headings_font_family || FALLBACK_FONT,
};
};
const TypographyControl = ( {
siteStyle,
handleFontClick,
importData,
importDataDefault,
} ) => {
const fontPairs = getFontPairs( importData );
if ( ! fontPairs ) {
return null;
}
const { body: defaultBodyFont, heading: defaultHeadingsFont } =
getDefaultFonts( importDataDefault );
const { font } = siteStyle;
return (
<div className="ob-ctrl">
<div className="ob-ctrl-head small-gap">
<h3>{ __( 'Typography', 'templates-patterns-collection' ) }</h3>
<Button
icon={ SVG.redo }
onClick={ () => handleFontClick( 'default' ) }
/>
</div>
<div className="ob-ctrl-wrap font">
<Button
className={ classnames( [
'ob-font-pair',
'ob-font-default',
{ 'ob-active': 'default' === font },
] ) }
title={ `${ defaultHeadingsFont } / ${ defaultBodyFont }` }
onClick={ () => handleFontClick( 'default' ) }
>
{ __( 'Default', 'templates-patterns-collection' ) }
</Button>
{ Object.keys( fontPairs ).map( ( slug ) => {
const { headingFont, bodyFont } = fontPairs[ slug ];
const headingStyle = {
fontFamily: headingFont.font,
};
return (
<Button
key={ slug }
className={ classnames( [
'ob-font-pair',
{ 'ob-active': slug === font },
] ) }
style={ headingStyle }
title={ `${ headingFont.font } / ${ bodyFont.font }` }
onClick={ () => handleFontClick( slug ) }
>
Abc
</Button>
);
} ) }
</div>
</div>
);
};
export default compose(
withSelect( ( select ) => {
const { getImportData } = select( 'ti-onboarding' );
return {
importData: getImportData(),
};
} ),
withDispatch(
(
dispatch,
{ importData, importDataDefault, siteStyle, setSiteStyle }
) => {
const { setImportData, setRefresh } = dispatch( 'ti-onboarding' );
const fontPairs = getFontPairs( importData );
const { body: defaultBodyFont, heading: defaultHeadingsFont } =
getDefaultFonts( importDataDefault );
return {
handleFontClick: ( fontKey ) => {
const newStyle = {
...siteStyle,
font: fontKey,
};
setSiteStyle( newStyle );
const pair =
fontKey !== 'default' && fontPairs && fontPairs[ fontKey ]
? fontPairs[ fontKey ]
: {
bodyFont: { font: defaultBodyFont },
headingFont: { font: defaultHeadingsFont },
};
const { bodyFont, headingFont } = pair;
const newImportData = {
...importData,
theme_mods: {
...importData.theme_mods,
neve_body_font_family: bodyFont.font,
neve_headings_font_family: headingFont.font,
},
};
setImportData( newImportData );
setRefresh( true );
},
};
}
)
)( TypographyControl );