From 6a0dd4e20d3a315f59347a4e1a0d322bacd5e30c Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:52:22 -0700 Subject: [PATCH] Desktop: Fixes #10895: Fix alt+up/alt+down fails to re-order multiple lines (#10899) --- .eslintignore | 2 - .gitignore | 2 - .../editorCommands/editorCommands.ts | 7 ++- .../editorCommands/swapLine.test.ts | 32 ------------ .../CodeMirror/editorCommands/swapLine.ts | 49 ------------------- 5 files changed, 3 insertions(+), 89 deletions(-) delete mode 100644 packages/editor/CodeMirror/editorCommands/swapLine.test.ts delete mode 100644 packages/editor/CodeMirror/editorCommands/swapLine.ts diff --git a/.eslintignore b/.eslintignore index 57b71d647..4f3b056b7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/.gitignore b/.gitignore index 9b429e349..08273fe02 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/packages/editor/CodeMirror/editorCommands/editorCommands.ts b/packages/editor/CodeMirror/editorCommands/editorCommands.ts index 5e292bd7a..e0037eedd 100644 --- a/packages/editor/CodeMirror/editorCommands/editorCommands.ts +++ b/packages/editor/CodeMirror/editorCommands/editorCommands.ts @@ -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.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, diff --git a/packages/editor/CodeMirror/editorCommands/swapLine.test.ts b/packages/editor/CodeMirror/editorCommands/swapLine.test.ts deleted file mode 100644 index 917bebe9e..000000000 --- a/packages/editor/CodeMirror/editorCommands/swapLine.test.ts +++ /dev/null @@ -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'); - }); -}); diff --git a/packages/editor/CodeMirror/editorCommands/swapLine.ts b/packages/editor/CodeMirror/editorCommands/swapLine.ts deleted file mode 100644 index c6fc21bae..000000000 --- a/packages/editor/CodeMirror/editorCommands/swapLine.ts +++ /dev/null @@ -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;