@pinegrow/vite-plugin 3.0.0-beta.31 → 3.0.0-beta.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/vue-plugin.js +89 -34
  2. package/package.json +1 -1
@@ -33,7 +33,11 @@ export function usePinegrow() {
33
33
  if (!pgIds[value.pgId]) {
34
34
  pgIds[value.pgId] = []
35
35
  }
36
- pgIds[value.pgId].push(value)
36
+ if (value.key === undefined || value.key === null) {
37
+ pgIds[value.pgId].push(value)
38
+ } else {
39
+ pgIds[value.pgId].push([value.key, value])
40
+ }
37
41
  }
38
42
  }
39
43
  return pgIds
@@ -59,21 +63,9 @@ export function usePinegrow() {
59
63
 
60
64
  winObj.pinegrow = {
61
65
  elCache,
62
- elCacheErrHandlerFn: (...args) => {
63
- // {vnode, hook, rootEl, pgId, key, el, instance, message}
64
- console.log(args.pop())
65
- // connectionManager.fallback_tagTransform(args)
66
- },
67
- elUpdateHanderFn: el => {
68
- if (this.elCache.has(el)) {
69
- const { pgId } = this.elCache.get(el)
70
- if (pgId && pgId === pinegrow.getSelectedElement()?.pgId) {
71
- console.log(`Reselect ${pgId}`)
72
- // pinegrow.reselectElement()
73
- }
74
- }
75
- },
76
-
66
+ // // Uncomment these two to test locally
67
+ // elCacheErrHandlerFn,
68
+ // elUpdateHanderFn,
77
69
  rootFragmentToPgIdComputed,
78
70
  pgIdToElComputed,
79
71
  localComponentToElComputed,
@@ -88,36 +80,39 @@ export function usePinegrow() {
88
80
  if (window?.process?.client && process?.client !== true) return
89
81
  if (!window.pinegrow) initCache()
90
82
 
91
- const el = vnode.el
83
+ let el = vnode.el
92
84
  const instance = vnode.component || vnode.ctx || el.__vueParentComponent
93
85
 
94
86
  try {
95
- key = key || vnode.key
87
+ if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
88
+ key = key || vnode.key
89
+ }
96
90
  localFile =
97
91
  localFile || (vnode.type.__file && !vnode.type.__file.includes('node_modules') && vnode.type.__file)
98
92
 
99
93
  let isRootFragment = false
100
- const isFragment = el.nodeType !== 1
94
+ let isFragment = el.nodeType !== 1
101
95
  let firstEl, lastEl
102
96
 
103
- if (hook === 'unmounted') {
104
- if (pinegrow.elCache.has(el)) {
105
- pinegrow.elCache.delete(el)
106
- }
107
- for (let [key, value] of pinegrow.elCache.entries()) {
108
- if (value.rootEl === rootEl) {
109
- pinegrow.elCache.delete(key)
110
- }
111
- }
112
- return
113
- }
97
+ // // Computed props anyway filters out unmounted ones, and we use the local components unmounted hooks to cleanup unmounted nodes. Using the unmounted hook seems to be out of sync when el is reused but instance is remounted.
98
+ // if (hook === 'unmounted') {
99
+ // if (pinegrow.elCache.has(el)) {
100
+ // pinegrow.elCache.delete(el)
101
+ // }
102
+ // for (let [key, value] of pinegrow.elCache.entries()) {
103
+ // if (value.rootEl === rootEl) {
104
+ // pinegrow.elCache.delete(key)
105
+ // }
106
+ // }
107
+ // return
108
+ // }
114
109
 
115
110
  if (pinegrow.elCache.has(el)) {
116
111
  // pgId could have been updated
117
112
  if (pgId) {
118
113
  pinegrow.elCache.get(el).pgId = pgId
119
114
  }
120
- if (key) {
115
+ if (key !== null && key !== undefined) {
121
116
  pinegrow.elCache.get(el).key = key
122
117
  }
123
118
  if (localFile) {
@@ -130,11 +125,54 @@ export function usePinegrow() {
130
125
  // root Fragment
131
126
  isRootFragment = true
132
127
  }
133
- const childVNodes = Array.isArray(instance.subTree.children) ? instance.subTree.children : []
128
+
129
+ function isFragmentEl(instance) {
130
+ // return appRecord.options.types.Fragment === instance.subTree?.type
131
+ return instance.subTree.el.nodeType !== 1
132
+ }
133
+
134
+ function getRootVNodesFromComponentInstance(instance) {
135
+ if (isFragmentEl(instance)) {
136
+ return getFragmentRootVNodes(instance.subTree)
137
+ }
138
+ if (!instance.subTree) return []
139
+ return [instance.subTree]
140
+ }
141
+
142
+ function getFragmentRootVNodes(vnode) {
143
+ if (!vnode.children) return []
144
+
145
+ const list = []
146
+
147
+ for (let i = 0, l = vnode.children.length; i < l; i++) {
148
+ const childVnode = vnode.children[i]
149
+ if (childVnode.component) {
150
+ list.push(...getRootVNodesFromComponentInstance(childVnode.component))
151
+ } else if (childVnode) {
152
+ list.push(childVnode)
153
+ }
154
+ }
155
+
156
+ return list
157
+ }
158
+
159
+ let childVNodes = getRootVNodesFromComponentInstance(instance)
160
+
161
+ if (!childVNodes.length) {
162
+ // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
163
+ if (el.nextElementSibling) {
164
+ const childEl = el.nextElementSibling
165
+ firstEl = lastEl = childEl
166
+ const childInstance = childEl.$ || childEl.__vueParentComponent
167
+ const childVNode = childInstance?.vnode // or subTree?
168
+ if (childVNode) {
169
+ childVNodes.push(childVNode)
170
+ }
171
+ }
172
+ }
134
173
 
135
174
  if (childVNodes.length) {
136
175
  if (childVNodes.length === 1) {
137
- // Invalid scenario, if length is just one, then it won't be a fragment at all
138
176
  firstEl = lastEl = childVNodes[0].el
139
177
  } else {
140
178
  firstEl = childVNodes[0].el
@@ -162,7 +200,9 @@ export function usePinegrow() {
162
200
  })
163
201
  }
164
202
 
165
- elUpdateHanderFn(el)
203
+ if (pinegrow.elUpdateHanderFn) {
204
+ pinegrow.elUpdateHanderFn(el)
205
+ }
166
206
  } catch (err) {
167
207
  if (pinegrow.elCacheErrHandlerFn) {
168
208
  pinegrow.elCacheErrHandlerFn(vnode, hook, rootEl, pgId, key, el, instance, err.message)
@@ -185,6 +225,20 @@ export function usePinegrow() {
185
225
  }
186
226
  }
187
227
 
228
+ const cleanupCache = () => {
229
+ // for (let [key, value] of pinegrow.elCache.entries()) {
230
+ // if (value.isFragment && !value.firstEl) {
231
+ // console.log(value)
232
+ // }
233
+ // }
234
+ for (let [key, value] of pinegrow.elCache.entries()) {
235
+ if (value.instance.isUnmounted) {
236
+ pinegrow.elCache.delete(key)
237
+ // console.log(value)
238
+ }
239
+ }
240
+ }
241
+
188
242
  // Local Component
189
243
  const rootVNode = ref(null)
190
244
 
@@ -204,6 +258,7 @@ export function usePinegrow() {
204
258
  if (rootVNode.value) {
205
259
  pgUpdateElCache('unmounted')(rootVNode.value)
206
260
  }
261
+ cleanupCache()
207
262
  }
208
263
 
209
264
  onBeforeMount(() => initCache())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.31",
3
+ "version": "3.0.0-beta.33",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [