mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-08 21:15:18 +09:00
20 lines
592 B
TypeScript
20 lines
592 B
TypeScript
import { type Instance } from "@webstudio-is/sdk";
|
|
|
|
export const findParentInstance = (
|
|
instance: Instance,
|
|
instanceId: Instance["id"]
|
|
): Instance | undefined => {
|
|
const find = (
|
|
childInstance: Instance,
|
|
parentInstance?: Instance
|
|
): Instance | undefined => {
|
|
if (childInstance.id === instanceId) return parentInstance;
|
|
for (const child of childInstance.children) {
|
|
if (typeof child === "string") continue;
|
|
const foundInstance = find(child, childInstance);
|
|
if (foundInstance !== undefined) return foundInstance;
|
|
}
|
|
};
|
|
return find(instance);
|
|
};
|