Ticket #4: Feature Request: Activate / Deactivate Custom Sort through command

- added two new commands to enable/suspend the plugin
- intentionally put in the description convenient names: sort-on and sort-off
- code area was not covered by unit tests, not adding new one at this point (simple logic, fresh mind ;-)
This commit is contained in:
SebastianMC 2022-09-07 22:47:16 +02:00
parent 52a28f63d7
commit 9962d9e76e
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,6 +101,43 @@ export default class CustomSortPlugin extends Plugin {
} }
} }
// Safe to suspend when suspended and re-enable when enabled
switchPluginStateTo(enabled: boolean, updateRibbonBtnIcon: boolean = true) {
this.settings.suspended = !enabled;
this.saveSettings()
let iconToSet: string
if (this.settings.suspended) {
new Notice('Custom sort OFF');
this.sortSpecCache = null
iconToSet = ICON_SORT_SUSPENDED
} else {
this.readAndParseSortingSpec();
if (this.sortSpecCache) {
new Notice('Custom sort ON');
this.initialAutoOrManualSortingTriggered = true
iconToSet = ICON_SORT_ENABLED_ACTIVE
} else {
iconToSet = ICON_SORT_SUSPENDED_SYNTAX_ERROR
this.settings.suspended = true
this.saveSettings()
}
}
const fileExplorerView: FileExplorerView = this.getFileExplorer()
if (fileExplorerView) {
fileExplorerView.requestSort();
} else {
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
}
}
if (updateRibbonBtnIcon) {
setIcon(this.ribbonIconEl, iconToSet)
}
this.updateStatusBar();
}
async onload() { async onload() {
console.log("loading custom-sort"); console.log("loading custom-sort");
@ -119,42 +156,15 @@ export default class CustomSortPlugin extends Plugin {
this.settings.suspended ? ICON_SORT_SUSPENDED : ICON_SORT_ENABLED_NOT_APPLIED, this.settings.suspended ? ICON_SORT_SUSPENDED : ICON_SORT_ENABLED_NOT_APPLIED,
'Toggle custom sorting', (evt: MouseEvent) => { 'Toggle custom sorting', (evt: MouseEvent) => {
// Clicking the icon toggles between the states of custom sort plugin // Clicking the icon toggles between the states of custom sort plugin
this.settings.suspended = !this.settings.suspended; this.switchPluginStateTo(this.settings.suspended)
this.saveSettings()
let iconToSet: string
if (this.settings.suspended) {
new Notice('Custom sort OFF');
this.sortSpecCache = null
iconToSet = ICON_SORT_SUSPENDED
} else {
this.readAndParseSortingSpec();
if (this.sortSpecCache) {
new Notice('Custom sort ON');
this.initialAutoOrManualSortingTriggered = true
iconToSet = ICON_SORT_ENABLED_ACTIVE
} else {
iconToSet = ICON_SORT_SUSPENDED_SYNTAX_ERROR
this.settings.suspended = true
this.saveSettings()
}
}
const fileExplorerView: FileExplorerView = this.getFileExplorer()
if (fileExplorerView) {
fileExplorerView.requestSort();
} else {
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
}
}
setIcon(this.ribbonIconEl, iconToSet)
this.updateStatusBar();
}); });
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')