@elementor/editor-site-navigation 4.5.0-1208 → 4.5.0-1210

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementor/editor-site-navigation",
3
- "version": "4.5.0-1208",
3
+ "version": "4.5.0-1210",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -39,16 +39,16 @@
39
39
  "dev": "tsup --config=../../tsup.dev.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@elementor/editor-app-bar": "4.5.0-1208",
43
- "@elementor/editor-documents": "4.5.0-1208",
44
- "@elementor/editor-panels": "4.5.0-1208",
45
- "@elementor/editor-sidebar": "4.5.0-1208",
46
- "@elementor/editor-ui": "4.5.0-1208",
47
- "@elementor/editor-v1-adapters": "4.5.0-1208",
48
- "@elementor/env": "4.5.0-1208",
42
+ "@elementor/editor-app-bar": "4.5.0-1210",
43
+ "@elementor/editor-documents": "4.5.0-1210",
44
+ "@elementor/editor-panels": "4.5.0-1210",
45
+ "@elementor/editor-sidebar": "4.5.0-1210",
46
+ "@elementor/editor-ui": "4.5.0-1210",
47
+ "@elementor/editor-v1-adapters": "4.5.0-1210",
48
+ "@elementor/env": "4.5.0-1210",
49
49
  "@elementor/icons": "~1.78.1",
50
- "@elementor/events": "4.5.0-1208",
51
- "@elementor/query": "4.5.0-1208",
50
+ "@elementor/events": "4.5.0-1210",
51
+ "@elementor/query": "4.5.0-1210",
52
52
  "@elementor/ui": "1.41.4",
53
53
  "@wordpress/api-fetch": "^6.42.0",
54
54
  "@wordpress/i18n": "^5.13.0"
package/src/api/post.ts CHANGED
@@ -40,6 +40,22 @@ export function getPostTypeCreateCapability( postTypeSlug: Slug ): string {
40
40
  return postTypesMap[ postTypeSlug ].createCapability;
41
41
  }
42
42
 
43
+ export function getAccessiblePanelPostTypes( capabilities: Record< string, boolean > ): Slug[] {
44
+ return panelPostTypeSlugs.filter( ( slug ) => capabilities[ getPostTypeCreateCapability( slug ) ] );
45
+ }
46
+
47
+ export function canAccessSiteContentPanel( capabilities: Record< string, boolean > ): boolean {
48
+ return getAccessiblePanelPostTypes( capabilities ).length > 0;
49
+ }
50
+
51
+ export function getCannotEditPanelItemMessage( postTypeSlug: Slug ): string {
52
+ if ( 'page' === postTypeSlug ) {
53
+ return __( 'You cannot edit this page.', 'elementor' );
54
+ }
55
+
56
+ return __( 'You cannot edit this post.', 'elementor' );
57
+ }
58
+
43
59
  export const POST_PER_PAGE = 10;
44
60
 
45
61
  type WpPostsResponse = {
@@ -3,16 +3,14 @@ import { CopyIcon } from '@elementor/icons';
3
3
  import { type PopupState } from '@elementor/ui';
4
4
  import { __ } from '@wordpress/i18n';
5
5
 
6
- import { getPostTypeCreateCapability } from '../../../../api/post';
7
6
  import { usePostListContext } from '../../../../contexts/post-list-context';
8
- import useUser from '../../../../hooks/use-user';
7
+ import useCanCreate from '../../../../hooks/use-can-create';
9
8
  import { type Post } from '../../../../types';
10
9
  import ActionMenuItem from '../action-menu-item';
11
10
 
12
11
  export default function Duplicate( { post, popupState }: { post: Post; popupState: PopupState } ) {
13
12
  const { setEditMode, type } = usePostListContext();
14
- const { data: user } = useUser();
15
- const createCapability = getPostTypeCreateCapability( type );
13
+ const canCreate = useCanCreate( type );
16
14
  const onClick = () => {
17
15
  popupState.close();
18
16
 
@@ -25,7 +23,7 @@ export default function Duplicate( { post, popupState }: { post: Post; popupStat
25
23
  } );
26
24
  };
27
25
 
28
- const isDisabled = ! user?.capabilities?.[ createCapability ];
26
+ const isDisabled = ! canCreate;
29
27
 
30
28
  return (
31
29
  <ActionMenuItem
@@ -3,9 +3,10 @@ import { PageTypeIcon, PostTypeIcon } from '@elementor/icons';
3
3
  import { bindMenu, bindTrigger, ListItemIcon, ListItemText, Menu, MenuItem, usePopupState } from '@elementor/ui';
4
4
  import { __ } from '@wordpress/i18n';
5
5
 
6
- import { getPostTypeCreateCapability, panelPostTypeSlugs, postTypesMap, type Slug } from '../../api/post';
6
+ import { postTypesMap, type Slug } from '../../api/post';
7
7
  import { useContentPanelCoordinator } from '../../contexts/content-panel-coordinator';
8
- import useUser from '../../hooks/use-user';
8
+ import useAccessiblePanelPostTypes from '../../hooks/use-accessible-panel-post-types';
9
+ import useCanCreate from '../../hooks/use-can-create';
9
10
  import ContentPanelAddIconButton from './content-panel-add-icon-button';
10
11
 
11
12
  const typeIcons: Record< Slug, typeof PageTypeIcon > = {
@@ -13,44 +14,51 @@ const typeIcons: Record< Slug, typeof PageTypeIcon > = {
13
14
  post: PostTypeIcon,
14
15
  };
15
16
 
17
+ function HeaderAddMenuItem( { slug, onSelect }: { slug: Slug; onSelect: ( slug: Slug ) => void } ) {
18
+ const canCreate = useCanCreate( slug );
19
+ const Icon = typeIcons[ slug ];
20
+ const singularName = postTypesMap[ slug ].labels.singular_name;
21
+
22
+ return (
23
+ <MenuItem disabled={ ! canCreate } onClick={ () => onSelect( slug ) }>
24
+ <ListItemIcon>
25
+ <Icon fontSize="small" />
26
+ </ListItemIcon>
27
+ <ListItemText primary={ singularName } primaryTypographyProps={ { variant: 'body2' } } />
28
+ </MenuItem>
29
+ );
30
+ }
31
+
16
32
  export default function ContentPanelHeaderAdd() {
17
33
  const { requestCreate } = useContentPanelCoordinator();
18
- const { data: user } = useUser();
34
+ const { accessiblePostTypes: accessibleSlugs } = useAccessiblePanelPostTypes();
19
35
  const popupState = usePopupState( {
20
36
  variant: 'popover',
21
37
  popupId: 'elementor-content-panel-header-add',
22
38
  } );
39
+ const canAddAny = accessibleSlugs.length > 0;
23
40
 
24
41
  const buttonProps = bindTrigger( popupState );
25
42
  const menuProps = bindMenu( popupState );
26
43
 
44
+ const handleSelectType = ( slug: Slug ) => {
45
+ popupState.close();
46
+ requestAnimationFrame( () => {
47
+ requestCreate( slug );
48
+ } );
49
+ };
50
+
27
51
  return (
28
52
  <>
29
- <ContentPanelAddIconButton aria-label={ __( 'Add new', 'elementor' ) } { ...buttonProps } />
53
+ <ContentPanelAddIconButton
54
+ aria-label={ __( 'Add new', 'elementor' ) }
55
+ { ...buttonProps }
56
+ disabled={ ! canAddAny }
57
+ />
30
58
  <Menu { ...menuProps }>
31
- { panelPostTypeSlugs.map( ( slug ) => {
32
- const Icon = typeIcons[ slug ];
33
- const createCapability = getPostTypeCreateCapability( slug );
34
- const singularName = postTypesMap[ slug ].labels.singular_name;
35
-
36
- return (
37
- <MenuItem
38
- key={ slug }
39
- disabled={ ! user?.capabilities?.[ createCapability ] }
40
- onClick={ () => {
41
- popupState.close();
42
- requestAnimationFrame( () => {
43
- requestCreate( slug );
44
- } );
45
- } }
46
- >
47
- <ListItemIcon>
48
- <Icon fontSize="small" />
49
- </ListItemIcon>
50
- <ListItemText primary={ singularName } primaryTypographyProps={ { variant: 'body2' } } />
51
- </MenuItem>
52
- );
53
- } ) }
59
+ { accessibleSlugs.map( ( slug ) => (
60
+ <HeaderAddMenuItem key={ slug } onSelect={ handleSelectType } slug={ slug } />
61
+ ) ) }
54
62
  </Menu>
55
63
  </>
56
64
  );
@@ -19,6 +19,7 @@ import {
19
19
  } from '@elementor/ui';
20
20
  import { __ } from '@wordpress/i18n';
21
21
 
22
+ import { getCannotEditPanelItemMessage, type Slug } from '../../../../api/post';
22
23
  import { usePostListContext } from '../../../../contexts/post-list-context';
23
24
  import { type Post } from '../../../../types';
24
25
  import PageTitleAndStatus from '../../../shared/page-title-and-status';
@@ -29,13 +30,21 @@ import SetHome from '../../actions-menu/actions/set-home';
29
30
  import View from '../../actions-menu/actions/view';
30
31
  import { contentPanelListItemButtonSx, contentPanelListRowPaddingSx } from '../../content-panel-chrome';
31
32
 
32
- const DisabledPostTooltip = ( { children, isDisabled }: { children: React.ReactNode; isDisabled: boolean } ) => {
33
+ const DisabledPostTooltip = ( {
34
+ children,
35
+ isDisabled,
36
+ postTypeSlug,
37
+ }: {
38
+ children: React.ReactNode;
39
+ isDisabled: boolean;
40
+ postTypeSlug: Slug;
41
+ } ) => {
33
42
  if ( isDisabled ) {
34
43
  const title = (
35
44
  <Typography variant="caption">
36
- You cannot edit this page.
45
+ { getCannotEditPanelItemMessage( postTypeSlug ) }
37
46
  <br />
38
- To edit it directly, contact the site owner
47
+ { __( 'To edit it directly, contact the site owner', 'elementor' ) }
39
48
  </Typography>
40
49
  );
41
50
 
@@ -69,7 +78,7 @@ export default function ListItemView( { post }: { post: Post } ) {
69
78
 
70
79
  return (
71
80
  <>
72
- <DisabledPostTooltip isDisabled={ isDisabled }>
81
+ <DisabledPostTooltip isDisabled={ isDisabled } postTypeSlug={ type }>
73
82
  <ListItem
74
83
  disablePadding
75
84
  sx={ contentPanelListRowPaddingSx }
@@ -1,9 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import { __, sprintf } from '@wordpress/i18n';
3
3
 
4
- import { getPostTypeCreateCapability, postTypesMap } from '../../api/post';
4
+ import { postTypesMap } from '../../api/post';
5
5
  import { usePostListContext } from '../../contexts/post-list-context';
6
- import useUser from '../../hooks/use-user';
6
+ import useCanCreate from '../../hooks/use-can-create';
7
7
  import ContentPanelAddIconButton from './content-panel-add-icon-button';
8
8
 
9
9
  type Props = {
@@ -12,8 +12,7 @@ type Props = {
12
12
 
13
13
  export default function SectionAddButton( { onBeforeCreate }: Props ) {
14
14
  const { type, setEditMode } = usePostListContext();
15
- const { data: user } = useUser();
16
- const createCapability = getPostTypeCreateCapability( type );
15
+ const canCreate = useCanCreate( type );
17
16
  const singularName = postTypesMap[ type ].labels.singular_name;
18
17
  const ariaLabel = sprintf(
19
18
  /* translators: %s: Post type singular name (e.g. Page, Post). */
@@ -24,7 +23,7 @@ export default function SectionAddButton( { onBeforeCreate }: Props ) {
24
23
  return (
25
24
  <ContentPanelAddIconButton
26
25
  aria-label={ ariaLabel }
27
- disabled={ ! user?.capabilities?.[ createCapability ] }
26
+ disabled={ ! canCreate }
28
27
  onClick={ ( event ) => {
29
28
  event.stopPropagation();
30
29
  onBeforeCreate?.();
@@ -0,0 +1,44 @@
1
+ import * as React from 'react';
2
+ import { List, Skeleton, Stack } from '@elementor/ui';
3
+
4
+ import { type Slug } from '../../api/post';
5
+ import { PostListContextProvider } from '../../contexts/post-list-context';
6
+ import PostsCollapsibleList from './posts-list/posts-collapsible-list';
7
+ import UserLoadError from './user-load-error';
8
+
9
+ type ShellPanelBodyContentProps = {
10
+ accessiblePostTypes: Slug[];
11
+ isUserPending: boolean;
12
+ onListError: () => void;
13
+ showUserLoadError: boolean;
14
+ };
15
+
16
+ export default function ShellPanelBodyContent( {
17
+ accessiblePostTypes,
18
+ isUserPending,
19
+ onListError,
20
+ showUserLoadError,
21
+ }: ShellPanelBodyContentProps ) {
22
+ if ( isUserPending ) {
23
+ return (
24
+ <Stack spacing={ 1 } sx={ { px: 2, py: 1 } }>
25
+ <Skeleton animation="wave" height={ 28 } variant="rounded" width="100%" />
26
+ <Skeleton animation="wave" height={ 28 } variant="rounded" width="80%" />
27
+ </Stack>
28
+ );
29
+ }
30
+
31
+ if ( showUserLoadError ) {
32
+ return <UserLoadError />;
33
+ }
34
+
35
+ return (
36
+ <List dense disablePadding sx={ { py: 0, width: '100%' } }>
37
+ { accessiblePostTypes.map( ( postTypeSlug, index ) => (
38
+ <PostListContextProvider key={ postTypeSlug } type={ postTypeSlug } setError={ onListError }>
39
+ <PostsCollapsibleList isOpenByDefault={ index === 0 } />
40
+ </PostListContextProvider>
41
+ ) ) }
42
+ </List>
43
+ );
44
+ }
@@ -1,20 +1,20 @@
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
+ import { Divider, Stack } from '@elementor/ui';
5
5
  import { __ } from '@wordpress/i18n';
6
6
 
7
- import { panelPostTypeSlugs } from '../../api/post';
8
7
  import { ContentPanelCoordinatorProvider } from '../../contexts/content-panel-coordinator';
9
- import { PostListContextProvider } from '../../contexts/post-list-context';
8
+ import useAccessiblePanelPostTypes from '../../hooks/use-accessible-panel-post-types';
10
9
  import { contentPanelHeaderStackSx } from './content-panel-chrome';
11
10
  import ContentPanelHeaderAdd from './content-panel-header-add';
12
11
  import ContentPanelSearch from './content-panel-search';
13
12
  import ErrorSnackbar from './error-snackbar';
14
- import PostsCollapsibleList from './posts-list/posts-collapsible-list';
13
+ import ShellPanelBodyContent from './shell-panel-body-content';
15
14
 
16
15
  const Shell = () => {
17
16
  const [ isErrorSnackbarOpen, setIsErrorSnackbarOpen ] = useState( false );
17
+ const { accessiblePostTypes, isUserPending, showUserLoadError } = useAccessiblePanelPostTypes();
18
18
 
19
19
  return (
20
20
  <ContentPanelCoordinatorProvider>
@@ -37,17 +37,12 @@ const Shell = () => {
37
37
  <Divider />
38
38
  <PanelBody>
39
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>
40
+ <ShellPanelBodyContent
41
+ accessiblePostTypes={ accessiblePostTypes }
42
+ isUserPending={ isUserPending }
43
+ onListError={ () => setIsErrorSnackbarOpen( true ) }
44
+ showUserLoadError={ showUserLoadError }
45
+ />
51
46
  <ErrorSnackbar open={ isErrorSnackbarOpen } onClose={ () => setIsErrorSnackbarOpen( false ) } />
52
47
  </PanelBody>
53
48
  </Panel>
@@ -0,0 +1,16 @@
1
+ import * as React from 'react';
2
+ import { Box, Typography } from '@elementor/ui';
3
+ import { __ } from '@wordpress/i18n';
4
+
5
+ export default function UserLoadError() {
6
+ return (
7
+ <Box sx={ { px: 2, py: 2 } }>
8
+ <Typography variant="body2" color="text.secondary">
9
+ { __(
10
+ 'We couldn’t load your content permissions. Try closing and reopening this panel.',
11
+ 'elementor'
12
+ ) }
13
+ </Typography>
14
+ </Box>
15
+ );
16
+ }
@@ -1,29 +1,83 @@
1
1
  import * as React from 'react';
2
- import { Box, Typography } from '@elementor/ui';
2
+ import { Box, Chip, type ChipProps, Typography } from '@elementor/ui';
3
+ import { __ } from '@wordpress/i18n';
3
4
 
4
5
  import { useReverseHtmlEntities } from '../../hooks/use-reverse-html-entities';
5
6
  import { contentPanelListRowTitleSx } from '../panel/content-panel-chrome';
6
7
 
8
+ type StatusChipConfig = {
9
+ color: ChipProps[ 'color' ];
10
+ label: string;
11
+ };
12
+
13
+ function humanizeStatusSlug( status: string ): string {
14
+ return status.replace( /[-_]+/g, ' ' ).trim();
15
+ }
16
+
17
+ const statusChipConfig: Record< string, StatusChipConfig > = {
18
+ draft: {
19
+ color: 'default',
20
+ label: __( 'Draft', 'elementor' ),
21
+ },
22
+ future: {
23
+ color: 'default',
24
+ label: __( 'Scheduled', 'elementor' ),
25
+ },
26
+ pending: {
27
+ color: 'warning',
28
+ label: __( 'Pending Review', 'elementor' ),
29
+ },
30
+ private: {
31
+ color: 'default',
32
+ label: __( 'Private', 'elementor' ),
33
+ },
34
+ };
35
+
36
+ export function getStatusChipConfig( status: string ): StatusChipConfig {
37
+ const known = statusChipConfig[ status ];
38
+
39
+ if ( known ) {
40
+ return known;
41
+ }
42
+
43
+ return {
44
+ color: 'default',
45
+ label: humanizeStatusSlug( status ),
46
+ };
47
+ }
48
+
49
+ function isKnownPanelStatus( status: string ): boolean {
50
+ return status in statusChipConfig;
51
+ }
52
+
7
53
  const PageStatus = ( { status }: { status: string } ) => {
8
54
  if ( 'publish' === status ) {
9
55
  return null;
10
56
  }
11
57
 
58
+ const { color, label } = getStatusChipConfig( status );
59
+ const isKnownStatus = isKnownPanelStatus( status );
60
+
12
61
  return (
13
- <Typography
62
+ <Chip
63
+ color={ color }
14
64
  component="span"
15
- variant="body2"
16
- color="text.secondary"
65
+ label={ label }
66
+ size="tiny"
17
67
  sx={ {
18
- ...contentPanelListRowTitleSx,
19
- textTransform: 'capitalize',
20
- fontStyle: 'italic',
21
- whiteSpace: 'nowrap',
22
- flexBasis: 'content',
68
+ flexShrink: isKnownStatus ? 0 : 1,
69
+ maxWidth: isKnownStatus ? undefined : '45%',
70
+ minWidth: 0,
71
+ ml: 0.5,
72
+ '& .MuiChip-label': {
73
+ overflow: 'hidden',
74
+ textOverflow: 'ellipsis',
75
+ whiteSpace: 'nowrap',
76
+ },
23
77
  } }
24
- >
25
- ({ status })
26
- </Typography>
78
+ title={ label }
79
+ variant="standard"
80
+ />
27
81
  );
28
82
  };
29
83
 
@@ -48,9 +102,8 @@ const PageTitle = ( { title }: { title: string } ) => {
48
102
 
49
103
  export default function PageTitleAndStatus( { title, status }: { title: string; status: string } ) {
50
104
  return (
51
- <Box display="flex">
105
+ <Box alignItems="center" display="flex" minWidth={ 0 }>
52
106
  <PageTitle title={ title } />
53
- &nbsp;
54
107
  <PageStatus status={ status } />
55
108
  </Box>
56
109
  );
package/src/env.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { parseEnv } from '@elementor/env';
2
2
 
3
3
  export const { env } = parseEnv< {
4
+ can_access_site_content_panel: boolean;
4
5
  is_pages_panel_active: boolean;
5
6
  } >( '@elementor/editor-site-navigation', ( envData ) => {
6
7
  return envData as {
8
+ can_access_site_content_panel: boolean;
7
9
  is_pages_panel_active: boolean;
8
10
  };
9
11
  } );
@@ -0,0 +1,29 @@
1
+ import { useMemo } from 'react';
2
+
3
+ import { getAccessiblePanelPostTypes, panelPostTypeSlugs } from '../api/post';
4
+ import { env } from '../env';
5
+ import useUser from './use-user';
6
+
7
+ export default function useAccessiblePanelPostTypes() {
8
+ const { data: user, isPending: isUserPending, isError: isUserError } = useUser();
9
+
10
+ const accessiblePostTypes = useMemo( () => {
11
+ if ( user?.capabilities ) {
12
+ return getAccessiblePanelPostTypes( user.capabilities );
13
+ }
14
+
15
+ if ( isUserError && env.can_access_site_content_panel ) {
16
+ return panelPostTypeSlugs;
17
+ }
18
+
19
+ return [];
20
+ }, [ user, isUserError ] );
21
+
22
+ const showUserLoadError = isUserError && accessiblePostTypes.length === 0;
23
+
24
+ return {
25
+ accessiblePostTypes,
26
+ isUserPending,
27
+ showUserLoadError,
28
+ };
29
+ }
@@ -0,0 +1,9 @@
1
+ import { getPostTypeCreateCapability, type Slug } from '../api/post';
2
+ import useUser from './use-user';
3
+
4
+ export default function useCanCreate( postTypeSlug: Slug ): boolean {
5
+ const { data: user } = useUser();
6
+ const capability = getPostTypeCreateCapability( postTypeSlug );
7
+
8
+ return !! user?.capabilities?.[ capability ];
9
+ }
package/src/init.ts CHANGED
@@ -10,7 +10,7 @@ import { useToggleButtonProps } from './hooks/use-toggle-button-props';
10
10
  export function init() {
11
11
  registerTopBarMenuItems();
12
12
  // TODO 06/06/2023 : remove if when we are production ready
13
- if ( env.is_pages_panel_active ) {
13
+ if ( env.is_pages_panel_active && env.can_access_site_content_panel ) {
14
14
  registerPanel( panel );
15
15
  registerButton();
16
16
  }