Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Cite selected chat text with Cmd+L / Ctrl+L (#4535)

AdityaUmaleaa77dd3b9625
diff --git a/src/components/CitationUI.tsx b/src/components/CitationUI.tsx
index e898764d98..20748568a4 100644
--- a/src/components/CitationUI.tsx
+++ b/src/components/CitationUI.tsx
@@ -8,7 +8,8 @@ import {
withCitationComment,
type CitationAttachment,
} from "@/lib/citations";
-import { captureCitationSelection, citationTabShortcut } from "@/lib/citations-dom";
+import { captureCitationSelection, citationShortcutHint, citationTabShortcut, isCitationShortcut } from "@/lib/citations-dom";
+import { isMacPlatform } from "@/lib/keyboard-shortcuts";
import { sendsMessage, useSendKey } from "@/lib/send-key";
type Point = { left: number; top: number };
@@ -110,6 +111,7 @@ export function CitationSelectionToolbar({
}) {
const buttonRef = useRef<HTMLButtonElement>(null);
const tabShortcut = useRef(citationTabShortcut()).current;
+ const isMac = useRef(isMacPlatform()).current;
const [captured, setCaptured] = useState<{
citation: CitationAttachment;
point: Point;
@@ -145,7 +147,15 @@ export function CitationSelectionToolbar({
viewportRef.current?.addEventListener("pointerup", update);
viewportRef.current?.addEventListener("keyup", update);
const focusAction = (event: KeyboardEvent) => {
- if (captured && !editing) tabShortcut.handle(event, buttonRef.current);
+ if (!captured || editing) return;
+ if (isCitationShortcut(event, isMac)) {
+ if (captured.citation.quote.length > CITATION_MAX_QUOTE_LENGTH) return;
+ event.preventDefault();
+ event.stopPropagation();
+ setEditing(true);
+ return;
+ }
+ tabShortcut.handle(event, buttonRef.current);
};
document.addEventListener("keydown", focusAction, true);
return () => {
@@ -154,7 +164,7 @@ export function CitationSelectionToolbar({
viewportRef.current?.removeEventListener("keyup", update);
document.removeEventListener("keydown", focusAction, true);
};
- }, [captured, editing, tabShortcut, viewportRef]);
+ }, [captured, editing, isMac, tabShortcut, viewportRef]);
useLayoutEffect(() => {
if (!captured || editing) return;
@@ -179,12 +189,15 @@ export function CitationSelectionToolbar({
/>;
}
const tooLong = captured.citation.quote.length > CITATION_MAX_QUOTE_LENGTH;
+ const shortcut = citationShortcutHint(isMac);
return createPortal(
<button
ref={buttonRef}
type="button"
disabled={tooLong}
aria-label={tooLong ? "Selection is too long to cite" : "Cite selected text"}
+ aria-keyshortcuts={tooLong ? undefined : isMac ? "Meta+L" : "Control+L"}
+ title={tooLong ? undefined : `Cite selected text (${shortcut})`}
onPointerDown={(event) => event.preventDefault()}
onClick={() => setEditing(true)}
onKeyDown={(event) => {
@@ -194,6 +207,7 @@ export function CitationSelectionToolbar({
style={captured.point}
>
<Quote size={13} aria-hidden="true" /> {tooLong ? "Shorten selection" : "Cite"}
+ {!tooLong && <kbd aria-hidden="true" className="font-sans text-[11px] font-normal text-ink-secondary">{shortcut}</kbd>}
</button>,
document.body,
);
diff --git a/src/lib/citations-dom.ts b/src/lib/citations-dom.ts
index 3f56391758..e12dc89e25 100644
--- a/src/lib/citations-dom.ts
+++ b/src/lib/citations-dom.ts
@@ -157,3 +157,16 @@ export function citationTabShortcut() {
},
};
}
+
+type CitationShortcutEvent = Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey" | "altKey" | "shiftKey" | "isComposing" | "defaultPrevented">;
+
+/** Cmd+L on Mac, Ctrl+L elsewhere: opens the cite card for the selected chat text. */
+export function isCitationShortcut(event: CitationShortcutEvent, isMac: boolean): boolean {
+ if (event.defaultPrevented || event.isComposing || event.altKey || event.shiftKey) return false;
+ if (isMac ? !event.metaKey || event.ctrlKey : !event.ctrlKey || event.metaKey) return false;
+ return event.key.toLowerCase() === "l";
+}
+
+export function citationShortcutHint(isMac: boolean): string {
+ return isMac ? "⌘L" : "Ctrl+L";
+}
diff --git a/src/lib/citations.test.ts b/src/lib/citations.test.ts
index 79286abb9a..92492f5eda 100644
--- a/src/lib/citations.test.ts
+++ b/src/lib/citations.test.ts
@@ -10,7 +10,7 @@ import {
splitTranscriptCitations,
withCitationComment,
} from "./citations";
-import { citationTabShortcut, findCitationSource, highlightCitationSource } from "./citations-dom";
+import { citationShortcutHint, citationTabShortcut, findCitationSource, highlightCitationSource, isCitationShortcut } from "./citations-dom";
const source = { ownerType: "bot" as const, ownerId: "bot-1", threadId: "thread-1", messageId: "message-1" };
@@ -131,6 +131,35 @@ describe("selected-text citations", () => {
});
});
+describe("cite shortcut (Cmd+L / Ctrl+L)", () => {
+ const key = (init: Partial<KeyboardEvent>) => ({ key: "l", metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, isComposing: false, defaultPrevented: false, ...init });
+
+ it("is Cmd+L on Mac and Ctrl+L elsewhere", () => {
+ expect(isCitationShortcut(key({ metaKey: true }), true)).toBe(true);
+ expect(isCitationShortcut(key({ metaKey: true, key: "L" }), true)).toBe(true);
+ expect(isCitationShortcut(key({ ctrlKey: true }), false)).toBe(true);
+ expect(isCitationShortcut(key({ ctrlKey: true }), true)).toBe(false);
+ expect(isCitationShortcut(key({ metaKey: true }), false)).toBe(false);
+ });
+
+ it("leaves other keys and chords alone", () => {
+ for (const event of [
+ key({}),
+ key({ ctrlKey: true, key: "k" }),
+ key({ ctrlKey: true, shiftKey: true }),
+ key({ ctrlKey: true, altKey: true }),
+ key({ ctrlKey: true, metaKey: true }),
+ key({ ctrlKey: true, isComposing: true }),
+ key({ ctrlKey: true, defaultPrevented: true }),
+ ]) expect(isCitationShortcut(event, false)).toBe(false);
+ });
+
+ it("shows the shortcut for the platform", () => {
+ expect(citationShortcutHint(true)).toBe("⌘L");
+ expect(citationShortcutHint(false)).toBe("Ctrl+L");
+ });
+});
+
describe("citation source highlight cleanup", () => {
const text = "quoted text in the source";
const citation = citationAttachment(source, createCitationTextSelector(text, 0, 11)!);