PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / query-page-numbers / edit.js
generateblocks / src / blocks / query-page-numbers Last commit date
components 1 year ago block.json 1 year ago edit.js 1 year ago index.js 1 year ago save.js 1 year ago
edit.js
154 lines
1 import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
2 import { useEffect, useMemo } from '@wordpress/element';
3 import { compose } from '@wordpress/compose';
4 import { __ } from '@wordpress/i18n';
5
6 import { BlockStyles, withUniqueId } from '@edge22/block-styles';
7
8 import { BlockSettings } from './components/BlockSettings';
9 import { withStyles } from '@hoc/withStyles';
10 import { BlockStylesBuilder } from '@components/index';
11 import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js';
12 import { getBlockClasses } from '@utils/getBlockClasses';
13
14 const createPaginationItem = ( content, Tag = 'a', extraClass = '', key = content ) => (
15 <Tag key={ key } className={ `page-numbers ${ extraClass }` }>
16 { content }
17 </Tag>
18 );
19
20 const previewPaginationNumbers = ( midSize, totalPages, currentPage ) => {
21 const paginationItems = [];
22
23 // First set of pagination items (from page 1 to currentPage - midSize).
24 const startPage = Math.max( 1, currentPage - midSize );
25 const endPage = Math.min( currentPage + midSize, totalPages );
26
27 // Add first page if it's not within the range.
28 if ( startPage > 1 ) {
29 paginationItems.push( createPaginationItem( 1, 'a', '', 'page-1' ) );
30 if ( startPage > 2 ) {
31 // Add dots if there's a gap between the first page and the range.
32 paginationItems.push( createPaginationItem( '...', 'span', 'dots', 'dots-start' ) );
33 }
34 }
35
36 // Loop through the pages in the calculated range.
37 for ( let i = startPage; i <= endPage; i++ ) {
38 if ( i === currentPage ) {
39 // Current page
40 paginationItems.push( createPaginationItem( i, 'span', 'current', `current-${ i }` ) );
41 } else {
42 paginationItems.push( createPaginationItem( i, 'a', '', `page-${ i }` ) );
43 }
44 }
45
46 // Add last page if it's not within the range.
47 if ( endPage < totalPages ) {
48 if ( endPage < totalPages - 1 ) {
49 // Add dots if there's a gap between the last page in the range and totalPages.
50 paginationItems.push( createPaginationItem( '...', 'span', 'dots', 'dots-end' ) );
51 }
52 paginationItems.push( createPaginationItem( totalPages, 'a', '', `last-${ totalPages }` ) );
53 }
54
55 return <>{ paginationItems }</>;
56 };
57
58 function EditBlock( props ) {
59 const {
60 attributes,
61 setAttributes,
62 onStyleChange,
63 editorHtmlAttributes,
64 styles,
65 name,
66 } = props;
67
68 const {
69 tagName,
70 midSize,
71 } = attributes;
72
73 const classNames = getBlockClasses(
74 'gb-query-page-numbers',
75 {
76 ...attributes,
77 styles,
78 }
79 );
80
81 useEffect( () => {
82 if ( ! tagName ) {
83 setAttributes( { tagName: 'nav' } );
84 }
85 }, [ tagName ] );
86
87 const blockProps = useBlockProps(
88 {
89 className: classNames.join( ' ' ).trim(),
90 ...editorHtmlAttributes,
91 }
92 );
93
94 const TagName = tagName || 'div';
95
96 const shortcuts = useMemo( () => {
97 const visibleSelectors = [
98 {
99 label: __( 'Main', 'generateblocks' ),
100 value: '',
101 },
102 ];
103
104 return {
105 selectorShortcuts: {
106 default: {
107 label: __( 'Numbers', 'generateblocks' ),
108 items: [
109 { label: __( 'Page Number', 'generateblocks' ), value: '.page-numbers' },
110 { label: __( 'Hovered Page Number', 'generateblocks' ), value: '.page-numbers:is(:hover, :focus)' },
111 { label: __( 'Current Page Number', 'generateblocks' ), value: '.page-numbers.current' },
112 { label: __( 'Dots', 'generateblocks' ), value: '.page-numbers.dots' },
113 ],
114 },
115 },
116 visibleShortcuts: visibleSelectors,
117 };
118 }, [] );
119
120 return (
121 <>
122 <InspectorControls>
123 <BlockStyles
124 settingsTab={ (
125 <BlockSettings
126 { ...props }
127 />
128 ) }
129 stylesTab={ (
130 <BlockStylesBuilder
131 attributes={ attributes }
132 setAttributes={ setAttributes }
133 shortcuts={ shortcuts }
134 onStyleChange={ onStyleChange }
135 name={ name }
136 />
137 ) }
138 />
139 </InspectorControls>
140 <TagName { ...blockProps }>
141 { previewPaginationNumbers( midSize, 6, 1 ) }
142 </TagName>
143 </>
144 );
145 }
146
147 const Edit = compose(
148 withHtmlAttributes,
149 withStyles,
150 withUniqueId
151 )( EditBlock );
152
153 export { Edit };
154