1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Desktop: Fixes #10007: Fixed Toggle Comment & Delete/Duplicate/Sort Line Options in Beta Editor (#10016)

This commit is contained in:
Sagnik Mandal 2024-03-02 21:28:15 +05:30 committed by GitHub
parent 4c6969b17d
commit d26d9f16d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 187 additions and 1 deletions

View File

@ -602,10 +602,15 @@ packages/editor/CodeMirror/CodeMirrorControl.js
packages/editor/CodeMirror/configFromSettings.js packages/editor/CodeMirror/configFromSettings.js
packages/editor/CodeMirror/createEditor.test.js packages/editor/CodeMirror/createEditor.test.js
packages/editor/CodeMirror/createEditor.js packages/editor/CodeMirror/createEditor.js
packages/editor/CodeMirror/editorCommands/duplicateLine.test.js
packages/editor/CodeMirror/editorCommands/duplicateLine.js
packages/editor/CodeMirror/editorCommands/editorCommands.js packages/editor/CodeMirror/editorCommands/editorCommands.js
packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js
packages/editor/CodeMirror/editorCommands/insertLineAfter.js 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/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.test.js
packages/editor/CodeMirror/editorCommands/swapLine.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

5
.gitignore vendored
View File

@ -582,10 +582,15 @@ packages/editor/CodeMirror/CodeMirrorControl.js
packages/editor/CodeMirror/configFromSettings.js packages/editor/CodeMirror/configFromSettings.js
packages/editor/CodeMirror/createEditor.test.js packages/editor/CodeMirror/createEditor.test.js
packages/editor/CodeMirror/createEditor.js packages/editor/CodeMirror/createEditor.js
packages/editor/CodeMirror/editorCommands/duplicateLine.test.js
packages/editor/CodeMirror/editorCommands/duplicateLine.js
packages/editor/CodeMirror/editorCommands/editorCommands.js packages/editor/CodeMirror/editorCommands/editorCommands.js
packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js
packages/editor/CodeMirror/editorCommands/insertLineAfter.js 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/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.test.js
packages/editor/CodeMirror/editorCommands/swapLine.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

View File

@ -57,4 +57,23 @@ describe('CodeMirrorControl', () => {
expect(control.execCommand('myTestCommand')).toBe('test'); expect(control.execCommand('myTestCommand')).toBe('test');
expect(command).toHaveBeenCalledTimes(1); expect(command).toHaveBeenCalledTimes(1);
}); });
it('should toggle comments', () => {
const control = createEditorControl('Hello\nWorld\n');
control.select(1, 5);
control.execCommand('toggleComment');
expect(control.getValue()).toBe('<!-- Hello -->\nWorld\n');
control.execCommand('toggleComment');
expect(control.getValue()).toBe('Hello\nWorld\n');
});
it('should delete line', () => {
const control = createEditorControl('Hello\nWorld\n');
control.setCursor(1, 0);
control.execCommand('deleteLine');
expect(control.getValue()).toBe('Hello\n');
});
}); });

View File

@ -0,0 +1,31 @@
import { EditorView } from '@codemirror/view';
import { EditorSelection } from '@codemirror/state';
import duplicateLine from './duplicateLine';
describe('duplicateLine', () => {
it('should duplicate line', () => {
const initialText = 'Hello\nWorld\n';
const editorView = new EditorView({
doc: initialText,
selection: EditorSelection.cursor(0),
});
duplicateLine(editorView);
const result = editorView.state.doc.toString();
expect(result).toBe('Hello\nHello\nWorld\n');
});
it('should duplicate range', () => {
const initialText = 'Hello\nWorld\n';
const editorView = new EditorView({
doc: initialText,
selection: EditorSelection.range(0, 8),
});
duplicateLine(editorView);
const result = editorView.state.doc.toString();
expect(result).toBe('Hello\nWoHello\nWorld\n');
});
});

View File

@ -0,0 +1,34 @@
import { EditorSelection } from '@codemirror/state';
import { Command, EditorView } from '@codemirror/view';
const duplicateLine: Command = (editor: EditorView) => {
const state = editor.state;
const doc = state.doc;
const transaction = state.changeByRange(range => {
const currentLine = doc.lineAt(range.anchor);
let text, insertPos, selectionRange;
if (range.empty) {
text = `\n${currentLine.text}`;
insertPos = currentLine.to;
selectionRange = EditorSelection.cursor(currentLine.to + text.length);
} else {
text = doc.slice(range.from, range.to);
insertPos = range.to;
selectionRange = EditorSelection.range(range.to, range.to + text.length);
}
return {
range: selectionRange,
changes: [{
from: insertPos,
to: insertPos,
insert: text,
}],
};
});
editor.dispatch(transaction);
return true;
};
export default duplicateLine;

View File

@ -1,6 +1,6 @@
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 } 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 } from '@codemirror/commands';
import { import {
decreaseIndent, increaseIndent, decreaseIndent, increaseIndent,
toggleBolded, toggleCode, toggleBolded, toggleCode,
@ -8,6 +8,8 @@ import {
toggleList, toggleMath, toggleList, toggleMath,
} from '../markdown/markdownCommands'; } from '../markdown/markdownCommands';
import swapLine, { SwapLineDirection } from './swapLine'; import swapLine, { SwapLineDirection } from './swapLine';
import duplicateLine from './duplicateLine';
import sortSelectedLines from './sortSelectedLines';
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search'; import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
export type EditorCommandFunction = (editor: EditorView)=> void; export type EditorCommandFunction = (editor: EditorView)=> void;
@ -22,6 +24,9 @@ const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
[EditorCommandType.ToggleItalicized]: toggleItalicized, [EditorCommandType.ToggleItalicized]: toggleItalicized,
[EditorCommandType.ToggleCode]: toggleCode, [EditorCommandType.ToggleCode]: toggleCode,
[EditorCommandType.ToggleMath]: toggleMath, [EditorCommandType.ToggleMath]: toggleMath,
[EditorCommandType.ToggleComment]: toggleComment,
[EditorCommandType.DuplicateLine]: duplicateLine,
[EditorCommandType.SortSelectedLines]: sortSelectedLines,
[EditorCommandType.ToggleNumberedList]: toggleList(ListType.OrderedList), [EditorCommandType.ToggleNumberedList]: toggleList(ListType.OrderedList),
[EditorCommandType.ToggleBulletedList]: toggleList(ListType.UnorderedList), [EditorCommandType.ToggleBulletedList]: toggleList(ListType.UnorderedList),
[EditorCommandType.ToggleCheckList]: toggleList(ListType.CheckList), [EditorCommandType.ToggleCheckList]: toggleList(ListType.CheckList),
@ -39,6 +44,7 @@ const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
}, },
[EditorCommandType.DeleteToLineEnd]: deleteToLineEnd, [EditorCommandType.DeleteToLineEnd]: deleteToLineEnd,
[EditorCommandType.DeleteToLineStart]: deleteToLineStart, [EditorCommandType.DeleteToLineStart]: deleteToLineStart,
[EditorCommandType.DeleteLine]: deleteLine,
[EditorCommandType.IndentMore]: increaseIndent, [EditorCommandType.IndentMore]: increaseIndent,
[EditorCommandType.IndentLess]: decreaseIndent, [EditorCommandType.IndentLess]: decreaseIndent,
[EditorCommandType.IndentAuto]: indentSelection, [EditorCommandType.IndentAuto]: indentSelection,

View File

@ -0,0 +1,18 @@
import { EditorView } from '@codemirror/view';
import { EditorSelection } from '@codemirror/state';
import sortSelectedLines from './sortSelectedLines';
describe('sortSelectedLines', () => {
it('should sort selected lines', () => {
const initialText = 'World\nHello\n';
const editorView = new EditorView({
doc: initialText,
selection: EditorSelection.range(0, 8),
});
sortSelectedLines(editorView);
const result = editorView.state.doc.toString();
expect(result).toBe('Hello\nWorld\n');
});
});

View File

@ -0,0 +1,32 @@
import { EditorSelection } from '@codemirror/state';
import { Command, EditorView } from '@codemirror/view';
const sortSelectedLines: Command = (editor: EditorView) => {
const state = editor.state;
const doc = state.doc;
const transaction = state.changeByRange(range => {
const startLine = doc.lineAt(range.from);
const endLine = doc.lineAt(range.to);
const lines = [];
for (let j = startLine.number; j <= endLine.number; j++) {
lines.push(doc.line(j).text);
}
const sortedText = lines.sort().join('\n');
return {
range: EditorSelection.cursor(startLine.from + sortedText.length),
changes: [{
from: startLine.from,
to: endLine.to,
insert: sortedText,
}],
};
});
editor.dispatch(transaction);
return true;
};
export default sortSelectedLines;

View File

@ -0,0 +1,32 @@
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

@ -15,6 +15,9 @@ export enum EditorCommandType {
ToggleItalicized = 'textItalic', ToggleItalicized = 'textItalic',
ToggleCode = 'textCode', ToggleCode = 'textCode',
ToggleMath = 'textMath', ToggleMath = 'textMath',
ToggleComment = 'toggleComment',
DuplicateLine = 'duplicateLine',
SortSelectedLines = 'sortSelectedLines',
ToggleNumberedList = 'textNumberedList', ToggleNumberedList = 'textNumberedList',
ToggleBulletedList = 'textBulletedList', ToggleBulletedList = 'textBulletedList',
@ -37,6 +40,7 @@ export enum EditorCommandType {
// Editing and navigation commands // Editing and navigation commands
ScrollSelectionIntoView = 'scrollSelectionIntoView', ScrollSelectionIntoView = 'scrollSelectionIntoView',
DeleteLine = 'deleteLine',
DeleteToLineEnd = 'killLine', DeleteToLineEnd = 'killLine',
DeleteToLineStart = 'delLineLeft', DeleteToLineStart = 'delLineLeft',
IndentMore = 'indentMore', IndentMore = 'indentMore',