-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
99 lines (88 loc) · 2.48 KB
/
index.js
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
/**
* WordPress dependencies
*/
import { RawHTML } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Disabled, SandBox } from '@wordpress/components';
import { getPhrasingContentSchema } from '@wordpress/blocks';
import { BlockControls, PlainText } from '@wordpress/editor';
import { withState } from '@wordpress/compose';
export const name = 'core/html';
export const settings = {
title: __( 'Custom HTML' ),
description: __( 'Add your own HTML (and view it right here as you edit!).' ),
icon: <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M4.5,11h-2V9H1v6h1.5v-2.5h2V15H6V9H4.5V11z M7,10.5h1.5V15H10v-4.5h1.5V9H7V10.5z M14.5,10l-1-1H12v6h1.5v-3.9 l1,1l1-1V15H17V9h-1.5L14.5,10z M19.5,13.5V9H18v6h5v-1.5H19.5z" /></svg>,
category: 'formatting',
keywords: [ __( 'embed' ) ],
supports: {
customClassName: false,
className: false,
html: false,
},
attributes: {
content: {
type: 'string',
source: 'html',
},
},
transforms: {
from: [
{
type: 'raw',
isMatch: ( node ) => node.nodeName === 'FIGURE' && !! node.querySelector( 'iframe' ),
schema: {
figure: {
require: [ 'iframe' ],
children: {
iframe: {
attributes: [ 'src', 'allowfullscreen', 'height', 'width' ],
},
figcaption: {
children: getPhrasingContentSchema(),
},
},
},
},
},
],
},
edit: withState( {
isPreview: false,
} )( ( { attributes, setAttributes, setState, isPreview } ) => (
<div className="wp-block-html">
<BlockControls>
<div className="components-toolbar">
<button
className={ `components-tab-button ${ ! isPreview ? 'is-active' : '' }` }
onClick={ () => setState( { isPreview: false } ) }
>
<span>HTML</span>
</button>
<button
className={ `components-tab-button ${ isPreview ? 'is-active' : '' }` }
onClick={ () => setState( { isPreview: true } ) }
>
<span>{ __( 'Preview' ) }</span>
</button>
</div>
</BlockControls>
<Disabled.Consumer>
{ ( isDisabled ) => (
( isPreview || isDisabled ) ? (
<SandBox html={ attributes.content } />
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
)
) }
</Disabled.Consumer>
</div>
) ),
save( { attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
},
};