Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to exclude notes #20

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true

[*.js]
ij_javascript_enforce_trailing_comma = keep
ij_javascript_force_quote_style = true
ij_javascript_space_before_function_left_parenth = false
ij_javascript_spaces_within_imports = true
ij_javascript_spaces_within_object_literal_braces = true
ij_javascript_use_double_quotes = false
ij_javascript_use_semicolon_after_statement = true

[*.ts]
ij_typescript_enforce_trailing_comma = keep
ij_typescript_force_quote_style = true
ij_typescript_space_before_function_left_parenth = false
ij_typescript_spaces_within_imports = true
ij_typescript_spaces_within_object_literal_braces = true
ij_typescript_use_double_quotes = false
ij_typescript_use_semicolon_after_statement = true
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
main.js
dist/main.js
31 changes: 16 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# OS X
.DS_Store

# Intellij
*.iml
.idea

# npm / yarn
node_modules
package-lock.json
yarn-error.log

# build
main.js
*.js.map
# macOS
.DS_Store

# IntelliJ
*.iml
.idea

# npm / yarn
node_modules
package-lock.json
yarn-error.log
yarn.lock

# build
dist/main.js
dist/data.json
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "obsidian-sample-plugin",
"version": "0.9.7",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"name": "obsidian-plugin-todo",
"version": "0.1.0",
"description": "Text-based GTD in Obsidian.",
"main": "dist/main.js",
"scripts": {
"build": "rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"lint": "eslint '**/*.{js,ts}' --fix",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "MIT",
"author": "https://github.com/larslockefeer",
"license": "GPL-3.0-or-later",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
Expand Down Expand Up @@ -39,7 +39,7 @@
}
},
"lint-staged": {
"*.{js,ts,ts}": [
"*.{js,ts}": [
"eslint --fix"
]
}
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
input: 'main.ts',
input: 'src/main.ts',
output: {
dir: '.',
dir: 'dist',
sourcemap: 'inline',
format: 'cjs',
exports: 'default',
Expand Down
File renamed without changes.
44 changes: 41 additions & 3 deletions main.ts → src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,46 @@ import { VIEW_TYPE_TODO } from './constants';
import { TodoItemView, TodoItemViewProps } from './ui/TodoItemView';
import { TodoItem, TodoItemStatus } from './model/TodoItem';
import { TodoIndex } from './model/TodoIndex';
import { DEFAULT_SETTINGS, TodoPluginSettings, TodoPluginSettingTab } from './settings';

export default class TodoPlugin extends Plugin {
settings: TodoPluginSettings;
private todoIndex: TodoIndex;
private view: TodoItemView;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.todoIndex = new TodoIndex(this.app.vault, this.tick.bind(this));
this.todoIndex = new TodoIndex(this.app.vault, this.tick.bind(this), this);
}

async onload(): Promise<void> {
await this.loadSettings();
this.addSettingTab(new TodoPluginSettingTab(this.app, this));

this.addCommand({
id: 'refresh-all',
name: 'Refresh All',
callback: () => {
this.prepareIndex(true);
},
});
Comment on lines +22 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is a great one! Maybe we can add one command to easily create a todo list. I like how easy to create a todo list at Craft, maybe we can bring it to obsidian
Screenshot 2022-08-16 at 18 47 22


this.addCommand({
id: 'ignore-note',
name: 'Ignore this note',
checkCallback: (checking: boolean) => {
const leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
this.settings.ignoredNotes[this.app.workspace.getActiveFile().path.trim()] = null;
this.saveSettings();
}
return true;
}
return false;
},
});

this.registerView(VIEW_TYPE_TODO, (leaf: WorkspaceLeaf) => {
const todos: TodoItem[] = [];
const props = {
Expand Down Expand Up @@ -52,8 +81,8 @@ export default class TodoPlugin extends Plugin {
});
}

async prepareIndex(): Promise<void> {
await this.todoIndex.initialize();
async prepareIndex(notify = false): Promise<void> {
await this.todoIndex.initialize(notify);
}

tick(todos: TodoItem[]): void {
Expand All @@ -64,4 +93,13 @@ export default class TodoPlugin extends Plugin {
};
});
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
await this.prepareIndex();
}
}
31 changes: 21 additions & 10 deletions model/TodoIndex.ts → src/model/TodoIndex.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import { TAbstractFile, TFile, Vault } from 'obsidian';
import { TodoItem, TodoItemStatus } from '../model/TodoItem';
import { TodoParser } from '../model/TodoParser';
import { Notice, TAbstractFile, TFile, Vault } from 'obsidian';
import { TodoItem, TodoItemStatus } from './TodoItem';
import { TodoParser } from './TodoParser';
import type TodoPlugin from '../main';

export class TodoIndex {
private vault: Vault;
private todos: Map<string, TodoItem[]>;
private listeners: ((todos: TodoItem[]) => void)[];
plugin: TodoPlugin;

constructor(vault: Vault, listener: (todos: TodoItem[]) => void) {
constructor(vault: Vault, listener: (todos: TodoItem[]) => void, plugin: TodoPlugin) {
this.vault = vault;
this.todos = new Map<string, TodoItem[]>();
this.listeners = [listener];
this.plugin = plugin;
}

async initialize(): Promise<void> {
async initialize(notify = false): Promise<void> {
// TODO: persist index & last sync timestamp; only parse files that changed since then.
const todoMap = new Map<string, TodoItem[]>();
let numberOfTodos = 0;
let ignoredNotes = 0;
const timeStart = new Date().getTime();

const markdownFiles = this.vault.getMarkdownFiles();
for (const file of markdownFiles) {
if (file.path in this.plugin.settings.ignoredNotes) {
console.log(`[obsidian-plugin-todo] ignoring ${file.path}`);
ignoredNotes++;
continue;
}
const todos = await this.parseTodosInFile(file);
numberOfTodos += todos.length;
if (todos.length > 0) {
Expand All @@ -29,11 +38,13 @@ export class TodoIndex {
}

const totalTimeMs = new Date().getTime() - timeStart;
console.log(
`[obsidian-plugin-todo] Parsed ${numberOfTodos} TODOs from ${markdownFiles.length} markdown files in (${
totalTimeMs / 1000.0
}s)`,
);
const msg = `Parsed ${numberOfTodos} TODO${numberOfTodos > 1 ? 's' : ''} from ${
markdownFiles.length - ignoredNotes
} note${markdownFiles.length > 1 ? 's' : ''}`;
console.log('[obsidian-plugin-todo] ' + msg + ` in (${totalTimeMs / 1000.0}s)`);
if (notify) {
new Notice(msg);
}
this.todos = todoMap;
this.registerEventHandlers();
this.invokeListeners();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { App, PluginSettingTab, Setting } from 'obsidian';
import type TodoPlugin from './main';

export const DEFAULT_SETTINGS: TodoPluginSettings = {
onlyRootTasks: false,
ignoredNotes: {},
};

export interface TodoPluginSettings {
onlyRootTasks: boolean;
ignoredNotes: { [key: string]: null };
}

export class TodoPluginSettingTab extends PluginSettingTab {
plugin: TodoPlugin;
app: App;

constructor(app: App, plugin: TodoPlugin) {
super(app, plugin);
this.plugin = plugin;
this.app = app;
}

display(): void {
const { containerEl } = this;

containerEl.empty();

containerEl.createEl('h2', { text: 'Obsidian TODO | Text-based GTD' });
containerEl.createEl('p', {
text:
'Collects all outstanding TODOs from your vault and presents them in lists Today, Scheduled, Inbox and Someday/Maybe.',
});
containerEl.createEl('h3', { text: 'General Settings' });
containerEl.createEl('p', {
text: '...',
});
containerEl.createEl('h3', { text: 'Ignored Notes' });
containerEl.createEl('p', {
text: 'You can ignore notes from this plugin by using the “Ignore this note” command',
});

for (const key in this.plugin.settings.ignoredNotes) {
const ignoredFilesSettingsObj = new Setting(containerEl).setDesc(key);

ignoredFilesSettingsObj.addButton((button) => {
button.setButtonText('Delete').onClick(async () => {
delete this.plugin.settings.ignoredNotes[key];
await this.plugin.saveSettings();
this.display();
});
});
}
}
}
File renamed without changes.
File renamed without changes.