#74 - Integration with Bookmarks core plugin and support for indirect drag & drop arrangement
- restructured the context menu to a single submenu of `Custom sort:` configurable in settings
This commit is contained in:
parent
8e6c8ded98
commit
39d7591ac1
153
src/main.ts
153
src/main.ts
|
@ -65,6 +65,7 @@ interface CustomSortPluginSettings {
|
||||||
notificationsEnabled: boolean
|
notificationsEnabled: boolean
|
||||||
mobileNotificationsEnabled: boolean
|
mobileNotificationsEnabled: boolean
|
||||||
automaticBookmarksIntegration: boolean
|
automaticBookmarksIntegration: boolean
|
||||||
|
customSortContextSubmenu: boolean
|
||||||
bookmarksContextMenus: boolean
|
bookmarksContextMenus: boolean
|
||||||
bookmarksGroupToConsumeAsOrderingReference: string
|
bookmarksGroupToConsumeAsOrderingReference: string
|
||||||
}
|
}
|
||||||
|
@ -75,6 +76,7 @@ const DEFAULT_SETTINGS: CustomSortPluginSettings = {
|
||||||
statusBarEntryEnabled: true,
|
statusBarEntryEnabled: true,
|
||||||
notificationsEnabled: true,
|
notificationsEnabled: true,
|
||||||
mobileNotificationsEnabled: false,
|
mobileNotificationsEnabled: false,
|
||||||
|
customSortContextSubmenu: true,
|
||||||
automaticBookmarksIntegration: false,
|
automaticBookmarksIntegration: false,
|
||||||
bookmarksContextMenus: false,
|
bookmarksContextMenus: false,
|
||||||
bookmarksGroupToConsumeAsOrderingReference: 'sortspec'
|
bookmarksGroupToConsumeAsOrderingReference: 'sortspec'
|
||||||
|
@ -94,6 +96,8 @@ const ERROR_NOTICE_TIMEOUT: number = 10000
|
||||||
// the monkey-around package doesn't export the below type
|
// the monkey-around package doesn't export the below type
|
||||||
type MonkeyAroundUninstaller = () => void
|
type MonkeyAroundUninstaller = () => void
|
||||||
|
|
||||||
|
type ContextMenuProvider = (item: MenuItem) => void
|
||||||
|
|
||||||
export default class CustomSortPlugin extends Plugin {
|
export default class CustomSortPlugin extends Plugin {
|
||||||
settings: CustomSortPluginSettings
|
settings: CustomSortPluginSettings
|
||||||
statusBarItemEl: HTMLElement
|
statusBarItemEl: HTMLElement
|
||||||
|
@ -340,16 +344,23 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
this.registerEvent(
|
const applyCustomSortMenuItem = (item: MenuItem) => {
|
||||||
app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => {
|
item.setTitle('Apply custom sorting');
|
||||||
if (!this.settings.bookmarksContextMenus) return; // Don't show the context menus at all
|
item.onClick(() => {
|
||||||
|
plugin.switchPluginStateTo(true, true)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const suspendCustomSortMenuItem = (item: MenuItem) => {
|
||||||
if (!bookmarksPlugin) return; // Don't show context menu if bookmarks plugin not available and not enabled
|
item.setTitle('Suspend custom sorting');
|
||||||
|
item.onClick(() => {
|
||||||
|
plugin.switchPluginStateTo(false, true)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
const bookmarkThisMenuItem = (item: MenuItem) => {
|
const getBookmarkThisMenuItemForFile = (file: TAbstractFile): ContextMenuProvider =>
|
||||||
item.setTitle('Custom sort: bookmark for sorting.');
|
(item: MenuItem) => {
|
||||||
item.setIcon('hashtag');
|
item.setTitle('Bookmark it for sorting.');
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -358,9 +369,10 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const unbookmarkThisMenuItem = (item: MenuItem) => {
|
|
||||||
item.setTitle('Custom sort: UNbookmark from sorting.');
|
const getUnbookmarkThisMenuItemForFile = (file: TAbstractFile): ContextMenuProvider =>
|
||||||
item.setIcon('hashtag');
|
(item: MenuItem) => {
|
||||||
|
item.setTitle('UNbookmark it from sorting.');
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -369,9 +381,10 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const bookmarkAllMenuItem = (item: MenuItem) => {
|
|
||||||
item.setTitle('Custom sort: bookmark+siblings for sorting.');
|
const getBookmarkAllMenuItemForFile = (file: TAbstractFile): ContextMenuProvider =>
|
||||||
item.setIcon('hashtag');
|
(item: MenuItem) => {
|
||||||
|
item.setTitle('Bookmark it+siblings for sorting.');
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -381,9 +394,10 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const unbookmarkAllMenuItem = (item: MenuItem) => {
|
|
||||||
item.setTitle('Custom sort: UNbookmark+all siblings from sorting.');
|
const getUnbookmarkAllMenuItemForFile = (file: TAbstractFile): ContextMenuProvider =>
|
||||||
item.setIcon('hashtag');
|
(item: MenuItem) => {
|
||||||
|
item.setTitle('UNbookmark it+siblings from sorting.');
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -394,30 +408,9 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const itemAlreadyBookmarkedForSorting: boolean = bookmarksPlugin.isBookmarkedForSorting(file)
|
const getBookmarkSelectedMenuItemForFiles = (files: TAbstractFile[]): ContextMenuProvider =>
|
||||||
if (!itemAlreadyBookmarkedForSorting) {
|
(item: MenuItem) => {
|
||||||
menu.addItem(bookmarkThisMenuItem)
|
|
||||||
} else {
|
|
||||||
menu.addItem(unbookmarkThisMenuItem)
|
|
||||||
}
|
|
||||||
menu.addItem(bookmarkAllMenuItem)
|
|
||||||
menu.addItem(unbookmarkAllMenuItem)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
if (requireApiVersion('1.4.11')) {
|
|
||||||
this.registerEvent(
|
|
||||||
// "files-menu" event was exposed in 1.4.11
|
|
||||||
// @ts-ignore
|
|
||||||
app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
|
|
||||||
if (!this.settings.bookmarksContextMenus) return; // Don't show the context menus at all
|
|
||||||
|
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
|
||||||
if (!bookmarksPlugin) return; // Don't show context menu if bookmarks plugin not available and not enabled
|
|
||||||
|
|
||||||
const bookmarkSelectedMenuItem = (item: MenuItem) => {
|
|
||||||
item.setTitle('Custom sort: bookmark selected for sorting.');
|
item.setTitle('Custom sort: bookmark selected for sorting.');
|
||||||
item.setIcon('hashtag');
|
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -428,9 +421,10 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const unbookmarkSelectedMenuItem = (item: MenuItem) => {
|
|
||||||
|
const getUnbookmarkSelectedMenuItemForFiles = (files: TAbstractFile[]): ContextMenuProvider =>
|
||||||
|
(item: MenuItem) => {
|
||||||
item.setTitle('Custom sort: UNbookmark selected from sorting.');
|
item.setTitle('Custom sort: UNbookmark selected from sorting.');
|
||||||
item.setIcon('hashtag');
|
|
||||||
item.onClick(() => {
|
item.onClick(() => {
|
||||||
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
if (bookmarksPlugin) {
|
if (bookmarksPlugin) {
|
||||||
|
@ -442,8 +436,66 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
menu.addItem(bookmarkSelectedMenuItem)
|
this.registerEvent(
|
||||||
menu.addItem(unbookmarkSelectedMenuItem)
|
app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => {
|
||||||
|
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
|
||||||
|
|
||||||
|
const customSortMenuItem = (item: MenuItem) => {
|
||||||
|
item.setTitle('Custom sort:');
|
||||||
|
item.setIcon('hashtag');
|
||||||
|
const submenu = item.setSubmenu()
|
||||||
|
|
||||||
|
submenu.addItem(applyCustomSortMenuItem)
|
||||||
|
submenu.addSeparator()
|
||||||
|
|
||||||
|
if (this.settings.bookmarksContextMenus) {
|
||||||
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
|
if (bookmarksPlugin) {
|
||||||
|
const itemAlreadyBookmarkedForSorting: boolean = bookmarksPlugin.isBookmarkedForSorting(file)
|
||||||
|
if (!itemAlreadyBookmarkedForSorting) {
|
||||||
|
submenu.addItem(getBookmarkThisMenuItemForFile(file))
|
||||||
|
} else {
|
||||||
|
submenu.addItem(getUnbookmarkThisMenuItemForFile(file))
|
||||||
|
}
|
||||||
|
submenu.addItem(getBookmarkAllMenuItemForFile(file))
|
||||||
|
submenu.addItem(getUnbookmarkAllMenuItemForFile(file))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
submenu.addItem(suspendCustomSortMenuItem)
|
||||||
|
};
|
||||||
|
|
||||||
|
menu.addItem(customSortMenuItem)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
if (requireApiVersion('1.4.11')) {
|
||||||
|
this.registerEvent(
|
||||||
|
// "files-menu" event was exposed in 1.4.11
|
||||||
|
// @ts-ignore
|
||||||
|
app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
|
||||||
|
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
|
||||||
|
|
||||||
|
const customSortMenuItem = (item: MenuItem) => {
|
||||||
|
item.setTitle('Custom sort:');
|
||||||
|
item.setIcon('hashtag');
|
||||||
|
const submenu = item.setSubmenu()
|
||||||
|
|
||||||
|
submenu.addItem(applyCustomSortMenuItem)
|
||||||
|
submenu.addSeparator()
|
||||||
|
|
||||||
|
|
||||||
|
if (this.settings.bookmarksContextMenus) {
|
||||||
|
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
|
||||||
|
if (bookmarksPlugin) {
|
||||||
|
submenu.addItem(getBookmarkSelectedMenuItemForFiles(files))
|
||||||
|
submenu.addItem(getUnbookmarkSelectedMenuItemForFiles(files))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
submenu.addItem(suspendCustomSortMenuItem)
|
||||||
|
};
|
||||||
|
|
||||||
|
menu.addItem(customSortMenuItem)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -704,6 +756,16 @@ class CustomSortSettingTab extends PluginSettingTab {
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Enable File Explorer context submenu`Custom sort:`')
|
||||||
|
.setDesc('Gives access to operations relevant for custom sorting, e.g. applying custom sorting.')
|
||||||
|
.addToggle(toggle => toggle
|
||||||
|
.setValue(this.plugin.settings.customSortContextSubmenu)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.customSortContextSubmenu = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
}));
|
||||||
|
|
||||||
containerEl.createEl('h2', {text: 'Bookmarks integration'});
|
containerEl.createEl('h2', {text: 'Bookmarks integration'});
|
||||||
const bookmarksIntegrationDescription: DocumentFragment = sanitizeHTMLToDom(
|
const bookmarksIntegrationDescription: DocumentFragment = sanitizeHTMLToDom(
|
||||||
'If enabled, order of files and folders in File Explorer will reflect the order '
|
'If enabled, order of files and folders in File Explorer will reflect the order '
|
||||||
|
@ -756,6 +818,9 @@ class CustomSortSettingTab extends PluginSettingTab {
|
||||||
.setValue(this.plugin.settings.bookmarksContextMenus)
|
.setValue(this.plugin.settings.bookmarksContextMenus)
|
||||||
.onChange(async (value) => {
|
.onChange(async (value) => {
|
||||||
this.plugin.settings.bookmarksContextMenus = value;
|
this.plugin.settings.bookmarksContextMenus = value;
|
||||||
|
if (value) {
|
||||||
|
this.plugin.settings.customSortContextSubmenu = true; // automatically enable custom sort context submenu
|
||||||
|
}
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,4 +56,8 @@ declare module 'obsidian' {
|
||||||
|
|
||||||
sortOrder: string
|
sortOrder: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MenuItem {
|
||||||
|
setSubmenu: () => Menu;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue