:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): backspace takes the bullet off a composer line (#103113)
ruby childs37f4d47170da
diff --git a/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.test.ts b/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.test.tsnew file mode 100644index 000000000000..7d92474c1fcf--- /dev/null+++ b/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.test.ts@@ -0,0 +1,102 @@+import { Editor } from "@tiptap/core";+import { TextSelection } from "@tiptap/pm/state";+import { describe, expect, it } from "vitest";+import { getEditorExtensions } from "./extensions";+import {+ editorContentToTiptapJson,+ tiptapJsonToEditorContent,+} from "./markdownDoc";++function makeEditor(markdown?: string): Editor {+ const element = document.createElement("div");+ document.body.appendChild(element);+ const editor = new Editor({+ element,+ extensions: getEditorExtensions({ sessionId: "session-1" }),+ });+ if (markdown !== undefined) {+ editor.commands.setContent(+ editorContentToTiptapJson({+ segments: [{ type: "text", text: markdown }],+ }),+ );+ }+ return editor;+}++function press(editor: Editor, key: string, shiftKey = false): boolean {+ return (+ editor.view.someProp("handleKeyDown", (fn) =>+ fn(editor.view, new KeyboardEvent("keydown", { key, shiftKey })),+ ) ?? false+ );+}++/** Puts the caret at either end of the first text node starting with `text`. */+function caretAt(editor: Editor, text: string, side: "before" | "after"): void {+ let pos: number | null = null;+ editor.state.doc.descendants((node, at) => {+ if (pos !== null || !node.isText || !node.text?.startsWith(text)) return;+ pos = side === "before" ? at : at + node.nodeSize;+ });+ if (pos === null) throw new Error(`no text node starting with ${text}`);+ editor.view.dispatch(+ editor.state.tr.setSelection(TextSelection.create(editor.state.doc, pos)),+ );+}++function markdown(editor: Editor): string {+ return tiptapJsonToEditorContent(editor.getJSON())+ .segments.map((segment) => (segment.type === "text" ? segment.text : ""))+ .join("");+}++describe("block shortcuts", () => {+ it.each([+ ["the first bullet", "- alpha\n- beta", "alpha", "alpha\n\n- beta"],+ [+ "a bullet in the middle",+ "- alpha\n- beta\n- gamma",+ "beta",+ "- alpha\n\nbeta\n\n- gamma",+ ],+ [+ "a nested bullet, one level per press",+ "- alpha\n - nested",+ "nested",+ "- alpha\n- nested",+ ],+ ])(+ "backspace at the head of %s takes the bullet off the line",+ (_name, source, target, expected) => {+ const editor = makeEditor(source);+ caretAt(editor, target, "before");+ expect(press(editor, "Backspace")).toBe(true);+ expect(markdown(editor)).toBe(expected);+ },+ );++ it("a second backspace on an outdented nested bullet leaves a plain line", () => {+ const editor = makeEditor("- alpha\n - nested");+ caretAt(editor, "nested", "before");+ expect(press(editor, "Backspace")).toBe(true);+ expect(press(editor, "Backspace")).toBe(true);+ expect(markdown(editor)).toBe("- alpha\n\nnested");+ });++ it("removes the empty bullet that shift+enter adds, keeping the line", () => {+ const editor = makeEditor("- alpha");+ caretAt(editor, "alpha", "after");+ press(editor, "Enter", true);+ expect(press(editor, "Backspace")).toBe(true);+ editor.commands.insertContent("plain line");+ expect(markdown(editor)).toBe("- alpha\n\nplain line");+ });++ it("leaves backspace inside a bullet to the browser", () => {+ const editor = makeEditor("- alpha");+ caretAt(editor, "alpha", "after");+ expect(press(editor, "Backspace")).toBe(false);+ expect(markdown(editor)).toBe("- alpha");+ });+});diff --git a/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.ts b/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.tsindex d7df35d7f976..072881f2ad88 100644--- a/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.ts+++ b/products/desktop/packages/ui/src/features/message-editor/tiptap/blockShortcuts.ts@@ -1,4 +1,12 @@import { Extension } from "@tiptap/core";+import type { EditorState } from "@tiptap/pm/state";++/** True when the caret sits at the very start of a list item's first block. */+function atListItemStart(state: EditorState): boolean {+ const { empty, $from } = state.selection;+ if (!empty || $from.parentOffset > 0 || $from.depth < 2) return false;+ return $from.node(-1).type.name === "listItem" && $from.index(-1) === 0;+}/*** Enter is the send key, so the list and code-block keymaps StarterKit binds to@@ -21,6 +29,18 @@ export const BlockShortcuts = Extension.create({editor.commands.liftListItem("listItem"));},++ // Backspace at the head of a bullet drops the bullet and keeps the line,+ // as every other composer does. The default joins the line into the item+ // above instead, which leaves no way to take a bullet off a line.+ Backspace: () => {+ const { editor } = this;+ if (!atListItemStart(editor.state)) return false;+ return (+ editor.commands.undoInputRule() ||+ editor.commands.liftListItem("listItem")+ );+ },};},});