-
Notifications
You must be signed in to change notification settings - Fork 329
/
Child.tsx
330 lines (321 loc) · 11.1 KB
/
Child.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import React from 'react'
import { Element } from '@craftjs/core'
import { useNode } from '@craftjs/core'
import { Text } from './Text'
import { Link } from './Link'
import { Button } from './Button'
import { Image } from './Image'
import { Svg } from './Svg'
import { RootProps } from '../../../types'
interface ChildProps {
root: RootProps
d?: number[]
}
const Child: React.FC<ChildProps> = ({ root, d = [0] }) => {
if (!root || root?.childNodes.length === 0) return null
return (
<>
{Array.from(root?.childNodes).map((r, i) => {
const key = d.concat(i).join('')
if (r.nodeType === 1) {
if (r.tagName === 'DIV')
return (
<div className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</div>
)
else if (r.tagName === 'H1')
return (
<h1 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h1>
)
else if (r.tagName === 'SECTION')
return (
<section className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</section>
)
else if (r.tagName === 'H2')
return (
<h2 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h2>
)
else if (r.tagName === 'H3')
return (
<h3 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h3>
)
else if (r.tagName === 'H4')
return (
<h4 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h4>
)
else if (r.tagName === 'H5')
return (
<h5 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h5>
)
else if (r.tagName === 'H6')
return (
<h6 className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</h6>
)
else if (r.tagName === 'P')
return (
<p className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</p>
)
else if (r.tagName === 'A')
return <Element is={Link} key={key} r={r} d={d} i={i} id={key} propId={key} />
else if (r.tagName === 'SPAN')
return (
<span className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</span>
)
else if (r.tagName === 'STRONG')
return (
<strong className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</strong>
)
else if (r.tagName === 'EM')
return (
<em className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</em>
)
else if (r.tagName === 'HEADER')
return (
<header className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</header>
)
else if (r.tagName === 'MAIN')
return (
<main className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</main>
)
else if (r.tagName === 'FOOTER')
return (
<footer className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</footer>
)
else if (r.tagName === 'NAV')
return (
<nav className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</nav>
)
else if (r.tagName === 'ASIDE')
return (
<aside className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</aside>
)
else if (r.tagName === 'DETAILS')
return (
<details className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</details>
)
else if (r.tagName === 'SUMMARY')
return (
<summary className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</summary>
)
else if (r.tagName === 'BLOCKQUOTE')
return (
<blockquote className={r.classNames} key={key} {...r.attrs}>
<Child root={r} d={d.concat(i)} />
</blockquote>
)
else if (r.tagName === 'INPUT')
return <input className={r.classNames} key={key} {...r.attrs} />
else if (r.tagName === 'LABEL')
return (
<label className={r.classNames} key={key} {...r.attrs}>
{r.innerText}
</label>
)
else if (r.tagName === 'TEXTAREA')
return (
<textarea
defaultValue={r.innerText}
className={r.classNames}
key={key}
{...r.attrs}
/>
)
else if (r.tagName === 'BUTTON')
return <Element is={Button} key={key} r={r} d={d} i={i} id={key} propId={key} />
else if (r.tagName === 'FORM')
return (
<form className={r.classNames} key={key} {...r.attrs}>
<Child root={r} d={d.concat(i)} />
</form>
)
else if (r.tagName === 'SVG')
// if (root.tagName === 'A') return <Svg key={key} r={r} propId={key} />
// else return <Element is={Svg} key={key} r={r} id={key} propId={key} />
return <Element is={Svg} key={key} r={r} id={key} propId={key} />
else if (r.tagName === 'ADDRESS')
return (
<address className={r.classNames} key={key} {...r.attrs}>
<Text className={''} text={r.innerText} key={key} id={key} />
</address>
)
else if (r.tagName === 'FIGURE')
return (
<figure className={r.classNames} key={key} {...r.attrs}>
<Child root={r} d={d.concat(i)} />
</figure>
)
else if (r.tagName === 'IMG') {
return (
<Element
is={Image}
key={key}
d={d}
i={i}
classNames={r.classNames}
attrs={r.attrs}
id={key}
propId={key}
/>
)
} else if (r.tagName === 'ARTICLE') {
return (
<article className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</article>
)
} else if (r.tagName === 'DL') {
return (
<article className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</article>
)
} else if (r.tagName === 'DD') {
return (
<article className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</article>
)
} else if (r.tagName === 'DT') {
return (
<article className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</article>
)
} else if (r.tagName === 'SCRIPT') {
return null
} else if (r.tagName === 'LINK') {
return <link className={r.classNames} {...r.attrs} key={key}></link>
} else if (r.tagName === 'BR') {
return <br className={r.classNames} key={key} />
} else if (r.tagName === 'UL') {
return (
<ul className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</ul>
)
} else if (r.tagName === 'LI') {
return (
<li className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</li>
)
} else if (r.tagName === 'CITE') {
return (
<cite className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</cite>
)
} else if (r.tagName === 'HR') {
return <hr className={r.classNames} key={key}></hr>
} else if (r.tagName === 'IFRAME') {
return <iframe className={r.classNames} {...r.attrs} key={key} />
} else if (r.tagName === 'STYLE') {
return <style key={key}>{r.innerText}</style>
} else if (r.tagName === 'TABLE') {
return (
<table className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</table>
)
} else if (r.tagName === 'THEAD') {
return (
<thead className={r.classNames} {...r.attrs} key={key}>
<Child root={r} d={d.concat(i)} />
</thead>
)
} else if (r.tagName === 'TBODY') {
return (
<tbody className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</tbody>
)
} else if (r.tagName === 'TR') {
return (
<tr className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</tr>
)
} else if (r.tagName === 'TD') {
return (
<td className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</td>
)
} else if (r.tagName === 'TH') {
return (
<th className={r.classNames} key={key}>
<Child root={r} d={d.concat(i)} />
</th>
)
} else if (r.tagName === 'FIGCAPTION') {
return (
<figcaption className={r.classNames}>
<Child root={r} d={d.concat(i)} />
</figcaption>
)
} else {
return <p key={key}>Unknown container</p>
}
} else if (r.nodeType === 3) {
if (r.innerText.trim() === '') return null
// className={r.parentNode.classNames}
if (r.constructor === 'TextNode' || r.constructor === 't')
return <Text className={r.classNames} text={r.innerText ?? ''} key={key} id={key} />
else return <p key={key}>Unknown node</p>
} else {
return <p key={key}>Unknown type</p>
}
})}
</>
)
}
export default Child
interface ComponentProps {
root: RootProps
}
const Component: React.FC<ComponentProps> = ({ root }) => {
const { connectors, node } = useNode((node) => ({ node }))
return (
<div id={node.data.props.id} ref={(ref) => connectors.connect(ref as HTMLDivElement)}>
<Child root={root} />
</div>
)
}
export { Component }