diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx index f58155ae4559e..6390bf0b93411 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx @@ -566,6 +566,17 @@ it.each([ expect(serializeEditorDoc(editor.state.doc).value).toBe(pasted); }); +it("types outside bold inserted by an editor command", async () => { + await using screen = await mountEditor(""); + const { editor } = screen; + await act(async () => { + editor.view.focus(); + editor.commands.insertContent({ type: "text", text: "DEV", marks: [{ type: "bold" }] }); + }); + await userEvent.keyboard("!"); + expect(serializeEditorDoc(editor.state.doc).value).toBe("**DEV**!"); +}); + it.each([ ["**DEV**", "**DEV**!"], ["*DEV*", "*DEV*!"], diff --git a/apps/web/src/composer-markdown-source.ts b/apps/web/src/composer-markdown-source.ts index 542bc4be7fc23..08b061163d295 100644 --- a/apps/web/src/composer-markdown-source.ts +++ b/apps/web/src/composer-markdown-source.ts @@ -27,7 +27,6 @@ import { type RichTextMark, } from "~/composer-rich-text"; import { caretTakesMarksBefore, serializeEditorDoc } from "~/composer-rich-text-doc"; -import { isPasteTransaction } from "~/composer-undo-grouping"; // Styled text the selection touches becomes source text with the source mark, // parsed back once the selection leaves. History steps only replay on the shape @@ -265,8 +264,10 @@ function mapRange(mapping: Mapping, { from, to }: TextRange): TextRange { return { from: mapping.map(from, 1), to: mapping.map(to, -1) }; } +type ContinuedStyleSide = "before" | "after" | null; + /** Shows styled text the selection touches as Markdown, returning the source it added. */ -function revealStyledText(tr: Transaction, type: MarkType, typed: boolean, takesBefore: boolean) { +function revealStyledText(tr: Transaction, type: MarkType, styleSide: ContinuedStyleSide) { const { schema } = tr.doc.type; const selection = tr.selection; const ranges = collectStyledRanges(tr.doc).filter( @@ -299,7 +300,7 @@ function revealStyledText(tr: Transaction, type: MarkType, typed: boolean, takes const map = (pos: number) => { let next = pos; for (const { at, length, closing } of insertions) { - const inside = typed && selection.empty && (closing ? takesBefore : !takesBefore); + const inside = selection.empty && styleSide === (closing ? "before" : "after"); if (at < pos || (at === pos && (selection instanceof NodeSelection || closing !== inside))) { next += length; } @@ -311,12 +312,15 @@ function revealStyledText(tr: Transaction, type: MarkType, typed: boolean, takes } /** Shows styled text the selection touches as Markdown, and parses source it left. */ -function normalizeMarkdownSource(state: EditorState, typed: boolean): Transaction | null { +function normalizeMarkdownSource( + state: EditorState, + styleSide: ContinuedStyleSide, +): Transaction | null { const type = state.schema.marks[MARKDOWN_SOURCE_MARK]; if (!type) return null; const tr = state.tr; collapseMarkdownSource(tr, (region) => touches(state.selection, region)); - revealStyledText(tr, type, typed, caretTakesMarksBefore(state)); + revealStyledText(tr, type, styleSide); return tr.docChanged ? tr : null; } @@ -335,7 +339,7 @@ function normalizeDetached(state: EditorState, revealed: TextRange[]): Transacti if (touches(state.selection, region)) kept.push(region); else collapseRegion(tr, region, type); } - const added = revealStyledText(tr, type, false, caretTakesMarksBefore(state)); + const added = revealStyledText(tr, type, null); if (!tr.docChanged) return null; const shown = [...kept.map((region) => mapRange(tr.mapping, region)), ...added]; return tr.setMeta(markdownSourceKey, shown).setMeta("addToHistory", false); @@ -439,6 +443,7 @@ type MarkdownSourceState = { }; const markdownSourceKey = new PluginKey("composer-markdown-source"); +const textInputKey = new PluginKey("composer-markdown-text-input"); function markdownSourcePlugin(type: MarkType): Plugin { let view: EditorView | null = null; @@ -497,11 +502,14 @@ function markdownSourcePlugin(type: MarkType): Plugin { if (!transactions.some((tr) => tr.docChanged || tr.selectionSet)) return null; const source = markdownSourceKey.getState(state); if (source?.detached) return normalizeDetached(state, source.revealed); - // A paste is not typing: the caret ends past the pasted text's closing markers. - const tr = normalizeMarkdownSource( - state, - transactions.some((item) => item.docChanged && !isPasteTransaction(item)), - ); + const continueStyle = + state.storedMarks !== null || + transactions.some( + (tr) => tr.getMeta(textInputKey) === true || tr.getMeta("composition") != null, + ); + let styleSide: ContinuedStyleSide = null; + if (continueStyle) styleSide = caretTakesMarksBefore(state) ? "before" : "after"; + const tr = normalizeMarkdownSource(state, styleSide); // With nothing to undo, a recorded rewrite would only add a no-op undo. if (tr && undoDepth(state) === 0) tr.setMeta("addToHistory", false); return tr; @@ -509,11 +517,24 @@ function markdownSourcePlugin(type: MarkType): Plugin { }); } -/** Keeps source `normalizeDetached` revealed out of what undo, redo and edits see. */ -const MarkdownSourceHistoryExtension = Extension.create({ - name: "composer-markdown-source-history", - // Runs after undo grouping, so a rebased edit keeps the grouping chosen for it. +/** Marks typed text, and keeps source `normalizeDetached` revealed out of undo, redo and edits. */ +const MarkdownSourceEditingExtension = Extension.create({ + name: "composer-markdown-source-editing", + // Runs after input rules and undo grouping, which must see the original edit. priority: 1, + addProseMirrorPlugins() { + return [ + new Plugin({ + key: textInputKey, + props: { + handleTextInput(view, _from, _to, _text, insert) { + view.dispatch(insert().setMeta(textInputKey, true)); + return true; + }, + }, + }), + ]; + }, dispatchTransaction({ transaction, next }) { const { editor } = this; const revealed = markdownSourceKey.getState(editor.state)?.revealed ?? []; @@ -587,7 +608,7 @@ export const ComposerMarkdownSourceExtension = TiptapMark.create({ }; }, addExtensions() { - return [MarkdownSourceHistoryExtension]; + return [MarkdownSourceEditingExtension]; }, addProseMirrorPlugins() { return [markdownSourcePlugin(this.type), replayPlugin]; diff --git a/apps/web/src/composer-undo-grouping.ts b/apps/web/src/composer-undo-grouping.ts index 2a7a5a94028e7..708b4f41fe52a 100644 --- a/apps/web/src/composer-undo-grouping.ts +++ b/apps/web/src/composer-undo-grouping.ts @@ -18,10 +18,6 @@ export function markAsClipboardEdit(tr: Transaction, event: "paste" | "cut"): Tr return tr.setMeta(CLIPBOARD_EDIT_META, event); } -export function isPasteTransaction(tr: Transaction): boolean { - return tr.getMeta("uiEvent") === "paste" || tr.getMeta(CLIPBOARD_EDIT_META) === "paste"; -} - /** Start a replacement undo group, keeping the IME edits together and prior typing separate. */ export function markAsCompositionReplace(tr: Transaction): Transaction { return closeHistory(tr).setMeta(COMPOSITION_REPLACE_META, true);