Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

1.9 KB
Raw Blame
export type DiagramFitBounds = {
  x: number;
  y: number;
  width: number;
  height: number;
};

export const DIAGRAM_CANVAS_READY_MIN_SIZE = 32;

export const diagramCanvasIsReady = (
  element: { clientWidth: number; clientHeight: number } | null | undefined,
) => Boolean(
  element
  && element.clientWidth >= DIAGRAM_CANVAS_READY_MIN_SIZE
  && element.clientHeight >= DIAGRAM_CANVAS_READY_MIN_SIZE,
);

const isFiniteNumber = (value: unknown): value is number =>
  typeof value === "number" && Number.isFinite(value);

export const isUsableDiagramBounds = (bounds: DiagramFitBounds | null | undefined): bounds is DiagramFitBounds => {
  if (!bounds) return false;
  return isFiniteNumber(bounds.x)
    && isFiniteNumber(bounds.y)
    && isFiniteNumber(bounds.width)
    && isFiniteNumber(bounds.height)
    && bounds.width >= 0
    && bounds.height >= 0
    && (bounds.width > 0 || bounds.height > 0);
};

const isEmptyContentArea = (area: unknown) => {
  if (!area || typeof area !== "object") return true;
  const box = area as { width?: unknown; height?: unknown };
  return (!isFiniteNumber(box.width) || box.width === 0)
    && (!isFiniteNumber(box.height) || box.height === 0);
};

// X6 Scroller.autoResize calls fitToContent. When autoResize is suspended it
// substitutes an empty rectangle, which shrinks the paper and clips nodes —
// especially mind-map branches left of the origin. Always prefer node geometry.
export const applyDiagramScrollerFitOptions = (
  options: Record<string, unknown>,
  nodeBounds: DiagramFitBounds | null | undefined,
): Record<string, unknown> => {
  if (isUsableDiagramBounds(nodeBounds)) {
    options.contentArea = {
      x: nodeBounds.x,
      y: nodeBounds.y,
      width: nodeBounds.width,
      height: nodeBounds.height,
    };
    return options;
  }
  if (isEmptyContentArea(options.contentArea)) {
    delete options.contentArea;
  }
  return options;
};