Skip to content

Commit c83e78f

Browse files
fix(web): Shift+Return after closed inline code or bold starts a plain line (#36285)
1 parent 9efd960 commit c83e78f

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,31 @@ describe("Markdown markers around styled text", () => {
599599
expected: "**bo**\n**ld** after",
600600
marks: { bold: ["bo", "ld"] },
601601
},
602+
{
603+
name: "a line break after typed inline code leaves the next line plain",
604+
value: "",
605+
caret: 1,
606+
keys: "`Test`{Shift>}{Enter}{/Shift}f",
607+
expected: "`Test`\nf",
608+
marks: { code: ["Test"] },
609+
},
610+
{
611+
name: "a line break after typed bold leaves the next line plain",
612+
value: "",
613+
caret: 1,
614+
keys: "**Test**{Shift>}{Enter}{/Shift}f",
615+
expected: "**Test**\nf",
616+
marks: { bold: ["Test"] },
617+
},
618+
{
619+
name: "bold turned on after typed bold carries across a line break",
620+
value: "",
621+
caret: 1,
622+
keys: `**Test**${isMacPlatform(navigator.platform) ? "{Meta>}b{/Meta}" : "{Control>}b{/Control}"}{Shift>}{Enter}{/Shift}f`,
623+
expected: "**Test**\n**f**",
624+
// The caret beside f shows it as source.
625+
marks: { bold: ["Test"] },
626+
},
602627
{
603628
name: "splitting a list item inside bold keeps both halves bold",
604629
value: "- **bold** after",

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ function collapseRegion(tr: Transaction, region: TextRange, type: MarkType): voi
221221
function collapseMarkdownSource(
222222
tr: Transaction,
223223
keep: (region: TextRange) => boolean = () => false,
224-
): void {
224+
): TextRange[] {
225225
const type = tr.doc.type.schema.marks[MARKDOWN_SOURCE_MARK];
226-
if (!type) return;
226+
if (!type) return [];
227227
const regions: TextRange[] = [];
228228
for (const region of sourceRegions(tr.doc, type)) {
229229
const extended = withAdjacentDelimiters(tr.doc, region);
@@ -235,6 +235,7 @@ function collapseMarkdownSource(
235235
if (keep(region)) tr.addMark(region.from, region.to, type.create());
236236
else collapseRegion(tr, region, type);
237237
}
238+
return regions;
238239
}
239240

240241
function mapSelection(tr: Transaction, selection: Selection, map: (pos: number) => number) {
@@ -248,7 +249,14 @@ function mapSelection(tr: Transaction, selection: Selection, map: (pos: number)
248249

249250
/** Parses all shown source first, so a command that splits it keeps its styles. */
250251
export const collapseShownMarkdownSource: Command = ({ tr }) => {
251-
collapseMarkdownSource(tr);
252+
const { selection } = tr;
253+
const type = tr.doc.type.schema.marks[MARKDOWN_SOURCE_MARK];
254+
const marks = tr.storedMarks ?? selection.$from.marks();
255+
const regions = collapseMarkdownSource(tr);
256+
// A caret after the closing markers is outside the styles, though parsed text there is inclusive.
257+
if (type && selection.empty && regions.some(({ to }) => to === selection.from)) {
258+
tr.setStoredMarks(type.removeFromSet(marks));
259+
}
252260
return true;
253261
};
254262

0 commit comments

Comments
 (0)