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