Skip to content

Commit d448da1

Browse files
authored
DEVREL 2725 2726 current parent component (#28)
* DEVREL-2726: getParentComponent * DEVREL-2725: getCurrentComponent
1 parent 16ed780 commit d448da1

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

src/components/PermissionsMap.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const permissionsMap: PermissionsMap = {
5353
getAllComponents: { permissions: ['canAccessCanvas'] },
5454
getComponentByName: { permissions: ['canAccessCanvas'] },
5555
getComponentByNameAndGroup: { permissions: ['canAccessCanvas'] },
56+
getCurrentComponent: { permissions: ['canAccessCanvas'] },
5657
searchComponents: { permissions: ['canAccessCanvas'] },
5758
getInstanceCount: { permissions: ['canAccessCanvas'] },
5859
getVariants: { permissions: ['canAccessCanvas'] },
@@ -97,6 +98,7 @@ export const permissionsMap: PermissionsMap = {
9798
getAllElements: { permissions: ['canAccessCanvas'] },
9899
getRootElement: { permissions: ['canAccessCanvas'] },
99100
remove: { permissions: ['canDesign'] },
101+
getParentComponent: { permissions: ['canAccessCanvas'] },
100102
},
101103

102104
// Image Elements

src/designer-extension-typings/api.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ interface WebflowApi {
185185
* ```
186186
*/
187187
getComponentByName(a: string, b?: string): Promise<Component>;
188+
/**
189+
* Retrieves the component that is currently being edited.
190+
* @returns A Promise that resolves to the current component, or null if no component is currently being edited.
191+
* @example
192+
* ```ts
193+
* const component = await webflow.getCurrentComponent();
194+
* if (component) {
195+
* const name = await component.getName();
196+
* console.log(`Currently editing component: ${name}`);
197+
* } else {
198+
* console.log('Not currently editing a component.');
199+
* }
200+
* ```
201+
*/
202+
getCurrentComponent(): Promise<Component | null>;
188203
/**
189204
* Searches for Components by name
190205
* @returns A Promise that resolves to an array or objects with information about matching Components, not the `Component` objects themselves.

src/designer-extension-typings/elements-generated.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface WebflowElement {
2222
remove(this: {id: FullElementId}): Promise<null>;
2323
readonly before: InsertOrMoveElement;
2424
readonly after: InsertOrMoveElement;
25+
getParentComponent(this: {id: FullElementId}): Promise<Component | null>;
2526
}
2627

2728
interface CustomAttributes {

src/examples/components.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ export const Components = {
6464
console.log(marketingHero.id);
6565
},
6666

67+
getCurrentComponent: async () => {
68+
// Get the component currently being edited
69+
const component = await webflow.getCurrentComponent();
70+
71+
if (component) {
72+
const name = await component.getName();
73+
console.log(`Currently editing component: ${name}`);
74+
75+
// Get all elements inside the active component
76+
const root = await component.getRootElement();
77+
const children = await root.getChildren();
78+
console.log(`Root has ${children.length} child element(s)`);
79+
} else {
80+
console.log('Not currently editing a component.');
81+
}
82+
},
83+
6784
searchComponents: async () => {
6885
const heroes = await webflow.searchComponents({ q: 'Hero' });
6986
console.log(heroes);

src/examples/elements.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ export const Elements = {
7979
// Remove the selected element
8080
await el?.remove()
8181
},
82+
83+
getParentComponent: async () => {
84+
// Get Selected Element
85+
const element = await webflow.getSelectedElement()
86+
87+
if (element) {
88+
// Get the component that directly contains the element
89+
const parentComponent = await element.getParentComponent()
90+
91+
if (parentComponent) {
92+
const name = await parentComponent.getName()
93+
console.log(`Element is inside component: ${name}`)
94+
} else {
95+
console.log('Element is not inside a component.')
96+
}
97+
} else {
98+
console.log('No element is currently selected.')
99+
}
100+
},
82101
},
83102

84103
// Element Creation

0 commit comments

Comments
 (0)