Parcourir la source

:sparkles: recursively dealing with content blocks

undefined
J il y a 5 ans
Parent
révision
08ba0314b7

+ 0
- 29
vue-theme/src/components/block.vue Voir le fichier

@@ -1,29 +0,0 @@
1
-<template lang="pug">
2
-.block
3
-    p block component
4
-    p {{ block }}
5
-</template>
6
-
7
-<script>
8
-
9
-export default {
10
-    props: {
11
-        block: { required: true }
12
-    },
13
-    data() {
14
-        return {
15
-        }
16
-    },
17
-    computed: {
18
-    },
19
-    methods: {
20
-    },
21
-    created() {
22
-  
23
-    }
24
-}
25
-</script>
26
-
27
-<style lang="postcss">
28
-.block
29
-</style>

+ 90
- 0
vue-theme/src/components/content-block/block-recursive.vue Voir le fichier

@@ -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
+

+ 30
- 0
vue-theme/src/components/content-block/block.vue Voir le fichier

@@ -0,0 +1,30 @@
1
+<template lang="pug">
2
+.block.gallery(v-if="block.hasOwnProperty('gallery')")
3
+    p place-holder gallery {{ block.gallery }}
4
+
5
+rblock(v-else-if="typeof block === 'object'" :block="block")
6
+        
7
+.block.single(v-else)
8
+    .single--content(v-html="block")
9
+</template>
10
+
11
+<script>
12
+import rblock from './block-recursive'
13
+
14
+export default {
15
+    components: { rblock },
16
+    props: {
17
+        block: { required: true }
18
+    },
19
+    data() {
20
+        return {
21
+        }
22
+    },
23
+    computed: {
24
+    }
25
+}
26
+</script>
27
+
28
+<style lang="postcss">
29
+.block
30
+</style>

+ 2
- 9
vue-theme/src/pages/single.vue Voir le fichier

@@ -9,14 +9,7 @@
9 9
                 p start: {{ dateFrom(post.start) }}
10 10
                 p end: {{ dateFrom(post.end) }}
11 11
 
12
-
13
-        .post-single.block-wrapper(v-for="(block, index) in post.blocks" :key="`block-${index}`")
14
-            block(:block="block")
15
-            p(v-if="block.hasOwnProperty('gallery')") {{ block }}
16
-            p(v-else-if="block.length && typeof block === 'object'" v-for="inner in block") {{ inner }}
17
-            p(v-else) single {{ block }}
18
-            //- Just a regular block (html or img)
19
-            //- .block(v-else v-html="block")
12
+        block.post-single.block-wrapper(v-for="(block, index) in post.blocks" :block="block" :key="`block-${index}`")
20 13
 
21 14
     sidebar(v-if="sidebar" :type="`${type}`")
22 15
         .shadow
@@ -28,7 +21,7 @@
28 21
 <script>
29 22
 import sidebar from '@/components/sidebars/sidebar'
30 23
 import gallery from '@/components/gallery/'
31
-import block from '@/components/block'
24
+import block from '@/components/content-block/block'
32 25
 
33 26
 import { postTypeGetters } from './mixin-post-types'
34 27
 

Chargement…
Annuler
Enregistrer