Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/PermissionsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const permissionsMap: PermissionsMap = {
registerComponent: { permissions: ['canCreateComponents'] },
unregisterComponent: { permissions: ['canCreateComponents'] },
getAllComponents: { permissions: ['canAccessCanvas'] },
getInstanceCount: { permissions: ['canAccessCanvas'] },
enterComponent: { permissions: ['canModifyComponents'] },
exitComponent: { permissions: ['canAccessCanvas'] },
},
Expand Down
23 changes: 23 additions & 0 deletions src/designer-extension-typings/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ interface WebflowApi {
* ```
*/
getAllComponents(): Promise<Array<Component>>;
/**
* Gets the number of instances of a component.
* @returns A Promise that resolves to the number of instances of the component across the entire site.
* @example
* ```ts
* // 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);
* }
* ```
*/
getInstanceCount(): Promise<number>;
/**
* Focus the designer on a Component. When a component is in focus, all Globals pertain specifically to that
* Component, not the entire Site.
Expand Down
18 changes: 18 additions & 0 deletions src/examples/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export const Components = {
}
},

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);
}
},

createComponent: async () => {
// Get selected element
const rootElement = await webflow.getSelectedElement()
Expand Down