Skip to content

Commit 9962d9e

Browse files
committed
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 ;-)
1 parent 52a28f6 commit 9962d9e

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

src/custom-sort/sorting-spec-processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ export class SortingSpecProcessor {
499499

500500
this.recentErrorMessage =
501501
`File: ${this.currentSortingSpecContainerFilePath}\n` +
502-
(hasLineContext ? `Line #${this.currentEntryLineIdx}: "${this.currentEntryLine}"\n` : '') +
502+
(hasLineContext ? `Specification line #${this.currentEntryLineIdx}: "${this.currentEntryLine}"\n` : '') +
503503
`Problem: ${code}:${problemLabel}\n` +
504504
`Details: ${details}`
505505
this.problemAlreadyReportedForCurrentLine = true

src/main.ts

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,43 @@ export default class CustomSortPlugin extends Plugin {
101101
}
102102
}
103103

104+
// Safe to suspend when suspended and re-enable when enabled
105+
switchPluginStateTo(enabled: boolean, updateRibbonBtnIcon: boolean = true) {
106+
this.settings.suspended = !enabled;
107+
this.saveSettings()
108+
let iconToSet: string
109+
if (this.settings.suspended) {
110+
new Notice('Custom sort OFF');
111+
this.sortSpecCache = null
112+
iconToSet = ICON_SORT_SUSPENDED
113+
} else {
114+
this.readAndParseSortingSpec();
115+
if (this.sortSpecCache) {
116+
new Notice('Custom sort ON');
117+
this.initialAutoOrManualSortingTriggered = true
118+
iconToSet = ICON_SORT_ENABLED_ACTIVE
119+
} else {
120+
iconToSet = ICON_SORT_SUSPENDED_SYNTAX_ERROR
121+
this.settings.suspended = true
122+
this.saveSettings()
123+
}
124+
}
125+
const fileExplorerView: FileExplorerView = this.getFileExplorer()
126+
if (fileExplorerView) {
127+
fileExplorerView.requestSort();
128+
} else {
129+
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
130+
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
131+
}
132+
}
133+
134+
if (updateRibbonBtnIcon) {
135+
setIcon(this.ribbonIconEl, iconToSet)
136+
}
137+
138+
this.updateStatusBar();
139+
}
140+
104141
async onload() {
105142
console.log("loading custom-sort");
106143

@@ -119,42 +156,15 @@ export default class CustomSortPlugin extends Plugin {
119156
this.settings.suspended ? ICON_SORT_SUSPENDED : ICON_SORT_ENABLED_NOT_APPLIED,
120157
'Toggle custom sorting', (evt: MouseEvent) => {
121158
// Clicking the icon toggles between the states of custom sort plugin
122-
this.settings.suspended = !this.settings.suspended;
123-
this.saveSettings()
124-
let iconToSet: string
125-
if (this.settings.suspended) {
126-
new Notice('Custom sort OFF');
127-
this.sortSpecCache = null
128-
iconToSet = ICON_SORT_SUSPENDED
129-
} else {
130-
this.readAndParseSortingSpec();
131-
if (this.sortSpecCache) {
132-
new Notice('Custom sort ON');
133-
this.initialAutoOrManualSortingTriggered = true
134-
iconToSet = ICON_SORT_ENABLED_ACTIVE
135-
} else {
136-
iconToSet = ICON_SORT_SUSPENDED_SYNTAX_ERROR
137-
this.settings.suspended = true
138-
this.saveSettings()
139-
}
140-
}
141-
const fileExplorerView: FileExplorerView = this.getFileExplorer()
142-
if (fileExplorerView) {
143-
fileExplorerView.requestSort();
144-
} else {
145-
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
146-
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
147-
}
148-
}
149-
150-
setIcon(this.ribbonIconEl, iconToSet)
151-
this.updateStatusBar();
159+
this.switchPluginStateTo(this.settings.suspended)
152160
});
153161

154162
this.addSettingTab(new CustomSortSettingTab(this.app, this));
155163

156164
this.registerEventHandlers()
157165

166+
this.registerCommands()
167+
158168
this.initialize();
159169
}
160170

@@ -187,6 +197,24 @@ export default class CustomSortPlugin extends Plugin {
187197
);
188198
}
189199

200+
registerCommands() {
201+
const plugin: CustomSortPlugin = this
202+
this.addCommand({
203+
id: 'enable-custom-sorting',
204+
name: 'Enable and apply the custom sorting, (re)parsing the sorting configuration first. Sort-on.',
205+
callback: () => {
206+
plugin.switchPluginStateTo(true, true)
207+
}
208+
});
209+
this.addCommand({
210+
id: 'suspend-custom-sorting',
211+
name: 'Suspend the custom sorting. Sort-off.',
212+
callback: () => {
213+
plugin.switchPluginStateTo(false, true)
214+
}
215+
});
216+
}
217+
190218
initialize() {
191219
this.app.workspace.onLayoutReady(() => {
192220
this.patchFileExplorerFolder();
@@ -276,7 +304,7 @@ class CustomSortSettingTab extends PluginSettingTab {
276304

277305
containerEl.empty();
278306

279-
containerEl.createEl('h2', {text: 'Settings for custom sorting plugin'});
307+
containerEl.createEl('h2', {text: 'Settings for Custom File Explorer Sorting Plugin'});
280308

281309
new Setting(containerEl)
282310
.setName('Path to the designated note containing sorting specification')

0 commit comments

Comments
 (0)