import { URLInput } from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import './editor.scss';
const blockAttributes = {
prev: {
type: 'string',
source: 'attribute',
selector: 'a:first-child',
attribute: 'href',
},
prevText: {
type: 'string',
source: 'attribute',
selector: 'a:first-child',
attribute: 'title',
},
next: {
type: 'string',
source: 'attribute',
selector: 'a:last-child',
attribute: 'href',
},
nextText: {
type: 'string',
source: 'attribute',
selector: 'a:last-child',
attribute: 'title',
},
};
const clampString = ( s, maxLength, tolerance = 4 ) => {
const codePoints = [ ...s ];
// add some hysteresis to prevent awkward cutoffs
const trimmed =
s.length > maxLength + tolerance ? [ ...codePoints.slice( 0, maxLength ), '…' ] : codePoints;
return trimmed.join( '' );
};
const save = ( { attributes: { prev, prevText, next, nextText }, className, isEditor } ) => {
if ( prev || next ) {
const prevTitle = clampString( prevText || '', 28 );
const nextTitle = clampString( nextText || '', 28 );
return (
);
}
return ;
};
const edit = ( { attributes, className, isSelected, setAttributes } ) => {
if ( isSelected ) {
return (
setAttributes( { prev: url, prevText: post?.title || 'Prev' } )
}
/>
setAttributes( { next: url, nextText: post?.title || 'Next' } )
}
/>
);
}
if ( attributes.prev || attributes.next ) {
return save( { attributes, className, isEditor: true } );
}
return (
← { __( 'Add prev/next links to related posts in a series.' ) } →
);
};
const save_v1 = ( { attributes: { prev, next }, className, isEditor } ) =>
prev || next ? (
) : (
);
const deprecations = [
{
attributes: {
prev: {
type: 'string',
source: 'attribute',
selector: 'a:first-child',
attribute: 'href',
},
next: {
type: 'string',
source: 'attribute',
selector: 'a:last-child',
attribute: 'href',
},
},
migrate: ( { prev, next } ) => ( {
prev,
prevText: 'Prev',
next,
nextText: 'Next',
} ),
save: save_v1,
},
];
registerBlockType( 'a8c/prev-next', {
title: __( 'Prev/Next Links' ),
icon: 'leftright',
category: 'a8c',
description: __( 'Link this post to sequential posts in a series of related posts.' ),
keywords: [ __( 'links' ) ],
attributes: blockAttributes,
edit,
save,
deprecated: deprecations,
} );