1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Desktop, Mobile: Fixes #10439: Maintain cursor position when changing list indentation (#10441)

This commit is contained in:
Henry Heino 2024-05-21 02:13:37 -07:00 committed by GitHub
parent 366517999f
commit c5e3672e9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 5 deletions

View File

@ -85,7 +85,7 @@ describe('markdownCommands', () => {
it('should set headers to the proper levels (when toggling)', async () => {
const initialDocText = 'Testing...\nThis is a test.';
const editor = await createTestEditor(initialDocText, EditorSelection.cursor(3), []);
const editor = await createTestEditor(initialDocText, EditorSelection.cursor('Testing...'.length), []);
toggleHeaderLevel(1)(editor);
@ -124,7 +124,7 @@ describe('markdownCommands', () => {
'Testing...\n\n> # This is a test.\n> ...a test',
);
expect(mainSel.empty).toBe(true);
expect(mainSel.from).toBe('Testing...\n\n> # This is a test.'.length);
expect(mainSel.from).toBe('Testing...\n\n> # This'.length);
toggleHeaderLevel(3)(editor);
@ -275,5 +275,23 @@ describe('markdownCommands', () => {
to: finalText.length,
});
});
it('insertOrIncreaseIndent should preserve the cursor location when in a list', async () => {
const initialText = '- a\n- b\n- c';
const editor = await createTestEditor(
initialText,
EditorSelection.cursor(5), // In the 2nd list item
['BulletList'],
);
insertOrIncreaseIndent(editor);
expect(editor.state.doc.toString()).toBe('- a\n\t- b\n- c');
expect(editor.state.selection.main).toMatchObject({
// The indent unit is a single tab, which has length 1.
from: 6,
to: 6,
});
});
});

View File

@ -547,6 +547,7 @@ export const toggleSelectedLinesStartWith = (
const toLine = doc.lineAt(sel.to);
let hasProp = false;
let charsAdded = 0;
let charsAddedBefore = 0;
const changes = [];
const lines = [];
@ -585,7 +586,13 @@ export const toggleSelectedLinesStartWith = (
insert: '',
});
charsAdded -= match[0].length;
const deletedSize = match[0].length;
if (contentFrom <= sel.from) {
// Math.min: Handles the case where some deleted characters are before sel.from
// and others are after.
charsAddedBefore -= Math.min(sel.from - contentFrom, deletedSize);
}
charsAdded -= deletedSize;
} else {
changes.push({
from: contentFrom,
@ -593,6 +600,9 @@ export const toggleSelectedLinesStartWith = (
});
charsAdded += template.length;
if (contentFrom <= sel.from) {
charsAddedBefore += template.length;
}
}
}
@ -601,8 +611,7 @@ export const toggleSelectedLinesStartWith = (
// added text isn't helpful)
let newSel;
if (sel.empty && fromLine.number === toLine.number) {
const regionEnd = toLine.to + charsAdded;
newSel = EditorSelection.cursor(regionEnd);
newSel = EditorSelection.cursor(sel.from + charsAddedBefore);
} else {
newSel = EditorSelection.range(fromLine.from, toLine.to + charsAdded);
}