Skip to content

Commit 12cfc11

Browse files
authored
DEVREL-2671: Example for component.getInstanceCount() (#18)
1 parent 614fa83 commit 12cfc11

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/components/PermissionsMap.tsx

Lines changed: 1 addition & 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+
getInstanceCount: { permissions: ['canAccessCanvas'] },
5657
enterComponent: { permissions: ['canModifyComponents'] },
5758
exitComponent: { permissions: ['canAccessCanvas'] },
5859
},

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,29 @@ interface WebflowApi {
184184
* ```
185185
*/
186186
getComponentByName(a: string, b?: string): Promise<Component>;
187+
/**
188+
* Gets the number of instances of a component.
189+
* @returns A Promise that resolves to the number of instances of the component across the entire site.
190+
* @example
191+
* ```ts
192+
* // Audit component usage across the site
193+
* const components = await webflow.getAllComponents();
194+
* for (const component of components) {
195+
* const name = await component.getName();
196+
* const count = await component.getInstanceCount();
197+
* console.log(`${name}: ${count} instances`);
198+
* }
199+
* // Guard against removing a component that's still in use
200+
* const hero = components[0];
201+
* const instanceCount = await hero.getInstanceCount();
202+
* if (instanceCount > 0) {
203+
* console.log(`Cannot safely remove — ${instanceCount} instances exist`);
204+
* } else {
205+
* await webflow.unregisterComponent(hero);
206+
* }
207+
* ```
208+
*/
209+
getInstanceCount(): Promise<number>;
187210
/**
188211
* Focus the designer on a Component. When a component is in focus, all Globals pertain specifically to that
189212
* Component, not the entire Site.

src/examples/components.ts

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

67+
getInstanceCount: async () => {
68+
// Audit component usage across the site
69+
const components = await webflow.getAllComponents();
70+
for (const component of components) {
71+
const name = await component.getName();
72+
const count = await component.getInstanceCount();
73+
console.log(`${name}: ${count} instances`);
74+
}
75+
// Guard against removing a component that's still in use
76+
const hero = components[0];
77+
const instanceCount = await hero.getInstanceCount();
78+
if (instanceCount > 0) {
79+
console.log(`Cannot safely remove — ${instanceCount} instances exist`);
80+
} else {
81+
await webflow.unregisterComponent(hero);
82+
}
83+
},
84+
6785
createComponent: async () => {
6886
// Get selected element
6987
const rootElement = await webflow.getSelectedElement()

0 commit comments

Comments
 (0)