File size: 3,592 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 (
			<div className={ isEditor ? className : '' }>
				{ prev ? (
					<a href={ prev } title={ prevText } disabled={ isEditor }>
						← { prevTitle }
					</a>
				) : (
					<span> </span>
				) }
				{ next ? (
					<a
						href={ next }
						title={ nextText }
						disabled={ isEditor }
						style={ prev ? { float: 'right' } : {} }
					>
						{ nextTitle } →
					</a>
				) : (
					<span> </span>
				) }
			</div>
		);
	}

	return <Fragment />;
};

const edit = ( { attributes, className, isSelected, setAttributes } ) => {
	if ( isSelected ) {
		return (
			<Fragment>
				<URLInput
					className="prev-next__link-entry"
					// eslint-disable-next-line jsx-a11y/no-autofocus
					autoFocus
					label="Previous link"
					value={ attributes.prev }
					onChange={ ( url, post ) =>
						setAttributes( { prev: url, prevText: post?.title || 'Prev' } )
					}
				/>
				<URLInput
					className="prev-next__link-entry"
					label="Next link"
					value={ attributes.next }
					onChange={ ( url, post ) =>
						setAttributes( { next: url, nextText: post?.title || 'Next' } )
					}
				/>
			</Fragment>
		);
	}

	if ( attributes.prev || attributes.next ) {
		return save( { attributes, className, isEditor: true } );
	}

	return (
		<div style={ { textAlign: 'center' } }>
			← { __( 'Add prev/next links to related posts in a series.' ) } →
		</div>
	);
};

const save_v1 = ( { attributes: { prev, next }, className, isEditor } ) =>
	prev || next ? (
		<div className={ isEditor ? className : '' }>
			{ prev ? <a href={ prev }>← Prev</a> : <span> </span> }
			{ next ? <a href={ next }>Next →</a> : <span> </span> }
		</div>
	) : (
		<Fragment />
	);

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,
} );