-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
70 lines (48 loc) · 1.9 KB
/
plugin.ts
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
import { Task } from "./task";
import i18n from './i18n';
const section = ui.createProjectPanelSection();
let requestId;
data.onProjectSelect.subscribe(async project => {
section.removeAllChildren();
if (project) {
const currentRequestId = requestId = Math.random();
const tasks: Task[] = await project.storage.organization.read<Task[]>('todo') || [];
if (requestId != currentRequestId) {
return;
}
const nameTextField = new ui.TextField('', '', i18n.Name());
section.add(nameTextField);
const createButton = new ui.Button(ui.icons.plus, i18n.Create_Task(), async () => {
const task = new Task(nameTextField.value);
tasks.unshift(task);
await project.storage.organization.write('todo', tasks);
const firstChild = tasksContainer.children[0];
if (firstChild) {
tasksContainer.insertBefore(renderTask(task), firstChild);
} else {
tasksContainer.add(renderTask(task));
}
});
section.add(createButton);
section.add(new ui.Separator());
const renderTask = (task) => {
const checkbox = new ui.Checkbox(task.name, false);
checkbox.onValueChange.subscribe(async () => {
if (task.done) {
return;
}
task.done = true;
await project.storage.organization.write('todo', tasks);
Timer.timeout(() => checkbox.parent?.remove(checkbox), 250);
});
return checkbox;
};
const tasksContainer = new ui.Container();
section.add(tasksContainer);
for (let task of tasks) {
if (!task.done) {
tasksContainer.add(renderTask(task));
}
}
}
});