Merge pull request #8 from SebastianMC/4-feature-request-activate-deactivate-custom-sort-through-command

Ticket #4: Feature Request: activate deactivate custom sort through command
This commit is contained in:
SebastianMC 2022-09-07 22:51:20 +02:00 committed by GitHub
commit 90baa3c0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 32 deletions

View File

@ -499,7 +499,7 @@ export class SortingSpecProcessor {
this.recentErrorMessage = this.recentErrorMessage =
`File: ${this.currentSortingSpecContainerFilePath}\n` + `File: ${this.currentSortingSpecContainerFilePath}\n` +
(hasLineContext ? `Line #${this.currentEntryLineIdx}: "${this.currentEntryLine}"\n` : '') + (hasLineContext ? `Specification line #${this.currentEntryLineIdx}: "${this.currentEntryLine}"\n` : '') +
`Problem: ${code}:${problemLabel}\n` + `Problem: ${code}:${problemLabel}\n` +
`Details: ${details}` `Details: ${details}`
this.problemAlreadyReportedForCurrentLine = true this.problemAlreadyReportedForCurrentLine = true

View File

@ -101,25 +101,9 @@ export default class CustomSortPlugin extends Plugin {
} }
} }
async onload() { // Safe to suspend when suspended and re-enable when enabled
console.log("loading custom-sort"); switchPluginStateTo(enabled: boolean, updateRibbonBtnIcon: boolean = true) {
this.settings.suspended = !enabled;
await this.loadSettings();
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
if (this.settings.statusBarEntryEnabled) {
this.statusBarItemEl = this.addStatusBarItem();
this.updateStatusBar()
}
addIcons();
// Create an icon button in the left ribbon.
this.ribbonIconEl = this.addRibbonIcon(
this.settings.suspended ? ICON_SORT_SUSPENDED : ICON_SORT_ENABLED_NOT_APPLIED,
'Toggle custom sorting', (evt: MouseEvent) => {
// Clicking the icon toggles between the states of custom sort plugin
this.settings.suspended = !this.settings.suspended;
this.saveSettings() this.saveSettings()
let iconToSet: string let iconToSet: string
if (this.settings.suspended) { if (this.settings.suspended) {
@ -147,14 +131,40 @@ export default class CustomSortPlugin extends Plugin {
} }
} }
if (updateRibbonBtnIcon) {
setIcon(this.ribbonIconEl, iconToSet) setIcon(this.ribbonIconEl, iconToSet)
}
this.updateStatusBar(); this.updateStatusBar();
}
async onload() {
console.log("loading custom-sort");
await this.loadSettings();
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
if (this.settings.statusBarEntryEnabled) {
this.statusBarItemEl = this.addStatusBarItem();
this.updateStatusBar()
}
addIcons();
// Create an icon button in the left ribbon.
this.ribbonIconEl = this.addRibbonIcon(
this.settings.suspended ? ICON_SORT_SUSPENDED : ICON_SORT_ENABLED_NOT_APPLIED,
'Toggle custom sorting', (evt: MouseEvent) => {
// Clicking the icon toggles between the states of custom sort plugin
this.switchPluginStateTo(this.settings.suspended)
}); });
this.addSettingTab(new CustomSortSettingTab(this.app, this)); this.addSettingTab(new CustomSortSettingTab(this.app, this));
this.registerEventHandlers() this.registerEventHandlers()
this.registerCommands()
this.initialize(); this.initialize();
} }
@ -187,6 +197,24 @@ export default class CustomSortPlugin extends Plugin {
); );
} }
registerCommands() {
const plugin: CustomSortPlugin = this
this.addCommand({
id: 'enable-custom-sorting',
name: 'Enable and apply the custom sorting, (re)parsing the sorting configuration first. Sort-on.',
callback: () => {
plugin.switchPluginStateTo(true, true)
}
});
this.addCommand({
id: 'suspend-custom-sorting',
name: 'Suspend the custom sorting. Sort-off.',
callback: () => {
plugin.switchPluginStateTo(false, true)
}
});
}
initialize() { initialize() {
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(() => {
this.patchFileExplorerFolder(); this.patchFileExplorerFolder();
@ -276,7 +304,7 @@ class CustomSortSettingTab extends PluginSettingTab {
containerEl.empty(); containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for custom sorting plugin'}); containerEl.createEl('h2', {text: 'Settings for Custom File Explorer Sorting Plugin'});
new Setting(containerEl) new Setting(containerEl)
.setName('Path to the designated note containing sorting specification') .setName('Path to the designated note containing sorting specification')