-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
48 lines (42 loc) · 1.02 KB
/
index.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { removeFormat, slice, isCollapsed } from '@wordpress/rich-text';
import { RichTextToolbarButton } from '@wordpress/block-editor';
import { help } from '@wordpress/icons';
const name = 'core/unknown';
const title = __( 'Clear Unknown Formatting' );
function selectionContainsUnknownFormats( value ) {
if ( isCollapsed( value ) ) {
return false;
}
const selectedValue = slice( value );
return selectedValue.formats.some( ( formats ) => {
return formats.some( ( format ) => format.type === name );
} );
}
export const unknown = {
name,
title,
tagName: '*',
className: null,
edit( { isActive, value, onChange, onFocus } ) {
if ( ! isActive && ! selectionContainsUnknownFormats( value ) ) {
return null;
}
function onClick() {
onChange( removeFormat( value, name ) );
onFocus();
}
return (
<RichTextToolbarButton
name="unknown"
icon={ help }
title={ title }
onClick={ onClick }
isActive
/>
);
},
};