-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathedit.js
137 lines (124 loc) · 3.6 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
135
136
137
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* Internal dependencies
*/
import applyWithColors from './colors';
import Controls from './controls';
import Inspector from './inspector';
import { computeFontSize } from '../../utils/helper';
/**
* 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 { createBlock } from '@wordpress/blocks';
/**
* Block edit function
*/
export class Edit extends Component {
constructor() {
super( ...arguments );
this.splitBlock = this.splitBlock.bind( this );
}
/**
* Split handler for RichText value, namely when content is pasted or the
* user presses the Enter key.
*
* @param {?Array} before Optional before value, to be used as content
* in place of what exists currently for the
* block. If undefined, the block is deleted.
* @param {?Array} after Optional after value, to be appended in a new
* paragraph block to the set of blocks passed
* as spread.
* @param {...WPBlock} blocks Optional blocks inserted between the before
* and after value blocks.
*/
splitBlock( before, after, ...blocks ) {
const {
attributes,
insertBlocksAfter,
setAttributes,
onReplace,
} = this.props;
if ( after ) {
// Append "After" content as a new paragraph block to the end of
// any other blocks being inserted after the current paragraph.
blocks.push( createBlock( 'core/paragraph', { content: after } ) );
}
if ( blocks.length && insertBlocksAfter ) {
insertBlocksAfter( blocks );
}
const { content } = attributes;
if ( ! before ) {
// If before content is omitted, treat as intent to delete block.
onReplace( [] );
} else if ( content !== before ) {
// Only update content if it has in-fact changed. In case that user
// has created a new paragraph at end of an existing one, the value
// of before will be strictly equal to the current content.
setAttributes( { content: before } );
}
}
render() {
const {
attributes,
backgroundColor,
className,
mergeBlocks,
onReplace,
setAttributes,
textColor,
fontSize,
} = this.props;
const {
content,
align,
} = attributes;
const classes = classnames( 'wp-block-coblocks-highlight__content',
backgroundColor && {
'has-background': backgroundColor.color,
[ backgroundColor.class ]: backgroundColor.class,
},
textColor && {
'has-text-color': textColor.color,
[ textColor.class ]: textColor.class,
},
fontSize?.class && {
[ fontSize?.class ]: fontSize?.class,
}
);
return (
<Fragment>
<Controls { ...this.props } />
<Inspector { ...this.props } />
<p className={ className } style={ { textAlign: align } }>
<RichText
tagName="mark"
placeholder={ __( 'Add highlighted text…', 'coblocks' ) }
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
onSplit={ this.splitBlock }
onRemove={ () => onReplace( [] ) }
className={ classes }
style={ {
backgroundColor: backgroundColor?.color,
color: textColor?.color,
fontSize: computeFontSize( fontSize ) ?? undefined,
} }
keepPlaceholderOnFocus
/>
</p>
</Fragment>
);
}
}
export default compose( [
applyWithColors,
withFontSizes( 'fontSize' ),
] )( Edit );