Skip to content

Commit b1e7a71

Browse files
committed
fix(web): keep selected chips selected when revealing Markdown
1 parent 7b6053c commit b1e7a71

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import type { ServerProviderSkill } from "@t3tools/contracts";
1111
import type { Editor } from "@tiptap/core";
1212
import { redoDepth, undoDepth } from "@tiptap/pm/history";
13+
import { NodeSelection } from "@tiptap/pm/state";
1314
import { createElement, createRef, useEffect, useState } from "react";
1415
import { describe, expect, it, vi } from "vite-plus/test";
1516
import { userEvent } from "vite-plus/test/browser";
@@ -351,6 +352,27 @@ async function focusAt(editor: Editor, position: number | "end") {
351352
}
352353

353354
describe("Markdown markers around styled text", () => {
355+
it.each([
356+
{ keys: "{Backspace}", expected: "**** after" },
357+
{ keys: "X", expected: "**X** after" },
358+
])("keeps a styled chip selected for $keys when showing source", async ({ keys, expected }) => {
359+
await using screen = await mountEditor("$babysit after", true, [babysit]);
360+
const { editor } = screen;
361+
await focusAt(editor, "end");
362+
await act(async () => {
363+
editor.view.dispatch(editor.state.tr.addMark(1, 2, editor.schema.marks.bold!.create()));
364+
});
365+
expect(editor.state.doc.nodeAt(1)?.type.name).toBe("composer-skill");
366+
await act(async () => {
367+
editor.commands.setNodeSelection(1);
368+
});
369+
expect(editor.state.selection).toBeInstanceOf(NodeSelection);
370+
expect((editor.state.selection as NodeSelection).node.type.name).toBe("composer-skill");
371+
await userEvent.keyboard(keys);
372+
await userEvent.keyboard("{End}");
373+
await vi.waitFor(() => expect(serializeEditorDoc(editor.state.doc).value).toBe(expected));
374+
});
375+
354376
it.each([
355377
{
356378
name: "deleting the opening markers removes bold",

‎apps/web/src/composer-markdown-source.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ function revealStyledText(tr: Transaction, type: MarkType, typed: boolean, takes
291291
let next = pos;
292292
for (const { at, length, closing } of insertions) {
293293
const inside = typed && selection.empty && (closing ? takesBefore : !takesBefore);
294-
if (at < pos || (at === pos && closing !== inside)) next += length;
294+
if (at < pos || (at === pos && (selection instanceof NodeSelection || closing !== inside))) {
295+
next += length;
296+
}
295297
}
296298
return next;
297299
};

0 commit comments

Comments
 (0)