Skip to content

Commit d96a546

Browse files
saphidclaude
andcommitted
fix(web): treat any composer content as owning undo, not just text
A rich draft holding only a rule has no DOM text, so Cmd+Z skipped it and undid the last sidebar action. The editor now reports ownership from its history and its document emptiness. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30b07ff commit d96a546

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎apps/web/src/components/ComposerPromptEditor.undo.test.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ afterEach(async () => {
3131
vi.unstubAllGlobals();
3232
});
3333

34-
function ControlledComposer() {
34+
function ControlledComposer(props: { richTextEnabled?: boolean }) {
3535
const [draft, updateDraft] = useState({ value: "", thread: "a" });
3636
useImperativeHandle(
3737
controls,
@@ -53,6 +53,7 @@ function ControlledComposer() {
5353
onChange={(value) => updateDraft((current) => ({ ...current, value }))}
5454
onPaste={() => {}}
5555
editorRef={editorRef}
56+
richTextEnabled={props.richTextEnabled ?? false}
5657
/>
5758
);
5859
}
@@ -64,8 +65,8 @@ function editor() {
6465
return (element as HTMLElement & { editor: Editor }).editor;
6566
}
6667

67-
async function mount() {
68-
await act(async () => root.render(<ControlledComposer />));
68+
async function mount(props: { richTextEnabled?: boolean } = {}) {
69+
await act(async () => root.render(<ControlledComposer {...props} />));
6970
// jsdom has no layout; keep focus/selection real without measuring its caret.
7071
editor().setOptions({
7172
editorProps: { ...editor().options.editorProps, handleScrollToSelection: () => true },
@@ -142,6 +143,15 @@ describe("controlled composer undo ownership", () => {
142143
expect(editorRef.current?.readSnapshot().value).toBe("");
143144
});
144145

146+
it("keeps the shortcut on a textless rich draft switched in without history", async () => {
147+
await mount({ richTextEnabled: true });
148+
await act(async () => setDraft("---", "b"));
149+
expect(editor().state.doc.firstChild?.type.name).toBe("horizontalRule");
150+
expect(editor().view.dom.textContent).toBe("");
151+
expect(editor().commands.undo()).toBe(false);
152+
expect(ownsUndo()).toBe(true);
153+
});
154+
145155
it("restores a chip-only draft after its editor deletion", async () => {
146156
await mount();
147157
const chip = formatInlineContextReference({

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,11 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
15051505

15061506
useEffect(() => {
15071507
if (!editor) return;
1508-
return registerEditableUndoHistory(editor.view.dom, () => undoDepth(editor.state) > 0);
1508+
// Any content (text, chips, rules) keeps the shortcut on the draft.
1509+
return registerEditableUndoHistory(
1510+
editor.view.dom,
1511+
() => undoDepth(editor.state) > 0 || !editor.isEmpty,
1512+
);
15091513
}, [editor]);
15101514

15111515
useEffect(() => {

‎apps/web/src/lib/editableFocus.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ describe("isEditableFocused", () => {
3131
function composer() {
3232
const editor = new Editor({ extensions: [StarterKit], content: "" });
3333
cleanups.push(() => editor.destroy());
34-
cleanups.push(registerEditableUndoHistory(editor.view.dom, () => undoDepth(editor.state) > 0));
34+
cleanups.push(
35+
registerEditableUndoHistory(
36+
editor.view.dom,
37+
() => undoDepth(editor.state) > 0 || !editor.isEmpty,
38+
),
39+
);
3540
return editor;
3641
}
3742

‎apps/web/src/lib/editableFocus.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ function focusedEditable(target: EventTarget | null): Element | null {
2626

2727
const editableUndoHistory = new WeakMap<Element, () => boolean>();
2828

29-
/** Lets an editor report its actual undo stack; native fields keep their keyboard. */
30-
export function registerEditableUndoHistory(element: Element, canUndo: () => boolean) {
31-
editableUndoHistory.set(element, canUndo);
29+
/**
30+
* Lets an editor report whether it owns the undo shortcut, from its actual
31+
* undo stack and document; native fields keep their keyboard.
32+
*/
33+
export function registerEditableUndoHistory(element: Element, ownsUndo: () => boolean) {
34+
editableUndoHistory.set(element, ownsUndo);
3235
return () => {
3336
editableUndoHistory.delete(element);
3437
};
@@ -38,8 +41,7 @@ export function registerEditableUndoHistory(element: Element, canUndo: () => boo
3841
export function editableOwnsUndo(target: EventTarget | null = document.activeElement): boolean {
3942
const element = focusedEditable(target);
4043
if (!element) return false;
41-
const canUndo = editableUndoHistory.get(element);
44+
const ownsUndo = editableUndoHistory.get(element);
4245
// Native fields do not expose their undo stack, so preserve their shortcut.
43-
if (!canUndo) return true;
44-
return canUndo() || (element.textContent ?? "").length > 0;
46+
return ownsUndo ? ownsUndo() : true;
4547
}

0 commit comments

Comments
 (0)