mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 22:14:29 +09:00
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
import { Instance } from "@webstudio-is/react-sdk";
|
|
|
|
export const getInstancePath = (
|
|
instance: Instance,
|
|
instanceId: Instance["id"]
|
|
) => {
|
|
const path = [];
|
|
|
|
const find = (instance: Instance) => {
|
|
if (instance.id === instanceId) return true;
|
|
for (const child of instance.children) {
|
|
if (typeof child === "string") continue;
|
|
const found = find(child);
|
|
if (found) {
|
|
path.push(child);
|
|
return true;
|
|
}
|
|
}
|
|
};
|
|
|
|
if (find(instance)) {
|
|
path.push(instance);
|
|
}
|
|
|
|
return path.reverse();
|
|
};
|
|
|
|
export const getInstancePathWithPositions = (
|
|
instance: Instance,
|
|
instanceId: Instance["id"]
|
|
) => {
|
|
const path = [];
|
|
|
|
const find = (instance: Instance) => {
|
|
if (instance.id === instanceId) return true;
|
|
for (let index = 0; index < instance.children.length; index++) {
|
|
const child = instance.children[index];
|
|
if (typeof child === "string") continue;
|
|
const found = find(child);
|
|
if (found) {
|
|
path.push({ item: child, position: index });
|
|
return true;
|
|
}
|
|
}
|
|
};
|
|
|
|
if (find(instance)) {
|
|
path.push({ item: instance, position: 0 });
|
|
}
|
|
|
|
return path.reverse();
|
|
};
|