Skip to content

Free open-source image editor for the browser

1.3 KB
Raw Blame
import { findLayer, locateLayer, type Scene } from "@/core/document";

export type LayerDrop = {
  id: string;
  target: string;
  position: "before" | "after" | "inside";
};

/** Resolve visual top-to-bottom placement into the document's bottom-to-top sibling index. */
export function layerDrop(
  scene: Scene,
  drop: LayerDrop,
): { index: number; parentId?: string } | undefined {
  const source = findLayer(scene.layers, drop.id);
  const location = locateLayer(scene.layers, drop.target);
  if (
    !source ||
    source.kind === "image" ||
    !location ||
    findLayer([source], location.layer.id)
  ) {
    return;
  }
  const target = location.layer;
  if (drop.position === "inside") {
    if (
      source.children.length ||
      target.kind === "image" ||
      !scene.layers.includes(target)
    ) {
      return;
    }
    return {
      parentId: target.id,
      index: target.children.filter((item) => item.id !== source.id).length,
    };
  }
  const { siblings, parent } = location;
  if (parent && source.children.length) {
    return;
  }
  const remaining = siblings.filter((item) => item.id !== source.id);
  const index =
    remaining.findIndex((item) => item.id === target.id) +
    Number(drop.position === "before");
  if (!parent && index === 0) {
    return;
  }
  return { parentId: parent?.id, index };
}