Skip to content

Commit 880acdc

Browse files
committed
fix(web): give a composer cut its own undo step
The composer's cut handler deletes the selection itself, so ProseMirror never tags it as a cut and it merged with deletions around it. It now tags the transaction the same way the paste handler does.
1 parent 12e8bbd commit 880acdc

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
COMPOSER_UNDO_GROUP_DELAY,
6161
type ComposerChangeKind,
6262
groupUndoByChangeKind,
63-
markAsPaste,
63+
markAsClipboardEdit,
6464
} from "~/composer-undo-grouping";
6565
import { collectInlineContextIds } from "~/lib/composerContextReferences";
6666
import { cn, isMacPlatform } from "~/lib/utils";
@@ -1081,7 +1081,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
10811081
editorInstance
10821082
.chain()
10831083
.command(({ tr }) => {
1084-
markAsPaste(tr);
1084+
markAsClipboardEdit(tr, "paste");
10851085
return true;
10861086
})
10871087
.insertContent(content)
@@ -1346,7 +1346,15 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
13461346
clipboardData.setData("text/html", encodeComposerContextClipboardHtml(text, fragment));
13471347
}
13481348
if (cut) {
1349-
editor.chain().focus().deleteSelection().run();
1349+
editor
1350+
.chain()
1351+
.focus()
1352+
.command(({ tr }) => {
1353+
markAsClipboardEdit(tr, "cut");
1354+
return true;
1355+
})
1356+
.deleteSelection()
1357+
.run();
13501358
}
13511359
},
13521360
[editor],

‎apps/web/src/composer-undo-grouping.test.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
COMPOSER_UNDO_GROUP_DELAY,
99
type ComposerChangeKind,
1010
groupUndoByChangeKind,
11-
markAsPaste,
11+
markAsClipboardEdit,
1212
} from "./composer-undo-grouping";
1313

1414
const schema = getSchema([StarterKit]);
@@ -54,12 +54,19 @@ function composer(text = "") {
5454
},
5555
/**
5656
* The transaction the composer's `handlePaste` dispatches: a plain insert,
57-
* tagged by `markAsPaste`. ProseMirror's own paste path would tag it, but
58-
* the composer takes every text paste before that path runs.
57+
* tagged by `markAsClipboardEdit`. ProseMirror's own paste path would tag
58+
* it, but the composer takes every text paste before that path runs.
5959
*/
6060
paste: (s: string) => {
6161
clock += 80;
62-
dispatch(markAsPaste(state.tr.insertText(s)));
62+
dispatch(markAsClipboardEdit(state.tr.insertText(s), "paste"));
63+
return api;
64+
},
65+
/** The composer's cut handler: a plain delete of the selection, tagged. */
66+
cut: (n: number) => {
67+
clock += 80;
68+
const at = state.selection.from;
69+
dispatch(markAsClipboardEdit(state.tr.delete(at - n, at), "cut"));
6370
return api;
6471
},
6572
/** A store-driven rewrite: autocomplete inserting a chip, list continuation. */
@@ -117,6 +124,14 @@ describe("composer undo grouping", () => {
117124
expect(editor.undo().text()).toBe("see ");
118125
});
119126

127+
it("gives a cut its own step between deletions", () => {
128+
const editor = composer().type("hello world").backspace(1).cut(3).backspace(1);
129+
expect(editor.text()).toBe("hello ");
130+
expect(editor.undo().text()).toBe("hello w");
131+
expect(editor.undo().text()).toBe("hello worl");
132+
expect(editor.undo().text()).toBe("hello world");
133+
});
134+
120135
it("keeps IME composition as one run of typing", () => {
121136
expect(composer().type("go ").compose("にほん").undo().text()).toBe("");
122137
});

‎apps/web/src/composer-undo-grouping.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ export const COMPOSER_UNDO_GROUP_DELAY = 1000;
1717
export type ComposerChangeKind = "insert" | "delete" | "other";
1818

1919
/**
20-
* Tags a transaction as a paste, so it becomes its own undo step. ProseMirror
21-
* tags pastes itself only on its built-in paste path; the composer's
22-
* `handlePaste` takes every text paste before that path runs, so each of its
23-
* dispatches must call this, or the paste merges with the typing around it.
20+
* Tags a transaction as a paste or cut, so it becomes its own undo step.
21+
* ProseMirror tags these itself only on its built-in clipboard paths; the
22+
* composer's `handlePaste` and cut handler replace those paths, so each of
23+
* their dispatches must call this, or the change merges with the typing or
24+
* deleting around it.
2425
*/
25-
export function markAsPaste(tr: Transaction): Transaction {
26-
return tr.setMeta("uiEvent", "paste");
26+
export function markAsClipboardEdit(tr: Transaction, event: "paste" | "cut"): Transaction {
27+
return tr.setMeta("uiEvent", event);
2728
}
2829

2930
function changeKindOf(tr: Transaction): ComposerChangeKind | null {

0 commit comments

Comments
 (0)