Skip to content

Commit

Permalink
feat: file explorer custom sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingislost committed Feb 2, 2022
1 parent 86abde1 commit a0cbeb6
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 54 deletions.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-bartender",
"name": "Bartender",
"version": "0.3.1",
"version": "0.4.0",
"minAppVersion": "0.12.5",
"description": "Allows for rearranging the elements in the status bar and sidebar ribbon",
"author": "NothingIsLost",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-bartender",
"name": "Bartender",
"version": "0.3.1",
"version": "0.4.0",
"minAppVersion": "0.12.5",
"description": "Allows for rearranging the elements in the status bar and sidebar ribbon",
"author": "NothingIsLost",
Expand Down
20 changes: 4 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-bartender",
"version": "0.0.1",
"version": "0.4.0",
"description": "Allows for rearranging the elements in the status bar and sidebar ribbon",
"main": "main.js",
"scripts": {
Expand All @@ -11,29 +11,17 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/autocomplete": "^0.19.9",
"@codemirror/commands": "^0.19.0",
"@codemirror/fold": "0.19.0",
"@codemirror/highlight": "^0.19.0",
"@codemirror/history": "^0.19.0",
"@codemirror/language": "^0.19.0",
"@codemirror/matchbrackets": "^0.19.0",
"@codemirror/panel": "^0.19.0",
"@codemirror/rangeset": "^0.19.0",
"@codemirror/search": "^0.19.0",
"@codemirror/state": "^0.19.0",
"@codemirror/stream-parser": "https://github.com/lishid/stream-parser",
"@codemirror/view": "^0.19.0",
"@types/node": "^16.11.6",
"@types/sortablejs": "^1.10.7",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0",
"esbuild": "0.13.12",
"monkey-around": "^2.2.0",
"obsidian": "^0.13.8",
"sortablejs": "^1.14.0",
"tslib": "2.3.1",
"typescript": "4.4.4",
"@types/sortablejs": "^1.10.7",
"sortablejs": "^1.14.0"
"i18next": "^21.6.10"
}
}
121 changes: 121 additions & 0 deletions src/file-explorer/custom-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Menu, TAbstractFile, TFile, TFolder } from "obsidian";

let Collator = new Intl.Collator(undefined, {
usage: "sort",
sensitivity: "base",
numeric: true,
}).compare;

let Sorter = {
alphabetical: function (first: TFile, second: TFile) {
return Collator(first.basename, second.basename);
},
alphabeticalReverse: function (first: TFile, second: TFile) {
return -Sorter.alphabetical(first, second);
},
byModifiedTime: function (first: TFile, second: TFile) {
return second.stat.mtime - first.stat.mtime;
},
byModifiedTimeReverse: function (first: TFile, second: TFile) {
return -Sorter.byModifiedTime(first, second);
},
byCreatedTime: function (first: TFile, second: TFile) {
return second.stat.ctime - first.stat.ctime;
},
byCreatedTimeReverse: function (first: TFile, second: TFile) {
return -Sorter.byCreatedTime(first, second);
},
};

const Translate = i18next.t.bind(i18next);

const SortGlyph = "up-and-down-arrows";

const sortOptionStrings = {
alphabetical: "plugins.file-explorer.label-sort-a-to-z",
alphabeticalReverse: "plugins.file-explorer.label-sort-z-to-a",
byModifiedTime: "plugins.file-explorer.label-sort-new-to-old",
byModifiedTimeReverse: "plugins.file-explorer.label-sort-old-to-new",
byCreatedTime: "plugins.file-explorer.label-sort-created-new-to-old",
byCreatedTimeReverse: "plugins.file-explorer.label-sort-created-old-to-new",
custom: "Custom",
};
const sortOptionGroups = [
["alphabetical", "alphabeticalReverse"],
["byModifiedTime", "byModifiedTimeReverse"],
["byCreatedTime", "byCreatedTimeReverse"],
["custom"],
];

export const folderSort = function (order: string[], foldersOnBottom?: boolean) {
let fileExplorer = this.fileExplorer,
folderContents = this.file.children.slice();
folderContents.sort(function (firstEl: TFile | TFolder, secondEl: TFile | TFolder) {
let firstIsFolder, secondIsFolder;
if (
foldersOnBottom &&
((firstIsFolder = firstEl instanceof TFolder) || (secondIsFolder = secondEl instanceof TFolder))
) {
return firstIsFolder && !secondIsFolder
? 1
: secondIsFolder && !firstIsFolder
? -1
: Collator(firstEl.name, secondEl.name);
} else {
if (!order) return Collator(firstEl.name, secondEl.name);

const index1 = order.indexOf(firstEl.path);
const index2 = order.indexOf(secondEl.path);

return (index1 > -1 ? index1 : Infinity) - (index2 > -1 ? index2 : Infinity);
}
});
/* Get all the file items that are children of the current folder. */

this.children = folderContents
.map((child: TAbstractFile) => fileExplorer.fileItems[child.path])
.filter((f: TAbstractFile) => f);
};

export const addSortButton = function (sorter: any, sortOption: any) {
let _this = this;
return this.addNavButton(
SortGlyph,
Translate("plugins.file-explorer.action-change-sort"),
function (event: MouseEvent) {
event.preventDefault();
let menu = new Menu(_this.app);
for (
let currentSortOption = sortOption(), groupIndex = 0, _sortOptionGroups = sortOptionGroups;
groupIndex < _sortOptionGroups.length;
groupIndex++
) {
for (
let addMenuItem = function (_sortOption: any) {
//@ts-ignore
let label = Translate(sortOptionStrings[_sortOption]);
menu.addItem(function (item) {
return item
.setTitle(label)
.setActive(_sortOption === currentSortOption)
.onClick(function () {
if (_sortOption !== currentSortOption) {
_this.app.workspace.trigger("file-explorer-sort-change", _sortOption);
}
sorter(_sortOption);
});
});
},
itemIndex = 0,
sortOptionGroup = _sortOptionGroups[groupIndex];
itemIndex < sortOptionGroup.length;
itemIndex++
) {
addMenuItem(sortOptionGroup[itemIndex]);
}
menu.addSeparator();
}
menu.showAtMouseEvent(event);
}
);
};
Loading

0 comments on commit a0cbeb6

Please sign in to comment.