/
tool.ts
253 lines (234 loc) · 6.62 KB
/
tool.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
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
import { PanelLayout } from '@lumino/widgets';
import { NotebookTools, INotebookTracker } from '@jupyterlab/notebook';
import { Cell } from '@jupyterlab/cells';
import { JupyterFrontEnd } from '@jupyterlab/application';
import { TagWidget } from './widget';
import { AddWidget } from './addwidget';
/**
* A Tool for tag operations.
*/
export class TagTool extends NotebookTools.Tool {
/**
* Construct a new tag Tool.
*
* @param tracker - The notebook tracker.
*/
constructor(tracker: INotebookTracker, app: JupyterFrontEnd) {
super();
app;
this.tracker = tracker;
this.layout = new PanelLayout();
this.createTagInput();
}
/**
* Add an AddWidget input box to the layout.
*/
createTagInput() {
let layout = this.layout as PanelLayout;
let input = new AddWidget();
input.id = 'add-tag';
layout.insertWidget(0, input);
}
/**
* Check whether a tag is applied to the current active cell
*
* @param name - The name of the tag.
*
* @returns A boolean representing whether it is applied.
*/
checkApplied(name: string): boolean {
if (this.tracker.activeCell) {
let tags = this.tracker.activeCell.model.metadata.get('tags') as string[];
if (tags) {
for (let i = 0; i < tags.length; i++) {
if (tags[i] === name) {
return true;
}
}
}
}
return false;
}
/**
* Add a tag to the current active cell.
*
* @param name - The name of the tag.
*/
addTag(name: string) {
let cell = this.tracker.activeCell;
let tags = cell.model.metadata.get('tags') as string[];
let newTags = name.split(/[,\s]+/);
if (tags === undefined) {
tags = [];
}
for (let i = 0; i < newTags.length; i++) {
if (newTags[i] !== '' && tags.indexOf(newTags[i]) < 0) {
tags.push(newTags[i]);
}
}
cell.model.metadata.set('tags', tags);
this.refreshTags();
this.loadActiveTags();
}
/**
* Remove a tag from the current active cell.
*
* @param name - The name of the tag.
*/
removeTag(name: string) {
let cell = this.tracker.activeCell;
let tags = cell.model.metadata.get('tags') as string[];
let idx = tags.indexOf(name);
if (idx > -1) {
tags.splice(idx, 1);
}
cell.model.metadata.set('tags', tags);
if (tags.length === 0) {
cell.model.metadata.delete('tags');
}
this.refreshTags();
this.loadActiveTags();
}
/**
* Update each tag widget to represent whether it is applied to the current
* active cell.
*/
loadActiveTags() {
let layout = this.layout as PanelLayout;
for (let i = 0; i < layout.widgets.length; i++) {
layout.widgets[i].update();
}
}
/**
* Pull from cell metadata all the tags used in the notebook and update the
* stored tag list.
*/
pullTags() {
let notebook = this.tracker.currentWidget;
if (this.tracker && this.tracker.currentWidget) {
let cells = notebook.model.cells;
let allTags: string[] = [];
for (let i = 0; i < cells.length; i++) {
let metadata = cells.get(i).metadata;
let tags = metadata.get('tags') as string[];
if (tags) {
for (let j = 0; j < tags.length; j++) {
let name = tags[j] as string;
if (name !== '') {
if (allTags.indexOf(name) < 0) {
allTags.push(name);
}
}
}
}
}
this.tagList = allTags;
}
}
/**
* Pull the most recent list of tags and update the tag widgets - dispose if
* the tag no longer exists, and create new widgets for new tags.
*/
refreshTags() {
this.pullTags();
let layout = this.layout as PanelLayout;
let tags: string[] = this.tagList;
let toDispose: TagWidget[] = [];
let nWidgets = layout.widgets.length;
for (let i = 0; i < nWidgets; i++) {
let idx = tags.indexOf((layout.widgets[i] as TagWidget).name);
if (idx < 0 && layout.widgets[i].id !== 'add-tag') {
toDispose.push(layout.widgets[i] as TagWidget);
} else if (layout.widgets[i].id !== 'add-tag') {
tags.splice(idx, 1);
}
}
for (let i = 0; i < toDispose.length; i++) {
toDispose[i].dispose();
}
for (let i = 0; i < tags.length; i++) {
let widget = new TagWidget(tags[i]);
let idx = layout.widgets.length - 1;
layout.insertWidget(idx, widget);
}
}
/**
* Validate the 'tags' of cell metadata, ensuring it is a list of strings and
* that each string doesn't include spaces.
*/
validateTags(cell: Cell, taglist: string[]) {
let results: string[] = [];
for (let i = 0; i < taglist.length; i++) {
if (taglist[i] !== '' && typeof taglist[i] === 'string') {
let spl = taglist[i].split(/[,\s]+/);
for (let j = 0; j < spl.length; j++) {
if (spl[j] !== '' && results.indexOf(spl[j]) < 0) {
results.push(spl[j]);
}
}
}
}
cell.model.metadata.set('tags', results);
this.refreshTags();
this.loadActiveTags();
}
/**
* Handle a change to the active cell.
*/
protected onActiveCellChanged(): void {
this.loadActiveTags();
}
/**
* Get all tags once available.
*/
protected onAfterShow() {
this.refreshTags();
this.loadActiveTags();
}
/**
* Upon attach, add header if it doesn't already exist and listen for changes
* from the notebook tracker.
*/
protected onAfterAttach() {
if (!this.header) {
const header = document.createElement('header');
header.textContent = 'Tags in Notebook';
header.className = 'tag-header';
this.parent.node.insertBefore(header, this.node);
this.header = true;
}
if (this.tracker.currentWidget) {
this.tracker.currentWidget.context.ready.then(() => {
this.refreshTags();
this.loadActiveTags();
});
this.tracker.currentWidget.model.cells.changed.connect(() => {
this.refreshTags();
this.loadActiveTags();
});
}
this.tracker.currentChanged.connect(() => {
this.refreshTags();
this.loadActiveTags();
});
}
/**
* Handle a change to active cell metadata.
*/
protected onActiveCellMetadataChanged(): void {
let tags = this.tracker.activeCell.model.metadata.get('tags');
let taglist: string[] = [];
if (tags === undefined) {
return;
}
if (typeof tags === 'string') {
taglist.push(tags);
} else {
taglist = tags as string[];
}
this.validateTags(this.tracker.activeCell, taglist);
}
public tracker: INotebookTracker = null;
private tagList: string[] = [];
private header: boolean = false;
}