@elementor/editor-site-navigation 4.5.0-1204 → 4.5.0-1206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +784 -425
- package/dist/index.mjs +776 -426
- package/package.json +10 -9
- package/src/api/post.ts +22 -1
- package/src/components/panel/actions-menu/actions/delete.tsx +12 -6
- package/src/components/panel/actions-menu/actions/duplicate.tsx +4 -2
- package/src/components/panel/content-panel-add-icon-button.tsx +15 -0
- package/src/components/panel/content-panel-chrome.ts +65 -0
- package/src/components/panel/content-panel-header-add.tsx +57 -0
- package/src/components/panel/content-panel-search.tsx +33 -0
- package/src/components/panel/posts-list/collapsible-list.tsx +87 -65
- package/src/components/panel/posts-list/list-items/edit-mode-template.tsx +83 -20
- package/src/components/panel/posts-list/list-items/list-item-create.tsx +9 -2
- package/src/components/panel/posts-list/list-items/list-item-view.tsx +28 -8
- package/src/components/panel/posts-list/posts-collapsible-list.tsx +99 -65
- package/src/components/panel/section-add-button.tsx +35 -0
- package/src/components/panel/shell.tsx +41 -11
- package/src/components/shared/page-title-and-status.tsx +3 -0
- package/src/contexts/content-panel-coordinator.tsx +70 -0
- package/src/hooks/use-posts-actions.ts +6 -6
- package/src/hooks/use-posts.ts +18 -5
- package/src/hooks/use-reverse-html-entities.ts +3 -10
- package/src/hooks/use-toggle-button-props.ts +1 -1
- package/src/utils/post-title-plain.ts +36 -0
- package/src/components/panel/add-new-button.tsx +0 -28
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from '@elementor/ui';
|
|
20
20
|
import { __ } from '@wordpress/i18n';
|
|
21
21
|
|
|
22
|
+
import { usePostListContext } from '../../../../contexts/post-list-context';
|
|
22
23
|
import { type Post } from '../../../../types';
|
|
23
24
|
import PageTitleAndStatus from '../../../shared/page-title-and-status';
|
|
24
25
|
import Delete from '../../actions-menu/actions/delete';
|
|
@@ -26,6 +27,7 @@ import Duplicate from '../../actions-menu/actions/duplicate';
|
|
|
26
27
|
import Rename from '../../actions-menu/actions/rename';
|
|
27
28
|
import SetHome from '../../actions-menu/actions/set-home';
|
|
28
29
|
import View from '../../actions-menu/actions/view';
|
|
30
|
+
import { contentPanelListItemButtonSx, contentPanelListRowPaddingSx } from '../../content-panel-chrome';
|
|
29
31
|
|
|
30
32
|
const DisabledPostTooltip = ( { children, isDisabled }: { children: React.ReactNode; isDisabled: boolean } ) => {
|
|
31
33
|
if ( isDisabled ) {
|
|
@@ -40,7 +42,7 @@ const DisabledPostTooltip = ( { children, isDisabled }: { children: React.ReactN
|
|
|
40
42
|
return (
|
|
41
43
|
<Tooltip title={ title } placement="bottom" arrow={ false }>
|
|
42
44
|
{ /* @see https://mui.com/material-ui/react-tooltip/#disabled-elements */ }
|
|
43
|
-
{ children }
|
|
45
|
+
<span>{ children }</span>
|
|
44
46
|
</Tooltip>
|
|
45
47
|
);
|
|
46
48
|
}
|
|
@@ -49,6 +51,7 @@ const DisabledPostTooltip = ( { children, isDisabled }: { children: React.ReactN
|
|
|
49
51
|
};
|
|
50
52
|
|
|
51
53
|
export default function ListItemView( { post }: { post: Post } ) {
|
|
54
|
+
const { type } = usePostListContext();
|
|
52
55
|
const activeDocument = useActiveDocument();
|
|
53
56
|
const navigateToDocument = useNavigateToDocument();
|
|
54
57
|
|
|
@@ -62,29 +65,42 @@ export default function ListItemView( { post }: { post: Post } ) {
|
|
|
62
65
|
const status = isActive ? activeDocument?.status.value : post.status;
|
|
63
66
|
const title = isActive ? activeDocument?.title : post.title.rendered;
|
|
64
67
|
const isDisabled = ! post.user_can.edit;
|
|
68
|
+
const menuTriggerProps = bindTrigger( popupState );
|
|
65
69
|
|
|
66
70
|
return (
|
|
67
71
|
<>
|
|
68
72
|
<DisabledPostTooltip isDisabled={ isDisabled }>
|
|
69
73
|
<ListItem
|
|
70
74
|
disablePadding
|
|
75
|
+
sx={ contentPanelListRowPaddingSx }
|
|
71
76
|
secondaryAction={
|
|
72
|
-
<IconButton
|
|
73
|
-
|
|
77
|
+
<IconButton
|
|
78
|
+
aria-label={ __( 'More actions', 'elementor' ) }
|
|
79
|
+
size="tiny"
|
|
80
|
+
{ ...menuTriggerProps }
|
|
81
|
+
onClick={ ( event: React.MouseEvent ) => {
|
|
82
|
+
event.stopPropagation();
|
|
83
|
+
menuTriggerProps.onClick?.( event );
|
|
84
|
+
} }
|
|
85
|
+
>
|
|
86
|
+
<DotsVerticalIcon fontSize="tiny" />
|
|
74
87
|
</IconButton>
|
|
75
88
|
}
|
|
76
89
|
>
|
|
77
90
|
<ListItemButton
|
|
78
|
-
|
|
91
|
+
dense
|
|
92
|
+
disableGutters
|
|
79
93
|
disabled={ isDisabled }
|
|
80
94
|
onClick={ () => {
|
|
81
95
|
if ( ! isActive ) {
|
|
82
96
|
navigateToDocument( post.id );
|
|
83
97
|
}
|
|
84
98
|
} }
|
|
85
|
-
|
|
99
|
+
selected={ isActive }
|
|
100
|
+
shape="rounded"
|
|
101
|
+
sx={ contentPanelListItemButtonSx }
|
|
86
102
|
>
|
|
87
|
-
<ListItemText disableTypography={
|
|
103
|
+
<ListItemText disableTypography sx={ { flex: 1, minWidth: 0 } }>
|
|
88
104
|
<PageTitleAndStatus title={ title } status={ status } />
|
|
89
105
|
</ListItemText>
|
|
90
106
|
{ post.isHome && <HomeIcon titleAccess={ __( 'Homepage', 'elementor' ) } color="disabled" /> }
|
|
@@ -100,8 +116,12 @@ export default function ListItemView( { post }: { post: Post } ) {
|
|
|
100
116
|
<Duplicate post={ post } popupState={ popupState } />
|
|
101
117
|
<Delete post={ post } />
|
|
102
118
|
<View post={ post } />
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
{ type === 'page' && (
|
|
120
|
+
<>
|
|
121
|
+
<Divider />
|
|
122
|
+
<SetHome post={ post } closeMenu={ () => popupState.close() } />
|
|
123
|
+
</>
|
|
124
|
+
) }
|
|
105
125
|
</Menu>
|
|
106
126
|
</>
|
|
107
127
|
);
|
|
@@ -1,42 +1,96 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { Box, Button, CircularProgress,
|
|
2
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { Box, Button, CircularProgress, Skeleton, Typography } from '@elementor/ui';
|
|
4
|
+
import { __ } from '@wordpress/i18n';
|
|
4
5
|
|
|
5
6
|
import { postTypesMap } from '../../../api/post';
|
|
7
|
+
import { useContentPanelCoordinator } from '../../../contexts/content-panel-coordinator';
|
|
6
8
|
import { usePostListContext } from '../../../contexts/post-list-context';
|
|
7
9
|
import { useHomepage } from '../../../hooks/use-homepage';
|
|
8
10
|
import { usePosts } from '../../../hooks/use-posts';
|
|
9
|
-
import
|
|
11
|
+
import SectionAddButton from '../section-add-button';
|
|
10
12
|
import CollapsibleList from './collapsible-list';
|
|
11
13
|
import ErrorState from './error-state';
|
|
12
14
|
import PostListItem from './post-list-item';
|
|
13
15
|
|
|
16
|
+
const SEARCH_DEBOUNCE_MS = 300;
|
|
17
|
+
|
|
14
18
|
type Props = {
|
|
15
19
|
isOpenByDefault?: boolean;
|
|
16
20
|
};
|
|
17
21
|
|
|
18
22
|
export default function PostsCollapsibleList( { isOpenByDefault = false }: Props ) {
|
|
19
|
-
const { type, editMode } = usePostListContext();
|
|
23
|
+
const { type, editMode, setEditMode } = usePostListContext();
|
|
24
|
+
const { registerSection, unregisterSection, searchQuery } = useContentPanelCoordinator();
|
|
25
|
+
const [ isOpen, setIsOpen ] = useState( isOpenByDefault );
|
|
26
|
+
const trimmedSearch = searchQuery.trim();
|
|
27
|
+
const [ debouncedSearch, setDebouncedSearch ] = useState( trimmedSearch );
|
|
28
|
+
|
|
29
|
+
useEffect( () => {
|
|
30
|
+
const handle = window.setTimeout( () => setDebouncedSearch( trimmedSearch ), SEARCH_DEBOUNCE_MS );
|
|
31
|
+
|
|
32
|
+
return () => window.clearTimeout( handle );
|
|
33
|
+
}, [ trimmedSearch ] );
|
|
34
|
+
|
|
20
35
|
const {
|
|
21
36
|
data: { posts, total },
|
|
22
|
-
isLoading: postsLoading,
|
|
23
37
|
isError: postsError,
|
|
38
|
+
isPending: postsPending,
|
|
24
39
|
fetchNextPage,
|
|
25
40
|
hasNextPage,
|
|
26
41
|
isFetchingNextPage,
|
|
27
|
-
} = usePosts( type );
|
|
42
|
+
} = usePosts( type, debouncedSearch );
|
|
28
43
|
const { data: homepageId } = useHomepage();
|
|
44
|
+
const isPageType = type === 'page';
|
|
45
|
+
|
|
46
|
+
useEffect( () => {
|
|
47
|
+
registerSection( type, {
|
|
48
|
+
setExpanded: setIsOpen,
|
|
49
|
+
startCreate: () => setEditMode( { mode: 'create', details: {} } ),
|
|
50
|
+
} );
|
|
51
|
+
|
|
52
|
+
return () => unregisterSection( type );
|
|
53
|
+
}, [ registerSection, unregisterSection, type, setEditMode ] );
|
|
54
|
+
|
|
55
|
+
const filteredPosts = useMemo( () => {
|
|
56
|
+
if ( ! posts ) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const mappedPosts = isPageType
|
|
61
|
+
? posts.map( ( post ) => ( post.id === homepageId ? { ...post, isHome: true } : post ) )
|
|
62
|
+
: posts;
|
|
63
|
+
|
|
64
|
+
const sorted = isPageType
|
|
65
|
+
? [ ...mappedPosts ].sort( ( a, b ) => {
|
|
66
|
+
if ( a.id === homepageId ) {
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if ( b.id === homepageId ) {
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return 0;
|
|
75
|
+
} )
|
|
76
|
+
: mappedPosts;
|
|
77
|
+
|
|
78
|
+
return sorted;
|
|
79
|
+
}, [ posts, isPageType, homepageId ] );
|
|
80
|
+
|
|
81
|
+
useEffect( () => {
|
|
82
|
+
if ( debouncedSearch && filteredPosts.length > 0 ) {
|
|
83
|
+
setIsOpen( true );
|
|
84
|
+
}
|
|
85
|
+
}, [ debouncedSearch, filteredPosts.length ] );
|
|
29
86
|
|
|
30
87
|
if ( postsError ) {
|
|
31
88
|
return <ErrorState />;
|
|
32
89
|
}
|
|
33
90
|
|
|
34
|
-
if ( ! posts
|
|
91
|
+
if ( postsPending && ! posts?.length ) {
|
|
35
92
|
return (
|
|
36
|
-
<Box sx={ { px:
|
|
37
|
-
<Box display="flex" justifyContent="flex-end" alignItems="center">
|
|
38
|
-
<Skeleton sx={ { my: 4 } } animation="wave" variant="rounded" width="110px" height="28px" />
|
|
39
|
-
</Box>
|
|
93
|
+
<Box sx={ { px: 2 } }>
|
|
40
94
|
<Box>
|
|
41
95
|
<Skeleton sx={ { my: 3 } } animation="wave" variant="rounded" width="100%" height="24px" />
|
|
42
96
|
<Skeleton sx={ { my: 3 } } animation="wave" variant="rounded" width="70%" height="24px" />
|
|
@@ -47,61 +101,41 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
47
101
|
);
|
|
48
102
|
}
|
|
49
103
|
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
if ( post.id === homepageId ) {
|
|
54
|
-
return { ...post, isHome: true };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return post;
|
|
58
|
-
} );
|
|
59
|
-
|
|
60
|
-
const sortedPosts = mappedPosts.sort( ( a, b ) => {
|
|
61
|
-
if ( a.id === homepageId ) {
|
|
62
|
-
return -1;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if ( b.id === homepageId ) {
|
|
66
|
-
return 1;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return 0;
|
|
70
|
-
} );
|
|
104
|
+
const displayCount = total;
|
|
105
|
+
const sectionTitle = postTypesMap[ type ].labels.plural_name;
|
|
106
|
+
const showNoResults = debouncedSearch && filteredPosts.length === 0;
|
|
71
107
|
|
|
72
108
|
return (
|
|
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
|
-
</List>
|
|
105
|
-
</>
|
|
109
|
+
<CollapsibleList
|
|
110
|
+
count={ displayCount }
|
|
111
|
+
isOpen={ isOpen }
|
|
112
|
+
onOpenChange={ setIsOpen }
|
|
113
|
+
title={ sectionTitle }
|
|
114
|
+
trailingActions={ <SectionAddButton onBeforeCreate={ () => setIsOpen( true ) } /> }
|
|
115
|
+
>
|
|
116
|
+
{ showNoResults && (
|
|
117
|
+
<Box sx={ { py: 2, px: 2 } }>
|
|
118
|
+
<Typography variant="body2" color="text.secondary">
|
|
119
|
+
{ __( 'No results found', 'elementor' ) }
|
|
120
|
+
</Typography>
|
|
121
|
+
</Box>
|
|
122
|
+
) }
|
|
123
|
+
{ filteredPosts.map( ( post ) => {
|
|
124
|
+
return <PostListItem key={ post.id } post={ post } />;
|
|
125
|
+
} ) }
|
|
126
|
+
{ [ 'duplicate', 'create' ].includes( editMode.mode ) && <PostListItem /> }
|
|
127
|
+
{ hasNextPage && (
|
|
128
|
+
<Box
|
|
129
|
+
sx={ {
|
|
130
|
+
display: 'flex',
|
|
131
|
+
justifyContent: 'center',
|
|
132
|
+
} }
|
|
133
|
+
>
|
|
134
|
+
<Button onClick={ fetchNextPage } color="secondary">
|
|
135
|
+
{ isFetchingNextPage ? <CircularProgress /> : __( 'Load more', 'elementor' ) }
|
|
136
|
+
</Button>
|
|
137
|
+
</Box>
|
|
138
|
+
) }
|
|
139
|
+
</CollapsibleList>
|
|
106
140
|
);
|
|
107
141
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { __, sprintf } from '@wordpress/i18n';
|
|
3
|
+
|
|
4
|
+
import { getPostTypeCreateCapability, postTypesMap } from '../../api/post';
|
|
5
|
+
import { usePostListContext } from '../../contexts/post-list-context';
|
|
6
|
+
import useUser from '../../hooks/use-user';
|
|
7
|
+
import ContentPanelAddIconButton from './content-panel-add-icon-button';
|
|
8
|
+
|
|
9
|
+
type Props = {
|
|
10
|
+
onBeforeCreate?: () => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default function SectionAddButton( { onBeforeCreate }: Props ) {
|
|
14
|
+
const { type, setEditMode } = usePostListContext();
|
|
15
|
+
const { data: user } = useUser();
|
|
16
|
+
const createCapability = getPostTypeCreateCapability( type );
|
|
17
|
+
const singularName = postTypesMap[ type ].labels.singular_name;
|
|
18
|
+
const ariaLabel = sprintf(
|
|
19
|
+
/* translators: %s: Post type singular name (e.g. Page, Post). */
|
|
20
|
+
__( 'New %s', 'elementor' ),
|
|
21
|
+
singularName
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<ContentPanelAddIconButton
|
|
26
|
+
aria-label={ ariaLabel }
|
|
27
|
+
disabled={ ! user?.capabilities?.[ createCapability ] }
|
|
28
|
+
onClick={ ( event ) => {
|
|
29
|
+
event.stopPropagation();
|
|
30
|
+
onBeforeCreate?.();
|
|
31
|
+
setEditMode( { mode: 'create', details: {} } );
|
|
32
|
+
} }
|
|
33
|
+
/>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import { Panel, PanelBody, PanelHeader, PanelHeaderTitle } from '@elementor/editor-panels';
|
|
4
|
+
import { Divider, List, Stack } from '@elementor/ui';
|
|
4
5
|
import { __ } from '@wordpress/i18n';
|
|
5
6
|
|
|
7
|
+
import { panelPostTypeSlugs } from '../../api/post';
|
|
8
|
+
import { ContentPanelCoordinatorProvider } from '../../contexts/content-panel-coordinator';
|
|
6
9
|
import { PostListContextProvider } from '../../contexts/post-list-context';
|
|
10
|
+
import { contentPanelHeaderStackSx } from './content-panel-chrome';
|
|
11
|
+
import ContentPanelHeaderAdd from './content-panel-header-add';
|
|
12
|
+
import ContentPanelSearch from './content-panel-search';
|
|
7
13
|
import ErrorSnackbar from './error-snackbar';
|
|
8
14
|
import PostsCollapsibleList from './posts-list/posts-collapsible-list';
|
|
9
15
|
|
|
@@ -11,17 +17,41 @@ const Shell = () => {
|
|
|
11
17
|
const [ isErrorSnackbarOpen, setIsErrorSnackbarOpen ] = useState( false );
|
|
12
18
|
|
|
13
19
|
return (
|
|
14
|
-
<
|
|
15
|
-
<
|
|
16
|
-
<
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
<ContentPanelCoordinatorProvider>
|
|
21
|
+
<Panel>
|
|
22
|
+
<PanelHeader>
|
|
23
|
+
<Stack
|
|
24
|
+
alignItems="center"
|
|
25
|
+
direction="row"
|
|
26
|
+
spacing={ 0.5 }
|
|
27
|
+
sx={ {
|
|
28
|
+
...contentPanelHeaderStackSx,
|
|
29
|
+
} }
|
|
30
|
+
>
|
|
31
|
+
<PanelHeaderTitle sx={ { flex: 1, minWidth: 0, textAlign: 'start' } }>
|
|
32
|
+
{ __( 'Site content', 'elementor' ) }
|
|
33
|
+
</PanelHeaderTitle>
|
|
34
|
+
<ContentPanelHeaderAdd />
|
|
35
|
+
</Stack>
|
|
36
|
+
</PanelHeader>
|
|
37
|
+
<Divider />
|
|
38
|
+
<PanelBody>
|
|
39
|
+
<ContentPanelSearch />
|
|
40
|
+
<List dense disablePadding sx={ { py: 0, width: '100%' } }>
|
|
41
|
+
{ panelPostTypeSlugs.map( ( postTypeSlug, index ) => (
|
|
42
|
+
<PostListContextProvider
|
|
43
|
+
key={ postTypeSlug }
|
|
44
|
+
type={ postTypeSlug }
|
|
45
|
+
setError={ () => setIsErrorSnackbarOpen( true ) }
|
|
46
|
+
>
|
|
47
|
+
<PostsCollapsibleList isOpenByDefault={ index === 0 } />
|
|
48
|
+
</PostListContextProvider>
|
|
49
|
+
) ) }
|
|
50
|
+
</List>
|
|
51
|
+
<ErrorSnackbar open={ isErrorSnackbarOpen } onClose={ () => setIsErrorSnackbarOpen( false ) } />
|
|
52
|
+
</PanelBody>
|
|
53
|
+
</Panel>
|
|
54
|
+
</ContentPanelCoordinatorProvider>
|
|
25
55
|
);
|
|
26
56
|
};
|
|
27
57
|
|
|
@@ -2,6 +2,7 @@ import * as React from 'react';
|
|
|
2
2
|
import { Box, Typography } from '@elementor/ui';
|
|
3
3
|
|
|
4
4
|
import { useReverseHtmlEntities } from '../../hooks/use-reverse-html-entities';
|
|
5
|
+
import { contentPanelListRowTitleSx } from '../panel/content-panel-chrome';
|
|
5
6
|
|
|
6
7
|
const PageStatus = ( { status }: { status: string } ) => {
|
|
7
8
|
if ( 'publish' === status ) {
|
|
@@ -14,6 +15,7 @@ const PageStatus = ( { status }: { status: string } ) => {
|
|
|
14
15
|
variant="body2"
|
|
15
16
|
color="text.secondary"
|
|
16
17
|
sx={ {
|
|
18
|
+
...contentPanelListRowTitleSx,
|
|
17
19
|
textTransform: 'capitalize',
|
|
18
20
|
fontStyle: 'italic',
|
|
19
21
|
whiteSpace: 'nowrap',
|
|
@@ -35,6 +37,7 @@ const PageTitle = ( { title }: { title: string } ) => {
|
|
|
35
37
|
color="text.secondary"
|
|
36
38
|
noWrap
|
|
37
39
|
sx={ {
|
|
40
|
+
...contentPanelListRowTitleSx,
|
|
38
41
|
flexBasis: 'auto',
|
|
39
42
|
} }
|
|
40
43
|
>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createContext, type ReactNode, useCallback, useContext, useMemo, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { type Slug } from '../api/post';
|
|
5
|
+
|
|
6
|
+
type SectionHandlers = {
|
|
7
|
+
setExpanded: ( expanded: boolean ) => void;
|
|
8
|
+
startCreate: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type ContentPanelCoordinatorValue = {
|
|
12
|
+
registerSection: ( type: Slug, handlers: SectionHandlers ) => void;
|
|
13
|
+
unregisterSection: ( type: Slug ) => void;
|
|
14
|
+
requestCreate: ( type: Slug ) => void;
|
|
15
|
+
searchQuery: string;
|
|
16
|
+
setSearchQuery: ( query: string ) => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const ContentPanelCoordinatorContext = createContext< ContentPanelCoordinatorValue | null >( null );
|
|
20
|
+
|
|
21
|
+
export function ContentPanelCoordinatorProvider( { children }: { children: ReactNode } ) {
|
|
22
|
+
const sectionsRef = useRef( new Map< Slug, SectionHandlers >() );
|
|
23
|
+
const [ searchQuery, setSearchQuery ] = useState( '' );
|
|
24
|
+
|
|
25
|
+
const registerSection = useCallback( ( type: Slug, handlers: SectionHandlers ) => {
|
|
26
|
+
sectionsRef.current.set( type, handlers );
|
|
27
|
+
}, [] );
|
|
28
|
+
|
|
29
|
+
const unregisterSection = useCallback( ( type: Slug ) => {
|
|
30
|
+
sectionsRef.current.delete( type );
|
|
31
|
+
}, [] );
|
|
32
|
+
|
|
33
|
+
const requestCreate = useCallback( ( type: Slug ) => {
|
|
34
|
+
const handlers = sectionsRef.current.get( type );
|
|
35
|
+
|
|
36
|
+
if ( ! handlers ) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
handlers.setExpanded( true );
|
|
41
|
+
handlers.startCreate();
|
|
42
|
+
}, [] );
|
|
43
|
+
|
|
44
|
+
const value = useMemo(
|
|
45
|
+
() => ( {
|
|
46
|
+
registerSection,
|
|
47
|
+
unregisterSection,
|
|
48
|
+
requestCreate,
|
|
49
|
+
searchQuery,
|
|
50
|
+
setSearchQuery,
|
|
51
|
+
} ),
|
|
52
|
+
[ registerSection, unregisterSection, requestCreate, searchQuery ]
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<ContentPanelCoordinatorContext.Provider value={ value }>{ children }</ContentPanelCoordinatorContext.Provider>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useContentPanelCoordinator() {
|
|
61
|
+
const context = useContext( ContentPanelCoordinatorContext );
|
|
62
|
+
|
|
63
|
+
if ( ! context ) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
'The `useContentPanelCoordinator()` hook must be used within a `<ContentPanelCoordinatorProvider />`'
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return context;
|
|
70
|
+
}
|
|
@@ -9,13 +9,13 @@ import {
|
|
|
9
9
|
type UpdatePost,
|
|
10
10
|
updateRequest,
|
|
11
11
|
} from '../api/post';
|
|
12
|
-
import {
|
|
12
|
+
import { postsQueryKeyBase } from './use-posts';
|
|
13
13
|
import { recentPostsQueryKey } from './use-recent-posts';
|
|
14
14
|
|
|
15
15
|
export function usePostActions( postTypeSlug: Slug ) {
|
|
16
16
|
const invalidatePosts = useInvalidatePosts( postTypeSlug );
|
|
17
17
|
|
|
18
|
-
const onSuccess = () => invalidatePosts(
|
|
18
|
+
const onSuccess = () => invalidatePosts();
|
|
19
19
|
|
|
20
20
|
const createPost = useMutation( {
|
|
21
21
|
mutationFn: ( newPost: NewPost ) => createRequest( postTypeSlug, newPost ),
|
|
@@ -48,9 +48,9 @@ export function usePostActions( postTypeSlug: Slug ) {
|
|
|
48
48
|
function useInvalidatePosts( postTypeSlug: string ) {
|
|
49
49
|
const queryClient = useQueryClient();
|
|
50
50
|
|
|
51
|
-
return (
|
|
52
|
-
const queryKey =
|
|
53
|
-
queryClient.invalidateQueries( { queryKey: recentPostsQueryKey }
|
|
54
|
-
return queryClient.invalidateQueries( { queryKey }
|
|
51
|
+
return () => {
|
|
52
|
+
const queryKey = postsQueryKeyBase( postTypeSlug );
|
|
53
|
+
queryClient.invalidateQueries( { queryKey: recentPostsQueryKey } );
|
|
54
|
+
return queryClient.invalidateQueries( { queryKey } );
|
|
55
55
|
};
|
|
56
56
|
}
|
package/src/hooks/use-posts.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import { useInfiniteQuery } from '@elementor/query';
|
|
1
|
+
import { keepPreviousData, useInfiniteQuery } from '@elementor/query';
|
|
2
2
|
|
|
3
3
|
import { getRequest, type PostsResponse, type Slug } from '../api/post';
|
|
4
4
|
import { type Post } from '../types';
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const postsQueryKeyBase = ( postTypeSlug: string ) => [ 'site-navigation', 'posts', postTypeSlug ];
|
|
7
|
+
|
|
8
|
+
export const postsQueryKey = ( postTypeSlug: string, search = '' ) => {
|
|
9
|
+
const trimmedSearch = search.trim();
|
|
10
|
+
|
|
11
|
+
if ( ! trimmedSearch ) {
|
|
12
|
+
return postsQueryKeyBase( postTypeSlug );
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return [ ...postsQueryKeyBase( postTypeSlug ), trimmedSearch ];
|
|
16
|
+
};
|
|
7
17
|
|
|
8
18
|
const flattenData = ( data?: { pages: PostsResponse[] } ) => {
|
|
9
19
|
if ( ! data ) {
|
|
@@ -19,14 +29,17 @@ const flattenData = ( data?: { pages: PostsResponse[] } ) => {
|
|
|
19
29
|
return flattened;
|
|
20
30
|
};
|
|
21
31
|
|
|
22
|
-
export function usePosts( postTypeSlug: Slug ) {
|
|
32
|
+
export function usePosts( postTypeSlug: Slug, search = '' ) {
|
|
33
|
+
const trimmedSearch = search.trim();
|
|
34
|
+
|
|
23
35
|
const query = useInfiniteQuery( {
|
|
24
|
-
queryKey: postsQueryKey( postTypeSlug ),
|
|
25
|
-
queryFn: ( { pageParam = 1 } ) => getRequest( postTypeSlug, pageParam ),
|
|
36
|
+
queryKey: postsQueryKey( postTypeSlug, trimmedSearch ),
|
|
37
|
+
queryFn: ( { pageParam = 1 } ) => getRequest( postTypeSlug, pageParam, trimmedSearch ),
|
|
26
38
|
initialPageParam: 1,
|
|
27
39
|
getNextPageParam: ( lastPage ) => {
|
|
28
40
|
return lastPage.currentPage < lastPage.totalPages ? lastPage.currentPage + 1 : undefined;
|
|
29
41
|
},
|
|
42
|
+
placeholderData: keepPreviousData,
|
|
30
43
|
} );
|
|
31
44
|
|
|
32
45
|
return { ...query, data: { posts: flattenData( query.data ), total: query.data?.pages[ 0 ]?.totalPosts ?? 0 } };
|
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
return useMemo( () => {
|
|
5
|
-
const textarea = document.createElement( 'textarea' );
|
|
6
|
-
textarea.innerHTML = escapedHTML;
|
|
7
|
-
|
|
8
|
-
const { value } = textarea;
|
|
3
|
+
import { getPostTitlePlain } from '../utils/post-title-plain';
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return value;
|
|
13
|
-
}, [ escapedHTML ] );
|
|
5
|
+
export function useReverseHtmlEntities( escapedHTML = '' ) {
|
|
6
|
+
return useMemo( () => getPostTitlePlain( escapedHTML ), [ escapedHTML ] );
|
|
14
7
|
}
|
|
@@ -10,7 +10,7 @@ export function useToggleButtonProps(): ToggleActionProps {
|
|
|
10
10
|
const { open, close } = usePanelActions();
|
|
11
11
|
|
|
12
12
|
return {
|
|
13
|
-
title: __( '
|
|
13
|
+
title: __( 'Site content', 'elementor' ),
|
|
14
14
|
icon: PagesIcon,
|
|
15
15
|
onClick: () => {
|
|
16
16
|
const extendedWindow = window as unknown as ExtendedWindow;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function getPostTitlePlain( renderedTitle: string ): string {
|
|
2
|
+
if ( typeof document === 'undefined' ) {
|
|
3
|
+
return renderedTitle;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const textarea = document.createElement( 'textarea' );
|
|
7
|
+
textarea.innerHTML = renderedTitle;
|
|
8
|
+
|
|
9
|
+
return textarea.value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function postTitleMatchesSearch( renderedTitle: string, searchQuery: string ): boolean {
|
|
13
|
+
const query = searchQuery.trim().toLowerCase();
|
|
14
|
+
|
|
15
|
+
if ( ! query ) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return getPostTitlePlain( renderedTitle ).toLowerCase().includes( query );
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function postRowMatchesSearch(
|
|
23
|
+
post: { id: number; title: { rendered: string } },
|
|
24
|
+
searchQuery: string,
|
|
25
|
+
activeDocument?: { id: number; title: string } | null
|
|
26
|
+
): boolean {
|
|
27
|
+
const query = searchQuery.trim();
|
|
28
|
+
|
|
29
|
+
if ( ! query ) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const title = activeDocument?.id === post.id ? activeDocument.title : post.title.rendered;
|
|
34
|
+
|
|
35
|
+
return postTitleMatchesSearch( title, query );
|
|
36
|
+
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { PlusIcon } from '@elementor/icons';
|
|
3
|
-
import { Button } from '@elementor/ui';
|
|
4
|
-
import { __ } from '@wordpress/i18n';
|
|
5
|
-
|
|
6
|
-
import { usePostListContext } from '../../contexts/post-list-context';
|
|
7
|
-
import useUser from '../../hooks/use-user';
|
|
8
|
-
|
|
9
|
-
export default function AddNewButton() {
|
|
10
|
-
const { setEditMode } = usePostListContext();
|
|
11
|
-
const { data: user } = useUser();
|
|
12
|
-
|
|
13
|
-
return (
|
|
14
|
-
<Button
|
|
15
|
-
size={ 'small' }
|
|
16
|
-
startIcon={ <PlusIcon /> }
|
|
17
|
-
disabled={ ! user?.capabilities?.edit_pages }
|
|
18
|
-
onClick={ () => {
|
|
19
|
-
setEditMode( { mode: 'create', details: {} } );
|
|
20
|
-
} }
|
|
21
|
-
sx={ {
|
|
22
|
-
px: 1.5,
|
|
23
|
-
} }
|
|
24
|
-
>
|
|
25
|
-
{ __( 'Add New', 'elementor' ) }
|
|
26
|
-
</Button>
|
|
27
|
-
);
|
|
28
|
-
}
|