import classnames from 'classnames/dedupe'; import { debounce } from 'throttle-debounce'; import apiFetch from '@wordpress/api-fetch'; import { BaseControl, Button, ButtonGroup, TextControl, } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; import { RawHTML, useState } from '@wordpress/element'; import { addFilter } from '@wordpress/hooks'; import { __, sprintf } from '@wordpress/i18n'; import Notice from '../components/notice'; import controlGetValue from '../utils/control-get-value'; const { VPGutenbergVariables } = window; const NOTICE_LIMIT = parseInt( VPGutenbergVariables.items_count_notice_limit, 10 ); const DISPLAY_NOTICE_AFTER = NOTICE_LIMIT + 5; function getNoticeState() { return VPGutenbergVariables.items_count_notice; } const maybeUpdateNoticeStateMeta = debounce(3000, (postId) => { apiFetch({ path: '/visual-portfolio/v1/update_gallery_items_count_notice_state', method: 'POST', data: { notice_state: getNoticeState(), post_id: postId, }, }); }); function updateNoticeState(postId) { const newState = getNoticeState() === 'hide' ? 'show' : 'hide'; VPGutenbergVariables.items_count_notice = newState; maybeUpdateNoticeStateMeta(postId); } function CountNotice(props) { const { onToggle, postId } = props; return ( decrease page loading speed. We recommend you add these improvements:', 'visual-portfolio' ), }} /> less than %d', 'visual-portfolio' ), NOTICE_LIMIT ), }} /> `Load More` or `Infinite Scroll` pagination for best results.', 'visual-portfolio' ), }} /> { updateNoticeState(postId); onToggle(); }} > {__('Ok, I understand', 'visual-portfolio')} ); } function shouldDisplayNotice(count, attributes) { let display = false; // When selected images number is lower, then needed, don't display notice, even is count is large. if (attributes.content_source === 'images') { display = attributes?.images?.length > DISPLAY_NOTICE_AFTER && (count > DISPLAY_NOTICE_AFTER || count === -1); } else { display = count > DISPLAY_NOTICE_AFTER || count === -1; } return display; } function ItemsCountControl({ data }) { const { description, attributes, onChange } = data; const [maybeReRender, setMaybeReRender] = useState(1); const { postId } = useSelect( (select) => ({ postId: select('core/editor')?.getCurrentPostId() || false, }), [] ); const renderControlHelp = description ? ( {description} ) : ( false ); const renderControlClassName = classnames( 'vpf-control-wrap', `vpf-control-wrap-${data.type}` ); const controlVal = parseInt(controlGetValue(data.name, attributes), 10); return ( {data.label} {getNoticeState() === 'hide' && shouldDisplayNotice(controlVal, attributes) ? ( { updateNoticeState(postId); setMaybeReRender(maybeReRender + 1); }} isSmall style={{ position: 'absolute', marginTop: '-5px', padding: '0 4px', color: '#cd7a0f', }} > ) : null} > } help={renderControlHelp} className={renderControlClassName} > { if (controlVal === -1) { onChange(parseFloat(data.default || 6)); } }} > {__('Custom Count', 'visual-portfolio')} { if ( controlVal !== -1 && // eslint-disable-next-line no-alert window.confirm( __( 'Be careful, the output of all your items can adversely affect the performance of your site, this option may be helpful for image galleries.', 'visual-portfolio' ) ) ) { onChange(-1); } }} > {__('All Items', 'visual-portfolio')} {controlVal !== -1 ? ( <> onChange(parseFloat(val))} /> > ) : null} {getNoticeState() === 'show' && shouldDisplayNotice(controlVal, attributes) ? ( { setMaybeReRender(maybeReRender + 1); }} /> ) : null} ); } // Items count with "All Items" button. addFilter( 'vpf.editor.controls-render', 'vpf/editor/controls-render/customize-controls', (render, data) => { if (data.name !== 'items_count') { return render; } return ( ); } );
decrease page loading speed. We recommend you add these improvements:', 'visual-portfolio' ), }} />
{ updateNoticeState(postId); onToggle(); }} > {__('Ok, I understand', 'visual-portfolio')}