-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
70 lines (65 loc) · 1.58 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
/**
* External dependencies
*/
import { registerBlock } from 'wp-blocks';
import { EditorParagraphIcon } from 'dashicons';
/**
* Internal dependencies
*/
import form from './form';
import { mergeInlineTextBlocks } from 'utils/state';
const createTextBlockWithContent = ( content = '' ) => {
return {
blockType: 'text',
align: 'no-align',
content
};
};
registerBlock( 'text', {
title: 'Text',
form: form,
icon: EditorParagraphIcon,
parse: ( rawBlock ) => {
const div = document.createElement( 'div' );
div.innerHTML = rawBlock.rawContent;
if (
div.childNodes.length !== 1 ||
div.firstChild.nodeName !== 'P'
) {
return false;
}
return {
blockType: 'text',
align: rawBlock.attrs.align || 'no-align',
content: div.firstChild.innerHTML,
};
},
serialize: ( block ) => {
const div = document.createElement( 'div' );
div.innerHTML = block.content;
// Should probably be handled in the form
const content = div.childNodes.length === 1 && div.firstChild.nodeName === 'P'
? div.firstChild.innerHTML
: block.content;
const alignStyle = block.align && block.align !== 'no-align'
? ` style="text-align: ${ block.align };"`
: '';
const rawContent = `<p${ alignStyle }>${ content }</p>`;
return {
blockType: 'text',
attrs: { align: block.align },
rawContent
};
},
create: createTextBlockWithContent,
transformations: [
{
blocks: [ 'heading', 'quote' ],
transform: ( block ) => createTextBlockWithContent( block.content )
}
],
merge: [ {
blocks: [ 'text', 'quote', 'heading' ],
merge: mergeInlineTextBlocks
} ]
} );