Repository navigation
Expand file tree
/
Copy pathviews.ts
More file actions
161 lines (155 loc) · 5.94 KB
/
Copy pathviews.ts
File metadata and controls
161 lines (155 loc) · 5.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Items, actions and the new task form.
import { type Action, action, copy, field, form, Icons, type Item, open, pop, submenu, toast } from "@backslash/api";
import type { Client } from "./api.ts";
import {
checklist,
colorOf,
dateFields,
dueChoices,
dueOf,
findProject,
INBOX,
parseQuickAdd,
parseWhen,
PRIORITIES,
priorityOf,
type Project,
repeatText,
shortDue,
type Task,
taskMarkdown,
taskMetadata,
taskSection,
taskUrl,
} from "./lib.ts";
import { notifyChanged } from "./live.ts";
async function edit(api: Client, task: Task, fields: Record<string, unknown>, message: string) {
await api.call("POST", `/task/${task.id}`, { id: task.id, projectId: task.projectId, ...fields });
notifyChanged();
await toast(message, { style: "success" });
}
export function taskActions(api: Client, task: Task, projects: Project[]): Action[] {
return [
action("Complete Task", async () => {
await api.call("POST", `/project/${task.projectId}/task/${task.id}/complete`);
notifyChanged();
await toast(task.repeatFlag ? "Done, next one scheduled" : `Completed “${task.title}”`, { style: "success" });
}, { icon: Icons.squareCheck }),
open(taskUrl(task), "Open in TickTick"),
submenu(
"Change Due Date",
dueChoices().filter((c) => c.date).map((c) =>
action(c.title, () => {
const when = parseWhen(c.date!)!;
// Keep the time of day of a task due at a time.
const due = dueOf(task);
if (due?.time) when.date.setHours(due.date.getHours(), due.date.getMinutes());
return edit(api, task, dateFields({ date: when.date, time: !!due?.time }), `Due ${c.title.toLowerCase()}`);
}, { icon: Icons.calendar })
),
{ icon: Icons.calendar, shortcut: "<Ctrl>d", section: "Task" },
),
submenu(
"Set Priority",
PRIORITIES.map((p) =>
action(p.name, () => edit(api, task, { priority: p.value }, `Priority ${p.name.toLowerCase()}`), {
icon: Icons.flag,
})
),
{ icon: Icons.flag, shortcut: "<Ctrl>p", section: "Task" },
),
action("New Task…", () => taskForm(api, projects, { project: task.projectId }), {
icon: Icons.plus,
shortcut: "<Ctrl>n",
section: "Task",
}),
copy(task.title, "Copy Title", { shortcut: "<Ctrl>period", section: "Copy" }),
copy(taskUrl(task), "Copy Link", { shortcut: "<Ctrl><Shift>c", section: "Copy" }),
action("Delete Task", async () => {
await api.call("DELETE", `/project/${task.projectId}/task/${task.id}`);
notifyChanged();
await toast("Deleted", { style: "success" });
}, {
icon: Icons.trash,
destructive: true,
confirm: `Delete “${task.title}”?`,
shortcut: "<Ctrl><Shift>Delete",
section: "Danger",
}),
];
}
export function taskItem(api: Client, task: Task, projects: Project[], now = new Date()): Item {
const project = projects.find((p) => p.id === task.projectId);
const due = dueOf(task);
const prio = priorityOf(task.priority);
const list = checklist(task);
const section = taskSection(task, now);
return {
id: task.id,
title: task.title || "Untitled",
subtitle: (task.content || task.desc || "").split("\n")[0].replace(/[*_`#>]/g, "").trim().slice(0, 80) || undefined,
icon: { source: Icons.square, tint: prio.color },
section,
keywords: [project?.name ?? "", ...(task.tags ?? [])].filter(Boolean),
accessories: [
...(list.total
? [{
icon: Icons.squareCheck,
text: `${list.done}/${list.total}`,
color: list.done === list.total ? "green" as const : undefined,
tooltip: "Checklist",
}]
: []),
...(task.repeatFlag ? [{ icon: Icons.repeat, tooltip: repeatText(task.repeatFlag) }] : []),
...(due
? [{
icon: Icons.calendar,
// In "Today", the time says more than "today".
text: section === "Today" && !due.time ? "today" : shortDue(due.date, now, due.time).replace(/^today /, ""),
color: section === "Overdue" ? "red" as const : undefined,
tooltip: "Due",
}]
: []),
...(project ? [{ icon: { source: Icons.dot, tint: colorOf(project.color) }, text: project.name }] : []),
],
detail: { markdown: taskMarkdown(task), metadata: taskMetadata(task, project, now) },
actions: taskActions(api, task, projects),
};
}
export async function createTask(api: Client, projects: Project[], text: string, projectId?: string) {
const q = parseQuickAdd(text);
const named = findProject(projects, q.project);
if (q.project && !named) throw new Error(`No TickTick list “${q.project}”`);
const target = named?.id ?? projectId;
const task = await api.call<Task>("POST", "/task", {
title: q.title,
priority: q.priority,
// Without a list, TickTick files it in the inbox.
...(target && target !== INBOX.id && !/^inbox/.test(target) ? { projectId: target } : {}),
...(q.when ? dateFields(q.when) : {}),
});
notifyChanged();
const where = named?.name ?? projects.find((p) => p.id === (target ?? task.projectId))?.name ?? "Inbox";
const when = q.when ? ` · ${shortDue(q.when.date, new Date(), q.when.time)}` : "";
return { task, summary: `Added “${q.title}” to ${where}${when}` };
}
export function taskForm(api: Client, projects: Project[], options: { project?: string } = {}) {
return form({
title: "New TickTick task",
submitTitle: "Add Task",
fields: [
field.text("title", "Task", {
required: true,
placeholder: "Call Bob tomorrow 3pm !high: dates and priority are understood",
}),
field.dropdown("project", "List", projects.map((p) => ({ value: p.id, title: p.name })), {
value: options.project ?? projects[0]?.id,
}),
],
async submit(values) {
const { summary } = await createTask(api, projects, String(values.title), String(values.project));
await toast(summary, { style: "success" });
return pop();
},
});
}