-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
213 lines (193 loc) · 4.85 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
useBlockProps,
useInnerBlocksProps,
__experimentalRecursionProvider as RecursionProvider,
__experimentalUseHasRecursion as useHasRecursion,
Warning,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { Placeholder, Button } from '@wordpress/components';
import { postContent as icon } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';
function ReadOnlyContent( {
layoutClassNames,
userCanEdit,
postType,
postId,
} ) {
const [ , , content ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const blockProps = useBlockProps( { className: layoutClassNames } );
return content?.protected && ! userCanEdit ? (
<div { ...blockProps }>
<Warning>{ __( 'This content is password protected.' ) }</Warning>
</div>
) : (
<div
{ ...blockProps }
dangerouslySetInnerHTML={ { __html: content?.rendered } }
></div>
);
}
function EmptyContentPlaceholder( { context, onClose, openInserter } ) {
const { postType } = context;
const label =
'page' === postType
? __( 'This page’s content is empty' )
: __( 'This post’s content is empty' );
return (
<Placeholder
icon={ icon }
label={ label }
instructions={ __(
'Add your first block or pattern to get started.'
) }
>
<Button variant="primary" onClick={ openInserter }>
{ __( 'Choose a pattern' ) }
</Button>
<Button variant="secondary" onClick={ onClose }>
{ __( 'Start blank' ) }
</Button>
</Placeholder>
);
}
function PostContentPlaceholder( { layoutClassNames } ) {
const blockProps = useBlockProps( { className: layoutClassNames } );
return (
<div { ...blockProps }>
<Placeholder
className="wp-block-post-content__content-placeholder"
withIllustration
>
<p>
{ __( 'This block will be replaced with your content.' ) }
</p>
</Placeholder>
</div>
);
}
function EditableContent( { context = {}, clientId } ) {
const { postType, postId } = context;
const { selectBlock, insertBlock } = useDispatch( blockEditorStore );
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
postType,
{ id: postId }
);
const setInserterIsOpened = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__experimentalSetIsInserterOpened,
[ postType, postId ]
);
const hasInnerBlocks = blocks?.length;
const { children, ...props } = useInnerBlocksProps(
useBlockProps( {
className: classnames( 'entry-content', {
'wp-block-post-content__placeholder': ! hasInnerBlocks,
} ),
} ),
{
value: blocks,
onInput,
onChange,
}
);
const onClose = () => {
const initialBlock = createBlock( 'core/paragraph' );
insertBlock( initialBlock, 0, clientId );
selectBlock( initialBlock.clientId );
};
const openInserter = () => {
setInserterIsOpened( {
initialTab: 'patterns',
rootClientId: clientId,
insertionIndex: 0,
} );
};
return (
<div { ...props }>
{ children }
{ ! hasInnerBlocks && (
<EmptyContentPlaceholder
context={ context }
onClose={ onClose }
openInserter={ openInserter }
/>
) }
</div>
);
}
function Content( props ) {
const { context: { queryId, postType, postId } = {}, layoutClassNames } =
props;
const userCanEdit = useCanEditEntity( 'postType', postType, postId );
if ( userCanEdit === undefined ) {
return null;
}
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const isEditable = userCanEdit && ! isDescendentOfQueryLoop;
return isEditable ? (
<EditableContent { ...props } />
) : (
<ReadOnlyContent
layoutClassNames={ layoutClassNames }
userCanEdit={ userCanEdit }
postType={ postType }
postId={ postId }
/>
);
}
function RecursionError() {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
);
}
export default function PostContentEdit( {
clientId,
context,
__unstableLayoutClassNames: layoutClassNames,
} ) {
const { postId: contextPostId, postType: contextPostType } = context;
const hasAlreadyRendered = useHasRecursion( contextPostId );
if ( contextPostId && contextPostType && hasAlreadyRendered ) {
return <RecursionError />;
}
return (
<RecursionProvider uniqueId={ contextPostId }>
{ contextPostId && contextPostType ? (
<Content
clientId={ clientId }
context={ context }
layoutClassNames={ layoutClassNames }
/>
) : (
<PostContentPlaceholder layoutClassNames={ layoutClassNames } />
) }
</RecursionProvider>
);
}