-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (155 loc) · 4.17 KB
/
index.js
File metadata and controls
179 lines (155 loc) · 4.17 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Internal dependencies
*/
import { Edit } from './edit'
import { addAttributes } from './attributes'
import { addStyles } from './style'
export { getTypographyClasses } from './get-typography-classes'
/**
* External dependencies
*/
import {
useBlockAttributesContext, useBlockSetAttributesContext, useFontLoader,
} from '~stackable/hooks'
import {
getAttributeName, getAttrName, getAttrNameFunction,
} from '~stackable/util'
import { useDynamicContent } from '~stackable/components/dynamic-content-control'
/**
* WordPress dependencies
*/
import { Spinner } from '@wordpress/components'
import { RichText } from '@wordpress/block-editor'
import {
useEffect,
useState,
forwardRef,
memo,
} from '@wordpress/element'
export { deprecateTypographyGradientColor, deprecateTypographyShadowColor } from './deprecated'
export const Typography = memo( forwardRef( ( props, ref ) => {
const {
className,
attrNameTemplate,
tagName,
defaultTag,
value: _value,
onChange: _onChange,
children,
editable,
identifier,
defaultValue,
withoutInteractiveFormatting = false,
allowedFormats = null,
enableDebounce = true, // If false, onChange will be called immediately.
...propsToPass
} = props
const getAttrName = getAttrNameFunction( attrNameTemplate )
const setAttributes = useBlockSetAttributesContext()
const {
textTag,
text,
fontFamily,
} = useBlockAttributesContext( attributes => {
const getAttrName = getAttrNameFunction( attrNameTemplate )
return {
textTag: attributes[ getAttrName( 'textTag' ) ],
text: attributes[ getAttrName( 'text' ) ],
fontFamily: attributes[ getAttrName( 'fontFamily' ) ],
}
} )
const TagName = ( tagName === null ? textTag : tagName ) || defaultTag || 'p'
const value = _value === null ? text : _value
const onChange = _onChange === null ? value => setAttributes( { [ getAttrName( 'text' ) ]: value } ) : _onChange
const [ debouncedText, setDebouncedText ] = useState( value )
// Load any Google Fonts used.
useFontLoader( fontFamily )
// If value was changed externally.
useEffect( () => {
if ( value !== debouncedText ) {
setDebouncedText( value )
}
}, [ value ] )
useEffect( () => {
let timeout
if ( value !== debouncedText && enableDebounce ) {
timeout = setTimeout( () => {
onChange( debouncedText || defaultValue )
}, 300 )
}
return () => clearTimeout( timeout )
}, [ debouncedText, onChange ] ) // Don't include `value` in the dependency list because it will cause a double triggering of the `onChange`.
const dynamicContentText = useDynamicContent( debouncedText )
if ( dynamicContentText.includes( 'data-stk-dynamic=' ) && dynamicContentText.includes( '></span>' ) ) {
return <Spinner />
}
if ( ! editable ) {
return <TagName className={ className }>{ dynamicContentText }</TagName>
}
return (
<RichText
identifier={ identifier }
className={ className }
tagName={ TagName }
value={ dynamicContentText }
onChange={ value => {
if ( enableDebounce ) {
setDebouncedText( value )
} else {
onChange( value )
}
} }
ref={ ref }
withoutInteractiveFormatting={ withoutInteractiveFormatting }
allowedFormats={ allowedFormats }
{ ...propsToPass }
>
{ children }
</RichText>
)
} ) )
Typography.defaultProps = {
attrNameTemplate: '%s',
tagName: null,
defaultTag: 'p',
value: null,
onChange: null,
editable: true,
identifier: 'text',
}
Typography.Content = props => {
const {
className,
attrNameTemplate,
attributes,
tagName,
defaultTag,
value,
children,
...rest
} = props
const getAttribute = _attrName => {
const attrName = getAttrName( attrNameTemplate, _attrName )
return attributes[ getAttributeName( attrName ) ]
}
return (
<RichText.Content
className={ className }
tagName={ ( tagName === null ? getAttribute( 'textTag' ) : tagName ) || defaultTag }
value={ value === null ? getAttribute( 'text' ) : value }
{ ...rest }
>
{ children }
</RichText.Content>
)
}
Typography.Content.defaultProps = {
attrNameTemplate: '%s',
attributes: {},
tagName: null,
defaultTag: 'p',
value: null,
}
Typography.InspectorControls = Edit
Typography.addAttributes = addAttributes
Typography.addStyles = addStyles