-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoEditor.ts
More file actions
439 lines (299 loc) · 12.5 KB
/
Copy pathTodoEditor.ts
File metadata and controls
439 lines (299 loc) · 12.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
namespace myplugin.example {
import controls = colibri.ui.controls;
import ide = colibri.ui.ide;
export const CAT_TODO_EDITOR = "myplugin.example.TodoEditorCategory";
export const CMD_ADD_TASK = "myplugin.example.AddTask";
export const CMD_MOVE_TASK_UP = "myplugin.example.MoveTaskUp";
export const CMD_MOVE_TASK_DOWN = "myplugin.example.MoveTaskDown";
/**
* A custom, rich HTML-based editor that opens ".todo" files.
*
* Unlike the built-in tree-viewer editors, this editor builds its own DOM: a
* scrollable list of task "cards" that pretty-print each task. It manages its
* own selection and feeds it to:
* - the Inspector view, through the editor Part's selection
* (setSelection / getPropertyProvider), and
* - the Outline view, through TodoEditorOutlineProvider.
*
* Selection is kept in sync in both directions between the editor, the
* outline, and the inspector.
*/
export class TodoEditor extends ide.FileEditor {
static ID = "myplugin.example.TodoEditor";
private static _factory: ide.ContentTypeEditorFactory;
static getFactory() {
return this._factory || (this._factory = new ide.ContentTypeEditorFactory(
"Todo Editor",
CONTENT_TYPE_TODO,
() => new TodoEditor()));
}
private _model: TodoModel;
private _outlineProvider: TodoEditorOutlineProvider;
private _propertyProvider: TodoEditorPropertySectionProvider;
private _contentElement: HTMLDivElement;
private _cardMap: Map<TodoTask, HTMLElement>;
private _selectedTasks: TodoTask[];
constructor() {
super(TodoEditor.ID, TodoEditor.getFactory());
this.addClass("TodoEditor");
this._model = new TodoModel();
this._outlineProvider = new TodoEditorOutlineProvider(this);
this._propertyProvider = new TodoEditorPropertySectionProvider(this);
this._cardMap = new Map();
this._selectedTasks = [];
}
getModel() {
return this._model;
}
protected createPart(): void {
this._contentElement = document.createElement("div");
this._contentElement.classList.add("TodoEditorArea");
// Clicking the empty area clears the selection.
this._contentElement.addEventListener("click", () => {
this.setSelectedTasks([], true, false);
});
this.getElement().appendChild(this._contentElement);
// Load the file content (async); the list renders when it is ready.
this.updateContent();
}
private async updateContent() {
const selectedNames = new Set(this._selectedTasks.map(t => t.getName()));
const content = await ide.FileUtils.preloadAndGetFileString(this.getInput());
let data: any = {};
try {
data = JSON.parse(content);
} catch (e) {
data = {};
}
this._model.readJSON(data);
this._selectedTasks = this._model.getTasks().filter(t => selectedNames.has(t.getName()));
this.render();
this.setSelection(this._selectedTasks);
this._outlineProvider.repaint();
}
protected async onEditorInputContentChangedByExternalEditor() {
await this.updateContent();
}
async doSave() {
const content = JSON.stringify(this._model.toJSON(), null, 4);
try {
await ide.FileUtils.setFileString_async(this.getInput(), content);
this.setDirty(false);
} catch (e) {
console.error(e);
}
}
// --- rendering ---
private render() {
if (!this._contentElement) {
return;
}
this._contentElement.innerHTML = "";
this._cardMap = new Map();
const tasks = this._model.getTasks();
if (tasks.length === 0) {
const empty = document.createElement("div");
empty.classList.add("TodoEditorEmpty");
empty.textContent = 'No tasks yet. Use "Add Task" to create one.';
this._contentElement.appendChild(empty);
} else {
for (const task of tasks) {
const card = this.buildCard(task);
this._cardMap.set(task, card);
this._contentElement.appendChild(card);
}
}
this.updateHighlights();
}
private buildCard(task: TodoTask): HTMLElement {
const card = document.createElement("div");
card.classList.add("TodoTaskCard", "state-" + task.getState());
const header = document.createElement("div");
header.classList.add("TodoTaskCardHeader");
const name = document.createElement("div");
name.classList.add("TodoTaskName");
name.textContent = task.getName();
const state = document.createElement("span");
state.classList.add("TodoTaskState", "state-" + task.getState());
state.textContent = getTodoStateLabel(task.getState());
header.appendChild(name);
header.appendChild(state);
const desc = document.createElement("div");
desc.classList.add("TodoTaskDesc");
if (task.getDescription()) {
desc.textContent = task.getDescription();
} else {
desc.classList.add("empty");
desc.textContent = "No description.";
}
card.appendChild(header);
card.appendChild(desc);
card.addEventListener("click", e => {
e.stopPropagation();
let selection: TodoTask[];
if (e.ctrlKey || e.metaKey) {
selection = this._selectedTasks.indexOf(task) >= 0
? this._selectedTasks.filter(t => t !== task)
: [...this._selectedTasks, task];
} else {
selection = [task];
}
this.setSelectedTasks(selection, true, false);
});
return card;
}
private updateHighlights() {
this._cardMap.forEach((card, task) => {
card.classList.toggle("selected", this._selectedTasks.indexOf(task) >= 0);
});
}
refreshViewers() {
this.render();
this._outlineProvider.repaint();
}
// --- selection ---
getSelectedTasks(): TodoTask[] {
return this._selectedTasks;
}
/**
* Set the selected tasks and propagate to the inspector (via the Part
* selection) and, when syncOutline is true, to the outline view.
*/
setSelectedTasks(tasks: TodoTask[], syncOutline: boolean, reveal = true) {
this._selectedTasks = tasks;
this.updateHighlights();
if (reveal && tasks.length > 0) {
const card = this._cardMap.get(tasks[tasks.length - 1]);
if (card) {
card.scrollIntoView({ block: "nearest" });
}
}
// Updates the Part selection, which the Inspector view listens to.
this.setSelection(tasks);
if (syncOutline) {
this._outlineProvider.setSelection(tasks, true, false);
this._outlineProvider.repaint();
}
}
// --- operations ---
addTask() {
const maker = new colibri.ui.ide.utils.NameMaker((t: TodoTask) => t.getName());
maker.update(this._model.getTasks());
const task = new TodoTask(maker.makeName("New Task"));
this._model.addTask(task);
this.setDirty(true);
this.refreshViewers();
this.setSelectedTasks([task], true);
}
deleteSelection() {
const tasks = this.getSelectedTasks();
if (tasks.length === 0) {
return;
}
this._model.removeTasks(tasks);
this.setDirty(true);
this.refreshViewers();
this.setSelectedTasks([], true, false);
}
moveSelection(dir: -1 | 1) {
const tasks = this.getSelectedTasks();
if (tasks.length === 0) {
return;
}
this._model.moveTasks(tasks, dir);
this.setDirty(true);
this.refreshViewers();
this.setSelectedTasks(tasks, true);
}
// --- outline & inspector wiring ---
getEditorViewerProvider(key: string): ide.EditorViewerProvider {
// "Outline" is phasereditor2d.outline.ui.views.OutlineView.EDITOR_VIEWER_PROVIDER_KEY.
// We compare against the literal to avoid depending on the outline plugin's types.
if (key === "Outline") {
return this._outlineProvider;
}
return null;
}
getPropertyProvider() {
return this._propertyProvider;
}
// --- toolbar & commands ---
createEditorToolbar(parent: HTMLElement) {
const manager = new controls.ToolbarManager(parent);
manager.addCommand(CMD_ADD_TASK, { showText: true, text: "Add Task" });
manager.addCommand(colibri.ui.ide.actions.CMD_DELETE, { showText: true, text: "Delete" });
manager.addCommand(CMD_MOVE_TASK_UP, { showText: true, text: "Move Up" });
manager.addCommand(CMD_MOVE_TASK_DOWN, { showText: true, text: "Move Down" });
return manager;
}
static registerCommands(manager: ide.commands.CommandManager) {
const editorScope = (args: ide.commands.HandlerArgs) =>
args.activeEditor instanceof TodoEditor;
const hasSelection = (args: ide.commands.HandlerArgs) =>
editorScope(args) && (args.activeEditor as TodoEditor).getSelectedTasks().length > 0;
manager.addCategory({
id: CAT_TODO_EDITOR,
name: "Todo Editor"
});
manager.add({
command: {
id: CMD_ADD_TASK,
name: "Add Task",
category: CAT_TODO_EDITOR,
tooltip: "Add a new task.",
icon: colibri.getIcon(colibri.ICON_PLUS)
},
handler: {
testFunc: editorScope,
executeFunc: args => (args.activeEditor as TodoEditor).addTask()
},
keys: {
key: "A"
}
});
// Reuse the global Delete command for our editor.
manager.add({
handler: {
testFunc: hasSelection,
executeFunc: args => (args.activeEditor as TodoEditor).deleteSelection()
}
}, colibri.ui.ide.actions.CMD_DELETE);
manager.add({
command: {
id: CMD_MOVE_TASK_UP,
name: "Move Task Up",
category: CAT_TODO_EDITOR,
tooltip: "Move the selected tasks up."
},
handler: {
testFunc: hasSelection,
executeFunc: args => (args.activeEditor as TodoEditor).moveSelection(-1)
}
});
manager.add({
command: {
id: CMD_MOVE_TASK_DOWN,
name: "Move Task Down",
category: CAT_TODO_EDITOR,
tooltip: "Move the selected tasks down."
},
handler: {
testFunc: hasSelection,
executeFunc: args => (args.activeEditor as TodoEditor).moveSelection(1)
}
});
}
}
/**
* Provides the tasks of the model as the roots of the tree. Used by the
* Outline view (which still uses a tree viewer). Tasks have no children.
*/
export class TodoContentProvider implements controls.viewers.ITreeContentProvider {
getRoots(input: any): any[] {
return input instanceof TodoModel ? input.getTasks() : [];
}
getChildren(parent: any): any[] {
return [];
}
}
}