TypeScript
fix(web): keep the composer open while selecting timeline text (#9499)
Julius Marminge522ebe65a542
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex df2f88f80a8..5baf34c4ae3 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -206,6 +206,7 @@ import {resetComposerScrollGesture,suppressActiveComposerScrollGesture,} from "./composerScrollGesture";+import { selectionHoldsComposerOpen } from "./composerSelectionHold";import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";function ComposerVideoThumbnail({ file }: { file: File }) {@@ -4293,9 +4294,42 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)) {return;}+ if (+ !isMobileViewport &&+ selectionHoldsComposerOpen(window.getSelection(), getTimelineScrollableNode())+ ) {+ // The check runs again once the selection clears.+ return;+ }setIsComposerFocused(false);});- }, [isMobileViewport]);+ }, [getTimelineScrollableNode, isMobileViewport]);++ // A held collapse settles when the selection goes away, whether the user+ // clicked elsewhere, pressed Escape, or used the selection toolbar.+ useEffect(() => {+ if (isMobileViewport || !isComposerFocused) return;+ let wasHolding = false;+ const handleSelectionChange = () => {+ const holding = selectionHoldsComposerOpen(+ window.getSelection(),+ getTimelineScrollableNode(),+ );+ if (wasHolding && !holding) {+ scheduleComposerCollapseCheck();+ }+ wasHolding = holding;+ };+ document.addEventListener("selectionchange", handleSelectionChange);+ return () => {+ document.removeEventListener("selectionchange", handleSelectionChange);+ };+ }, [+ getTimelineScrollableNode,+ isComposerFocused,+ isMobileViewport,+ scheduleComposerCollapseCheck,+ ]);useEffect(() => {if (isMobileViewport || !isComposerFocused) return;diff --git a/apps/web/src/components/chat/composerSelectionHold.test.ts b/apps/web/src/components/chat/composerSelectionHold.test.tsnew file mode 100644index 00000000000..fb4d52552a1--- /dev/null+++ b/apps/web/src/components/chat/composerSelectionHold.test.ts@@ -0,0 +1,55 @@+import { describe, expect, it } from "vite-plus/test";++import { selectionHoldsComposerOpen } from "./composerSelectionHold";++function node(parent: Node | null = null) {+ const self = {+ parent,+ contains(other: Node | null): boolean {+ let cursor: { parent: Node | null } | null = other as unknown as { parent: Node | null };+ while (cursor) {+ if (cursor === (self as unknown)) return true;+ cursor = cursor.parent as { parent: Node | null } | null;+ }+ return false;+ },+ };+ return self as unknown as Node;+}++function selection(anchor: Node, collapsed = false) {+ return {+ isCollapsed: collapsed,+ rangeCount: 1,+ getRangeAt: () => ({ commonAncestorContainer: anchor }) as Range,+ };+}++describe("selectionHoldsComposerOpen", () => {+ const timeline = node();+ const message = node(timeline);+ const elsewhere = node();++ it("holds for a range inside the timeline", () => {+ expect(selectionHoldsComposerOpen(selection(message), timeline)).toBe(true);+ });++ it("ignores a caret with nothing selected", () => {+ expect(selectionHoldsComposerOpen(selection(message, true), timeline)).toBe(false);+ });++ it("ignores selections outside the timeline", () => {+ expect(selectionHoldsComposerOpen(selection(elsewhere), timeline)).toBe(false);+ });++ it("ignores an empty or missing selection", () => {+ expect(selectionHoldsComposerOpen(null, timeline)).toBe(false);+ expect(+ selectionHoldsComposerOpen(+ { isCollapsed: false, rangeCount: 0, getRangeAt: () => ({}) as Range },+ timeline,+ ),+ ).toBe(false);+ expect(selectionHoldsComposerOpen(selection(message), null)).toBe(false);+ });+});diff --git a/apps/web/src/components/chat/composerSelectionHold.ts b/apps/web/src/components/chat/composerSelectionHold.tsnew file mode 100644index 00000000000..9c75c631e97--- /dev/null+++ b/apps/web/src/components/chat/composerSelectionHold.ts@@ -0,0 +1,15 @@+/**+ * Whether a live text selection inside the timeline should hold the composer+ * open. A drag-select in the conversation blurs the composer, and letting it+ * rest mid-gesture reflows the timeline, which dismisses the selection+ * toolbar the user was about to use.+ */+export function selectionHoldsComposerOpen(+ selection: Pick<Selection, "isCollapsed" | "rangeCount" | "getRangeAt"> | null,+ timeline: Node | null,+): boolean {+ if (!timeline || !selection || selection.isCollapsed || selection.rangeCount === 0) {+ return false;+ }+ return timeline.contains(selection.getRangeAt(0).commonAncestorContainer);+}