Skip to content

Commit 8f7c6f6

Browse files
committed
feat: support status switcher
1 parent b256362 commit 8f7c6f6

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

src/autoCompleteParent.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@ function findTaskCompletion(tr: Transaction): {
8686
) => {
8787
// If a change involves inserting an 'x' character
8888
const insertedText = inserted.toString();
89-
if (insertedText === "x" || insertedText === "X") {
89+
console.log(insertedText);
90+
if (
91+
insertedText === "x" ||
92+
insertedText === "X" ||
93+
insertedText.trim() === "- [x]"
94+
) {
9095
// Get the position context
9196
const pos = fromB;
9297
const line = tr.newDoc.lineAt(pos);
9398
const lineText = line.text;
94-
console.log(lineText);
9599

96100
// Check if this is a task being completed ([ ] to [x])
97101
// Matches the pattern where the cursor is between [ and ]

src/taskProgressBarIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "./taskProgressBarSetting";
1515
import { EditorView } from "@codemirror/view";
1616
import { autoCompleteParentExtension } from "./autoCompleteParent";
17-
import { taskStatusSwitcherExtension } from "./task-status-switcher";
17+
import { taskStatusSwitcherExtension } from "./taskStatusSwitcher";
1818

1919
class TaskProgressBarPopover extends HoverPopover {
2020
plugin: TaskProgressBarPlugin;

src/taskProgressBarSetting.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { App, PluginSettingTab, Setting, Modal } from "obsidian";
22
import TaskProgressBarPlugin from "./taskProgressBarIndex";
33
import { allStatusCollections } from "./task-status";
4-
import { STATE_MARK_MAP, TaskState } from "./task-status-switcher";
4+
import { STATE_MARK_MAP, TaskState } from "./taskStatusSwitcher";
55

66
export interface TaskProgressBarSettings {
7+
showProgressBar: boolean;
78
addTaskProgressBarToHeading: boolean;
89
enableHeadingProgressBar: boolean;
910
addNumberToProgressBar: boolean;
@@ -46,6 +47,7 @@ export interface TaskProgressBarSettings {
4647
}
4748

4849
export const DEFAULT_SETTINGS: TaskProgressBarSettings = {
50+
showProgressBar: false,
4951
addTaskProgressBarToHeading: false,
5052
enableHeadingProgressBar: false,
5153
addNumberToProgressBar: false,
@@ -118,6 +120,18 @@ export class TaskProgressBarSettingTab extends PluginSettingTab {
118120

119121
containerEl.createEl("h2", { text: "📍 Task Progress Bar" });
120122

123+
new Setting(containerEl)
124+
.setName("Show progress bar")
125+
.setDesc("Toggle this to show the progress bar.")
126+
.addToggle((toggle) =>
127+
toggle
128+
.setValue(this.plugin.settings.showProgressBar)
129+
.onChange(async (value) => {
130+
this.plugin.settings.showProgressBar = value;
131+
this.applySettingsUpdate();
132+
})
133+
);
134+
121135
new Setting(containerEl)
122136
.setName("Add progress bar to Heading")
123137
.setDesc(

src/widget.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,23 @@ class TaskProgressBarWidget extends WidgetType {
168168
// Calculate percentage of completed tasks
169169
const percentage =
170170
Math.round((this.completed / this.total) * 10000) / 100;
171-
171+
172172
// Check if custom progress ranges are enabled
173173
if (this.plugin?.settings.customizeProgressRanges) {
174174
// Find a matching range for the current percentage
175-
const matchingRange = this.plugin.settings.progressRanges.find(
176-
range => percentage >= range.min && percentage <= range.max
177-
);
178-
175+
const matchingRange =
176+
this.plugin.settings.progressRanges.find(
177+
(range) =>
178+
percentage >= range.min &&
179+
percentage <= range.max
180+
);
181+
179182
// If a matching range is found, use its custom text
180183
if (matchingRange) {
181-
text = matchingRange.text.replace("{{PROGRESS}}", percentage.toString());
184+
text = matchingRange.text.replace(
185+
"{{PROGRESS}}",
186+
percentage.toString()
187+
);
182188
} else {
183189
text = `${percentage}%`;
184190
}
@@ -257,6 +263,11 @@ class TaskProgressBarWidget extends WidgetType {
257263
cls: "progress-bar-inline-background",
258264
});
259265

266+
this.progressBackGroundEl.toggleClass(
267+
"hidden",
268+
!this.plugin?.settings.showProgressBar
269+
);
270+
260271
// Create elements for each status type
261272
this.progressEl = this.progressBackGroundEl.createEl("div", {
262273
cls: "progress-bar-inline progress-completed",
@@ -286,17 +297,23 @@ class TaskProgressBarWidget extends WidgetType {
286297
if (this.plugin?.settings.showPercentage) {
287298
const percentage =
288299
Math.round((this.completed / this.total) * 10000) / 100;
289-
300+
290301
// Check if custom progress ranges are enabled
291302
if (this.plugin?.settings.customizeProgressRanges) {
292303
// Find a matching range for the current percentage
293-
const matchingRange = this.plugin.settings.progressRanges.find(
294-
range => percentage >= range.min && percentage <= range.max
295-
);
296-
304+
const matchingRange =
305+
this.plugin.settings.progressRanges.find(
306+
(range) =>
307+
percentage >= range.min &&
308+
percentage <= range.max
309+
);
310+
297311
// If a matching range is found, use its custom text
298312
if (matchingRange) {
299-
text = matchingRange.text.replace("{{PROGRESS}}", percentage.toString());
313+
text = matchingRange.text.replace(
314+
"{{PROGRESS}}",
315+
percentage.toString()
316+
);
300317
} else {
301318
text = `${percentage}%`;
302319
}

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
overflow: hidden;
154154
}
155155

156+
.progress-bar-inline-background.hidden {
157+
display: none;
158+
}
159+
156160
/* Status indicators in the task number display */
157161
.cm-task-progress-bar .task-status-indicator {
158162
display: inline-block;

0 commit comments

Comments
 (0)