Skip to content

Commit

Permalink
new: command "Sort selected lines (insensitive)"
Browse files Browse the repository at this point in the history
  • Loading branch information
alondmnt committed May 30, 2023
1 parent 0790ebf commit 58209e1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joplin-plugin-suitcase",
"version": "0.2.2",
"version": "0.3.0",
"scripts": {
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive",
"prepare": "npm run dist",
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import joplin from 'api';
import { MenuItemLocation } from 'api/types';
import { titleCase } from 'title-case';
import { sortSelectedLines } from './sort';

const KATAKANA = {
"half": "。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚",
Expand Down Expand Up @@ -184,5 +185,14 @@ joplin.plugins.register({
accelerator: 'CmdOrCtrl+Alt+Shift+H',
},
], MenuItemLocation.Edit);

joplin.commands.register({
name: 'suitcase.sort',
label: 'Sort selected lines (insensitive)',
execute: async () => {
sortSelectedLines();
}
});
await joplin.views.menuItems.create('suitcaseSort', 'suitcase.sort', MenuItemLocation.EditorContextMenu, { accelerator: 'CmdOrCtrl+Alt+Shift+A' });
},
});
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 1,
"id": "joplin.plugin.alondmnt.suitcase",
"app_min_version": "2.8",
"version": "0.2.2",
"version": "0.3.0",
"name": "Suitcase",
"description": "Change the capitalization of selected text",
"author": "Alon Diament",
Expand Down
49 changes: 49 additions & 0 deletions src/sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import joplin from 'api';

// based on: joplin/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useLineSorting.ts
export async function sortSelectedLines(): Promise<void> {
let ranges = await joplin.commands.execute('editor.execCommand', {
name: 'listSelections',
});

for (let i = 0; i < ranges.length; i++) {
const { anchor, head } = ranges[i];
const start = Math.min(anchor.line, head.line);
const end = Math.max(anchor.line, head.line);

const lines = [];
const linesWithNumbers = [];
for (let j = start; j <= end; j++) {
let line = await joplin.commands.execute('editor.execCommand', {
name: 'getLine',
args: [j],
});
lines.push(line);
}

lines.sort((a, b) => {
const numA = a.trim().match(/^\s*\d+(\.\d+)*/); // Parse the leading number(s)
const numB = b.trim().match(/^\s*\d+(\.\d+)*/); // Parse the leading number(s)
if (numA && numB) {
// Compare the numbers considering them as version numbers
return numA[0].localeCompare(numB[0], undefined, { numeric: true, sensitivity: 'base' });
} else if (numA) {
return -1; // lines with numbers come first
} else if (numB) {
return 1; // lines with numbers come first
} else {
// Case insensitive sorting for lines without numbers
return a.toLowerCase().localeCompare(b.toLowerCase());
}
});

const text = lines.join('\n');
const ch = lines[lines.length - 1].length;

await joplin.commands.execute('editor.execCommand', {
name: 'replaceRange',
args: [text, { line: start, ch: 0 }, { line: end, ch: ch }],
});
}

}

0 comments on commit 58209e1

Please sign in to comment.