forked from dpim/wf-react-app
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathelements.ts
More file actions
758 lines (640 loc) · 22.6 KB
/
elements.ts
File metadata and controls
758 lines (640 loc) · 22.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import { removeIfPresent } from 'typedoc/dist/lib/utils'
export enum LinkModeSettings {
url = 'url',
page = 'page',
pageSection = 'pageSection',
email = 'email',
phone = 'phone',
attachment = 'attachment',
}
export enum MethodEnum {
get = 'get',
post = 'post',
}
export enum StateEnum {
success = 'success',
error = 'error',
loading = 'loading',
}
export const Elements = {
// Element Management
elementManagement: {
getSelectedElement: async () => {
// Get Selected Element
const element = await webflow.getSelectedElement()
// Print element info
if (element) {
console.log(element)
console.log(`Element type: ${element.type}`)
} else {
console.log('No element is currently selected.')
}
},
setSelectedElement: async () => {
// Get the Root Element
const rootElement = await webflow.getRootElement()
if (rootElement) {
// Select the root element
const selectedElement = await webflow.setSelectedElement(rootElement)
if (selectedElement?.children) {
// Start building elements on the selected element
await selectedElement?.append(webflow.elementPresets.DOM)
}
}
},
getAllElements: async () => {
// Retrieve all elements in the current context
const allElements = await webflow.getAllElements()
// Print element list
if (allElements.length > 0) {
console.log('List of all elements:')
allElements.forEach((element, index) => {
console.log(
index + 1,
'Element ID:',
JSON.stringify(element),
'Element Type:',
element.type,
)
})
} else {
console.log('No elements found in the current context.')
}
},
getRootElement: async () => {
// Get Root Element
const rootElement = await webflow.getRootElement()
// Print element details
console.log(
`Type: ${rootElement?.type} \n ID: ${JSON.stringify(rootElement?.id)}`,
)
},
removeElement: async () => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Remove the selected element
await el?.remove()
},
},
// Element Creation
elementCreation: {
BulkAddElements: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
// Build an element. We only support DOM elements and append for now.
const rootElement = webflow.elementBuilder(webflow.elementPresets.DOM)
// Append a DOM element to the element structure.
const childElement = rootElement.append(webflow.elementPresets.DOM)
childElement.setTag('svg')
if (selectedElement?.children) {
// Add root element to the deisgner by appending to selected Element
await selectedElement.append(rootElement)
}
},
BulkAddElementSVG: async () => {
const childElementIds = []
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
// Build an element. We only support DOM elements and append for now.
const rootElement = webflow.elementBuilder(webflow.elementPresets.DOM)
// Append a DOM element to the element structure.
const svgElement = rootElement.append(webflow.elementPresets.DOM)
svgElement.setTag('svg')
childElementIds.push(svgElement)
// Append a DOM element to the element structure.
const rectElement = svgElement.append(webflow.elementPresets.DOM)
const styles = await webflow.getAllStyles()
rectElement.setTag('rect')
rectElement.setAttribute('hello', 'world')
rectElement.setStyles([styles[0]])
childElementIds.push(rectElement)
if (selectedElement?.children) {
// Add root element to the deisgner by appending to selected Element
const newEl = await selectedElement.append(rootElement)
// get the children of the newly appended element
if (newEl.children) {
const newElChildren = await newEl.getChildren()
const promises = []
// loop through child elements that we added to the array during creation
// and create an array of promises to later await
for (let i = 0; i < childElementIds.length; i++) {
const childEl = newElChildren.find(
(x) => x.id.element === childElementIds[i].id,
)
console.log(childEl)
switch (childEl?.getTag()) {
case 'svg':
promises.push(
childEl.setAttribute('xmlns', 'http://www.w3.org/2000/svg'),
)
break
case 'rect':
promises.push(childEl.setAttribute('width', '200'))
promises.push(childEl.setAttribute('height', '100'))
promises.push(childEl.setAttribute('x', '10'))
promises.push(childEl.setAttribute('y', '10'))
promises.push(childEl.setAttribute('rx', '20'))
promises.push(childEl.setAttribute('ry', '20'))
promises.push(childEl.setAttribute('fill', 'blue'))
break
}
}
await Promise.all(promises)
}
}
},
insertElementBefore: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement) {
// Insert DIV before selected Element
const newDiv = await selectedElement.before(
webflow.elementPresets.DivBlock,
)
// Print element details
console.log(`${JSON.stringify(newDiv)}`)
}
},
insertElementAfter: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement) {
// Insert DIV after selected Element
const newDiv = await selectedElement.after(
webflow.elementPresets.DivBlock,
)
// Print element details
console.log(JSON.stringify(newDiv))
}
},
appendElement: async () => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Check if element supports child elements
if (el?.children) {
// Append newElement as a child to of the selected element
const newElement = await el?.append(webflow.elementPresets.DivBlock)
// Print element Details
console.log(JSON.stringify(newElement))
}
},
prependElement: async () => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Check if element supports child elements
if (el?.children) {
// Prepend newElement as a child to of the selected element
const newElement = await el?.prepend(webflow.elementPresets.DivBlock)
// Print element Details
console.log(JSON.stringify(newElement))
}
},
},
// Custom Attributes
customAttributes: {
getAllCustomAttributes: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.customAttributes) {
// Get All Custom Attributes
const customAttributes = await selectedElement.getAllCustomAttributes()
console.log(customAttributes)
}
},
getCustomAttribute: async (name: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.customAttributes) {
// Get Custom Attribute by Name
const customAttribute = await selectedElement.getCustomAttribute(name)
console.log(customAttribute)
}
},
setCustomAttribute: async (name: string, value: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.customAttributes) {
// Set Custom Attribute
const newAttribute = await selectedElement.setCustomAttribute(
name,
value,
)
}
},
removeCustomAttribute: async (name: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.customAttributes) {
// Remove Custom Attribute
await selectedElement.removeCustomAttribute(name)
}
},
},
// Text Content
textContent: {
getTextContent: async () => {
const selectedElement = await webflow.getSelectedElement()
// Check if element is a String type
if (selectedElement?.type === 'String') {
const text = await selectedElement.getText()
console.log(text)
}
// Otherwise, check if element has child String elements
else if (selectedElement?.children) {
const children = await selectedElement.getChildren()
const stringElements = children.filter(
(child) => child.type === 'String',
)
if (stringElements.length > 0) {
const textContentPromises = stringElements.map(
async (stringElement: StringElement) => {
const text = await stringElement.getText()
return text
},
)
const textContent = await Promise.all(textContentPromises)
console.log(textContent)
} else {
console.log('Element does not support text content')
}
}
},
setTextContent: async (myText: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
// Check if element supports text content
if (selectedElement?.textContent) {
// Set and print text content
const text = await selectedElement.setTextContent(myText)
console.log(selectedElement.textContent)
// Check if element has child String elements
} else if (selectedElement?.children) {
const children = await selectedElement.getChildren()
const stringElements = children.filter(
(child) => child.type === 'String',
)
if (stringElements.length > 0) {
// Set text content on child String elements
const text = await stringElements[0].setText(myText)
console.log(text)
}
} else {
console.log('Element does not support text content')
}
},
getText: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.textContent && selectedElement?.children) {
// Get Child Elements
const children = await selectedElement.getChildren()
// Filter string elements from children
const strings = children.filter((child) => child.type === 'String')
// Initialize an array to hold text content
let textContent = []
// Loop over string elements to get text
for (const myString of strings) {
if (myString.type === 'String') {
const text = await myString.getText()
textContent.push(text)
}
}
// Print text
console.log(textContent)
}
},
setText: async (myText: string) => {
// Get all elements and find the first StringElement
const allElements = await webflow.getAllElements()
const foundElement = allElements.find((el) => el.type === 'String')
if (foundElement) {
// Check that element has the method in order to use it
if ('setText' in foundElement) {
const elementText = foundElement.setText(myText) // Set Text
}
} else {
console.log('Element not found on page')
}
},
},
// Styles
styles: {
getStyles: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.styles) {
// Get Styles
const styles = await selectedElement.getStyles()
// Get Style Details
const styleDetails = styles.map(async (style) => {
const styleName = await style.getName()
const styleProperties = await style.getProperties()
return {
Name: styleName,
Properties: styleProperties,
ID: style.id,
}
})
// Print Style Details
console.log(await Promise.all(styleDetails))
}
},
setStyles: async (styleName: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.styles) {
// Get Style by name
const myStyle = await webflow.getStyleByName(styleName)
if (myStyle) {
// Set and print style
await selectedElement.setStyles([myStyle])
const styles = await selectedElement.getStyles()
console.log(`Styles: ${JSON.stringify(styles)}`)
}
}
},
createAndSetStyle: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.styles) {
// Create a new style
const newStyle = await webflow.createStyle('MyCustomStyle')
// Set properties for the style
newStyle.setProperties({
'background-color': 'blue',
'font-size': '32px',
'font-weight': 'bold',
})
// Set style on selected element
selectedElement.setStyles([newStyle])
}
},
},
// DOM Operations
domOperations: {
getTag: async () => {
// Get All Elements and find first DOM Element
const elements = await webflow.getAllElements()
const DOMElement = elements.find((element) => element.type === 'DOM')
if (DOMElement?.type === 'DOM') {
// Get DOM Element's Tag
const tag = await DOMElement.getTag()
console.log(tag)
} else {
console.log('No DOM Element Found')
}
},
setTag: async (myTag: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.children) {
// Create and append DOM Element
const DOMElement = await selectedElement.append(
webflow.elementPresets.DOM,
)
console.log(DOMElement)
// Set Tag
await DOMElement?.setTag(myTag)
const tag = await DOMElement.getTag()
}
},
getAllAttributes: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.type === 'DOM') {
const customAttributes = await selectedElement.getAllAttributes()
console.log(customAttributes)
}
},
getAttribute: async (name: string) => {
// Get All Elements and find first DOM Element
const elements = await webflow.getAllElements()
const DOMElement = elements.find((element) => element.type === 'DOM')
if (DOMElement?.type === 'DOM') {
// Get DOM Element's Attribute by Name
const attribute = await DOMElement.getAttribute(name)
console.log(attribute)
} else {
console.log('No DOM Element Found')
}
},
setAttribute: async (name: string, value: string) => {
// Get All Elements and find first DOM Element
const elements = await webflow.getAllElements()
const DOMElement = elements.find((element) => element.type === 'DOM')
if (DOMElement?.type === 'DOM') {
// Set and print DOM Element's Attributes
await DOMElement.setAttribute(name, value)
const attributes = await DOMElement.getAllAttributes()
console.log(attributes)
} else {
console.log('No DOM Element Found')
}
},
removeAttribute: async (name: string) => {
// Get All Elements and find first DOM Element
const elements = await webflow.getAllElements()
const DOMElement = elements.find((element) => element.type === 'DOM')
if (DOMElement?.type === 'DOM') {
// Get Attributes
const customAttributes = await DOMElement.getAllAttributes()
console.log(customAttributes)
// Remove and print DOM Element's Attributes
await DOMElement.removeAttribute(name)
const attributes = await DOMElement.getAllAttributes()
console.log(attributes)
} else {
console.log('No DOM Element Found')
}
},
},
// Heading Operations
headingOperations: {
getHeadingLevel: async () => {
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.type === 'Heading') {
const headingLevel = await selectedElement.getHeadingLevel()
console.log(headingLevel)
} else {
console.log('Selected Element is not a Heading Element')
}
},
setHeadingLevel: async (level: 2 | 1 | 3 | 4 | 5 | 6) => {
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.type === 'Heading') {
const headingLevel = await selectedElement.setHeadingLevel(
parseInt(level) as 2 | 1 | 3 | 4 | 5 | 6,
)
console.log(headingLevel)
} else {
console.log('Selected Element is not a Heading Element')
}
},
},
// Image Operations
imageOperations: {
getAsset: async () => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Check if element can have children
if (el?.children) {
// Create a new Image Element using Element Presets
const imgEl = await el.append(webflow.elementPresets.Image)
// Check element type
if (imgEl.type === 'Image') {
// Get asset from Image element
const myAsset = await imgEl.getAsset()
console.log(myAsset)
}
}
},
setAsset: async (assetId: string) => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Check if element can have children
if (el?.children) {
// Create a new Image Element using Element Presets
const imgEl = await el.append(webflow.elementPresets.Image)
// Get asset by ID
const asset = await webflow.getAssetById(assetId)
// Check element type
if (imgEl.type === 'Image') {
// Set asset as the "src" of the Image Element
await imgEl.setAsset(asset)
}
}
},
getAltText: async () => {
// Get Selected Element
const el = await webflow.getSelectedElement()
if (el?.type === 'Image') {
// Get alt text
const alt = await el.getAltText()
console.log(alt)
} else {
console.error('Please select an image element')
await webflow.notify({
type: 'Error',
message: 'Please select an Image Element',
})
}
},
setAltText: async (altText: string) => {
// Get Selected Element
const el = await webflow.getSelectedElement()
// Check element type
if (el?.type === 'Image') {
// Set alt text. If a null is passed, the API will set the alt text as "decorative"
await el.setAltText(altText)
// Get alt text
const alt = await el.getAltText()
console.log(alt) // true
} else {
console.error('Please select an Image Element')
await webflow.notify({
type: 'Error',
message: 'Please select an Image Element',
})
}
},
},
// Link Operations
linkOperations: {
setLinkBlockSettings: async (mode: LinkModeSettings, target: string) => {
// Get Selected Element
const element = await webflow.getSelectedElement()
if (element) {
const newLink = await element.after(webflow.elementPresets.LinkBlock) // Create new link element
const metadata = { openInNewTab: true }
await newLink.setSettings(mode, target, metadata) // Set link element settings
const targetValue = await newLink.getTarget() // Get target value
console.log(targetValue)
}
},
getLinkTarget: async () => {
const elements = await webflow.getAllElements() // Get All Elements
const links = elements.filter((element) => element.type === 'Link') // Filter for Link elements
// Print target value of each link element
for (const link of links) {
const targetValue = await link.getTarget()
console.log(`ID: ${link.id.element}, Target Value: ${targetValue}`)
}
},
typeChecking: async () => {
// Example: Type checking in Webflow API
// Get a selected element
let element = await webflow.getSelectedElement()
// Check the type of the element
if (element?.type === 'String') {
// It's safe to use methods specific to StringElement
let text = element.getText()
console.log('Text content:', text)
} else if (element?.children) {
// Check if the element can have children
let imageUrl = await element.getChildren()
console.log('Image URL:', imageUrl)
} else {
// Handle other types or default case
console.log(
"Element is not a StringElement and doesn't have child properties",
)
}
},
},
// Form Operations
formOperations: {
getFormName: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (
selectedElement?.type === 'FormForm' ||
selectedElement?.type === 'FormWrapper'
) {
const formName = await selectedElement?.getName()
console.log(formName)
} else {
console.log('Selected Element is not a Form')
}
},
setFormName: async (name: string) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (
selectedElement?.type === 'FormForm' ||
selectedElement?.type === 'FormWrapper'
) {
await selectedElement?.setName(name)
}
},
getFormSettings: async () => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (
selectedElement?.type === 'FormForm' ||
selectedElement?.type === 'FormWrapper'
) {
const formSettings = await selectedElement?.getSettings()
console.log(formSettings)
}
},
setFormSettings: async (
name: string,
method: MethodEnum,
state: StateEnum,
action: string,
) => {
// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (
selectedElement?.type === 'FormForm' ||
selectedElement?.type === 'FormWrapper'
) {
await selectedElement?.setSettings({
method: 'post',
name: 'New Form',
state: 'success',
})
}
},
},
}