forked from dpim/wf-react-app
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomponents.ts
More file actions
227 lines (192 loc) · 6.75 KB
/
components.ts
File metadata and controls
227 lines (192 loc) · 6.75 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
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.')
}
},
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.',
)
}
},
createComponentWithoutRoot: async () => {
// Create a hero component in the Sections group that is not within an existing element
const hero = await webflow.registerComponent({
name: 'Hero Section',
group: 'Sections',
description: 'A reusable hero section with heading and CTA',
});
console.log(`Component registered with ID: ${hero.id}`)
},
createComponentFromElement: async () => {
// Convert an existing element into a component, by default replacing the element with the new component
const selectedElement = await webflow.getSelectedElement()
if (selectedElement) {
const heroComponent = await webflow.registerComponent(
{
name: 'Hero Section',
group: 'Sections',
description: 'Main hero with heading and CTA'
},
selectedElement
)
}
},
duplicateComponent: async () => {
// Duplicate a component
const [original] = await webflow.getAllComponents()
const copy = await webflow.registerComponent({ name: 'Card Copy' }, original)
},
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.',
)
}
},
enterComponent: 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(webflow.elementPresets.DivBlock)
}
},
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')
}
},
}