GeezerVison is showing github.com — fix(web): the Cite button comes back once the timeline stops scrolling by Mnigos · Pull Request #15818 · pingdotgg/t3code
fix(web): the Cite button comes back once the timeline stops scrolling
#15818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter
Filter by extension
.ts
(2)
.tsx
(1)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -12,6 +12,7 @@ import {
type AssistantCitationSourceAnchor,
} from "~/lib/assistantTextSelection";
import {
isSelectionRectVisible,
observeSelectionActions,
resolveSelectionActionPosition,
type SelectionActionPoint,
Expand Down
Expand Up
@@ -56,7 +57,16 @@ export function AssistantSelectionToolbar({
}
const rect = captured.range.getBoundingClientRect();
const viewportRect = viewport.getBoundingClientRect();
if (rect.bottom < viewportRect.top || rect.top > viewportRect.bottom || rect.width === 0) {
// Code blocks and other nested scrollers can clip the range inside the timeline.
const clips = [viewportRect];
const common = captured.range.commonAncestorContainer;
let element = common instanceof Element ? common : common.parentElement;
for (; element && element !== viewport; element = element.parentElement) {
if (getComputedStyle(element).overflow !== "visible") {
clips.push(element.getBoundingClientRect());
}
}
if (!isSelectionRectVisible(rect, clips)) {
clear();
return;
}
Expand Down
Expand Up
@@ -89,6 +99,7 @@ export function AssistantSelectionToolbar({
const actions = observeSelectionActions({
element: viewport,
getActionElement: () => toolbarRef.current,
restoreAfterScroll: true,
onSelection: update,
onDismiss: clear,
});
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,16 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { observeSelectionActions, resolveSelectionActionPosition } from "./selectionActions";
import {
isSelectionRectVisible,
observeSelectionActions,
resolveSelectionActionPosition,
} from "./selectionActions";
function event(type: string, values: Record<string, unknown> = {}) {
return Object.assign(new Event(type, { cancelable: true }), values);
}
function createSelectionSurface({ interactiveActions = false } = {}) {
function createSelectionSurface({ interactiveActions = false, restoreAfterScroll = false } = {}) {
const frames = new Map<number, FrameRequestCallback>();
let frameId = 0;
const view = Object.assign(new EventTarget(), {
Expand Down
Expand Up
@@ -42,6 +46,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
...(interactiveActions
? { getActionElement: () => actionElement as unknown as HTMLElement }
: {}),
restoreAfterScroll,
onSelection,
onDismiss,
});
Expand Down
Expand Up
@@ -294,6 +299,58 @@ describe("selection action gestures", () => {
expect(surface.onSelection).toHaveBeenCalledOnce();
});
it("re-reads the selection once scrolling settles when restoring after scroll", () => {
const surface = createSelectionSurface({ restoreAfterScroll: true });
surface.down();
surface.up({ x: 420, y: 310 });
surface.flush();
surface.onDismiss.mockClear();
surface.element.dispatchEvent(event("scroll"));
surface.flush(100);
surface.element.dispatchEvent(event("scroll"));
surface.flush(100);
expect(surface.onDismiss).toHaveBeenCalledWith("cancel");
expect(surface.onSelection).toHaveBeenCalledOnce();
surface.flush(50);
expect(surface.onSelection).toHaveBeenLastCalledWith(null);
expect(surface.onSelection).toHaveBeenCalledTimes(2);
});
it("keeps an Escape dismissal through a later scroll when restoring after scroll", () => {
const surface = createSelectionSurface({ restoreAfterScroll: true });
surface.down();
surface.up();
surface.flush();
surface.key("Escape");
surface.element.dispatchEvent(event("scroll"));
surface.change();
surface.flush(1000);
expect(surface.onSelection).toHaveBeenCalledOnce();
});
it("keeps an Escape during a settling scroll dismissed when restoring after scroll", () => {
const surface = createSelectionSurface({ restoreAfterScroll: true });
surface.down();
surface.up();
surface.flush();
surface.element.dispatchEvent(event("scroll"));
surface.key("Escape");
surface.change();
surface.flush(1000);
expect(surface.onSelection).toHaveBeenCalledOnce();
});
it("teardown cancels a pending scroll restore", () => {
const surface = createSelectionSurface({ restoreAfterScroll: true });
surface.down();
surface.up();
surface.flush();
surface.element.dispatchEvent(event("scroll"));
surface.actions.dispose();
surface.flush(1000);
expect(surface.onSelection).toHaveBeenCalledOnce();
});
it("cancels a queued animation frame when a new press supersedes the release", () => {
const surface = createSelectionSurface();
surface.down();
Expand Down
Expand Up
@@ -387,6 +444,39 @@ describe("interactive selection actions", () => {
});
});
describe("selection visibility", () => {
const rect = (left: number, top: number, right: number, bottom: number) => ({
left,
top,
right,
bottom,
width: right - left,
});
const timeline = rect(0, 0, 800, 600);
const codeBlock = rect(100, 200, 400, 260);
it("shows a selection inside every clipping box", () => {
expect(isSelectionRectVisible(rect(150, 210, 220, 226), [timeline, codeBlock])).toBe(true);
});
it.each([
["scrolled left out of a code block", rect(40, 210, 90, 226)],
["scrolled right out of a code block", rect(420, 210, 480, 226)],
["touching the code block's edge", rect(60, 210, 100, 226)],
])("hides a selection %s", (_, selection) => {
expect(isSelectionRectVisible(selection, [timeline, codeBlock])).toBe(false);
});
it("hides a selection above the timeline or with no width", () => {
expect(isSelectionRectVisible(rect(150, -40, 220, -24), [timeline])).toBe(false);
expect(isSelectionRectVisible(rect(150, 210, 150, 226), [timeline])).toBe(false);
});
it("shows a selection that is only partly clipped", () => {
expect(isSelectionRectVisible(rect(380, 210, 460, 226), [timeline, codeBlock])).toBe(true);
});
});
describe("selection action positioning", () => {
const bounds = { left: 100, top: 50, width: 500, height: 220 };
const viewport = { width: 1024, height: 768 };
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
export type SelectionActionPoint = { x: number; y: number };
export const SELECTION_MULTI_CLICK_INTERVAL_MS = 500;
const SELECTION_SCROLL_SETTLE_MS = 150;
export function resolveSelectionActionPosition(options: {
bounds: { left: number; top: number; width: number; height: number };
Expand All
@@ -25,18 +26,37 @@ export function resolveSelectionActionPosition(options: {
};
}
type SelectionRect = { left: number; top: number; right: number; bottom: number; width: number };
/** Whether a selection still shows inside every box that clips it. */
export function isSelectionRectVisible(rect: SelectionRect, clips: readonly SelectionRect[]) {
return (
rect.width > 0 &&
clips.every(
(clip) =>
rect.right > clip.left &&
rect.left < clip.right &&
rect.bottom > clip.top &&
rect.top < clip.bottom,
)
);
}
/**
* Interactive popovers pass getActionElement to keep editing their fields from replacing the
* captured source selection.
* captured source selection. Surfaces that can reposition their actions pass
* restoreAfterScroll to re-read the selection once scrolling settles.
*/
export function observeSelectionActions({
element,
getActionElement,
restoreAfterScroll = false,
onSelection,
onDismiss,
}: {
element: HTMLElement;
getActionElement?: () => HTMLElement | null;
restoreAfterScroll?: boolean;
onSelection: (pointer: SelectionActionPoint | null) => void;
onDismiss: (reason: "interaction" | "cancel") => void;
}) {
Expand Down
Expand Up
@@ -135,7 +155,8 @@ export function observeSelectionActions({
// Autoscroll during a drag must not cancel that gesture's eventual release.
if (!pointerDown) {
pointer = null;
dismissed = true;
if (restoreAfterScroll && !dismissed) schedule(SELECTION_SCROLL_SETTLE_MS);
else dismissed = true;
}
onDismiss("cancel");
};
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.