Skip to content

Commit

Permalink
feat: added change task status context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
chrabia committed Dec 4, 2023
1 parent 978ffaf commit 2f67b32
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ export class Task {

const newStatus = StatusRegistry.getInstance().getNextStatusOrCreate(this.status);

const newTasks = this.handleNewStatus(newStatus);
logEndOfTaskEdit(logger, codeLocation, newTasks);
return newTasks;
}

private handleNewStatus(newStatus: Status): Task[] {
let newDoneDate = null;

let nextOccurrence: {
Expand Down Expand Up @@ -430,7 +436,6 @@ export class Task {
// Write next occurrence before previous occurrence.
newTasks.push(toggledTask);

logEndOfTaskEdit(logger, codeLocation, newTasks);
return newTasks;
}

Expand Down Expand Up @@ -458,6 +463,17 @@ export class Task {
return recurrenceOnNextLine ? newTasks.reverse() : newTasks;
}

public handleStatusChangeFromContextMenuWithRecurrenceInUsersOrder(newStatus: Status): Task[] {
const logger = logging.getLogger('tasks.Task');
logger.trace(
`changed task ${this.taskLocation.path} ${this.taskLocation.lineNumber} ${this.originalMarkdown} status to ${newStatus}`,
);

const newTasks = this.handleNewStatus(newStatus);
const { recurrenceOnNextLine: recurrenceOnNextLine } = getSettings();
return recurrenceOnNextLine ? newTasks.reverse() : newTasks;
}

/**
* Return whether the task is considered done.
* @returns true if the status type is {@link StatusType.DONE}, {@link StatusType.CANCELLED} or {@link StatusType.NON_TASK}, and false otherwise.
Expand Down
29 changes: 28 additions & 1 deletion src/TaskLineRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Moment } from 'moment';
import { Component, MarkdownRenderer } from 'obsidian';
import { Component, MarkdownRenderer, Menu, MenuItem } from 'obsidian';
import { GlobalFilter } from './Config/GlobalFilter';
import { TASK_FORMATS, getSettings } from './Config/Settings';
import { replaceTaskWithTasks } from './File';
import { StatusRegistry } from './StatusRegistry';
import type { Task } from './Task';
import * as taskModule from './Task';
import { TaskFieldRenderer } from './TaskFieldRenderer';
Expand Down Expand Up @@ -123,6 +124,32 @@ export class TaskLineRenderer {
});
});

checkbox.addEventListener('contextmenu', async (ev: MouseEvent) => {
const menu = new Menu();
const commonTitle = 'Change status to: ';

const getMenuItemCallback = (item: MenuItem, statusName: string, newStatusSymbol: string) => {
item.setTitle(`${commonTitle} ${statusName}`).onClick(() => {
const status = StatusRegistry.getInstance().bySymbol(newStatusSymbol);
const newTask = task.handleStatusChangeFromContextMenuWithRecurrenceInUsersOrder(status);
replaceTaskWithTasks({
originalTask: task,
newTasks: newTask,
});
});
};

const { statusSettings } = getSettings();
for (const status of statusSettings.coreStatuses) {
menu.addItem((item) => getMenuItemCallback(item, status.name, status.symbol));
}
for (const status of statusSettings.customStatuses) {
menu.addItem((item) => getMenuItemCallback(item, status.name, status.symbol));
}

menu.showAtPosition({ x: ev.clientX, y: ev.clientY });
});

li.prepend(checkbox);

// Set these to be compatible with stock obsidian lists:
Expand Down

0 comments on commit 2f67b32

Please sign in to comment.