mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-02 12:47:41 +02:00
This commit is contained in:
parent
ea420967c4
commit
6a0dd4e20d
@ -798,8 +798,6 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
|
|||||||
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
|
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
|
||||||
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
|
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
|
||||||
packages/editor/CodeMirror/editorCommands/supportsCommand.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/getScrollFraction.js
|
||||||
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
|
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
|
||||||
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
|
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -775,8 +775,6 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
|
|||||||
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
|
packages/editor/CodeMirror/editorCommands/sortSelectedLines.test.js
|
||||||
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
|
packages/editor/CodeMirror/editorCommands/sortSelectedLines.js
|
||||||
packages/editor/CodeMirror/editorCommands/supportsCommand.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/getScrollFraction.js
|
||||||
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
|
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
|
||||||
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
|
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
import { EditorView } from '@codemirror/view';
|
import { EditorView } from '@codemirror/view';
|
||||||
import { EditorCommandType, ListType } from '../../types';
|
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 {
|
import {
|
||||||
decreaseIndent, increaseIndent,
|
decreaseIndent, increaseIndent,
|
||||||
toggleBolded, toggleCode,
|
toggleBolded, toggleCode,
|
||||||
toggleHeaderLevel, toggleItalicized,
|
toggleHeaderLevel, toggleItalicized,
|
||||||
toggleList, toggleMath,
|
toggleList, toggleMath,
|
||||||
} from '../markdown/markdownCommands';
|
} from '../markdown/markdownCommands';
|
||||||
import swapLine, { SwapLineDirection } from './swapLine';
|
|
||||||
import duplicateLine from './duplicateLine';
|
import duplicateLine from './duplicateLine';
|
||||||
import sortSelectedLines from './sortSelectedLines';
|
import sortSelectedLines from './sortSelectedLines';
|
||||||
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
|
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
|
||||||
@ -55,8 +54,8 @@ const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
|
|||||||
[EditorCommandType.IndentLess]: decreaseIndent,
|
[EditorCommandType.IndentLess]: decreaseIndent,
|
||||||
[EditorCommandType.IndentAuto]: indentSelection,
|
[EditorCommandType.IndentAuto]: indentSelection,
|
||||||
[EditorCommandType.InsertNewlineAndIndent]: insertNewlineAndIndent,
|
[EditorCommandType.InsertNewlineAndIndent]: insertNewlineAndIndent,
|
||||||
[EditorCommandType.SwapLineUp]: swapLine(SwapLineDirection.Up),
|
[EditorCommandType.SwapLineUp]: moveLineUp,
|
||||||
[EditorCommandType.SwapLineDown]: swapLine(SwapLineDirection.Down),
|
[EditorCommandType.SwapLineDown]: moveLineDown,
|
||||||
[EditorCommandType.GoDocEnd]: cursorDocEnd,
|
[EditorCommandType.GoDocEnd]: cursorDocEnd,
|
||||||
[EditorCommandType.GoDocStart]: cursorDocStart,
|
[EditorCommandType.GoDocStart]: cursorDocStart,
|
||||||
[EditorCommandType.GoLineStart]: cursorLineStart,
|
[EditorCommandType.GoLineStart]: cursorLineStart,
|
||||||
|
@ -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');
|
|
||||||
});
|
|
||||||
});
|
|
@ -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;
|
|
Loading…
Reference in New Issue
Block a user