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' ), }} />

  1. less than %d', 'visual-portfolio' ), NOTICE_LIMIT ), }} />
  2. `Load More` or `Infinite Scroll` pagination for best results.', '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) ? ( ) : null} } help={renderControlHelp} className={renderControlClassName} >
{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 ( ); } );