obsidian-tasks icon indicating copy to clipboard operation
obsidian-tasks copied to clipboard

Add hooks to let other plugins extend the Tasks plugin.

Open alexthewilde opened this issue 2 years ago • 29 comments

Description

Add hooks to let other plugins extend the Tasks plugin.

Motivation and Context

I needed a way to extend the Tasks plugin with my own functionality (see discussion).

How has this been tested?

Screenshots (if appropriate)

Here's an example of how other plugins can make use of the new hooks to extend the Tasks plugin's functionality:

Use like image

Renders as image

Implemented in new plugin

export const extendTask = (listItem: HTMLLIElement, task: any, api: any) => {
  if (api.layoutOptions.showDoTodayButton) {
    addDoTodayButton(listItem, task, api);
  }
}

export const extendParser = (line: string, layoutOptions: any) => {
  if (line === 'show do today button') {
    layoutOptions.showDoTodayButton = true;
    return true;
  }
  
  return false;
}

function addDoTodayButton(listItem: HTMLLIElement, task: any, api: any) {
  const today = window.moment();
  const isToday = task.scheduledDate && task.scheduledDate.format('YYYY-MM-DD') === today.format('YYYY-MM-DD');
  const doTodayBtn = listItem.createEl('a', {
      text: isToday ? 'not today' : 'do today',
      cls: 'tasks-do-today',
  });

  doTodayBtn.onClickEvent((event: MouseEvent) => {
      const updatedTask = new api.Task({
          ...task,
          scheduledDate: isToday ? null : today,
      });

      api.replaceTaskWithTasks({
          originalTask: task,
          newTasks: [updatedTask],
      });
  });
}

Types of changes

Changes visible to users:

  • [ ] Bug fix (prefix: fix - non-breaking change which fixes an issue)
  • [x] New feature (prefix: feat - non-breaking change which adds functionality)
  • [ ] Breaking change (prefix: feat!! or fix!! - fix or feature that would cause existing functionality to not work as expected)
  • [ ] Documentation (prefix: docs - improvements to any documentation content)
  • [ ] Sample vault (prefix: vault - improvements to the Tasks-Demo sample vault)

Internal changes:

  • [ ] Refactor (prefix: refactor - non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)
  • [ ] Tests (prefix: test - additions and improvements to unit tests and the smoke tests)
  • [ ] Infrastructure (prefix: chore - examples include GitHub Actions, issue templates)

Checklist

  • [x] My code follows the code style of this project and passes yarn run lint.
  • [x] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [ ] My change has adequate Unit Test coverage.

Terms

alexthewilde avatar Oct 22 '22 18:10 alexthewilde