-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
171 lines (163 loc) · 4.27 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
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
InspectorControls,
useBlockProps,
PlainText,
HeadingLevelDropdown,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { ToggleControl, TextControl, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { useEntityProp } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';
import { unlock } from '../lock-unlock';
const { useBlockEditingMode } = unlock( blockEditorPrivateApis );
export default function PostTitleEdit( {
attributes: { level, textAlign, isLink, rel, linkTarget },
setAttributes,
context: { postType, postId, queryId },
insertBlocksAfter,
} ) {
const TagName = 'h' + level;
const isDescendentOfQueryLoop = Number.isFinite( queryId );
/**
* Hack: useCanEditEntity may trigger an OPTIONS request to the REST API via the canUser resolver.
* However, when the Post Title is a descendant of a Query Loop block, the title cannot be edited.
* In order to avoid these unnecessary requests, we call the hook without
* the proper data, resulting in returning early without making them.
*/
const userCanEdit = useCanEditEntity(
'postType',
! isDescendentOfQueryLoop && postType,
postId
);
const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp(
'postType',
postType,
'title',
postId
);
const [ link ] = useEntityProp( 'postType', postType, 'link', postId );
const onSplitAtEnd = () => {
insertBlocksAfter( createBlock( getDefaultBlockName() ) );
};
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
const blockEditingMode = useBlockEditingMode();
let titleElement = <TagName { ...blockProps }>{ __( 'Title' ) }</TagName>;
if ( postType && postId ) {
titleElement = userCanEdit ? (
<PlainText
tagName={ TagName }
placeholder={ __( 'No Title' ) }
value={ rawTitle }
onChange={ setTitle }
__experimentalVersion={ 2 }
__unstableOnSplitAtEnd={ onSplitAtEnd }
{ ...blockProps }
/>
) : (
<TagName
{ ...blockProps }
dangerouslySetInnerHTML={ { __html: fullTitle?.rendered } }
/>
);
}
if ( isLink && postType && postId ) {
titleElement = userCanEdit ? (
<TagName { ...blockProps }>
<PlainText
tagName="a"
href={ link }
target={ linkTarget }
rel={ rel }
placeholder={ ! rawTitle.length ? __( 'No Title' ) : null }
value={ rawTitle }
onChange={ setTitle }
__experimentalVersion={ 2 }
__unstableOnSplitAtEnd={ onSplitAtEnd }
/>
</TagName>
) : (
<TagName { ...blockProps }>
<a
href={ link }
target={ linkTarget }
rel={ rel }
onClick={ ( event ) => event.preventDefault() }
dangerouslySetInnerHTML={ {
__html: fullTitle?.rendered,
} }
/>
</TagName>
);
}
return (
<>
{ blockEditingMode === 'default' && (
<BlockControls group="block">
<HeadingLevelDropdown
value={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
) }
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Make title a link' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
<TextControl
__nextHasNoMarginBottom
label={ __( 'Link rel' ) }
value={ rel }
onChange={ ( newRel ) =>
setAttributes( { rel: newRel } )
}
/>
</>
) }
</PanelBody>
</InspectorControls>
{ titleElement }
</>
);
}