-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtransforms.js
More file actions
155 lines (146 loc) · 3.81 KB
/
Copy pathtransforms.js
File metadata and controls
155 lines (146 loc) · 3.81 KB
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
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks'
import {
create, split, toHTMLString,
} from '@wordpress/rich-text'
/**
* External dependencies
*/
import { settings } from 'stackable'
/**
* Internal dependencies
*/
import { getIconListDefaultAttributes } from './util'
// import { createListBlockFromDOMElement } from './util'
// function getListContentSchema( { phrasingContentSchema } ) {
// const listContentSchema = {
// ...phrasingContentSchema,
// ul: {},
// ol: { attributes: [ 'type', 'start', 'reversed' ] },
// };
// // Recursion is needed.
// // Possible: ul > li > ul.
// // Impossible: ul > ul.
// [ 'ul', 'ol' ].forEach( tag => {
// listContentSchema[ tag ].children = {
// li: {
// children: listContentSchema,
// },
// }
// } )
// return listContentSchema
// }
function getListContentFlat( blocks ) {
return blocks.flatMap( ( {
name, attributes, innerBlocks = [],
} ) => {
if ( name === 'stackable/icon-list-item' ) {
return [ attributes.text, ...getListContentFlat( innerBlocks ) ]
}
return getListContentFlat( innerBlocks )
} )
}
const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph', 'core/heading', 'stackable/text', 'stackable/heading', 'stackable/subtitle' ],
transform: blockAttributes => {
let childBlocks = []
if ( blockAttributes.length > 1 ) {
childBlocks = blockAttributes.map( ( { content, text } ) => {
return createBlock( 'stackable/icon-list-item', { text: content ?? text ?? '' } )
} )
} else if ( blockAttributes.length === 1 ) {
const value = create( {
html: blockAttributes[ 0 ].content ?? blockAttributes[ 0 ].text ?? '',
} )
childBlocks = split( value, '\n' ).map( result => {
return createBlock( 'stackable/icon-list-item', {
text: toHTMLString( { value: result } ),
} )
} )
}
return createBlock(
'stackable/icon-list',
{
...getIconListDefaultAttributes( settings ),
anchor: blockAttributes.anchor,
},
childBlocks
)
},
},
{
type: 'block',
blocks: [ 'core/list' ],
transform: ( blockAttributes, _childBlocks ) => {
const childBlocks = _childBlocks.map( ( { attributes } ) => {
return createBlock( 'stackable/icon-list-item', { text: attributes.content } )
} )
return createBlock(
'stackable/icon-list',
{
...getIconListDefaultAttributes( settings ),
anchor: blockAttributes.anchor,
},
childBlocks
)
},
},
// Commented out for now to prevent bullet lists from a word doc to be converted to icon lists.
// {
// type: 'raw',
// selector: 'ol,ul',
// schema: args => ( {
// ol: getListContentSchema( args ).ol,
// ul: getListContentSchema( args ).ul,
// } ),
// transform: createListBlockFromDOMElement,
// },
],
to: [
...[ 'core/paragraph', 'core/heading' ].map( block => ( {
type: 'block',
blocks: [ block ],
transform: ( _attributes, childBlocks ) => {
return getListContentFlat( childBlocks ).map( content =>
createBlock( block, {
content,
} )
)
},
} ) ),
...[ 'stackable/text', 'stackable/heading' ].map( block => ( {
type: 'block',
blocks: [ block ],
transform: ( _attributes, childBlocks ) => {
return getListContentFlat( childBlocks ).map( content =>
createBlock( block, {
text: content,
} )
)
},
} ) ),
{
type: 'block',
blocks: [ 'core/list' ],
transform: ( blockAttributes, _childBlocks ) => {
const childBlocks = _childBlocks.map( ( { attributes } ) => {
return createBlock( 'core/list-item', { content: attributes.text } )
} )
return createBlock(
'core/list',
{
anchor: blockAttributes.anchor,
},
childBlocks
)
},
},
],
}
export default transforms