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

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.
@@ -0,0 +1,217 @@
1
+ import { onBeforeMount, onMounted, onBeforeUnmount, getCurrentInstance, ref, reactive, computed } from 'vue'
2
+
3
+ export function usePinegrow() {
4
+ const initCache = () => {
5
+ // conditional
6
+ const winObj = window
7
+
8
+ if (!(winObj?.process?.client && winObj.process.client !== true)) {
9
+ if (!winObj.pinegrow) {
10
+ // conditional
11
+ // console.log('Cache initialized by Pinegrow Vue Plugin!')
12
+ const elCache = reactive(new Map())
13
+
14
+ const rootFragmentToPgIdComputed = computed(() => {
15
+ const rootEls = new Map()
16
+ for (let [key, value] of elCache.entries()) {
17
+ if (value.instance.isMounted && !value.instance.isUnmounted && value.isRootFragment) {
18
+ rootEls.set(key, value)
19
+ }
20
+ }
21
+ return rootEls
22
+ })
23
+
24
+ const pgIdToElComputed = computed(() => {
25
+ const pgIds = {}
26
+ for (let [key, value] of elCache.entries()) {
27
+ if (
28
+ value.instance.isMounted &&
29
+ !value.instance.isUnmounted &&
30
+ value.pgId &&
31
+ (!value.rootEl || value.isRootFragment)
32
+ ) {
33
+ if (!pgIds[value.pgId]) {
34
+ pgIds[value.pgId] = []
35
+ }
36
+ pgIds[value.pgId].push(value)
37
+ }
38
+ }
39
+ return pgIds
40
+ })
41
+
42
+ const localComponentToElComputed = computed(() => {
43
+ const localComponents = {}
44
+ for (let [key, value] of elCache.entries()) {
45
+ if (
46
+ value.instance.isMounted &&
47
+ !value.instance.isUnmounted &&
48
+ value.localFile &&
49
+ (!value.rootEl || value.isRootFragment)
50
+ ) {
51
+ if (!localComponents[value.localFile]) {
52
+ localComponents[value.localFile] = []
53
+ }
54
+ localComponents[value.localFile].push(value)
55
+ }
56
+ }
57
+ return localComponents
58
+ })
59
+
60
+ winObj.pinegrow = {
61
+ 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
+
77
+ rootFragmentToPgIdComputed,
78
+ pgIdToElComputed,
79
+ localComponentToElComputed,
80
+ }
81
+ }
82
+ }
83
+ }
84
+
85
+ // pgId & key can be optional
86
+ const pgUpdateElCache = (hook, pgId, rootEl, key, localFile) => async vnode => {
87
+ if (!vnode) return
88
+ if (window?.process?.client && process?.client !== true) return
89
+ if (!window.pinegrow) initCache()
90
+
91
+ const el = vnode.el
92
+ const instance = vnode.component || vnode.ctx || el.__vueParentComponent
93
+
94
+ try {
95
+ key = key || vnode.key
96
+ localFile =
97
+ localFile || (vnode.type.__file && !vnode.type.__file.includes('node_modules') && vnode.type.__file)
98
+
99
+ let isRootFragment = false
100
+ const isFragment = el.nodeType !== 1
101
+ let firstEl, lastEl
102
+
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
+ }
114
+
115
+ if (pinegrow.elCache.has(el)) {
116
+ // pgId could have been updated
117
+ if (pgId) {
118
+ pinegrow.elCache.get(el).pgId = pgId
119
+ }
120
+ if (key) {
121
+ pinegrow.elCache.get(el).key = key
122
+ }
123
+ if (localFile) {
124
+ pinegrow.elCache.get(el).localFile = localFile
125
+ }
126
+ } else {
127
+ // Text/comment node
128
+ if (isFragment) {
129
+ if (!rootEl) {
130
+ // root Fragment
131
+ isRootFragment = true
132
+ }
133
+ const childVNodes = Array.isArray(instance.subTree.children) ? instance.subTree.children : []
134
+
135
+ if (childVNodes.length) {
136
+ if (childVNodes.length === 1) {
137
+ // Invalid scenario, if length is just one, then it won't be a fragment at all
138
+ firstEl = lastEl = childVNodes[0].el
139
+ } else {
140
+ firstEl = childVNodes[0].el
141
+ lastEl = childVNodes[childVNodes.length - 1].el
142
+ }
143
+
144
+ childVNodes.forEach(childVNode => {
145
+ pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
146
+ })
147
+ }
148
+ }
149
+
150
+ pinegrow.elCache.set(el, {
151
+ el,
152
+ isRootFragment,
153
+ rootEl,
154
+ vnode,
155
+ instance,
156
+ isFragment,
157
+ firstEl,
158
+ lastEl,
159
+ pgId,
160
+ key,
161
+ localFile,
162
+ })
163
+ }
164
+
165
+ elUpdateHanderFn(el)
166
+ } catch (err) {
167
+ if (pinegrow.elCacheErrHandlerFn) {
168
+ pinegrow.elCacheErrHandlerFn(vnode, hook, rootEl, pgId, key, el, instance, err.message)
169
+ }
170
+ }
171
+ }
172
+
173
+ const elUpdateHanderFn = el => {
174
+ // if (pinegrow.elCache.has(el)) {
175
+ // const { pgId } = pinegrow.elCache.get(el)
176
+ // if (pgId) {
177
+ // // console.log(`Reselect ${pgId}`)
178
+ // }
179
+ // }
180
+ }
181
+
182
+ const elCacheErrHandlerFn = () => {
183
+ if (message) {
184
+ console.log(message)
185
+ }
186
+ }
187
+
188
+ // Local Component
189
+ const rootVNode = ref(null)
190
+
191
+ const mountLocalComponent = () => {
192
+ const instance = getCurrentInstance()
193
+ const vnode = instance?.vnode
194
+ const el = vnode?.el
195
+ const localFile = instance.type.__file && !instance.type.__file.includes('node_modules') && instance.type.__file
196
+
197
+ if (instance && vnode && el) {
198
+ rootVNode.value = vnode
199
+ pgUpdateElCache('mounted', null, null, null, localFile)(vnode)
200
+ }
201
+ }
202
+
203
+ const unmountLocalComponent = () => {
204
+ if (rootVNode.value) {
205
+ pgUpdateElCache('unmounted')(rootVNode.value)
206
+ }
207
+ }
208
+
209
+ onBeforeMount(() => initCache())
210
+ onMounted(() => {
211
+ mountLocalComponent()
212
+ })
213
+ onBeforeUnmount(() => unmountLocalComponent())
214
+
215
+ return { pgUpdateElCache }
216
+ }
217
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0-beta.31",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [
@@ -8,6 +8,7 @@
8
8
  ],
9
9
  "main": "./dist/index.cjs",
10
10
  "module": "./dist/index.cjs",
11
+ "types": "./types.d.ts",
11
12
  "exports": {
12
13
  ".": {
13
14
  "import": "./dist/index.cjs",
@@ -16,6 +17,9 @@
16
17
  "./dev": {
17
18
  "import": "./src/index.js",
18
19
  "require": "./src/index.js"
20
+ },
21
+ "./vue": {
22
+ "import": "./dist/vue-plugin.js"
19
23
  }
20
24
  },
21
25
  "keywords": [
@@ -33,7 +37,8 @@
33
37
  "increment-beta-version": "npm version prerelease --preid=beta"
34
38
  },
35
39
  "dependencies": {
36
- "node-html-parser": "^5.2.0",
37
- "@pinegrow/tailwind-plugin": "latest"
40
+ "@vue/compiler-sfc": "^3.2.45",
41
+ "magic-string": "^0.27.0",
42
+ "node-html-parser": "^5.2.0"
38
43
  }
39
44
  }