Kaynağa Gözat

creating click handler AFTER next render tick

tags/0.9.0
J 6 yıl önce
ebeveyn
işleme
c908874e8b

+ 4
- 1
vue-theme/src/components/gallery.vue Dosyayı Görüntüle

@@ -19,7 +19,7 @@ export default {
19 19
     },
20 20
     data() { 
21 21
         return {
22
-            selected: 0
22
+            selected: -1
23 23
         }
24 24
     },
25 25
     methods: {
@@ -52,6 +52,9 @@ export default {
52 52
         }
53 53
     },
54 54
     mounted() {
55
+        // Set the first selection
56
+        this.selected = this.fullscreengallery
57
+        
55 58
         window.addEventListener('keydown', this.interpretKeypress)
56 59
     },
57 60
     destroyed() {

+ 23
- 16
vue-theme/src/pages/single.vue Dosyayı Görüntüle

@@ -60,47 +60,54 @@ export default {
60 60
                 postsMap[post.slug] = post
61 61
                 return postsMap
62 62
             }, {})
63
-        },
63
+        }
64 64
     },
65 65
     methods: {
66
-        checkForImages(posts) {
67
-            const allBlocks = posts[this.$route.params.slug].blocks.map(block => {
66
+        pageBlocks(posts) {
67
+            return posts[this.$route.params.slug].blocks.map(block => {
68 68
                 if(block) return block
69 69
             })
70
-            allBlocks.forEach(block => {
70
+        },
71
+        checkForImages(posts) {
72
+            this.pageBlocks(posts).forEach(block => {
71 73
                 if(!block) return
72 74
                 const doc = new DOMParser().parseFromString(block, 'text/html')
73 75
                 const gallery = doc.querySelectorAll('.blocks-gallery-item img')
74 76
                 // add captions
75
-                if(gallery) {
77
+                if(gallery.length) {
76 78
                     this.images = [].slice.call(gallery).map(
77 79
                         item => { return item.src }
78 80
                     )
79 81
                 }
80 82
             })
81
-        },
82
-        wrapImagesWithLink() {
83
-            const gallery = document.querySelectorAll('.blocks-gallery-item figure, figure.wp-block-image')
84
-            gallery.forEach((item, i) => {
85
-                item.addEventListener('click', () => {
86
-                    this.fullscreengallery = i
87
-                })
88
-            })
89 83
         }
90 84
     },
91 85
     watch: {
92 86
         posts(newVal, oldVal) { 
93 87
             // Loads images from the DOM
94 88
             this.checkForImages(newVal)
89
+            
90
+            // Gottas be on the next render tick
91
+            this.$nextTick(() => {
92
+                let gallery
93
+                this.pageBlocks(newVal).forEach(block => {
94
+                    if(!block) return
95
+                    gallery = document.querySelectorAll('.blocks-gallery-item figure')
96
+                })
97
+                
98
+                if(gallery.length < 1) return
99
+
100
+                gallery.forEach((item, i) => {
101
+                    item.addEventListener('mouseup', event => { this.fullscreengallery = i })
102
+                })
103
+            })
95 104
         }
96 105
     },
97
-    mounted() {
106
+    created() {
98 107
         // TODO: Only makes req if this hasn't been loaded yet
99 108
         let type = this.$route.params.type
100 109
         type = type.charAt(0).toUpperCase() + type.slice(1)
101 110
         this.$store.dispatch(`getAll${type}`)
102
-
103
-        this.wrapImagesWithLink()
104 111
     }
105 112
 }
106 113
 </script>

Loading…
İptal
Kaydet