Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.3 KB
Raw Blame
/**
 * The part of [x0, x1] that no blocked range covers, preferring the free stretch that contains
 * `near` (where Lia already stands) and otherwise the widest one. Returns null when nothing at
 * least `minWidth` wide is left.
 */
export function freeSpan(
  x0: number,
  x1: number,
  blocked: ReadonlyArray<readonly [number, number]>,
  near: number,
  minWidth = 0,
): readonly [number, number] | null {
  let spans: Array<[number, number]> = [[x0, x1]];
  for (const [a, b] of blocked) {
    const b0 = Math.min(a, b);
    const b1 = Math.max(a, b);
    /* An empty range covers nothing, so it splits nothing. */
    if (b1 <= b0) {
      continue;
    }
    const next: Array<[number, number]> = [];
    for (const [s0, s1] of spans) {
      if (b1 <= s0 || b0 >= s1) {
        next.push([s0, s1]);
        continue;
      }
      if (b0 > s0) {
        next.push([s0, b0]);
      }
      if (b1 < s1) {
        next.push([b1, s1]);
      }
    }
    spans = next;
  }
  const usable = spans.filter(([s0, s1]) => s1 - s0 >= minWidth);
  if (usable.length === 0) {
    return null;
  }
  const containing = usable.find(([s0, s1]) => near >= s0 && near <= s1);
  if (containing) {
    return containing;
  }
  return usable.reduce((best, span) => (span[1] - span[0] > best[1] - best[0] ? span : best));
}