feat(web): add surround selection in composer (#1502)
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
7c0849fe2e
commit
65d797c13e
4 files changed
+888
-17
No files matched your search
@@ -915,13 +915,189 @@ async function waitForComposerEditor(): Promise<HTMLElement> {
|
||||
);
|
||||
}
|
||||
|
||||
async function pressComposerKey(key: string): Promise<void> {
|
||||
const composerEditor = await waitForComposerEditor();
|
||||
composerEditor.focus();
|
||||
const keydownEvent = new KeyboardEvent("keydown", {
|
||||
key,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
composerEditor.dispatchEvent(keydownEvent);
|
||||
if (keydownEvent.defaultPrevented) {
|
||||
await waitForLayout();
|
||||
return;
|
||||
}
|
||||
|
||||
const beforeInputEvent = new InputEvent("beforeinput", {
|
||||
data: key,
|
||||
inputType: "insertText",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
composerEditor.dispatchEvent(beforeInputEvent);
|
||||
if (beforeInputEvent.defaultPrevented) {
|
||||
await waitForLayout();
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof document.execCommand === "function" &&
|
||||
document.execCommand("insertText", false, key)
|
||||
) {
|
||||
await waitForLayout();
|
||||
return;
|
||||
}
|
||||
|
||||
const selection = window.getSelection();
|
||||
if (!selection || selection.rangeCount === 0) {
|
||||
throw new Error("Unable to resolve composer selection for text input.");
|
||||
}
|
||||
const range = selection.getRangeAt(0);
|
||||
range.deleteContents();
|
||||
const textNode = document.createTextNode(key);
|
||||
range.insertNode(textNode);
|
||||
range.setStartAfter(textNode);
|
||||
range.collapse(true);
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
composerEditor.dispatchEvent(
|
||||
new InputEvent("input", {
|
||||
data: key,
|
||||
inputType: "insertText",
|
||||
bubbles: true,
|
||||
}),
|
||||
);
|
||||
await waitForLayout();
|
||||
}
|
||||
|
||||
async function pressComposerUndo(): Promise<void> {
|
||||
const composerEditor = await waitForComposerEditor();
|
||||
const useMetaForMod = isMacPlatform(navigator.platform);
|
||||
composerEditor.focus();
|
||||
composerEditor.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "z",
|
||||
metaKey: useMetaForMod,
|
||||
ctrlKey: !useMetaForMod,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await waitForLayout();
|
||||
}
|
||||
|
||||
async function waitForComposerText(expectedText: string): Promise<void> {
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(useComposerDraftStore.getState().draftsByThreadKey[THREAD_KEY]?.prompt ?? "").toBe(
|
||||
expectedText,
|
||||
);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
}
|
||||
|
||||
async function setComposerSelectionByTextOffsets(options: {
|
||||
start: number;
|
||||
end: number;
|
||||
direction?: "forward" | "backward";
|
||||
}): Promise<void> {
|
||||
const composerEditor = await waitForComposerEditor();
|
||||
composerEditor.focus();
|
||||
const resolvePoint = (targetOffset: number) => {
|
||||
const traversedRef = { value: 0 };
|
||||
|
||||
const visitNode = (node: Node): { node: Node; offset: number } | null => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
const textLength = node.textContent?.length ?? 0;
|
||||
if (targetOffset <= traversedRef.value + textLength) {
|
||||
return {
|
||||
node,
|
||||
offset: Math.max(0, Math.min(targetOffset - traversedRef.value, textLength)),
|
||||
};
|
||||
}
|
||||
traversedRef.value += textLength;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (node instanceof HTMLBRElement) {
|
||||
const parent = node.parentNode;
|
||||
if (!parent) {
|
||||
return null;
|
||||
}
|
||||
const siblingIndex = Array.prototype.indexOf.call(parent.childNodes, node);
|
||||
if (targetOffset <= traversedRef.value) {
|
||||
return { node: parent, offset: siblingIndex };
|
||||
}
|
||||
if (targetOffset <= traversedRef.value + 1) {
|
||||
return { node: parent, offset: siblingIndex + 1 };
|
||||
}
|
||||
traversedRef.value += 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (node instanceof Element || node instanceof DocumentFragment) {
|
||||
for (const child of node.childNodes) {
|
||||
const point = visitNode(child);
|
||||
if (point) {
|
||||
return point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
return (
|
||||
visitNode(composerEditor) ?? {
|
||||
node: composerEditor,
|
||||
offset: composerEditor.childNodes.length,
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const startPoint = resolvePoint(options.start);
|
||||
const endPoint = resolvePoint(options.end);
|
||||
const selection = window.getSelection();
|
||||
if (!selection) {
|
||||
throw new Error("Unable to resolve window selection.");
|
||||
}
|
||||
selection.removeAllRanges();
|
||||
|
||||
if (options.direction === "backward" && "setBaseAndExtent" in selection) {
|
||||
selection.setBaseAndExtent(endPoint.node, endPoint.offset, startPoint.node, startPoint.offset);
|
||||
await waitForLayout();
|
||||
return;
|
||||
}
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(startPoint.node, startPoint.offset);
|
||||
range.setEnd(endPoint.node, endPoint.offset);
|
||||
selection.addRange(range);
|
||||
await waitForLayout();
|
||||
}
|
||||
|
||||
async function selectAllComposerContent(): Promise<void> {
|
||||
const composerEditor = await waitForComposerEditor();
|
||||
composerEditor.focus();
|
||||
const selection = window.getSelection();
|
||||
if (!selection) {
|
||||
throw new Error("Unable to resolve window selection.");
|
||||
}
|
||||
selection.removeAllRanges();
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(composerEditor);
|
||||
selection.addRange(range);
|
||||
await waitForLayout();
|
||||
}
|
||||
|
||||
async function waitForComposerMenuItem(itemId: string): Promise<HTMLElement> {
|
||||
return waitForElement(
|
||||
() => document.querySelector<HTMLElement>(`[data-composer-item-id="${itemId}"]`),
|
||||
`Unable to find composer menu item "${itemId}".`,
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForSendButton(): Promise<HTMLButtonElement> {
|
||||
return waitForElement(
|
||||
() => document.querySelector<HTMLButtonElement>('button[aria-label="Send message"]'),
|
||||
@@ -2336,6 +2512,202 @@ describe("ChatView timeline estimator parity (full app)", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("surrounds selected plain text and preserves the inner selection for repeated wrapping", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-basic" as MessageId,
|
||||
targetText: "surround basic",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "selected");
|
||||
await waitForComposerText("selected");
|
||||
await setComposerSelectionByTextOffsets({ start: 0, end: "selected".length });
|
||||
await pressComposerKey("(");
|
||||
await waitForComposerText("(selected)");
|
||||
|
||||
await pressComposerKey("[");
|
||||
await waitForComposerText("([selected])");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves collapsed-caret typing unchanged for surround symbols", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "selected");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-collapsed" as MessageId,
|
||||
targetText: "surround collapsed",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForComposerText("selected");
|
||||
await setComposerSelectionByTextOffsets({
|
||||
start: "selected".length,
|
||||
end: "selected".length,
|
||||
});
|
||||
await pressComposerKey("(");
|
||||
await waitForComposerText("selected(");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("supports symmetric and backward-selection surrounds", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "backward");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-backward" as MessageId,
|
||||
targetText: "surround backward",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForComposerText("backward");
|
||||
await setComposerSelectionByTextOffsets({
|
||||
start: 0,
|
||||
end: "backward".length,
|
||||
direction: "backward",
|
||||
});
|
||||
await pressComposerKey("*");
|
||||
await waitForComposerText("*backward*");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("supports option-produced surround symbols like guillemets", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "quoted");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-guillemet" as MessageId,
|
||||
targetText: "surround guillemet",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForComposerText("quoted");
|
||||
await setComposerSelectionByTextOffsets({ start: 0, end: "quoted".length });
|
||||
await pressComposerKey("«");
|
||||
await waitForComposerText("«quoted»");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("supports dead-key composition that resolves to another surround symbol without an extra undo step", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "quoted");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-dead-quote" as MessageId,
|
||||
targetText: "surround dead quote",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForComposerText("quoted");
|
||||
await setComposerSelectionByTextOffsets({ start: 0, end: "quoted".length });
|
||||
const composerEditor = await waitForComposerEditor();
|
||||
composerEditor.focus();
|
||||
composerEditor.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Dead",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
composerEditor.dispatchEvent(
|
||||
new InputEvent("beforeinput", {
|
||||
data: "'",
|
||||
inputType: "insertCompositionText",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
const resolvedInputEvent = new InputEvent("beforeinput", {
|
||||
data: "'",
|
||||
inputType: "insertText",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
composerEditor.dispatchEvent(resolvedInputEvent);
|
||||
expect(resolvedInputEvent.defaultPrevented).toBe(true);
|
||||
await waitForComposerText("'quoted'");
|
||||
await pressComposerUndo();
|
||||
await waitForComposerText("quoted");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("surrounds text after a mention using the correct expanded offsets", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "hi @package.json there");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-after-mention" as MessageId,
|
||||
targetText: "surround after mention",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(document.body.textContent).toContain("package.json");
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
await waitForComposerText("hi @package.json there");
|
||||
await setComposerSelectionByTextOffsets({
|
||||
start: "hi package.json ".length,
|
||||
end: "hi package.json there".length,
|
||||
});
|
||||
await pressComposerKey("(");
|
||||
await waitForComposerText("hi @package.json (there)");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("falls back to normal replacement when the selection includes a mention token", async () => {
|
||||
useComposerDraftStore.getState().setPrompt(THREAD_REF, "hi @package.json there ");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-surround-token" as MessageId,
|
||||
targetText: "surround token",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(document.body.textContent).toContain("package.json");
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
await selectAllComposerContent();
|
||||
await pressComposerKey("(");
|
||||
await waitForComposerText("(");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("shows runtime mode descriptions in the desktop composer access select", async () => {
|
||||
setDraftThreadWithoutWorktree();
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
KEY_BACKSPACE_COMMAND,
|
||||
$getRoot,
|
||||
HISTORY_MERGE_TAG,
|
||||
DecoratorNode,
|
||||
type ElementNode,
|
||||
type LexicalNode,
|
||||
@@ -58,7 +59,10 @@ import {
|
||||
expandCollapsedComposerCursor,
|
||||
isCollapsedCursorAdjacentToInlineToken,
|
||||
} from "~/composer-logic";
|
||||
import { splitPromptIntoComposerSegments } from "~/composer-editor-mentions";
|
||||
import {
|
||||
selectionTouchesMentionBoundary,
|
||||
splitPromptIntoComposerSegments,
|
||||
} from "~/composer-editor-mentions";
|
||||
import {
|
||||
INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
|
||||
type TerminalContextDraft,
|
||||
@@ -73,6 +77,21 @@ import {
|
||||
import { ComposerPendingTerminalContextChip } from "./chat/ComposerPendingTerminalContexts";
|
||||
|
||||
const COMPOSER_EDITOR_HMR_KEY = `composer-editor-${Math.random().toString(36).slice(2)}`;
|
||||
const SURROUND_SYMBOLS: [string, string][] = [
|
||||
["(", ")"],
|
||||
["[", "]"],
|
||||
["{", "}"],
|
||||
["'", "'"],
|
||||
['"', '"'],
|
||||
["“", "”"],
|
||||
["`", "`"],
|
||||
["<", ">"],
|
||||
["«", "»"],
|
||||
["*", "*"],
|
||||
["_", "_"],
|
||||
];
|
||||
const SURROUND_SYMBOLS_MAP = new Map<string, string>(SURROUND_SYMBOLS);
|
||||
const BACKTICK_SURROUND_CLOSE_SYMBOL = SURROUND_SYMBOLS_MAP.get("`") ?? null;
|
||||
|
||||
type SerializedComposerMentionNode = Spread<
|
||||
{
|
||||
@@ -553,6 +572,53 @@ function $setSelectionAtComposerOffset(nextOffset: number): void {
|
||||
$setSelection(selection);
|
||||
}
|
||||
|
||||
function $setSelectionRangeAtComposerOffsets(startOffset: number, endOffset: number): void {
|
||||
const root = $getRoot();
|
||||
const composerLength = $getComposerRootLength();
|
||||
const boundedStart = Math.max(0, Math.min(startOffset, composerLength));
|
||||
const boundedEnd = Math.max(0, Math.min(endOffset, composerLength));
|
||||
const anchorRemainingRef = { value: boundedStart };
|
||||
const focusRemainingRef = { value: boundedEnd };
|
||||
const anchorPoint = findSelectionPointAtOffset(root, anchorRemainingRef) ?? {
|
||||
key: root.getKey(),
|
||||
offset: root.getChildren().length,
|
||||
type: "element" as const,
|
||||
};
|
||||
const focusPoint = findSelectionPointAtOffset(root, focusRemainingRef) ?? {
|
||||
key: root.getKey(),
|
||||
offset: root.getChildren().length,
|
||||
type: "element" as const,
|
||||
};
|
||||
const selection = $createRangeSelection();
|
||||
selection.anchor.set(anchorPoint.key, anchorPoint.offset, anchorPoint.type);
|
||||
selection.focus.set(focusPoint.key, focusPoint.offset, focusPoint.type);
|
||||
$setSelection(selection);
|
||||
}
|
||||
|
||||
function getSelectionRangeForExpandedComposerOffsets(selection: ReturnType<typeof $getSelection>): {
|
||||
start: number;
|
||||
end: number;
|
||||
} | null {
|
||||
if (!$isRangeSelection(selection)) {
|
||||
return null;
|
||||
}
|
||||
const anchorNode = selection.anchor.getNode();
|
||||
const focusNode = selection.focus.getNode();
|
||||
const anchorOffset = getExpandedAbsoluteOffsetForPoint(anchorNode, selection.anchor.offset);
|
||||
const focusOffset = getExpandedAbsoluteOffsetForPoint(focusNode, selection.focus.offset);
|
||||
return {
|
||||
start: Math.min(anchorOffset, focusOffset),
|
||||
end: Math.max(anchorOffset, focusOffset),
|
||||
};
|
||||
}
|
||||
|
||||
function $selectionTouchesInlineToken(selection: ReturnType<typeof $getSelection>): boolean {
|
||||
if (!$isRangeSelection(selection)) {
|
||||
return false;
|
||||
}
|
||||
return selection.getNodes().some((node) => isComposerInlineTokenNode(node));
|
||||
}
|
||||
|
||||
function $readSelectionOffsetFromEditorState(fallback: number): number {
|
||||
const selection = $getSelection();
|
||||
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
|
||||
@@ -878,6 +944,269 @@ function ComposerInlineTokenBackspacePlugin() {
|
||||
return null;
|
||||
}
|
||||
|
||||
function ComposerSurroundSelectionPlugin(props: {
|
||||
terminalContexts: ReadonlyArray<TerminalContextDraft>;
|
||||
}) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const terminalContextsRef = useRef(props.terminalContexts);
|
||||
const pendingSurroundSelectionRef = useRef<{
|
||||
value: string;
|
||||
expandedStart: number;
|
||||
expandedEnd: number;
|
||||
} | null>(null);
|
||||
const pendingDeadKeySelectionRef = useRef<{
|
||||
value: string;
|
||||
expandedStart: number;
|
||||
expandedEnd: number;
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
terminalContextsRef.current = props.terminalContexts;
|
||||
}, [props.terminalContexts]);
|
||||
|
||||
const applySurroundInsertion = useCallback(
|
||||
(inputData: string): boolean => {
|
||||
const surroundCloseSymbol = SURROUND_SYMBOLS_MAP.get(inputData);
|
||||
const pendingSurroundSelection = pendingSurroundSelectionRef.current;
|
||||
if (!surroundCloseSymbol) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
let handled = false;
|
||||
editor.update(() => {
|
||||
const selectionSnapshot =
|
||||
pendingSurroundSelection ??
|
||||
(() => {
|
||||
const selection = $getSelection();
|
||||
if (!$isRangeSelection(selection) || selection.isCollapsed()) {
|
||||
return null;
|
||||
}
|
||||
if ($selectionTouchesInlineToken(selection)) {
|
||||
return null;
|
||||
}
|
||||
const range = getSelectionRangeForExpandedComposerOffsets(selection);
|
||||
if (!range || range.start === range.end) {
|
||||
return null;
|
||||
}
|
||||
const value = $getRoot().getTextContent();
|
||||
if (selectionTouchesMentionBoundary(value, range.start, range.end)) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
value,
|
||||
expandedStart: range.start,
|
||||
expandedEnd: range.end,
|
||||
};
|
||||
})();
|
||||
|
||||
if (!selectionSnapshot || !surroundCloseSymbol) {
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedText = selectionSnapshot.value.slice(
|
||||
selectionSnapshot.expandedStart,
|
||||
selectionSnapshot.expandedEnd,
|
||||
);
|
||||
const nextValue = `${selectionSnapshot.value.slice(0, selectionSnapshot.expandedStart)}${inputData}${selectedText}${surroundCloseSymbol}${selectionSnapshot.value.slice(selectionSnapshot.expandedEnd)}`;
|
||||
$setComposerEditorPrompt(nextValue, terminalContextsRef.current);
|
||||
const selectionStart = collapseExpandedComposerCursor(
|
||||
nextValue,
|
||||
selectionSnapshot.expandedStart,
|
||||
);
|
||||
$setSelectionRangeAtComposerOffsets(
|
||||
selectionStart + inputData.length,
|
||||
selectionStart + inputData.length + selectedText.length,
|
||||
);
|
||||
handled = true;
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
});
|
||||
|
||||
return handled;
|
||||
},
|
||||
[editor],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (pendingDeadKeySelectionRef.current) {
|
||||
if (event.key === "Dead" || event.key === " " || event.code === "Space") {
|
||||
return;
|
||||
}
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
}
|
||||
|
||||
if (event.defaultPrevented || event.isComposing || event.metaKey || event.ctrlKey) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
editor.getEditorState().read(() => {
|
||||
const selection = $getSelection();
|
||||
if (!$isRangeSelection(selection) || selection.isCollapsed()) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
if ($selectionTouchesInlineToken(selection)) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
const range = getSelectionRangeForExpandedComposerOffsets(selection);
|
||||
if (!range || range.start === range.end) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
const value = $getRoot().getTextContent();
|
||||
if (selectionTouchesMentionBoundary(value, range.start, range.end)) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
const snapshot = {
|
||||
value,
|
||||
expandedStart: range.start,
|
||||
expandedEnd: range.end,
|
||||
};
|
||||
pendingSurroundSelectionRef.current = snapshot;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
});
|
||||
};
|
||||
|
||||
const onBeforeInput = (event: InputEvent) => {
|
||||
if (
|
||||
event.inputType === "insertCompositionText" &&
|
||||
event.data === "`" &&
|
||||
BACKTICK_SURROUND_CLOSE_SYMBOL !== null &&
|
||||
pendingSurroundSelectionRef.current
|
||||
) {
|
||||
pendingDeadKeySelectionRef.current = pendingSurroundSelectionRef.current;
|
||||
return;
|
||||
}
|
||||
|
||||
if (pendingDeadKeySelectionRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.inputType === "insertCompositionText") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof event.data !== "string") {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
const inputData = event.inputType === "insertText" ? event.data : null;
|
||||
if (!inputData || inputData.length !== 1) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (!applySurroundInsertion(inputData)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
event.stopImmediatePropagation();
|
||||
};
|
||||
|
||||
const tryApplyDeadKeyBacktickSurround = (options?: { finalAttempt?: boolean }) => {
|
||||
queueMicrotask(() => {
|
||||
editor.update(
|
||||
() => {
|
||||
const pendingDeadKeySelection = pendingDeadKeySelectionRef.current;
|
||||
if (!pendingDeadKeySelection) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentValue = $getRoot().getTextContent();
|
||||
const backtickCloseSymbol = BACKTICK_SURROUND_CLOSE_SYMBOL;
|
||||
if (backtickCloseSymbol === null) {
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const expectedResolvedValue = `${pendingDeadKeySelection.value.slice(0, pendingDeadKeySelection.expandedStart)}\`${pendingDeadKeySelection.value.slice(pendingDeadKeySelection.expandedEnd)}`;
|
||||
if (currentValue !== expectedResolvedValue) {
|
||||
if (options?.finalAttempt) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedText = pendingDeadKeySelection.value.slice(
|
||||
pendingDeadKeySelection.expandedStart,
|
||||
pendingDeadKeySelection.expandedEnd,
|
||||
);
|
||||
const replacementStart = collapseExpandedComposerCursor(
|
||||
currentValue,
|
||||
pendingDeadKeySelection.expandedStart,
|
||||
);
|
||||
$setSelectionRangeAtComposerOffsets(replacementStart, replacementStart + 1);
|
||||
const replacementSelection = $getSelection();
|
||||
if (!$isRangeSelection(replacementSelection)) {
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
return;
|
||||
}
|
||||
replacementSelection.insertText(`\`${selectedText}${backtickCloseSymbol}`);
|
||||
$setSelectionRangeAtComposerOffsets(
|
||||
replacementStart + 1,
|
||||
replacementStart + 1 + selectedText.length,
|
||||
);
|
||||
pendingSurroundSelectionRef.current = null;
|
||||
pendingDeadKeySelectionRef.current = null;
|
||||
},
|
||||
{ tag: HISTORY_MERGE_TAG },
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const onInput = (event: Event) => {
|
||||
const inputEvent = event as InputEvent;
|
||||
if (
|
||||
inputEvent.inputType === "insertText" ||
|
||||
inputEvent.inputType === "insertCompositionText"
|
||||
) {
|
||||
tryApplyDeadKeyBacktickSurround();
|
||||
}
|
||||
};
|
||||
|
||||
const onCompositionEnd = () => {
|
||||
tryApplyDeadKeyBacktickSurround({ finalAttempt: true });
|
||||
};
|
||||
|
||||
let activeRootElement: HTMLElement | null = null;
|
||||
const unregisterRootListener = editor.registerRootListener((rootElement, prevRootElement) => {
|
||||
prevRootElement?.removeEventListener("keydown", onKeyDown);
|
||||
prevRootElement?.removeEventListener("beforeinput", onBeforeInput, true);
|
||||
prevRootElement?.removeEventListener("input", onInput);
|
||||
prevRootElement?.removeEventListener("compositionend", onCompositionEnd);
|
||||
rootElement?.addEventListener("keydown", onKeyDown);
|
||||
rootElement?.addEventListener("beforeinput", onBeforeInput, true);
|
||||
rootElement?.addEventListener("input", onInput);
|
||||
rootElement?.addEventListener("compositionend", onCompositionEnd);
|
||||
activeRootElement = rootElement;
|
||||
});
|
||||
|
||||
return () => {
|
||||
if (activeRootElement) {
|
||||
activeRootElement.removeEventListener("keydown", onKeyDown);
|
||||
activeRootElement.removeEventListener("beforeinput", onBeforeInput, true);
|
||||
activeRootElement.removeEventListener("input", onInput);
|
||||
activeRootElement.removeEventListener("compositionend", onCompositionEnd);
|
||||
}
|
||||
unregisterRootListener();
|
||||
};
|
||||
}, [applySurroundInsertion, editor]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function ComposerPromptEditorInner({
|
||||
value,
|
||||
cursor,
|
||||
@@ -1113,6 +1442,7 @@ function ComposerPromptEditorInner({
|
||||
/>
|
||||
<OnChangePlugin onChange={handleEditorChange} />
|
||||
<ComposerCommandKeyPlugin {...(onCommandKeyDown ? { onCommandKeyDown } : {})} />
|
||||
<ComposerSurroundSelectionPlugin terminalContexts={terminalContexts} />
|
||||
<ComposerInlineTokenArrowPlugin />
|
||||
<ComposerInlineTokenSelectionNormalizePlugin />
|
||||
<ComposerInlineTokenBackspacePlugin />
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
|
||||
import {
|
||||
selectionTouchesMentionBoundary,
|
||||
splitPromptIntoComposerSegments,
|
||||
} from "./composer-editor-mentions";
|
||||
import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
|
||||
|
||||
describe("splitPromptIntoComposerSegments", () => {
|
||||
@@ -38,4 +41,59 @@ describe("splitPromptIntoComposerSegments", () => {
|
||||
{ type: "text", text: " please" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves consecutive terminal context placeholders without dropping positions", () => {
|
||||
expect(
|
||||
splitPromptIntoComposerSegments(
|
||||
`${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}tail`,
|
||||
),
|
||||
).toEqual([
|
||||
{ type: "terminal-context", context: null },
|
||||
{ type: "terminal-context", context: null },
|
||||
{ type: "text", text: "tail" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("selectionTouchesMentionBoundary", () => {
|
||||
it("returns true when selection includes the whitespace after a mention", () => {
|
||||
expect(
|
||||
selectionTouchesMentionBoundary(
|
||||
"hi @package.json there",
|
||||
"hi @package.json".length,
|
||||
"hi @package.json there".length,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when selection includes the whitespace before a mention", () => {
|
||||
expect(
|
||||
selectionTouchesMentionBoundary(
|
||||
"hi there @package.json later",
|
||||
"hi there".length,
|
||||
"hi there ".length,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when selection starts after the mention boundary whitespace", () => {
|
||||
expect(
|
||||
selectionTouchesMentionBoundary(
|
||||
"hi @package.json there",
|
||||
"hi @package.json ".length,
|
||||
"hi @package.json there".length,
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true when selection includes whitespace after a mention following a terminal placeholder", () => {
|
||||
const prompt = `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md there`;
|
||||
expect(
|
||||
selectionTouchesMentionBoundary(
|
||||
prompt,
|
||||
`${INLINE_TERMINAL_CONTEXT_PLACEHOLDER}@AGENTS.md`.length,
|
||||
prompt.length,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -19,6 +19,10 @@ export type ComposerPromptSegment =
|
||||
|
||||
const MENTION_TOKEN_REGEX = /(^|\s)@([^\s@]+)(?=\s)/g;
|
||||
|
||||
function rangeIncludesIndex(start: number, end: number, index: number): boolean {
|
||||
return start <= index && index < end;
|
||||
}
|
||||
|
||||
function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
|
||||
if (!text) return;
|
||||
const last = segments[segments.length - 1];
|
||||
@@ -29,6 +33,84 @@ function pushTextSegment(segments: ComposerPromptSegment[], text: string): void
|
||||
segments.push({ type: "text", text });
|
||||
}
|
||||
|
||||
function forEachPromptSegmentSlice(
|
||||
prompt: string,
|
||||
visitor: (
|
||||
slice:
|
||||
| {
|
||||
type: "text";
|
||||
text: string;
|
||||
promptOffset: number;
|
||||
}
|
||||
| {
|
||||
type: "terminal-context";
|
||||
promptOffset: number;
|
||||
},
|
||||
) => boolean | void,
|
||||
): boolean {
|
||||
let textCursor = 0;
|
||||
|
||||
for (let index = 0; index < prompt.length; index += 1) {
|
||||
if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
index > textCursor &&
|
||||
visitor({
|
||||
type: "text",
|
||||
text: prompt.slice(textCursor, index),
|
||||
promptOffset: textCursor,
|
||||
}) === true
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (visitor({ type: "terminal-context", promptOffset: index }) === true) {
|
||||
return true;
|
||||
}
|
||||
textCursor = index + 1;
|
||||
}
|
||||
|
||||
if (
|
||||
textCursor < prompt.length &&
|
||||
visitor({
|
||||
type: "text",
|
||||
text: prompt.slice(textCursor),
|
||||
promptOffset: textCursor,
|
||||
}) === true
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function forEachPromptTextSlice(
|
||||
prompt: string,
|
||||
visitor: (text: string, promptOffset: number) => boolean | void,
|
||||
): boolean {
|
||||
return forEachPromptSegmentSlice(prompt, (slice) => {
|
||||
if (slice.type !== "text") {
|
||||
return false;
|
||||
}
|
||||
return visitor(slice.text, slice.promptOffset);
|
||||
});
|
||||
}
|
||||
|
||||
function forEachMentionMatch(
|
||||
prompt: string,
|
||||
visitor: (match: RegExpMatchArray, promptOffset: number) => boolean | void,
|
||||
): boolean {
|
||||
return forEachPromptTextSlice(prompt, (text, promptOffset) => {
|
||||
for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
|
||||
if (visitor(match, promptOffset) === true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
|
||||
const segments: ComposerPromptSegment[] = [];
|
||||
if (!text) {
|
||||
@@ -64,6 +146,43 @@ function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegmen
|
||||
return segments;
|
||||
}
|
||||
|
||||
export function selectionTouchesMentionBoundary(
|
||||
prompt: string,
|
||||
start: number,
|
||||
end: number,
|
||||
): boolean {
|
||||
if (!prompt || start >= end) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return forEachMentionMatch(prompt, (match, promptOffset) => {
|
||||
const fullMatch = match[0];
|
||||
const prefix = match[1] ?? "";
|
||||
const matchIndex = match.index ?? 0;
|
||||
const mentionStart = promptOffset + matchIndex + prefix.length;
|
||||
const mentionEnd = mentionStart + fullMatch.length - prefix.length;
|
||||
const beforeMentionIndex = mentionStart - 1;
|
||||
const afterMentionIndex = mentionEnd;
|
||||
|
||||
if (
|
||||
beforeMentionIndex >= 0 &&
|
||||
/\s/.test(prompt[beforeMentionIndex] ?? "") &&
|
||||
rangeIncludesIndex(start, end, beforeMentionIndex)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
afterMentionIndex < prompt.length &&
|
||||
/\s/.test(prompt[afterMentionIndex] ?? "") &&
|
||||
rangeIncludesIndex(start, end, afterMentionIndex)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
export function splitPromptIntoComposerSegments(
|
||||
prompt: string,
|
||||
terminalContexts: ReadonlyArray<TerminalContextDraft> = [],
|
||||
@@ -73,28 +192,20 @@ export function splitPromptIntoComposerSegments(
|
||||
}
|
||||
|
||||
const segments: ComposerPromptSegment[] = [];
|
||||
let textCursor = 0;
|
||||
let terminalContextIndex = 0;
|
||||
|
||||
for (let index = 0; index < prompt.length; index += 1) {
|
||||
if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
|
||||
continue;
|
||||
forEachPromptSegmentSlice(prompt, (slice) => {
|
||||
if (slice.type === "text") {
|
||||
segments.push(...splitPromptTextIntoComposerSegments(slice.text));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index > textCursor) {
|
||||
segments.push(...splitPromptTextIntoComposerSegments(prompt.slice(textCursor, index)));
|
||||
}
|
||||
segments.push({
|
||||
type: "terminal-context",
|
||||
context: terminalContexts[terminalContextIndex] ?? null,
|
||||
});
|
||||
terminalContextIndex += 1;
|
||||
textCursor = index + 1;
|
||||
}
|
||||
|
||||
if (textCursor < prompt.length) {
|
||||
segments.push(...splitPromptTextIntoComposerSegments(prompt.slice(textCursor)));
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return segments;
|
||||
}
|
||||
Reference in new issue
Block a user