-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
154 lines (146 loc) · 3.22 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
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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { DataForm } from '@wordpress/dataviews';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import { __experimentalVStack as VStack } from '@wordpress/components';
import { useState, useMemo, useEffect } from '@wordpress/element';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
/**
* Internal dependencies
*/
import Page from '../page';
import PostEditHeader from '../post-edit/header';
import { unlock } from '../../lock-unlock';
const { usePostFields } = unlock( editorPrivateApis );
const fieldsWithBulkEditSupport = [
'title',
'status',
'date',
'author',
'comment_status',
];
function PostEditForm( { postType, postId } ) {
const ids = useMemo( () => postId.split( ',' ), [ postId ] );
const { record } = useSelect(
( select ) => {
return {
record:
ids.length === 1
? select( coreDataStore ).getEditedEntityRecord(
'postType',
postType,
ids[ 0 ]
)
: null,
};
},
[ postType, ids ]
);
const [ multiEdits, setMultiEdits ] = useState( {} );
const { editEntityRecord } = useDispatch( coreDataStore );
const { fields: _fields } = usePostFields( { postType } );
const fields = useMemo(
() =>
_fields?.map( ( field ) => {
if ( field.id === 'status' ) {
return {
...field,
elements: field.elements.filter(
( element ) => element.value !== 'trash'
),
};
}
return field;
} ),
[ _fields ]
);
const form = useMemo(
() => ( {
type: 'panel',
fields: [
{
id: 'featured_media',
layout: 'regular',
},
'title',
{
id: 'status',
label: __( 'Status & Visibility' ),
children: [ 'status', 'password' ],
},
'author',
'date',
'slug',
'parent',
'comment_status',
].filter(
( field ) =>
ids.length === 1 ||
fieldsWithBulkEditSupport.includes( field )
),
} ),
[ ids ]
);
const onChange = ( edits ) => {
for ( const id of ids ) {
if (
edits.status &&
edits.status !== 'future' &&
record?.status === 'future' &&
new Date( record.date ) > new Date()
) {
edits.date = null;
}
if (
edits.status &&
edits.status === 'private' &&
record.password
) {
edits.password = '';
}
editEntityRecord( 'postType', postType, id, edits );
if ( ids.length > 1 ) {
setMultiEdits( ( prev ) => ( {
...prev,
...edits,
} ) );
}
}
};
useEffect( () => {
setMultiEdits( {} );
}, [ ids ] );
return (
<VStack spacing={ 4 }>
<PostEditHeader postType={ postType } postId={ postId } />
<DataForm
data={ ids.length === 1 ? record : multiEdits }
fields={ fields }
form={ form }
onChange={ onChange }
/>
</VStack>
);
}
export function PostEdit( { postType, postId } ) {
return (
<Page
className={ clsx( 'edit-site-post-edit', {
'is-empty': ! postId,
} ) }
label={ __( 'Post Edit' ) }
>
{ postId && (
<PostEditForm postType={ postType } postId={ postId } />
) }
{ ! postId && <p>{ __( 'Select a page to edit' ) }</p> }
</Page>
);
}