mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-17 18:44:45 +02:00
Desktop,Mobile: Fix list renumbering in the Markdown editor resets the first list item number to 1 (#11220)
This commit is contained in:
parent
e77fa19fea
commit
59feec1fe2
@ -38,4 +38,30 @@ describe('renumberSelectedLists', () => {
|
|||||||
'# End',
|
'# End',
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should preserve the first list number if not 1', async () => {
|
||||||
|
const listText = [
|
||||||
|
'2. This',
|
||||||
|
'4. is',
|
||||||
|
'5. a',
|
||||||
|
'6. test',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
const editor = await createTestEditor(
|
||||||
|
`${listText}\n\n# End`,
|
||||||
|
EditorSelection.range(0, listText.length),
|
||||||
|
['OrderedList', 'ATXHeading1'],
|
||||||
|
);
|
||||||
|
|
||||||
|
editor.dispatch(renumberSelectedLists(editor.state));
|
||||||
|
|
||||||
|
expect(editor.state.doc.toString()).toBe([
|
||||||
|
'2. This',
|
||||||
|
'3. is',
|
||||||
|
'4. a',
|
||||||
|
'5. test',
|
||||||
|
'',
|
||||||
|
'# End',
|
||||||
|
].join('\n'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -13,6 +13,14 @@ const renumberSelectedLists = (state: EditorState): TransactionSpec => {
|
|||||||
// Re-numbers ordered lists and sublists with numbers on each line in [linesToHandle]
|
// Re-numbers ordered lists and sublists with numbers on each line in [linesToHandle]
|
||||||
const handleLines = (linesToHandle: Line[]) => {
|
const handleLines = (linesToHandle: Line[]) => {
|
||||||
const changes: ChangeSpec[] = [];
|
const changes: ChangeSpec[] = [];
|
||||||
|
if (linesToHandle.length === 0) {
|
||||||
|
return changes;
|
||||||
|
}
|
||||||
|
|
||||||
|
let firstListNumber = Number(listItemRegex.exec(linesToHandle[0].text)?.[2]);
|
||||||
|
if (!isFinite(firstListNumber) || firstListNumber < 1) {
|
||||||
|
firstListNumber = 1;
|
||||||
|
}
|
||||||
|
|
||||||
type ListItemRecord = {
|
type ListItemRecord = {
|
||||||
nextListNumber: number;
|
nextListNumber: number;
|
||||||
@ -20,7 +28,7 @@ const renumberSelectedLists = (state: EditorState): TransactionSpec => {
|
|||||||
};
|
};
|
||||||
const listNumberStack: ListItemRecord[] = [];
|
const listNumberStack: ListItemRecord[] = [];
|
||||||
let currentGroupIndentation = '';
|
let currentGroupIndentation = '';
|
||||||
let nextListNumber = 1;
|
let nextListNumber = firstListNumber;
|
||||||
let prevLineNumber;
|
let prevLineNumber;
|
||||||
|
|
||||||
for (const line of linesToHandle) {
|
for (const line of linesToHandle) {
|
||||||
|
Loading…
Reference in New Issue
Block a user