fix(web): keep the composer open while selecting timeline text (#9499)
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
1 parent
6382268323
commit
522ebe65a5
3 files changed
+105
-1
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user