File size: 2,308 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
import { RichText } from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { InnerBlocks } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { transforms } from './transform';

const blockAttributes = {
	summary: {
		type: 'string',
		source: 'html',
		selector: 'summary',
	},
};

const save = ( { attributes: { summary } }, className ) => {
	return (
		<details className={ className }>
			<RichText.Content tagName="summary" value={ summary || 'Click to show/hide' } />
			<div style={ { marginTop: '1em' } }>
				<InnerBlocks.Content />
			</div>
		</details>
	);
};

const edit = ( { attributes: { summary }, className, isSelected, setAttributes } ) => {
	return (
		<div className={ className }>
			{ isSelected || ! summary ? (
				<RichText
					tagName="heading"
					placeholder="Enter a preview or description of what's hidden inside"
					keepPlaceholderOnFocus
					value={ summary }
					onChange={ ( newSummary ) => setAttributes( { summary: newSummary } ) }
				/>
			) : (
				<RichText.Content tagName="heading" value={ summary } />
			) }
			<hr />
			<InnerBlocks />
		</div>
	);
};

///////////////////////////////////////
// Deprecations
///////////////////////////////////////

const blockAttributes_v1 = {
	hideId: {
		type: 'string',
	},
	showId: {
		type: 'string',
	},
};

const migrate_v1 = () => ( {} );

const save_v1 = ( { attributes: { hideId, showId }, className } ) => {
	return (
		<div className={ className }>
			<a
				href={ `#spoiler_block_hide_${ hideId }` }
				className="hide btn"
				id={ `spoiler_block_hide_${ hideId }` }
			>
				Reveal hidden content
			</a>
			<a
				href={ `#spoiler_block_show_${ showId }` }
				className="show btn"
				id={ `spoiler_block_show_${ showId }` }
			>
				Hide content
			</a>
			<div className="spoiler">
				<InnerBlocks.Content />
			</div>
		</div>
	);
};

registerBlockType( 'a8c/spoiler', {
	title: __( 'Spoiler!' ),
	icon: 'warning',
	category: 'a8c',
	description: __( 'Hide content until the reader wants to see it.' ),
	keywords: [ __( 'spoiler' ), __( 'accordion' ), __( 'Dropdown' ) ],
	attributes: blockAttributes,
	edit,
	save,
	transforms,
	deprecated: [
		{
			attributes: blockAttributes_v1,
			migrate: migrate_v1,
			save: save_v1,
		},
	],
} );