forked from dpim/wf-react-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomponents.ts
More file actions
289 lines (246 loc) · 8.73 KB
/
components.ts
File metadata and controls
289 lines (246 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type { ComponentInfo } from '../types/dynamic-enums'
// Types for component collections
export type ComponentsEnum = {
[key: string]: ComponentInfo
}
// Utility function to get all components as an enum-like object
export const getComponentsEnum = async (): Promise<ComponentsEnum> => {
const components = await webflow.getAllComponents()
const componentsMap: ComponentsEnum = {}
await Promise.all(
components.map(async (component) => {
const name = await component.getName()
componentsMap[name] = {
id: component.id,
name,
component,
}
}),
)
return componentsMap
}
// Helper function to get a specific component by name
export const getComponentByName = async (
name: string,
): Promise<ComponentInfo | undefined> => {
const components = await getComponentsEnum()
return components[name]
}
export const Components = {
getAllComponents: async () => {
// Get all components
const components = await webflow.getAllComponents()
// Print Component Details
if (components.length > 0) {
console.log('List of registered components:')
for (let component in components) {
const currentComponentName = await components[component].getName()
console.log(
`${component + 1}. Component Name: ${currentComponentName}, Component ID: ${components[component].id}`,
)
}
} else {
console.log('No components are currently registered.')
}
},
getComponentByName: async () => {
// Fetch a component by name only
const heroSection = await webflow.getComponentByName('Hero');
console.log(heroSection.id);
},
getComponentByNameAndGroup: async () => {
// Fetch a component scoped to a group
const marketingHero = await webflow.getComponentByName('Marketing', 'Hero');
console.log(marketingHero.id);
},
searchComponents: async () => {
const heroes = await webflow.searchComponents({ q: 'Hero' });
console.log(heroes);
},
getInstanceCount: async () => {
// Audit component usage across the site
const components = await webflow.getAllComponents();
for (const component of components) {
const name = await component.getName();
const count = await component.getInstanceCount();
console.log(`${name}: ${count} instances`);
}
// Guard against removing a component that's still in use
const hero = components[0];
const instanceCount = await hero.getInstanceCount();
if (instanceCount > 0) {
console.log(`Cannot safely remove — ${instanceCount} instances exist`);
} else {
await webflow.unregisterComponent(hero);
}
},
getVariants: async () => {
const component = (await webflow.getAllComponents())[0]
const variants = await component.getVariants()
console.log(variants)
// [
// { id: 'base', name: 'Primary', isSelected: true },
// { id: 'xxxx', name: 'Secondary', isSelected: false },
// ]
// Find which variant the user is currently editing
const activeVariant = variants.find(v => v.isSelected)
console.log(`Currently editing: ${activeVariant?.name}`)
},
getSelectedVariant: async () => {
const heroComponent = webflow.getComponentByName('hero')
// When no variant is explicitly selected, returns base
const base = await heroComponent.getSelectedVariant()
console.log(JSON.stringify(base))
/*
{
id: 'base',
name: 'Primary',
isSelected: true,
}
*/
},
createVariant: async () => {
const component = (await webflow.getAllComponents())[0]
// Create a new variant and select it immediately
const variant = await component.createVariant({
name: 'Secondary Hero',
isSelected: true,
})
console.log(variant)
// { id: 'variant-123', name: 'Secondary Hero', isSelected: true }
// Name conflicts auto-increment
const variant2 = await component.createVariant({ name: 'Secondary Hero' })
console.log(variant2.name) // 'Secondary Hero 2'
// Duplicate a variant by passing its ID as the second parameter
const duplicateVariant = await component.createVariant({
name: 'Duplicate of Secondary Hero',
isSelected: true,
}, variant.id)
console.log(duplicateVariant.name) // 'Duplicate of Secondary Hero'
},
createComponent: async () => {
// Get selected element
const rootElement = await webflow.getSelectedElement()
if (rootElement) {
// Create a component from the Root Element
const component = await webflow.registerComponent(
'MyCustomComponent',
rootElement,
)
console.log(`Component registered with ID: ${component.id}`)
} else {
console.log(
'No element is currently selected. Please select a root element first.',
)
}
},
deleteComponent: async () => {
// Get selected element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement) {
// Create component from selected element
const myNewComponent = await webflow.registerComponent(
'Hero Component',
selectedElement,
)
// Delete Component
await webflow.unregisterComponent(myNewComponent)
} else {
console.log(
'No element is currently selected. Please select a root element first.',
)
}
},
openComponentCanvas: async () => {
// Open the canvas for the Component that has an instance selected in the Designer
const selected = await webflow.getSelectedElement();
if (selected?.type === 'ComponentInstance') {
await webflow.openCanvas(selected);
}
},
selectComponent: async () => {
// Step 1: Fetch the currently selected element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement && selectedElement.type === 'ComponentInstance') {
// Step 2: Enter the context of the selected ComponentElement
await webflow.enterComponent(selectedElement as ComponentElement)
console.log('Successfully entered the component context.')
// Step 3: After entering the component's context, fetch the root element
const rootElement = await webflow.getRootElement()
if (rootElement) {
console.log('Root element of the component:', rootElement)
} else {
console.log('No root element found in this component context.')
}
} else {
console.log('The selected element is not a ComponentElement.')
}
},
editComponent: async () => {
// Get Component
const all = await webflow.getAllComponents()
const firstComponent = all[0]
// Get Root Element on the Component
const root = (await firstComponent?.getRootElement()) as AnyElement
if (root.children) {
// Append DIV block to Root element
await root?.append('div')
}
},
createComponentInstance: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
// Get Component
const allComponents = await webflow.getAllComponents()
const firstComponent = allComponents[0]
// Add Component instance onto a page
await selectedElement?.before(firstComponent)
},
exitComponent: async () => {
await webflow.exitComponent()
const rootElement = await webflow.getRootElement()
const rootElementType = rootElement?.type
// Print Root Element Type. If element type is Body, the designer has exited out of the Component context
console.log(`Element Type: ${rootElementType}`)
},
getRootElement: async () => {
// Get Component
const all = await webflow.getAllComponents()
const firstComponent = all[0]
// Get Root Element of Component
const root = await firstComponent?.getRootElement()
console.log(root)
},
getName: async (name: string) => {
const components = await webflow.getAllComponents()
// Check if component exists
for (const c in components) {
const currentComponentName = await components[c].getName()
if (name === currentComponentName) {
console.log(`Found ${name} Component`)
}
}
},
setName: async () => {
// Get Component
const components = await webflow.getAllComponents()
const myComponent = components[0]
// Set Component Name
await myComponent.setName('My New Component Name')
},
getComponent: async () => {
// Select Component Element on Page
const elements = await webflow.getAllElements()
const componentInstance = elements?.find(
(el) => el.type === 'ComponentInstance',
)
if (componentInstance?.type === 'ComponentInstance') {
// Get Component object from instance
const component = await componentInstance?.getComponent()
const componentName = await component?.getName()
console.log(componentName)
} else {
console.log('No component element found')
}
},
}