-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
156 lines (141 loc) · 3.68 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
useBlockProps,
useInnerBlocksProps,
__experimentalRecursionProvider as RecursionProvider,
__experimentalUseHasRecursion as useHasRecursion,
Warning,
} from '@wordpress/block-editor';
import {
useEntityProp,
useEntityBlockEditor,
store as coreStore,
} from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';
function ReadOnlyContent( { userCanEdit, postType, postId } ) {
const [ , , content ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const blockProps = useBlockProps();
return content?.protected && ! userCanEdit ? (
<div { ...blockProps }>
<Warning>{ __( 'This content is password protected.' ) }</Warning>
</div>
) : (
<div
{ ...blockProps }
dangerouslySetInnerHTML={ { __html: content?.rendered } }
></div>
);
}
function EditableContent( { context = {} } ) {
const { postType, postId } = context;
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
postType,
{ id: postId }
);
const entityRecord = useSelect(
( select ) => {
return select( coreStore ).getEntityRecord(
'postType',
postType,
postId
);
},
[ postType, postId ]
);
const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length;
const initialInnerBlocks = [ [ 'core/paragraph' ] ];
const props = useInnerBlocksProps(
useBlockProps( { className: 'entry-content' } ),
{
value: blocks,
onInput,
onChange,
template: ! hasInnerBlocks ? initialInnerBlocks : undefined,
}
);
return <div { ...props } />;
}
function Content( props ) {
const { context: { queryId, postType, postId } = {} } = 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
userCanEdit={ userCanEdit }
postType={ postType }
postId={ postId }
/>
);
}
function Placeholder( { layoutClassNames } ) {
const blockProps = useBlockProps( { className: layoutClassNames } );
return (
<div { ...blockProps }>
<p>
{ __(
'This is the Post Content block, it will display all the blocks in any single post or page.'
) }
</p>
<p>
{ __(
'That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.'
) }
</p>
<p>
{ __(
'If there are any Custom Post Types registered at your site, the Post Content block can display the contents of those entries as well.'
) }
</p>
</div>
);
}
function RecursionError() {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
);
}
export default function PostContentEdit( {
context,
attributes,
__unstableLayoutClassNames: layoutClassNames,
} ) {
const { postId: contextPostId, postType: contextPostType } = context;
const { layout = {} } = attributes;
const hasAlreadyRendered = useHasRecursion( contextPostId );
if ( contextPostId && contextPostType && hasAlreadyRendered ) {
return <RecursionError />;
}
return (
<RecursionProvider uniqueId={ contextPostId }>
{ contextPostId && contextPostType ? (
<Content context={ context } layout={ layout } />
) : (
<Placeholder layoutClassNames={ layoutClassNames } />
) }
</RecursionProvider>
);
}