|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 |
|
3 | | -import { observeSelectionActions, resolveSelectionActionPosition } from "./selectionActions"; |
| 3 | +import { |
| 4 | + isSelectionRectVisible, |
| 5 | + observeSelectionActions, |
| 6 | + resolveSelectionActionPosition, |
| 7 | +} from "./selectionActions"; |
4 | 8 |
|
5 | 9 | function event(type: string, values: Record<string, unknown> = {}) { |
6 | 10 | return Object.assign(new Event(type, { cancelable: true }), values); |
7 | 11 | } |
8 | 12 |
|
9 | | -function createSelectionSurface({ interactiveActions = false } = {}) { |
| 13 | +function createSelectionSurface({ interactiveActions = false, restoreAfterScroll = false } = {}) { |
10 | 14 | const frames = new Map<number, FrameRequestCallback>(); |
11 | 15 | let frameId = 0; |
12 | 16 | const view = Object.assign(new EventTarget(), { |
@@ -42,6 +46,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) { |
42 | 46 | ...(interactiveActions |
43 | 47 | ? { getActionElement: () => actionElement as unknown as HTMLElement } |
44 | 48 | : {}), |
| 49 | + restoreAfterScroll, |
45 | 50 | onSelection, |
46 | 51 | onDismiss, |
47 | 52 | }); |
@@ -294,6 +299,58 @@ describe("selection action gestures", () => { |
294 | 299 | expect(surface.onSelection).toHaveBeenCalledOnce(); |
295 | 300 | }); |
296 | 301 |
|
| 302 | + it("re-reads the selection once scrolling settles when restoring after scroll", () => { |
| 303 | + const surface = createSelectionSurface({ restoreAfterScroll: true }); |
| 304 | + surface.down(); |
| 305 | + surface.up({ x: 420, y: 310 }); |
| 306 | + surface.flush(); |
| 307 | + surface.onDismiss.mockClear(); |
| 308 | + surface.element.dispatchEvent(event("scroll")); |
| 309 | + surface.flush(100); |
| 310 | + surface.element.dispatchEvent(event("scroll")); |
| 311 | + surface.flush(100); |
| 312 | + expect(surface.onDismiss).toHaveBeenCalledWith("cancel"); |
| 313 | + expect(surface.onSelection).toHaveBeenCalledOnce(); |
| 314 | + surface.flush(50); |
| 315 | + expect(surface.onSelection).toHaveBeenLastCalledWith(null); |
| 316 | + expect(surface.onSelection).toHaveBeenCalledTimes(2); |
| 317 | + }); |
| 318 | + |
| 319 | + it("keeps an Escape dismissal through a later scroll when restoring after scroll", () => { |
| 320 | + const surface = createSelectionSurface({ restoreAfterScroll: true }); |
| 321 | + surface.down(); |
| 322 | + surface.up(); |
| 323 | + surface.flush(); |
| 324 | + surface.key("Escape"); |
| 325 | + surface.element.dispatchEvent(event("scroll")); |
| 326 | + surface.change(); |
| 327 | + surface.flush(1000); |
| 328 | + expect(surface.onSelection).toHaveBeenCalledOnce(); |
| 329 | + }); |
| 330 | + |
| 331 | + it("keeps an Escape during a settling scroll dismissed when restoring after scroll", () => { |
| 332 | + const surface = createSelectionSurface({ restoreAfterScroll: true }); |
| 333 | + surface.down(); |
| 334 | + surface.up(); |
| 335 | + surface.flush(); |
| 336 | + surface.element.dispatchEvent(event("scroll")); |
| 337 | + surface.key("Escape"); |
| 338 | + surface.change(); |
| 339 | + surface.flush(1000); |
| 340 | + expect(surface.onSelection).toHaveBeenCalledOnce(); |
| 341 | + }); |
| 342 | + |
| 343 | + it("teardown cancels a pending scroll restore", () => { |
| 344 | + const surface = createSelectionSurface({ restoreAfterScroll: true }); |
| 345 | + surface.down(); |
| 346 | + surface.up(); |
| 347 | + surface.flush(); |
| 348 | + surface.element.dispatchEvent(event("scroll")); |
| 349 | + surface.actions.dispose(); |
| 350 | + surface.flush(1000); |
| 351 | + expect(surface.onSelection).toHaveBeenCalledOnce(); |
| 352 | + }); |
| 353 | + |
297 | 354 | it("cancels a queued animation frame when a new press supersedes the release", () => { |
298 | 355 | const surface = createSelectionSurface(); |
299 | 356 | surface.down(); |
@@ -387,6 +444,39 @@ describe("interactive selection actions", () => { |
387 | 444 | }); |
388 | 445 | }); |
389 | 446 |
|
| 447 | +describe("selection visibility", () => { |
| 448 | + const rect = (left: number, top: number, right: number, bottom: number) => ({ |
| 449 | + left, |
| 450 | + top, |
| 451 | + right, |
| 452 | + bottom, |
| 453 | + width: right - left, |
| 454 | + }); |
| 455 | + const timeline = rect(0, 0, 800, 600); |
| 456 | + const codeBlock = rect(100, 200, 400, 260); |
| 457 | + |
| 458 | + it("shows a selection inside every clipping box", () => { |
| 459 | + expect(isSelectionRectVisible(rect(150, 210, 220, 226), [timeline, codeBlock])).toBe(true); |
| 460 | + }); |
| 461 | + |
| 462 | + it.each([ |
| 463 | + ["scrolled left out of a code block", rect(40, 210, 90, 226)], |
| 464 | + ["scrolled right out of a code block", rect(420, 210, 480, 226)], |
| 465 | + ["touching the code block's edge", rect(60, 210, 100, 226)], |
| 466 | + ])("hides a selection %s", (_, selection) => { |
| 467 | + expect(isSelectionRectVisible(selection, [timeline, codeBlock])).toBe(false); |
| 468 | + }); |
| 469 | + |
| 470 | + it("hides a selection above the timeline or with no width", () => { |
| 471 | + expect(isSelectionRectVisible(rect(150, -40, 220, -24), [timeline])).toBe(false); |
| 472 | + expect(isSelectionRectVisible(rect(150, 210, 150, 226), [timeline])).toBe(false); |
| 473 | + }); |
| 474 | + |
| 475 | + it("shows a selection that is only partly clipped", () => { |
| 476 | + expect(isSelectionRectVisible(rect(380, 210, 460, 226), [timeline, codeBlock])).toBe(true); |
| 477 | + }); |
| 478 | +}); |
| 479 | + |
390 | 480 | describe("selection action positioning", () => { |
391 | 481 | const bounds = { left: 100, top: 50, width: 500, height: 220 }; |
392 | 482 | const viewport = { width: 1024, height: 768 }; |
|
0 commit comments