Repository navigation
Expand file tree
/
Copy pathTodoModel.ts
More file actions
160 lines (120 loc) · 4.04 KB
/
Copy pathTodoModel.ts
File metadata and controls
160 lines (120 loc) · 4.04 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
namespace myplugin.example {
/**
* The content type id used to recognize ".todo" files. The content type
* resolver is registered in ExamplePlugin.registerExtensions().
*/
export const CONTENT_TYPE_TODO = "myplugin.example.TodoContentType";
/**
* The possible states of a task. Feel free to add your own.
*/
export type TodoTaskState = "todo" | "doing" | "done";
export const TODO_STATES: { value: TodoTaskState, label: string }[] = [
{ value: "todo", label: "To Do" },
{ value: "doing", label: "In Progress" },
{ value: "done", label: "Done" },
];
export function getTodoStateLabel(state: TodoTaskState): string {
const found = TODO_STATES.find(s => s.value === state);
return found ? found.label : state;
}
/**
* A single task of a todo document.
*/
export class TodoTask {
private _name: string;
private _description: string;
private _state: TodoTaskState;
constructor(name = "New Task", description = "", state: TodoTaskState = "todo") {
this._name = name;
this._description = description;
this._state = state;
}
getName() {
return this._name;
}
setName(name: string) {
this._name = name;
}
getDescription() {
return this._description;
}
setDescription(description: string) {
this._description = description;
}
getState() {
return this._state;
}
setState(state: TodoTaskState) {
this._state = state;
}
toJSON() {
return {
name: this._name,
description: this._description,
state: this._state,
};
}
static fromJSON(data: any): TodoTask {
return new TodoTask(
data.name === undefined ? "New Task" : data.name,
data.description === undefined ? "" : data.description,
data.state === undefined ? "todo" : data.state);
}
}
/**
* The model of a todo document: an ordered list of tasks.
*
* This is the object set as the input of the editor's tree viewer and the
* outline view.
*/
export class TodoModel {
private _tasks: TodoTask[] = [];
getTasks() {
return this._tasks;
}
setTasks(tasks: TodoTask[]) {
this._tasks = tasks;
}
addTask(task: TodoTask) {
this._tasks.push(task);
}
removeTasks(tasks: TodoTask[]) {
const set = new Set(tasks);
this._tasks = this._tasks.filter(t => !set.has(t));
}
/**
* Move the given tasks one position up (dir = -1) or down (dir = 1),
* keeping the relative order of the moved tasks.
*/
moveTasks(tasks: TodoTask[], dir: -1 | 1) {
const selected = new Set(tasks);
const list = this._tasks;
if (dir === -1) {
for (let i = 1; i < list.length; i++) {
if (selected.has(list[i]) && !selected.has(list[i - 1])) {
const tmp = list[i - 1];
list[i - 1] = list[i];
list[i] = tmp;
}
}
} else {
for (let i = list.length - 2; i >= 0; i--) {
if (selected.has(list[i]) && !selected.has(list[i + 1])) {
const tmp = list[i + 1];
list[i + 1] = list[i];
list[i] = tmp;
}
}
}
}
toJSON() {
return {
tasks: this._tasks.map(t => t.toJSON()),
};
}
readJSON(data: any) {
const tasks = data && Array.isArray(data.tasks) ? data.tasks : [];
this._tasks = tasks.map((d: any) => TodoTask.fromJSON(d));
}
}
}