@elementor/editor-site-navigation 4.5.0-1250 → 4.5.0-1252

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.
@@ -0,0 +1,149 @@
1
+ import * as React from 'react';
2
+ import { ChevronRightIcon, PageTypeIcon, PostTypeIcon } from '@elementor/icons';
3
+ import { Box, Chip, Rotate, Skeleton, Stack, Typography, useTheme } from '@elementor/ui';
4
+ import { __ } from '@wordpress/i18n';
5
+
6
+ import { postTypesMap, type Slug } from '../../api/post';
7
+ import { type ContentPanelSectionId } from '../../contexts/content-panel-coordinator';
8
+ import { usePostsBaselineTotal } from '../../hooks/use-posts-baseline-total';
9
+ import { getIconsMap } from '../../icons-map';
10
+ import { getSiteContentPanelSectionDescriptors } from '../../site-content-panel-sections';
11
+ import { getSiteContentSectionById } from '../../site-content-sections';
12
+ import { onKeyboardClick } from '../../utils/keyboard-click';
13
+
14
+ const POST_TYPE_ICONS: Record< Slug, typeof PageTypeIcon > = {
15
+ page: PageTypeIcon,
16
+ post: PostTypeIcon,
17
+ };
18
+
19
+ const POST_TYPE_SUBTITLES: Record< Slug, string > = {
20
+ page: __( 'Website pages', 'elementor' ),
21
+ post: __( 'Blog & articles', 'elementor' ),
22
+ };
23
+
24
+ type HomeItem = {
25
+ hubCount?: React.ReactNode;
26
+ id: ContentPanelSectionId;
27
+ title: string;
28
+ description?: string;
29
+ icon: React.ReactElement;
30
+ isPostType: boolean;
31
+ };
32
+
33
+ export type SiteContentHomeProps = {
34
+ accessiblePostTypes: Slug[];
35
+ onSelect: ( sectionId: ContentPanelSectionId ) => void;
36
+ };
37
+
38
+ export function SiteContentHome( { accessiblePostTypes, onSelect }: SiteContentHomeProps ) {
39
+ const isRtl = 'rtl' === useTheme().direction;
40
+ const items: HomeItem[] = getSiteContentPanelSectionDescriptors( accessiblePostTypes ).map( ( descriptor ) => {
41
+ const isPostType = descriptor.id in postTypesMap;
42
+
43
+ if ( isPostType ) {
44
+ const slug = descriptor.id as Slug;
45
+ const Icon = POST_TYPE_ICONS[ slug ];
46
+ return {
47
+ id: descriptor.id,
48
+ title: descriptor.label,
49
+ description: POST_TYPE_SUBTITLES[ slug ],
50
+ icon: <Icon fontSize="small" color="action" />,
51
+ isPostType: true,
52
+ };
53
+ }
54
+
55
+ const RegisteredIcon = getIconsMap()[ descriptor.id ] ?? PageTypeIcon;
56
+ const registeredSection = getSiteContentSectionById( descriptor.id );
57
+
58
+ return {
59
+ description: registeredSection?.description,
60
+ hubCount: registeredSection?.renderHubCount?.(),
61
+ id: descriptor.id,
62
+ title: descriptor.label,
63
+ icon: <RegisteredIcon fontSize="small" color="action" />,
64
+ isPostType: false,
65
+ };
66
+ } );
67
+
68
+ return (
69
+ <Stack
70
+ role="list"
71
+ aria-label={ __( 'Site content sections', 'elementor' ) }
72
+ sx={ {
73
+ flex: 1,
74
+ minHeight: 0,
75
+ overflow: 'auto',
76
+ px: 2,
77
+ py: 1.5,
78
+ gap: 1,
79
+ } }
80
+ >
81
+ { items.map( ( item ) => (
82
+ <Box key={ item.id } role="listitem">
83
+ <HomeRow { ...item } isRtl={ isRtl } onSelect={ () => onSelect( item.id ) } />
84
+ </Box>
85
+ ) ) }
86
+ </Stack>
87
+ );
88
+ }
89
+
90
+ type HomeRowProps = HomeItem & {
91
+ isRtl: boolean;
92
+ onSelect: () => void;
93
+ };
94
+
95
+ function HomeRow( { hubCount, title, description, icon, id, isPostType, isRtl, onSelect }: HomeRowProps ) {
96
+ return (
97
+ <Box
98
+ role="button"
99
+ tabIndex={ 0 }
100
+ aria-label={ title }
101
+ onClick={ onSelect }
102
+ onKeyDown={ onKeyboardClick( onSelect ) }
103
+ sx={ {
104
+ display: 'flex',
105
+ alignItems: 'center',
106
+ gap: 1.5,
107
+ px: 1.5,
108
+ py: 1.25,
109
+ cursor: 'pointer',
110
+ borderRadius: 1,
111
+ border: 1,
112
+ borderColor: 'divider',
113
+ '&:hover': { bgcolor: 'action.hover' },
114
+ outline: 'none',
115
+ '&:focus-visible': { outline: '2px solid', outlineColor: 'primary.main' },
116
+ } }
117
+ >
118
+ { icon }
119
+ <Stack sx={ { flex: 1, minWidth: 0 } }>
120
+ <Typography variant="body2" fontWeight="bold">
121
+ { title }
122
+ </Typography>
123
+ { description ? (
124
+ <Typography variant="caption" color="text.secondary">
125
+ { description }
126
+ </Typography>
127
+ ) : null }
128
+ </Stack>
129
+ { isPostType ? <HubCountBadge postTypeSlug={ id as Slug } /> : hubCount }
130
+ <Rotate in={ isRtl }>
131
+ <ChevronRightIcon fontSize="small" color="action" />
132
+ </Rotate>
133
+ </Box>
134
+ );
135
+ }
136
+
137
+ function HubCountBadge( { postTypeSlug }: { postTypeSlug: Slug } ) {
138
+ const { data, isPending } = usePostsBaselineTotal( postTypeSlug, true, 'any' );
139
+
140
+ if ( isPending ) {
141
+ return <Skeleton variant="circular" width={ 24 } height={ 24 } />;
142
+ }
143
+
144
+ if ( data === undefined ) {
145
+ return null;
146
+ }
147
+
148
+ return <Chip size="tiny" variant="default" label={ String( data ) } />;
149
+ }
@@ -0,0 +1,60 @@
1
+ import * as React from 'react';
2
+ import { ArrowLeftIcon, PageTypeIcon, PostTypeIcon } from '@elementor/icons';
3
+ import { IconButton, Rotate, Stack, Typography, useTheme } from '@elementor/ui';
4
+ import { __ } from '@wordpress/i18n';
5
+
6
+ import { postTypesMap, type Slug } from '../../api/post';
7
+ import { type ContentPanelSectionId } from '../../contexts/content-panel-coordinator';
8
+ import { getIconsMap } from '../../icons-map';
9
+ import { getSiteContentSections } from '../../site-content-sections';
10
+
11
+ const POST_TYPE_ICONS: Record< Slug, typeof PageTypeIcon > = {
12
+ page: PageTypeIcon,
13
+ post: PostTypeIcon,
14
+ };
15
+
16
+ function getSectionTitle( sectionId: ContentPanelSectionId ): string {
17
+ if ( sectionId in postTypesMap ) {
18
+ return postTypesMap[ sectionId as Slug ].labels.plural_name;
19
+ }
20
+
21
+ const registered = getSiteContentSections().find( ( section ) => section.id === sectionId );
22
+ return registered?.label ?? sectionId;
23
+ }
24
+
25
+ function getSectionIcon( sectionId: ContentPanelSectionId ) {
26
+ if ( sectionId in POST_TYPE_ICONS ) {
27
+ return POST_TYPE_ICONS[ sectionId as Slug ];
28
+ }
29
+
30
+ return getIconsMap()[ sectionId ] ?? PageTypeIcon;
31
+ }
32
+
33
+ export type SiteContentSectionHeaderProps = {
34
+ sectionId: ContentPanelSectionId;
35
+ onBack: () => void;
36
+ };
37
+
38
+ export function SiteContentSectionHeader( { sectionId, onBack }: SiteContentSectionHeaderProps ) {
39
+ const isRtl = 'rtl' === useTheme().direction;
40
+ const Icon = getSectionIcon( sectionId );
41
+
42
+ return (
43
+ <Stack direction="row" alignItems="center" spacing={ 0.5 } sx={ { minWidth: 0, flex: 1 } }>
44
+ <IconButton
45
+ size="small"
46
+ aria-label={ __( 'Back', 'elementor' ) }
47
+ onClick={ onBack }
48
+ sx={ { flexShrink: 0 } }
49
+ >
50
+ <Rotate in={ isRtl }>
51
+ <ArrowLeftIcon fontSize="small" />
52
+ </Rotate>
53
+ </IconButton>
54
+ <Icon fontSize="small" color="action" />
55
+ <Typography variant="subtitle2" fontWeight="bold" noWrap sx={ { flex: 1, minWidth: 0 } }>
56
+ { getSectionTitle( sectionId ) }
57
+ </Typography>
58
+ </Stack>
59
+ );
60
+ }
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { createContext, type ReactNode, useCallback, useContext, useMemo, useRef } from 'react';
2
+ import { createContext, type ReactNode, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
3
3
 
4
4
  import { type Slug } from '../api/post';
5
5
 
@@ -11,6 +11,7 @@ type SectionHandlers = {
11
11
  };
12
12
 
13
13
  type ContentPanelCoordinatorValue = {
14
+ clearPendingCreate: () => void;
14
15
  registerSection: ( type: ContentPanelSectionId, handlers: SectionHandlers ) => void;
15
16
  requestCreate: ( type: ContentPanelSectionId ) => void;
16
17
  unregisterSection: ( type: ContentPanelSectionId ) => void;
@@ -18,35 +19,88 @@ type ContentPanelCoordinatorValue = {
18
19
 
19
20
  const ContentPanelCoordinatorContext = createContext< ContentPanelCoordinatorValue | null >( null );
20
21
 
21
- export function ContentPanelCoordinatorProvider( { children }: { children: ReactNode } ) {
22
+ type ContentPanelCoordinatorProviderProps = {
23
+ children: ReactNode;
24
+ clearPendingCreateRef?: React.MutableRefObject< ( () => void ) | undefined >;
25
+ onNavigateToSection?: ( type: ContentPanelSectionId ) => void;
26
+ };
27
+
28
+ export function ContentPanelCoordinatorProvider( {
29
+ children,
30
+ clearPendingCreateRef,
31
+ onNavigateToSection,
32
+ }: ContentPanelCoordinatorProviderProps ) {
33
+ const pendingCreateSectionRef = useRef< ContentPanelSectionId | null >( null );
22
34
  const sectionsRef = useRef( new Map< ContentPanelSectionId, SectionHandlers >() );
23
35
 
24
- const registerSection = useCallback( ( type: ContentPanelSectionId, handlers: SectionHandlers ) => {
25
- sectionsRef.current.set( type, handlers );
36
+ const clearPendingCreate = useCallback( () => {
37
+ pendingCreateSectionRef.current = null;
26
38
  }, [] );
27
39
 
28
- const unregisterSection = useCallback( ( type: ContentPanelSectionId ) => {
29
- sectionsRef.current.delete( type );
30
- }, [] );
40
+ useEffect( () => {
41
+ if ( ! clearPendingCreateRef ) {
42
+ return;
43
+ }
44
+
45
+ clearPendingCreateRef.current = clearPendingCreate;
31
46
 
32
- const requestCreate = useCallback( ( type: ContentPanelSectionId ) => {
33
- const handlers = sectionsRef.current.get( type );
47
+ return () => {
48
+ clearPendingCreateRef.current = undefined;
49
+ };
50
+ }, [ clearPendingCreate, clearPendingCreateRef ] );
34
51
 
35
- if ( ! handlers ) {
52
+ const runPendingCreate = useCallback( ( type: ContentPanelSectionId, handlers: SectionHandlers ) => {
53
+ if ( pendingCreateSectionRef.current !== type ) {
36
54
  return;
37
55
  }
38
56
 
57
+ pendingCreateSectionRef.current = null;
39
58
  handlers.setExpanded( true );
40
59
  handlers.startCreate();
41
60
  }, [] );
42
61
 
62
+ const registerSection = useCallback(
63
+ ( type: ContentPanelSectionId, handlers: SectionHandlers ) => {
64
+ sectionsRef.current.set( type, handlers );
65
+ runPendingCreate( type, handlers );
66
+ },
67
+ [ runPendingCreate ]
68
+ );
69
+
70
+ const unregisterSection = useCallback( ( type: ContentPanelSectionId ) => {
71
+ sectionsRef.current.delete( type );
72
+
73
+ if ( pendingCreateSectionRef.current === type ) {
74
+ pendingCreateSectionRef.current = null;
75
+ }
76
+ }, [] );
77
+
78
+ const requestCreate = useCallback(
79
+ ( type: ContentPanelSectionId ) => {
80
+ onNavigateToSection?.( type );
81
+
82
+ const handlers = sectionsRef.current.get( type );
83
+
84
+ if ( handlers ) {
85
+ handlers.setExpanded( true );
86
+ handlers.startCreate();
87
+ pendingCreateSectionRef.current = null;
88
+ return;
89
+ }
90
+
91
+ pendingCreateSectionRef.current = type;
92
+ },
93
+ [ onNavigateToSection ]
94
+ );
95
+
43
96
  const value = useMemo(
44
97
  () => ( {
98
+ clearPendingCreate,
45
99
  registerSection,
46
100
  requestCreate,
47
101
  unregisterSection,
48
102
  } ),
49
- [ registerSection, requestCreate, unregisterSection ]
103
+ [ clearPendingCreate, registerSection, requestCreate, unregisterSection ]
50
104
  );
51
105
 
52
106
  return (
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export { extendIconsMap } from './icons-map';
2
2
 
3
3
  export { init } from './init';
4
4
  export {
5
+ getSiteContentSectionById,
5
6
  getSiteContentSections,
6
7
  registerSiteContentSection,
7
8
  resetSiteContentSections,
@@ -61,6 +62,14 @@ export {
61
62
  } from './components/panel/popups/popup-section-add-button';
62
63
  export { POPUPS_SITE_CONTENT_SECTION_ID } from './popups/constants';
63
64
  export { env, type PopupsSiteContentEnv } from './env';
65
+ export {
66
+ getActiveSiteContentView,
67
+ getInitialSiteContentView,
68
+ notifySiteContentViewChange,
69
+ setPendingSiteContentHome,
70
+ setPendingSiteContentSection,
71
+ type SiteContentView,
72
+ } from './site-content-view';
64
73
  export {
65
74
  injectIntoSiteContentListItemActions,
66
75
  SiteContentListItemActionsSlot,
@@ -1,10 +1,14 @@
1
1
  import type { ReactNode } from 'react';
2
2
 
3
3
  export type SiteContentSection = {
4
+ description?: string;
4
5
  id: string;
5
6
  label?: string;
6
7
  priority?: number;
7
8
  render: () => ReactNode;
9
+ renderHubCount?: () => ReactNode;
10
+ usesDrillInToolbarShell?: boolean;
11
+ wrapDrillInPanel?: ( panel: ReactNode ) => ReactNode;
8
12
  };
9
13
 
10
14
  const registeredSections = new Map< string, SiteContentSection >();
@@ -15,6 +19,10 @@ export function registerSiteContentSection( section: SiteContentSection ) {
15
19
  registeredSections.set( section.id, section );
16
20
  }
17
21
 
22
+ export function getSiteContentSectionById( sectionId: string ): SiteContentSection | undefined {
23
+ return registeredSections.get( sectionId );
24
+ }
25
+
18
26
  export function getSiteContentSections(): SiteContentSection[] {
19
27
  return Array.from( registeredSections.values() ).sort(
20
28
  ( first, second ) =>
@@ -0,0 +1,47 @@
1
+ import { type ContentPanelSectionId } from './contexts/content-panel-coordinator';
2
+
3
+ export type SiteContentView = ContentPanelSectionId | null;
4
+
5
+ let pendingOpenView: SiteContentView | undefined;
6
+
7
+ let activeViewInMemory: SiteContentView = null;
8
+
9
+ export function setPendingSiteContentSection( sectionId: ContentPanelSectionId ): void {
10
+ pendingOpenView = sectionId;
11
+ }
12
+
13
+ export function setPendingSiteContentHome(): void {
14
+ pendingOpenView = null;
15
+ }
16
+
17
+ function consumePendingOpen(): SiteContentView | undefined {
18
+ if ( pendingOpenView === undefined ) {
19
+ return undefined;
20
+ }
21
+ const view = pendingOpenView;
22
+ pendingOpenView = undefined;
23
+ activeViewInMemory = view;
24
+ return view;
25
+ }
26
+
27
+ export function getInitialSiteContentView(): SiteContentView {
28
+ const pending = consumePendingOpen();
29
+ if ( pending !== undefined ) {
30
+ return pending;
31
+ }
32
+ activeViewInMemory = null;
33
+ return null;
34
+ }
35
+
36
+ export function notifySiteContentViewChange( view: SiteContentView ): void {
37
+ activeViewInMemory = view;
38
+ }
39
+
40
+ export function getActiveSiteContentView(): SiteContentView {
41
+ return activeViewInMemory;
42
+ }
43
+
44
+ export function resetSiteContentViewStateForTests(): void {
45
+ pendingOpenView = undefined;
46
+ activeViewInMemory = null;
47
+ }
@@ -0,0 +1,10 @@
1
+ import { type KeyboardEvent } from 'react';
2
+
3
+ export function onKeyboardClick( callback: () => void ) {
4
+ return ( event: KeyboardEvent ) => {
5
+ if ( event.key === 'Enter' || event.key === ' ' ) {
6
+ event.preventDefault();
7
+ callback();
8
+ }
9
+ };
10
+ }