-
Notifications
You must be signed in to change notification settings - Fork 4k
/
parent.js
179 lines (158 loc) · 4.44 KB
/
parent.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
/**
* External dependencies
*/
import {
get,
unescape as unescapeString,
debounce,
repeat,
find,
flatten,
deburr,
} from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { ComboboxControl } from '@wordpress/components';
import { useState, useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { buildTermsTree } from '../../utils/terms';
import { store as editorStore } from '../../store';
function getTitle( post ) {
return post?.title?.rendered
? decodeEntities( post.title.rendered )
: `#${ post.id } (${ __( 'no title' ) })`;
}
export const getItemPriority = ( name, searchValue ) => {
const normalizedName = deburr( name ).toLowerCase();
const normalizedSearch = deburr( searchValue ).toLowerCase();
if ( normalizedName === normalizedSearch ) {
return 0;
}
if ( normalizedName.startsWith( normalizedSearch ) ) {
return normalizedName.length;
}
return Infinity;
};
export function PageAttributesParent() {
const { editPost } = useDispatch( editorStore );
const [ fieldValue, setFieldValue ] = useState( false );
const { parentPost, parentPostId, items, postType } = useSelect(
( select ) => {
const { getPostType, getEntityRecords, getEntityRecord } = select(
coreStore
);
const { getCurrentPostId, getEditedPostAttribute } = select(
editorStore
);
const postTypeSlug = getEditedPostAttribute( 'type' );
const pageId = getEditedPostAttribute( 'parent' );
const pType = getPostType( postTypeSlug );
const postId = getCurrentPostId();
const isHierarchical = get( pType, [ 'hierarchical' ], false );
const query = {
per_page: 100,
exclude: postId,
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
_fields: 'id,title,parent',
};
// Perform a search when the field is changed.
if ( !! fieldValue ) {
query.search = fieldValue;
}
return {
parentPostId: pageId,
parentPost: pageId
? getEntityRecord( 'postType', postTypeSlug, pageId )
: null,
items: isHierarchical
? getEntityRecords( 'postType', postTypeSlug, query )
: [],
postType: pType,
};
},
[ fieldValue ]
);
const isHierarchical = get( postType, [ 'hierarchical' ], false );
const parentPageLabel = get( postType, [ 'labels', 'parent_item_colon' ] );
const pageItems = items || [];
const parentOptions = useMemo( () => {
const getOptionsFromTree = ( tree, level = 0 ) => {
const mappedNodes = tree.map( ( treeNode ) => [
{
value: treeNode.id,
label:
repeat( '— ', level ) + unescapeString( treeNode.name ),
rawName: treeNode.name,
},
...getOptionsFromTree( treeNode.children || [], level + 1 ),
] );
const sortedNodes = mappedNodes.sort( ( [ a ], [ b ] ) => {
const priorityA = getItemPriority( a.rawName, fieldValue );
const priorityB = getItemPriority( b.rawName, fieldValue );
return priorityA >= priorityB ? 1 : -1;
} );
return flatten( sortedNodes );
};
let tree = pageItems.map( ( item ) => ( {
id: item.id,
parent: item.parent,
name: getTitle( item ),
} ) );
// Only build a hierarchical tree when not searching.
if ( ! fieldValue ) {
tree = buildTermsTree( tree );
}
const opts = getOptionsFromTree( tree );
// Ensure the current parent is in the options list.
const optsHasParent = find(
opts,
( item ) => item.value === parentPostId
);
if ( parentPost && ! optsHasParent ) {
opts.unshift( {
value: parentPostId,
label: getTitle( parentPost ),
} );
}
return opts;
}, [ pageItems, fieldValue ] );
if ( ! isHierarchical || ! parentPageLabel ) {
return null;
}
/**
* Handle user input.
*
* @param {string} inputValue The current value of the input field.
*/
const handleKeydown = ( inputValue ) => {
setFieldValue( inputValue );
};
/**
* Handle author selection.
*
* @param {Object} selectedPostId The selected Author.
*/
const handleChange = ( selectedPostId ) => {
editPost( { parent: selectedPostId } );
};
return (
<ComboboxControl
className="editor-page-attributes__parent"
label={ parentPageLabel }
value={ parentPostId }
options={ parentOptions }
onFilterValueChange={ debounce( handleKeydown, 300 ) }
onChange={ handleChange }
/>
);
}
export default PageAttributesParent;