Skip to content

Free open-source image editor for the browser

4.0 KB
Raw Blame
import type { EditorDocument } from "./index";
import type {
  Layer,
  Mask,
  MaskLayer,
  Painting,
  ProcessingLayer,
  RangeMask,
  RemovePatch,
  Scene,
} from "./scene";

type LayerLocation = {
  layer: Layer;
  siblings: readonly Layer[];
  parent?: Layer;
};

/** Every layer with its position, parents before their children. */
export function* walkLayers(
  layers: readonly Layer[],
  parent?: Layer,
): Generator<LayerLocation> {
  for (const layer of layers) {
    yield { layer, siblings: layers, parent };
    yield* walkLayers(layer.children, layer);
  }
}

/** Every Remove patch with the Healing layer that holds it. */
export function* removePatches(
  layers: readonly Layer[],
): Generator<{ layer: Layer; patch: RemovePatch }> {
  for (const { layer } of walkLayers(layers)) {
    if (layer.kind !== "heal") continue;
    for (const patch of layer.patches) {
      if (patch.mode === "remove") yield { layer, patch };
    }
  }
}

export function locateLayer(layers: readonly Layer[], id: string) {
  for (const location of walkLayers(layers)) {
    if (location.layer.id === id) {
      return location;
    }
  }
  return undefined;
}

export function findLayer(layers: readonly Layer[], id: string) {
  return locateLayer(layers, id)?.layer;
}

/** A mask inside a mask only shapes coverage; its parent owns the adjustments and curve. */
export function adjustmentTarget(layers: readonly Layer[], id: string) {
  const location = locateLayer(layers, id);
  if (!location) {
    return undefined;
  }
  const { layer, parent } = location;
  return layer.kind === "mask" && parent?.kind === "mask" ? parent : layer;
}

/** The child masks that currently add to or subtract from a mask's coverage. */
export function maskModifiers(layer: MaskLayer) {
  return layer.children.filter(
    (child): child is MaskLayer =>
      child.kind === "mask" && child.visible && child.opacity > 0,
  );
}

/** Whether a mask selects by the image below it rather than by position. */
export function isRangeMask(mask: Mask): mask is RangeMask {
  return mask.kind === "luminance-range" || mask.kind === "color-range";
}

/** Whether a painting holds anything: settled pixels or strokes over them. */
export function hasPaint(painting: Painting) {
  return painting.raster !== undefined || painting.strokes.length > 0;
}

/** What a paint layer or brush mask painted; other layers paint nothing. */
export function paintingOf(layer: Layer): Painting | undefined {
  if (layer.kind === "paint") {
    return layer;
  }
  if (layer.kind === "mask" && layer.mask.kind === "brush") {
    return layer.mask;
  }
  return undefined;
}

function withChildren<L extends Layer>(
  layer: L,
  children: readonly ProcessingLayer[],
): L {
  return children.every((child, index) => child === layer.children[index])
    ? layer
    : { ...layer, children };
}

export function updateLayer(
  scene: Scene,
  id: string,
  update: (layer: Layer) => Layer,
): Scene {
  const [image, ...layers] = scene.layers;
  let matched = image.id === id;
  function updateChildren(
    items: readonly ProcessingLayer[],
  ): readonly ProcessingLayer[] {
    return items.map((layer) => {
      if (layer.id !== id) {
        return withChildren(layer, updateChildren(layer.children));
      }
      matched = true;
      const next = update(layer);
      if (next.kind === "image") {
        throw Error("An image cannot replace a processing layer.");
      }
      return next;
    });
  }
  const children = updateChildren(image.children);
  const base = image.id === id ? update(image) : image;
  if (base.kind !== "image") {
    throw Error("The image layer is pinned.");
  }
  const next: Scene["layers"] = [
    withChildren(base, children),
    ...updateChildren(layers),
  ];
  if (!matched) {
    throw Error("Layer is unavailable.");
  }
  return { ...scene, layers: next };
}

export function editLayer(
  document: EditorDocument,
  id: string,
  update: (layer: Layer) => Layer,
) {
  document.edit(updateLayer(document.scene.getState(), id, update));
}