mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-08 13:05:08 +09:00
20 lines
574 B
TypeScript
20 lines
574 B
TypeScript
import { type Instance } from "@webstudio-is/sdk";
|
|
|
|
// @todo should be optimized because it will be a frequent operation on a potentially large tree.
|
|
export const findInstanceById = (
|
|
instance: Instance,
|
|
id: Instance["id"]
|
|
): Instance | undefined => {
|
|
if (instance.id === id) {
|
|
return instance;
|
|
}
|
|
|
|
if (instance.children !== undefined) {
|
|
for (const child of instance.children) {
|
|
if (typeof child === "string") continue;
|
|
const foundInstance = findInstanceById(child, id);
|
|
if (foundInstance !== undefined) return foundInstance;
|
|
}
|
|
}
|
|
};
|