Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep the app in place when jumping to a message (#8026)
Bugfixer 2f0c8f71bc247
diff --git a/src/lib/citations-dom.ts b/src/lib/citations-dom.tsindex e12dc89e25..61b4101de0 100644--- a/src/lib/citations-dom.ts+++ b/src/lib/citations-dom.ts@@ -1,4 +1,5 @@import { createCitationTextSelector, findCitationText, type CitationAttachment } from "./citations";+import { scrollToCenter } from "./scroll-within";type TextChunk = { node: Text; start: number; end: number };const EXCLUDED_SELECTOR = "button, input, textarea, select, [role=button], [contenteditable], [hidden], [aria-hidden=true], script, style, template, noscript, svg";@@ -122,7 +123,7 @@ export async function highlightCitationSource(citation: CitationAttachment): Proconst source = findCitationSource(document, citation);const range = source ? resolveCitationRange(source, citation) : null;if (source && range) {- source.scrollIntoView({ block: "center", behavior: "smooth" });+ scrollToCenter(source, "smooth");const highlights = (CSS as unknown as { highlights?: { set(name: string, value: unknown): void; delete(name: string): void } }).highlights;const HighlightConstructor = (globalThis as unknown as { Highlight?: new (range: Range) => unknown }).Highlight;window.clearTimeout(highlightCleanup);diff --git a/src/lib/focus-message.test.ts b/src/lib/focus-message.test.tsindex 9081dc0d43..84be0e9a6d 100644--- a/src/lib/focus-message.test.ts+++ b/src/lib/focus-message.test.ts@@ -14,15 +14,18 @@ import { useFocusMessage } from "./focus-message";let cleanup: (() => void) | undefined;let ranges: Array<{ start: number; end: number }>;let highlights: Map<string, unknown>;-let target: { scrollIntoView: ReturnType<typeof vi.fn>; classList: { add: ReturnType<typeof vi.fn>; remove: ReturnType<typeof vi.fn> }; querySelector: () => unknown; querySelectorAll: () => { item: () => unknown; length: number } };+let scroller: { parentElement: null; scrollTop: number; scrollHeight: number; clientHeight: number; getBoundingClientRect: () => { top: number }; scrollTo: ReturnType<typeof vi.fn> };+let target: { parentElement: typeof scroller; offsetHeight: number; getBoundingClientRect: () => { top: number }; scrollIntoView: ReturnType<typeof vi.fn>; classList: { add: ReturnType<typeof vi.fn>; remove: ReturnType<typeof vi.fn> }; querySelector: () => unknown; querySelectorAll: () => { item: () => unknown; length: number } };beforeEach(() => {vi.useFakeTimers();fixture.dispatch.mockClear();ranges = [];highlights = new Map();const node = { data: "alpha\nbeta a.b", length: 14 };- target = { scrollIntoView: vi.fn(), classList: { add: vi.fn(), remove: vi.fn() }, querySelector: () => target, querySelectorAll: () => ({ item: () => target, length: 1 }) };+ scroller = { parentElement: null, scrollTop: 1000, scrollHeight: 4000, clientHeight: 800, getBoundingClientRect: () => ({ top: 100 }), scrollTo: vi.fn() };+ target = { parentElement: scroller, offsetHeight: 40, getBoundingClientRect: () => ({ top: 300 }), scrollIntoView: vi.fn(), classList: { add: vi.fn(), remove: vi.fn() }, querySelector: () => target, querySelectorAll: () => ({ item: () => target, length: 1 }) };vi.stubGlobal("window", { matchMedia: () => ({ matches: true }) });+ vi.stubGlobal("getComputedStyle", (node: unknown) => ({ overflowY: node === scroller ? "auto" : "visible" }));vi.stubGlobal("NodeFilter", { SHOW_TEXT: 4 });vi.stubGlobal("CSS", { escape: (value: string) => value, highlights });vi.stubGlobal("Highlight", class { constructor(readonly range: unknown) {} });@@ -56,7 +59,7 @@ describe("search match highlighting", () => {useFocusMessage("thread", true, { current: scoped as unknown as HTMLElement });cleanup = fixture.effect!() as () => void;expect(scoped.querySelector).toHaveBeenCalledWith('[data-mid="message"]');- expect(target.scrollIntoView).toHaveBeenCalledOnce();+ expect(scroller.scrollTo).toHaveBeenCalledOnce();});it.each(["", " ", "\t\r\n"])("does not create an empty range for %j", (text) => {@@ -64,7 +67,9 @@ describe("search match highlighting", () => {expect(ranges).toEqual([]);expect(highlights.size).toBe(0);expect(requestAnimationFrame).not.toHaveBeenCalled();- expect(target.scrollIntoView).toHaveBeenCalledWith({ block: "center", behavior: "auto" });+ // Only the transcript moves: scrollIntoView would also scroll the page and slide the whole app up.+ expect(scroller.scrollTo).toHaveBeenCalledWith({ top: 820, behavior: "auto" });+ expect(target.scrollIntoView).not.toHaveBeenCalled();expect(fixture.dispatch).toHaveBeenCalledWith({ type: "focusMessageConsumed", nonce: 1 });});diff --git a/src/lib/focus-message.ts b/src/lib/focus-message.tsindex 545e27ad72..7ba45688c8 100644--- a/src/lib/focus-message.ts+++ b/src/lib/focus-message.ts@@ -4,6 +4,7 @@ import { api, MESSAGE_PAGE_SIZE, type Action, type AppState, type Message } fromimport { useStoreDispatch, useStoreSelector } from "@/state/store-select";import type { SearchHit } from "@/lib/search-hit";import { t } from "@/lib/i18n";+import { scrollToCenter } from "@/lib/scroll-within";const FLASH_CLASSES = ["ring-2", "ring-accent/70", "rounded-2xl", "transition-shadow"];@@ -122,7 +123,7 @@ export function useFocusMessage(threadId: string, ready: boolean, rootRef?: RefOreturn;}const reducedMotion = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;- target.scrollIntoView({ block: "center", behavior: reducedMotion ? "auto" : "smooth" });+ scrollToCenter(target, reducedMotion ? "auto" : "smooth");target.classList.add(...FLASH_CLASSES);// A reply split into bubbles and table cards has several text roots; a quoted reply's text is not the message's.const found = target.querySelectorAll<HTMLElement>(".chat-md, .chat-text");diff --git a/src/lib/scroll-within.test.ts b/src/lib/scroll-within.test.tsnew file mode 100644index 0000000000..cf317d7684--- /dev/null+++ b/src/lib/scroll-within.test.ts@@ -0,0 +1,25 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { scrollToCenter } from "./scroll-within";++afterEach(() => { vi.unstubAllGlobals(); });++function setup(padding: { scrollPaddingTop?: string; scrollPaddingBottom?: string } = {}) {+ const scroller = { parentElement: null, scrollTop: 1000, scrollHeight: 4000, clientHeight: 800, getBoundingClientRect: () => ({ top: 100 }), scrollTo: vi.fn() };+ const target = { parentElement: scroller, offsetHeight: 40, getBoundingClientRect: () => ({ top: 300 }), scrollIntoView: vi.fn() };+ vi.stubGlobal("getComputedStyle", (node: unknown) => (node === scroller ? { overflowY: "auto", ...padding } : { overflowY: "visible" }));+ scrollToCenter(target as unknown as HTMLElement, "smooth");+ return { scroller, target };+}++describe("scrollToCenter", () => {+ it("moves only the nearest scroller, never the page", () => {+ const { scroller, target } = setup();+ expect(scroller.scrollTo).toHaveBeenCalledWith({ top: 820, behavior: "smooth" });+ expect(target.scrollIntoView).not.toHaveBeenCalled();+ });++ it("centres in the room the scroll-padding leaves above the docked composer", () => {+ const { scroller } = setup({ scrollPaddingTop: "auto", scrollPaddingBottom: "160px" });+ expect(scroller.scrollTo).toHaveBeenCalledWith({ top: 900, behavior: "smooth" });+ });+});diff --git a/src/lib/scroll-within.ts b/src/lib/scroll-within.tsindex e3144992be..7f8c4acf28 100644--- a/src/lib/scroll-within.ts+++ b/src/lib/scroll-within.ts@@ -1,5 +1,9 @@type Box = { getBoundingClientRect(): { top: number } };+function topIn(scroller: Box & { scrollTop: number }, target: Box): number {+ return target.getBoundingClientRect().top - scroller.getBoundingClientRect().top + scroller.scrollTop;+}+/** Moves `scroller` alone so `target` sits at its top or centre. Element.scrollIntoView would also scroll every* overflow-hidden ancestor, sliding a dialog's frame and its header out of view. */export function scrollWithin(@@ -8,7 +12,7 @@ export function scrollWithin(block: "start" | "center" = "start",margin = 0,): void {- const top = target.getBoundingClientRect().top - scroller.getBoundingClientRect().top + scroller.scrollTop;+ const top = topIn(scroller, target);scroller.scrollTop = Math.max(0, block === "center" ? top - (scroller.clientHeight - (target.offsetHeight ?? 0)) / 2 : top - margin);}@@ -19,3 +23,14 @@ export function scrollParent(element: Element): HTMLElement | null {}return null;}++/** Centres `target` in its nearest scrolling ancestor only, within that scroller's scroll-padding (the docked+ * composer's room), so the page and the app's frames stay where they are. */+export function scrollToCenter(target: HTMLElement, behavior: ScrollBehavior): void {+ const scroller = scrollParent(target);+ if (!scroller) return;+ const style = getComputedStyle(scroller);+ const padTop = parseFloat(style.scrollPaddingTop) || 0;+ const visible = scroller.clientHeight - padTop - (parseFloat(style.scrollPaddingBottom) || 0);+ scroller.scrollTo({ top: Math.max(0, topIn(scroller, target) - padTop - (visible - target.offsetHeight) / 2), behavior });+}