-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit.js
129 lines (117 loc) · 3.12 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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useEffect, Platform } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import {
AlignmentControl,
BlockControls,
RichText,
useBlockProps,
store as blockEditorStore,
HeadingLevelDropdown,
useBlockEditingMode,
} from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { generateAnchor, setAnchor } from './autogenerate-anchors';
function HeadingEdit( {
attributes,
setAttributes,
mergeBlocks,
onReplace,
style,
clientId,
} ) {
const { textAlign, content, level, levelOptions, placeholder, anchor } =
attributes;
const tagName = 'h' + level;
const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
style,
} );
const blockEditingMode = useBlockEditingMode();
const { canGenerateAnchors } = useSelect( ( select ) => {
const { getGlobalBlockCount, getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
canGenerateAnchors:
!! settings.generateAnchors ||
getGlobalBlockCount( 'core/table-of-contents' ) > 0,
};
}, [] );
const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
// Initially set anchor for headings that have content but no anchor set.
// This is used when transforming a block to heading, or for legacy anchors.
useEffect( () => {
if ( ! canGenerateAnchors ) {
return;
}
if ( ! anchor && content ) {
// This side-effect should not create an undo level.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( {
anchor: generateAnchor( clientId, content ),
} );
}
setAnchor( clientId, anchor );
// Remove anchor map when block unmounts.
return () => setAnchor( clientId, null );
}, [ anchor, content, clientId, canGenerateAnchors ] );
const onContentChange = ( value ) => {
const newAttrs = { content: value };
if (
canGenerateAnchors &&
( ! anchor ||
! value ||
generateAnchor( clientId, content ) === anchor )
) {
newAttrs.anchor = generateAnchor( clientId, value );
}
setAttributes( newAttrs );
};
return (
<>
{ blockEditingMode === 'default' && (
<BlockControls group="block">
<HeadingLevelDropdown
value={ level }
options={ levelOptions }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
) }
<RichText
identifier="content"
tagName={ tagName }
value={ content }
onChange={ onContentChange }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ () => onReplace( [] ) }
placeholder={ placeholder || __( 'Heading' ) }
textAlign={ textAlign }
{ ...( Platform.isNative && { deleteEnter: true } ) } // setup RichText on native mobile to delete the "Enter" key as it's handled by the JS/RN side
{ ...blockProps }
/>
</>
);
}
export default HeadingEdit;