-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathaddTask.ts
More file actions
60 lines (52 loc) · 1.68 KB
/
addTask.ts
File metadata and controls
60 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { MakeCommand } from "@/commands";
import type { Translations } from "@/i18n/translation";
import type TodoistPlugin from "@/index";
import type { TaskCreationOptions } from "@/ui/createTaskModal";
import { MarkdownView, type TFile } from "obsidian";
export const addTask: MakeCommand = (plugin: TodoistPlugin, i18n: Translations["commands"]) => {
return {
name: i18n.addTask,
callback: makeCallback(plugin),
};
};
export const addTaskWithPageInContent: MakeCommand = (
plugin: TodoistPlugin,
i18n: Translations["commands"],
) => {
return {
id: "add-task-page-content",
name: i18n.addTaskPageContent,
callback: makeCallback(plugin, { appendLinkTo: "content" }),
};
};
export const addTaskWithPageInDescription: MakeCommand = (
plugin: TodoistPlugin,
i18n: Translations["commands"],
) => {
return {
id: "add-task-page-description",
name: i18n.addTaskPageDescription,
callback: makeCallback(plugin, { appendLinkTo: "description" }),
};
};
const makeCallback = (plugin: TodoistPlugin, opts?: Partial<TaskCreationOptions>) => {
return () => {
plugin.services.modals.taskCreation({
initialContent: grabSelection(plugin),
fileContext: getFileContext(plugin),
options: {
...(opts ?? {}),
},
});
};
};
const grabSelection = (plugin: TodoistPlugin): string => {
const editorView = plugin.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
if (editorView !== undefined) {
return editorView.getSelection();
}
return window.getSelection()?.toString() ?? "";
};
const getFileContext = (plugin: TodoistPlugin): TFile | undefined => {
return plugin.app.workspace.getActiveFile() ?? undefined;
};