-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathedit.js
134 lines (123 loc) · 3.09 KB
/
edit.js
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* Internal dependencies
*/
import applyWithColors from './colors';
import Inspector from './inspector';
import Controls from './controls';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { RichText, withFontSizes } from '@wordpress/block-editor';
import { withSelect } from '@wordpress/data';
/**
* Block constants
*/
const applyWithSelect = withSelect( ( select ) => {
const { getPermalink } = select( 'core/editor' );
return {
postLink: getPermalink(),
};
} );
/**
* Block edit function
*/
class Edit extends Component {
UNSAFE_componentWillReceiveProps( { postLink } ) { // eslint-disable-line camelcase
if ( postLink ) {
this.props.setAttributes( {
url: postLink,
} );
}
}
render() {
const {
attributes,
buttonColor,
className,
isSelected,
setAttributes,
textColor,
fontSize,
onReplace,
} = this.props;
const {
buttonText,
content,
textAlign,
} = attributes;
const blockquoteClasses = classnames( className, { [ `has-text-align-${ textAlign }` ]: textAlign } );
return (
<Fragment>
{ isSelected && (
<Controls
{ ...this.props }
/>
) }
{ isSelected && (
<Inspector
{ ...this.props }
/>
) }
<blockquote className={ blockquoteClasses }>
<RichText
tagName="p"
multiline="false"
/* translators: the text of the click to tweet element */
placeholder={ __( 'Add a quote to tweet…', 'coblocks' ) }
value={ content }
formattingControls={ [] } // disable controls
className={ classnames(
'wp-block-coblocks-click-to-tweet__text', {
'has-text-color': textColor.color,
[ textColor.class ]: textColor.class,
[ fontSize.class ]: fontSize.class,
}
) }
style={ {
color: textColor.color,
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
} }
onChange={ ( nextContent ) => setAttributes( { content: nextContent } ) }
keepPlaceholderOnFocus
onRemove={ ( forward ) => {
const hasEmptyTweet = content.length === 0 || content.length === 1;
if ( ! forward && hasEmptyTweet ) {
onReplace( [] );
}
} }
/>
<RichText
tagName="span"
multiline="false"
placeholder={ __( 'Add button…', 'coblocks' ) }
value={ buttonText }
formattingControls={ [] } // disable controls
className={ classnames(
'wp-block-coblocks-click-to-tweet__twitter-btn', {
'has-button-color': buttonColor.color,
[ buttonColor.class ]: buttonColor.class,
}
) }
style={ {
backgroundColor: buttonColor.color,
} }
onChange={ ( nextButtonText ) => setAttributes( { buttonText: nextButtonText } ) }
keepPlaceholderOnFocus
/>
</blockquote>
</Fragment>
);
}
}
export default compose( [
applyWithSelect,
applyWithColors,
withFontSizes( 'fontSize' ),
] )( Edit );