Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/canvas/ChatReferenceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import {
import { referenceInsertion } from "./chatReferenceInsertion.ts";
import type { ReferenceRules } from "./chatReferenceInsertion.ts";
import {
isCaretNavigationKey,
mentionCategories,
mentionDismissAfterEvent,
mentionRange,
slashRange,
REFERENCE_CATEGORIES,
Expand Down Expand Up @@ -160,6 +162,8 @@ export function ChatReferenceEditor({
alive = useRef(true);
const preparation = useRef<AbortController | null>(null),
dismissed = useRef<number | null>(null),
leftSinceDismiss = useRef(false),
pendingCaretNav = useRef(false),
explicitPicker = useRef(false);
const [range, setRange] = useState<Caret | null>(null),
rangeRef = useRef(range);
Expand All @@ -184,6 +188,8 @@ export function ChatReferenceEditor({
preparation.current?.abort();
explicitPicker.current = false;
dismissed.current = rangeRef.current?.start ?? null;
leftSinceDismiss.current = false;
pendingCaretNav.current = false;
rangeRef.current = null;
setRange(null);
}
Expand Down Expand Up @@ -396,19 +402,33 @@ export function ChatReferenceEditor({
// text. A focus selectionchange must not immediately close that picker.
if (explicitPicker.current) return;
dismissed.current = null;
leftSinceDismiss.current = false;
rangeRef.current = null;
setRange(null);
} else if (dismissed.current !== next.start) open(next);
} else {
const decision = mentionDismissAfterEvent(
dismissed.current,
leftSinceDismiss.current,
next.start,
);
dismissed.current = decision.dismissedStart;
leftSinceDismiss.current = decision.leftSinceDismiss;
if (decision.open) open(next);
}
}
useEffect(() => {
function selectionChanged() {
if (!root.current || document.activeElement !== root.current) return;
const at = selection(root.current);
if (!at) return;
if (
at &&
(at.start !== saved.current.start || at.end !== saved.current.end)
)
pendingCaretNav.current ||
at.start !== saved.current.start ||
at.end !== saved.current.end
) {
pendingCaretNav.current = false;
updateMention();
}
}
// selectionchange runs after the browser moves its native caret. A
// microtask from keydown still sees the previous position in Chromium.
Expand Down Expand Up @@ -909,6 +929,14 @@ export function ChatReferenceEditor({
event.preventDefault();
return;
}
if (
!range &&
dismissed.current !== null &&
isCaretNavigationKey(event.key)
) {
leftSinceDismiss.current = true;
pendingCaretNav.current = true;
}
if (
(event.ctrlKey || event.metaKey) &&
!event.altKey &&
Expand Down Expand Up @@ -983,6 +1011,18 @@ export function ChatReferenceEditor({
}
}
}}
onKeyUp={(event) => {
if (
pendingCaretNav.current &&
isCaretNavigationKey(
event.key,
composing.current || event.nativeEvent.isComposing,
)
) {
pendingCaretNav.current = false;
updateMention();
}
}}
onCopy={(event) => clipboard(event, false)}
onCut={(event) => clipboard(event, true)}
onDrop={(event) => {
Expand Down
35 changes: 35 additions & 0 deletions src/canvas/referenceMentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,38 @@ export function nodeMentionOptions(
];
});
}

const CARET_NAVIGATION_KEYS = new Set([
"ArrowLeft",
"ArrowRight",
"ArrowUp",
"ArrowDown",
"Home",
"End",
"PageUp",
"PageDown",
]);

/** Keys that move the caret without inserting text. */
export function isCaretNavigationKey(key: string, composing = false) {
return !composing && CARET_NAVIGATION_KEYS.has(key);
}

/** After Escape dismisses a mention, keep the picker closed until the caret
* has left that mention. A later return to the same mention reopens it.
* `leftSinceDismiss` is set when a navigation key is pressed, so a coalesced
* selectionchange that never reports the outside caret still reopens. */
export function mentionDismissAfterEvent(
dismissedStart: number | null,
leftSinceDismiss: boolean,
mentionStart: number | null,
): { dismissedStart: number | null; leftSinceDismiss: boolean; open: boolean } {
if (mentionStart === null) {
return { dismissedStart: null, leftSinceDismiss: false, open: false };
}
if (dismissedStart !== mentionStart || leftSinceDismiss) {
return { dismissedStart: null, leftSinceDismiss: false, open: true };
}
return { dismissedStart, leftSinceDismiss: false, open: false };
}

8 changes: 8 additions & 0 deletions tests/browser/reference_picker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ try {
await editor.press("ArrowLeft");
assert.equal(await caret(), 2);
await opened();
// Rapid leave/re-enter after Escape must reopen even if selectionchange
// never reports the outside caret (#394). Escape without motion stays closed.
await editor.press("Escape");
await closed();
await editor.press("ArrowRight");
await editor.press("ArrowLeft");
assert.equal(await caret(), 2);
await opened();
// Clicking elsewhere inside the same editor must dismiss the portal too.
await editor.click({ position: { x: 2, y: 8 } });
await closed();
Expand Down
42 changes: 42 additions & 0 deletions tests/referenceMentions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { createNode } from "../src/canvas/model.ts";
import { referenceCandidates } from "../src/canvas/referenceCandidates.ts";
import {
insertMention,
isCaretNavigationKey,
mentionCategories,
mentionDismissAfterEvent,
mentionRange,
slashRange,
nodeMentionOptions,
Expand Down Expand Up @@ -143,3 +145,43 @@ test("skill slash commands follow the caret after text and atomic references", (
test("choosing a skill from the middle of a slash token replaces the complete token", () => {
assert.deepEqual(slashRange("\ufffc /character 后文", 7), {start: 2, end: 12, query: "char"});
});

test("a dismissed mention stays closed until the caret leaves and returns", () => {
const mention = 1;
assert.equal(isCaretNavigationKey("ArrowRight"), true);
assert.equal(isCaretNavigationKey("ArrowRight", true), false);
assert.equal(isCaretNavigationKey("a"), false);

const stayed = mentionDismissAfterEvent(mention, false, mention);
assert.deepEqual(stayed, {
dismissedStart: mention,
leftSinceDismiss: false,
open: false,
});

const left = mentionDismissAfterEvent(mention, false, null);
assert.deepEqual(left, {
dismissedStart: null,
leftSinceDismiss: false,
open: false,
});
const returned = mentionDismissAfterEvent(
left.dismissedStart,
left.leftSinceDismiss,
mention,
);
assert.deepEqual(returned, {
dismissedStart: null,
leftSinceDismiss: false,
open: true,
});

// Coalesced 2 → 3 → 2: the outside caret is never reported, but a
// navigation key has already marked the leave.
const coalesced = mentionDismissAfterEvent(mention, true, mention);
assert.deepEqual(coalesced, {
dismissedStart: null,
leftSinceDismiss: false,
open: true,
});
});
Loading