Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

901 B
Raw Blame
import type { Instance, Instances, Pages } from "@webstudio-is/sdk";
import {
  findPageAndSelectorByInstanceId,
  getInstancePath,
} from "@webstudio-is/project-build/runtime";
import { getInstanceLabel } from "~/builder/shared/instance-label";

/**
 * Build the ancestor path array for an instance.
 * Returns an array of labels for all ancestors from root to parent.
 */
export const buildInstancePath = (
  instanceId: Instance["id"],
  pages: Pages,
  instances: Instances
): string[] => {
  const awareness = findPageAndSelectorByInstanceId(
    pages,
    instances,
    instanceId
  );
  if (!awareness.instanceSelector) {
    return [];
  }

  const instancePath = getInstancePath(awareness.instanceSelector, instances);

  if (!instancePath) {
    return [];
  }

  return instancePath
    .slice()
    .reverse()
    .slice(0, -1)
    .map(({ instance }) => getInstanceLabel(instance));
};