|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+<template lang="pug">
|
|
|
2
|
+.block.recursive(:id="`block-${this.$.uid}`")
|
|
|
3
|
+</template>
|
|
|
4
|
+
|
|
|
5
|
+<script>
|
|
|
6
|
+export default {
|
|
|
7
|
+ name: 'rblock',
|
|
|
8
|
+ props: {
|
|
|
9
|
+ block: { required: true }
|
|
|
10
|
+ },
|
|
|
11
|
+ data() {
|
|
|
12
|
+ return {
|
|
|
13
|
+ id: null,
|
|
|
14
|
+ root: null,
|
|
|
15
|
+ treeDepth: 0,
|
|
|
16
|
+ currentParentNode: null,
|
|
|
17
|
+ prevDepth: 0,
|
|
|
18
|
+ ancestors: []
|
|
|
19
|
+ }
|
|
|
20
|
+ },
|
|
|
21
|
+ methods: {
|
|
|
22
|
+ toEl(html) {
|
|
|
23
|
+ var template = document.createElement('template')
|
|
|
24
|
+ html = html.trim() // Never return a text node of whitespace as the result
|
|
|
25
|
+ template.innerHTML = html
|
|
|
26
|
+ return template.content.firstChild
|
|
|
27
|
+ },
|
|
|
28
|
+ processBlock(block, root) {
|
|
|
29
|
+ this.treeDepth++
|
|
|
30
|
+
|
|
|
31
|
+ if(this.treeDepth === 1) {
|
|
|
32
|
+ // Add the base nodes
|
|
|
33
|
+ // Don't forget that this is recursive (see line: 49)
|
|
|
34
|
+ this.currentParentNode = this.toEl(block.innerHTML)
|
|
|
35
|
+ this.ancestors.push(this.currentParentNode)
|
|
|
36
|
+ root.appendChild(this.currentParentNode)
|
|
|
37
|
+ } else {
|
|
|
38
|
+
|
|
|
39
|
+ // You need to create a REFERENCE to this node
|
|
|
40
|
+ // So you can target it later as a parent
|
|
|
41
|
+ // If you don't create a reference then you'll
|
|
|
42
|
+ // be creating a new <el> and will lose all
|
|
|
43
|
+ // parent-child relationship
|
|
|
44
|
+ if(this.treeDepth > this.prevDepth) {
|
|
|
45
|
+ this.ancestors.push(this.currentParentNode)
|
|
|
46
|
+ } else {
|
|
|
47
|
+ this.ancestors.pop()
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ this.currentParentNode = this.toEl(block.innerHTML)
|
|
|
51
|
+ this.ancestors[this.ancestors.length - 1].appendChild(this.currentParentNode)
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ if(this.currentParentNode.className === 'wp-block-columns') {
|
|
|
55
|
+ this.currentParentNode.classList.add('f-row')
|
|
|
56
|
+ this.currentParentNode.classList.add('start')
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ if(!block.innerBlocks) return
|
|
|
60
|
+
|
|
|
61
|
+ this.prevDepth = this.treeDepth
|
|
|
62
|
+
|
|
|
63
|
+ // !: Process any children recursively
|
|
|
64
|
+ for(let childBlock of block.innerBlocks) {
|
|
|
65
|
+ this.processBlock(childBlock, this.currentParentNode)
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ this.treeDepth--
|
|
|
69
|
+ },
|
|
|
70
|
+ },
|
|
|
71
|
+ mounted() {
|
|
|
72
|
+ if(typeof this.block !== 'object' || this.block.hasOwnProperty('gallery')) return
|
|
|
73
|
+
|
|
|
74
|
+ // Add the top-level blocks to the document tree
|
|
|
75
|
+ const tree = new DocumentFragment()
|
|
|
76
|
+ this.root = document.createElement('div')
|
|
|
77
|
+ tree.appendChild(this.root)
|
|
|
78
|
+
|
|
|
79
|
+ // Loop over all the nodes and append children
|
|
|
80
|
+ for(const block of this.block) {
|
|
|
81
|
+ this.processBlock(block, this.root)
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ // Append the final product to DOM
|
|
|
85
|
+ document.getElementById(`block-${this.$.uid}`).appendChild(tree)
|
|
|
86
|
+
|
|
|
87
|
+ }
|
|
|
88
|
+}
|
|
|
89
|
+</script>
|
|
|
90
|
+
|