1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-08-10 22:11:50 +02:00

Desktop,Mobile: Fixes #12744: Fix adding lists to blank lines using toolbar buttons (#12745)

This commit is contained in:
Henry Heino
2025-07-21 10:32:14 -07:00
committed by GitHub
parent 4d5097b585
commit e62cba5048
2 changed files with 48 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ describe('markdownCommands.toggleList', () => {
const checklistEndText = ['- [ ] a', '- [ ] test'].join('\n');
const input = `${checklistStartText}\n\n${checklistEndText}`;
const expected = `${checklistStartText}\n\n${checklistEndText}`; // no change
const expected = `${checklistStartText}\n- [ ] \n${checklistEndText}`; // new item
const editor = await createTestEditor(
input,
@@ -466,4 +466,23 @@ A block quote:
expect(editor.state.doc.toString()).toBe(expectedDocText);
});
it.each([
[ListType.CheckList, '', '- [ ] '],
[ListType.OrderedList, '', '1. '],
[ListType.UnorderedList, '', '- '],
[ListType.UnorderedList, '> ', '> - '],
[ListType.UnorderedList, '# Test\n\n', '# Test\n\n- '],
])('should add lists when activated on an empty line or empty block quote (list type: %d, initial doc: %j)', async (
listType, originalDocument, expected,
) => {
const editor = await createTestEditor(
originalDocument,
EditorSelection.cursor(originalDocument.length),
[],
);
toggleList(listType)(editor);
expect(editor.state.doc.toString()).toBe(expected);
});
});

View File

@@ -213,12 +213,28 @@ export const toggleList = (listType: ListType): Command => {
return ListAction.SwitchFormatting;
};
const areAllLinesBlankInRange = (fromLine: Line, toLine: Line) => {
for (let lineNumber = fromLine.number; lineNumber <= toLine.number; lineNumber++) {
const line = state.doc.line(lineNumber);
// Consider lines within block quotes with no other content to be blank (this
// command should behave similarly regardless of whether in or out of a block
// quote).
if (stripBlockquote(line).trim() !== '') {
return false;
}
}
return true;
};
const changes: TransactionSpec = state.changeByRange((sel: SelectionRange) => {
const lineRange = getNextLineRange(sel);
if (!lineRange) return { range: sel };
const { fromLine, toLine } = lineRange;
const baselineIndent = getBaselineIndent(fromLine, toLine);
const action = getAction(fromLine, toLine);
const allLinesBlank = areAllLinesBlankInRange(fromLine, toLine);
// Outermost list item number
let outerCounter = 1;
@@ -230,8 +246,18 @@ export const toggleList = (listType: ListType): Command => {
for (let lineNum = fromLine.number; lineNum <= toLine.number; lineNum++) {
const line = doc.line(lineNum);
const origLineContent = stripBlockquote(line);
if (origLineContent.trim() === '') {
continue; // skip blank lines
const currentLineBlank = origLineContent.trim() === '';
// To support changing the formatting of non-tight lists, skip blank lines within
// larger content. This allows changing the list format without adding a potentially
// large number of empty list items.
//
// However, if all lines are blank (e.g. if the cursor at the beginning of an empty)
// document, we're not changing the formatting of an existing list. In this case,
// skipping blank lines would cause the command to do nothing.
// See https://github.com/laurent22/joplin/pull/12745.
if (currentLineBlank && !allLinesBlank) {
continue;
}
// Content excluding the block quote start