History

fix(web): keep the composer open while selecting timeline text (#9499)

Julius Marminge committed 2 months ago

Commit 522ebe61 parent 63822683 files · +105 −1Browse files

Unified · Split

3 files
  1. apps/web/src/components/chat/ChatComposer.tsx +35 −1
  2. apps/web/src/components/chat/composerSelectionHold.test.ts +55 −0
  3. apps/web/src/components/chat/composerSelectionHold.ts +15 −0
apps/web/src/components/chat/ChatComposer.tsx+35−1
@@ -206,6 +206,7 @@ import { · 205 unmodified lines
206206 resetComposerScrollGesture,
207207 suppressActiveComposerScrollGesture,
208208 } from "./composerScrollGesture";
209+import { selectionHoldsComposerOpen } from "./composerSelectionHold";
209210 import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";
210211
211212 function ComposerVideoThumbnail({ file }: { file: File }) {
@@ -4293,9 +4294,42 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) · 4081 unmodified lines
42934294 ) {
42944295 return;
42954296 }
4297+ if (
4298+ !isMobileViewport &&
4299+ selectionHoldsComposerOpen(window.getSelection(), getTimelineScrollableNode())
4300+ ) {
4301+ // The check runs again once the selection clears.
4302+ return;
4303+ }
42964304 setIsComposerFocused(false);
42974305 });
4298− }, [isMobileViewport]);
4306+ }, [getTimelineScrollableNode, isMobileViewport]);
4307+
4308+ // A held collapse settles when the selection goes away, whether the user
4309+ // clicked elsewhere, pressed Escape, or used the selection toolbar.
4310+ useEffect(() => {
4311+ if (isMobileViewport || !isComposerFocused) return;
4312+ let wasHolding = false;
4313+ const handleSelectionChange = () => {
4314+ const holding = selectionHoldsComposerOpen(
4315+ window.getSelection(),
4316+ getTimelineScrollableNode(),
4317+ );
4318+ if (wasHolding && !holding) {
4319+ scheduleComposerCollapseCheck();
4320+ }
4321+ wasHolding = holding;
4322+ };
4323+ document.addEventListener("selectionchange", handleSelectionChange);
4324+ return () => {
4325+ document.removeEventListener("selectionchange", handleSelectionChange);
4326+ };
4327+ }, [
4328+ getTimelineScrollableNode,
4329+ isComposerFocused,
4330+ isMobileViewport,
4331+ scheduleComposerCollapseCheck,
4332+ ]);
42994333
43004334 useEffect(() => {
43014335 if (isMobileViewport || !isComposerFocused) return;
apps/web/src/components/chat/composerSelectionHold.test.ts+55−0
@@ -0,0 +1,55 @@
1+import { describe, expect, it } from "vite-plus/test";
2+
3+import { selectionHoldsComposerOpen } from "./composerSelectionHold";
4+
5+function node(parent: Node | null = null) {
6+ const self = {
7+ parent,
8+ contains(other: Node | null): boolean {
9+ let cursor: { parent: Node | null } | null = other as unknown as { parent: Node | null };
10+ while (cursor) {
11+ if (cursor === (self as unknown)) return true;
12+ cursor = cursor.parent as { parent: Node | null } | null;
13+ }
14+ return false;
15+ },
16+ };
17+ return self as unknown as Node;
18+}
19+
20+function selection(anchor: Node, collapsed = false) {
21+ return {
22+ isCollapsed: collapsed,
23+ rangeCount: 1,
24+ getRangeAt: () => ({ commonAncestorContainer: anchor }) as Range,
25+ };
26+}
27+
28+describe("selectionHoldsComposerOpen", () => {
29+ const timeline = node();
30+ const message = node(timeline);
31+ const elsewhere = node();
32+
33+ it("holds for a range inside the timeline", () => {
34+ expect(selectionHoldsComposerOpen(selection(message), timeline)).toBe(true);
35+ });
36+
37+ it("ignores a caret with nothing selected", () => {
38+ expect(selectionHoldsComposerOpen(selection(message, true), timeline)).toBe(false);
39+ });
40+
41+ it("ignores selections outside the timeline", () => {
42+ expect(selectionHoldsComposerOpen(selection(elsewhere), timeline)).toBe(false);
43+ });
44+
45+ it("ignores an empty or missing selection", () => {
46+ expect(selectionHoldsComposerOpen(null, timeline)).toBe(false);
47+ expect(
48+ selectionHoldsComposerOpen(
49+ { isCollapsed: false, rangeCount: 0, getRangeAt: () => ({}) as Range },
50+ timeline,
51+ ),
52+ ).toBe(false);
53+ expect(selectionHoldsComposerOpen(selection(message), null)).toBe(false);
54+ });
55+});
apps/web/src/components/chat/composerSelectionHold.ts+15−0
@@ -0,0 +1,15 @@
1+/**
2+ * Whether a live text selection inside the timeline should hold the composer
3+ * open. A drag-select in the conversation blurs the composer, and letting it
4+ * rest mid-gesture reflows the timeline, which dismisses the selection
5+ * toolbar the user was about to use.
6+ */
7+export function selectionHoldsComposerOpen(
8+ selection: Pick<Selection, "isCollapsed" | "rangeCount" | "getRangeAt"> | null,
9+ timeline: Node | null,
10+): boolean {
11+ if (!timeline || !selection || selection.isCollapsed || selection.rangeCount === 0) {
12+ return false;
13+ }
14+ return timeline.contains(selection.getRangeAt(0).commonAncestorContainer);
15+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t