fix(app): close the slash commands on a second press or Backspace (#61840)

This commit is contained in:
Luke Parker
2026-11-04 18:30:41 -06:00
committed by GitHub
Unverified
parent 6d365198c4
commit fb26077935
6 changed files with 189 additions and 4 deletions
@@ -952,6 +952,53 @@ test.describe("touch keyboard", () => {
.poll(() => prompts.map((body) => body.text))
.toEqual(["first line\nsecond line", "from the iPad keyboard"])
})
test("the / and @ keys toggle their lists, and Backspace or typing in the editor closes them", async ({ page }) => {
const view = await openSession(page, { name: "ComposerTouchSlash" })
const suggestions = page.locator('[data-component="composer-suggestions"]')
const slash = page.locator('[data-slot="composer-touch-bar"] [data-action="composer-commands"]')
await slash.tap()
await expect(suggestions).toBeVisible()
await expect(view.editor).toHaveText("/")
await slash.tap()
await expect(suggestions).toBeHidden()
await expect(view.editor).toBeEmpty()
await slash.tap()
await expect(suggestions).toBeVisible()
await page.keyboard.press("Backspace")
await expect(suggestions).toBeHidden()
// With a draft, / opens the searchable menu instead. Backspace backs out of it and keeps the draft, from the editor
// as well: iOS keeps the keyboard there rather than moving it to the menu's search.
await view.editor.fill("keep me")
await slash.tap()
await expect(suggestions).toBeVisible()
await view.editor.press("Backspace")
await expect(suggestions).toBeHidden()
await expect(view.editor).toHaveText("keep me")
await slash.tap()
await expect(suggestions).toBeVisible()
// Focusing the editor programmatically leaves the caret wherever the browser puts it, so move it to the end first.
await expect(suggestions.getByRole("textbox", { name: "Commands", exact: true })).toBeFocused()
await view.editor.focus()
await page.keyboard.press("End")
await page.keyboard.type(" now")
await expect(suggestions).toBeHidden()
await expect(view.editor).toHaveText("keep me now")
// @ toggles the same way, taking back only the @ it typed.
const at = page.locator('[data-slot="composer-touch-bar"] [data-action="composer-context"]')
await view.editor.pressSequentially(" ")
await at.tap()
await expect(suggestions).toBeVisible()
await expect(view.editor).toHaveText("keep me now @")
await at.tap()
await expect(suggestions).toBeHidden()
await expect(view.editor).toHaveText("keep me now ")
})
})
test("a key-up never moves the caret back after later navigation", async ({ page }) => {
@@ -68,6 +68,32 @@ describe("Composer store", () => {
expect(prompt.state.cursor).toBe(5)
})
test("removes text before the cursor without touching mentions or later text", () => {
const [state, setState] = createStore<ComposerPersistedState>({
prompt: [
{ type: "text", content: "A ", start: 0, end: 2 },
{ type: "file", path: "one", content: "@one", start: 2, end: 6 },
{ type: "text", content: " @ B", start: 6, end: 10 },
],
cursor: 8,
context: { items: [] },
})
const prompt = createComposerEditorActions([state, setState])
prompt.removeTextBefore("/")
expect(prompt.state.cursor).toBe(8)
prompt.removeTextBefore("@")
expect(prompt.state.prompt).toEqual([
{ type: "text", content: "A ", start: 0, end: 2 },
{ type: "file", path: "one", content: "@one", start: 2, end: 6 },
{ type: "text", content: " B", start: 6, end: 9 },
])
expect(prompt.state.cursor).toBe(7)
})
test("mutates mentions, attachments, and context through editor actions", () => {
const prompt = createPromptStore()
@@ -72,6 +72,14 @@ export function createComposerEditorActions(input: ComposerStateStoreInput) {
})),
)
},
// Removes `content` from just before the cursor, when it is there, leaving every other part as it was.
removeTextBefore(content: string) {
const cursor = store().cursor ?? promptLength(store().prompt)
const prompt = removeTextBefore(store().prompt, cursor, content)
if (!prompt) return
batch(() => setStore()({ prompt, cursor: cursor - content.length, retry: undefined }))
},
removeContext(key: string) {
setStore()("context", "items", (items) => items.filter((item) => item.key !== key))
clearRetry()
@@ -131,6 +139,18 @@ function insertText(prompt: ComposerPrompt, cursor: number, content: string): Co
return withOffsets(parts)
}
function removeTextBefore(prompt: ComposerPrompt, cursor: number, content: string): ComposerPrompt | undefined {
const parts = withOffsets(prompt)
const index = parts.findIndex((item) => item.type === "text" && cursor > item.start && cursor <= item.end)
const part = parts[index]
if (!part || isAttachment(part) || !part.content.slice(0, cursor - part.start).endsWith(content)) return
const offset = cursor - part.start
const next = { ...part, content: part.content.slice(0, offset - content.length) + part.content.slice(offset) }
return withOffsets(parts.map((item, position) => (position === index ? next : item)))
}
function insertMention(
prompt: ComposerPrompt,
start: number,
@@ -312,6 +312,12 @@ export function createComposerEditor(input: {
return
}
if (command.type === "draft.removeTextBefore") {
draft.removeTextBefore(command.value)
return
}
if (command.type === "mention.add") {
if (command.item.mention) draft.addMention(command.item.mention, command.range)
@@ -51,6 +51,68 @@ test.each<{
...menu,
command: { type: "focus.command-search" },
},
{
name: "closes inline commands on a second press and takes back the slash it typed",
from: { popover: { type: "command-inline", query: "" } },
event: { type: "commands.open" },
draft: persisted("/"),
popover: { type: "closed" },
command: { type: "draft.setText", value: "" },
},
{
name: "closes inline commands on a second press and keeps a typed query",
from: { popover: { type: "command-inline", query: "re" } },
event: { type: "commands.open" },
draft: persisted("/re"),
popover: { type: "closed" },
command: { type: "focus.editor" },
},
{
name: "closes the command menu on a second press",
from: menu,
event: { type: "commands.open" },
draft: persisted("existing text"),
popover: { type: "closed" },
focus: "editor",
},
{
name: "closes the command menu when its empty search is backspaced",
from: menu,
event: key("Backspace"),
draft: persisted("existing text"),
popover: { type: "closed" },
command: { type: "focus.editor" },
handled: true,
},
{
name: "keeps the command menu while its search has a query to delete",
from: { popover: { type: "command-menu", query: "re" }, focus: "command-search" },
event: key("Backspace"),
draft: persisted("existing text"),
popover: { type: "command-menu", query: "re" },
},
{
name: "closes the command menu when the editor is typed in",
from: menu,
event: { type: "input.changed", value: "" },
popover: { type: "closed" },
},
{
name: "closes context on a second press and takes back the @ it typed",
from: { popover: { type: "context", query: "" } },
event: { type: "context.open" },
draft: persisted("see @"),
popover: { type: "closed" },
command: { type: "draft.removeTextBefore", value: "@" },
},
{
name: "closes context on a second press and keeps a typed query",
from: { popover: { type: "context", query: "sr" } },
event: { type: "context.open" },
draft: persisted("see @sr"),
popover: { type: "closed" },
command: { type: "focus.editor" },
},
{
name: "opens inline commands when slash is the entire prompt",
event: { type: "input.changed", value: "/re" },
@@ -36,6 +36,7 @@ export type ComposerInteractionEvent =
export type ComposerInteractionCommand =
| { type: "draft.setText"; value: string }
| { type: "draft.addText"; value: string }
| { type: "draft.removeTextBefore"; value: string }
| { type: "mention.add"; item: ComposerSuggestion; range?: { start: number; end: number } }
| { type: "popover.filter"; popover: "command" | "context"; query: string }
| { type: "suggestion.select"; id: string }
@@ -143,13 +144,21 @@ function inputChanged(
])
}
return changed(
{ ...state, popover: state.popover.type === "command-menu" ? state.popover : { type: "closed" }, focus: "editor" },
setText,
)
// Typing in the editor leaves the command menu, whose own search field takes its input.
return changed({ ...state, popover: { type: "closed" }, focus: "editor" }, setText)
}
function openCommands(state: ComposerInteractionState, persisted: ComposerPersistedState): ComposerEditorTransition {
// A second press closes the commands, taking back the slash the first one typed.
if (state.popover.type === "command-inline" || state.popover.type === "command-menu") {
const typed = state.popover.type === "command-inline" && promptText(persisted) === "/"
return changed({ ...state, popover: { type: "closed" }, focus: "editor" }, [
...(typed ? [{ type: "draft.setText" as const, value: "" }] : []),
{ type: "focus.editor" },
])
}
if (!populated(persisted)) {
return changed({ ...state, popover: { type: "command-inline", query: "" }, focus: "editor" }, [
{ type: "draft.addText", value: "/" },
@@ -165,6 +174,14 @@ function openCommands(state: ComposerInteractionState, persisted: ComposerPersis
}
function openContext(state: ComposerInteractionState): ComposerEditorTransition {
// A second press closes the context list, taking back the @ it typed when nothing follows it yet.
if (state.popover.type === "context") {
return changed({ ...state, popover: { type: "closed" }, focus: "editor" }, [
...(state.popover.query ? [] : [{ type: "draft.removeTextBefore" as const, value: "@" }]),
{ type: "focus.editor" },
])
}
return changed({ ...state, popover: { type: "context", query: "" }, focus: "editor" }, [
{ type: "draft.addText", value: "@" },
{ type: "popover.filter", popover: "context", query: "" },
@@ -270,6 +287,13 @@ function keyDown(
return unchanged(state)
}
// Backspace on an empty search backs out of the command menu, as it closes inline commands by deleting their slash.
// iOS does not move the keyboard to the search field the menu focuses, so the key can arrive from the editor; it is
// handled there too, so backing out never also deletes from the draft.
if (event.key === "Backspace" && state.popover.type === "command-menu" && !state.popover.query) {
return changed({ ...state, popover: { type: "closed" }, focus: "editor" }, [{ type: "focus.editor" }], true)
}
if (event.key === "Escape" || (state.popover.type === "history" && event.key === "Tab")) {
return changed({ ...state, popover: { type: "closed" }, focus: "editor" }, [{ type: "focus.editor" }], true)
}