1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Desktop: Fixes #10895: Fix alt+up/alt+down fails to re-order multiple lines (#10899)

This commit is contained in:
Henry Heino 2024-08-22 13:52:22 -07:00 committed by GitHub
parent ea420967c4
commit 6a0dd4e20d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 3 additions and 89 deletions

View File

@ -798,8 +798,6 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
packages/editor/CodeMirror/editorCommands/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.test.js
packages/editor/CodeMirror/editorCommands/swapLine.js
packages/editor/CodeMirror/getScrollFraction.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js

2
.gitignore vendored
View File

@ -775,8 +775,6 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
packages/editor/CodeMirror/editorCommands/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.test.js
packages/editor/CodeMirror/editorCommands/swapLine.js
packages/editor/CodeMirror/getScrollFraction.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js

View File

@ -1,13 +1,12 @@
import { EditorView } from '@codemirror/view';
import { EditorCommandType, ListType } from '../../types';
import { undo, redo, selectAll, indentSelection, cursorDocStart, cursorDocEnd, cursorLineStart, cursorLineEnd, deleteToLineStart, deleteToLineEnd, undoSelection, redoSelection, cursorPageDown, cursorPageUp, cursorCharRight, cursorCharLeft, insertNewlineAndIndent, cursorLineDown, cursorLineUp, toggleComment, deleteLine } from '@codemirror/commands';
import { undo, redo, selectAll, indentSelection, cursorDocStart, cursorDocEnd, cursorLineStart, cursorLineEnd, deleteToLineStart, deleteToLineEnd, undoSelection, redoSelection, cursorPageDown, cursorPageUp, cursorCharRight, cursorCharLeft, insertNewlineAndIndent, cursorLineDown, cursorLineUp, toggleComment, deleteLine, moveLineUp, moveLineDown } from '@codemirror/commands';
import {
decreaseIndent, increaseIndent,
toggleBolded, toggleCode,
toggleHeaderLevel, toggleItalicized,
toggleList, toggleMath,
} from '../markdown/markdownCommands';
import swapLine, { SwapLineDirection } from './swapLine';
import duplicateLine from './duplicateLine';
import sortSelectedLines from './sortSelectedLines';
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
@ -55,8 +54,8 @@ const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
[EditorCommandType.IndentLess]: decreaseIndent,
[EditorCommandType.IndentAuto]: indentSelection,
[EditorCommandType.InsertNewlineAndIndent]: insertNewlineAndIndent,
[EditorCommandType.SwapLineUp]: swapLine(SwapLineDirection.Up),
[EditorCommandType.SwapLineDown]: swapLine(SwapLineDirection.Down),
[EditorCommandType.SwapLineUp]: moveLineUp,
[EditorCommandType.SwapLineDown]: moveLineDown,
[EditorCommandType.GoDocEnd]: cursorDocEnd,
[EditorCommandType.GoDocStart]: cursorDocStart,
[EditorCommandType.GoLineStart]: cursorLineStart,

View File

@ -1,32 +0,0 @@
import { EditorView } from '@codemirror/view';
import { EditorSelection } from '@codemirror/state';
import swapLine, { SwapLineDirection } from './swapLine';
describe('swapLine', () => {
it('should swap line down', () => {
const initialText = 'Hello\nWorld\nJoplin\n';
const editorView = new EditorView({
doc: initialText,
selection: EditorSelection.cursor(0),
});
swapLine(SwapLineDirection.Down)(editorView);
const result = editorView.state.doc.toString();
expect(result).toBe('World\nHello\nJoplin\n');
});
it('should swap line up', () => {
const initialText = 'Hello\nWorld\nJoplin\n';
const editorView = new EditorView({
doc: initialText,
selection: EditorSelection.cursor(6),
});
swapLine(SwapLineDirection.Up)(editorView);
const result = editorView.state.doc.toString();
expect(result).toBe('World\nHello\nJoplin\n');
});
});

View File

@ -1,49 +0,0 @@
import { EditorSelection } from '@codemirror/state';
import { Command, EditorView } from '@codemirror/view';
export enum SwapLineDirection {
Up = -1,
Down = 1,
}
const swapLine = (direction: SwapLineDirection): Command => (editor: EditorView) => {
const state = editor.state;
const doc = state.doc;
const transaction = state.changeByRange(range => {
const currentLine = doc.lineAt(range.anchor);
const otherLineNumber = currentLine.number + direction;
// Out of range? No changes.
if (otherLineNumber <= 0 || otherLineNumber > doc.lines) {
return { range };
}
const otherLine = doc.line(otherLineNumber);
let deltaPos;
if (direction === SwapLineDirection.Down) {
// +1: include newline
deltaPos = otherLine.length + 1;
} else {
deltaPos = otherLine.from - currentLine.from;
}
return {
range: EditorSelection.range(range.anchor + deltaPos, range.head + deltaPos),
changes: [{
from: currentLine.from,
to: currentLine.to,
insert: otherLine.text,
}, {
from: otherLine.from,
to: otherLine.to,
insert: currentLine.text,
}],
};
});
editor.dispatch(transaction);
return true;
};
export default swapLine;