/
tokens.ts
302 lines (268 loc) · 7.26 KB
/
tokens.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
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import type { ToolbarRegistry } from '@jupyterlab/apputils';
import type { IObservableList } from '@jupyterlab/observables';
import type { VDomRenderer } from '@jupyterlab/ui-components';
import type { JSONObject } from '@lumino/coreutils';
import { Token } from '@lumino/coreutils';
import type { IDisposable } from '@lumino/disposable';
import type { ISignal } from '@lumino/signaling';
import type { Widget } from '@lumino/widgets';
/**
* Interface describing the table of contents registry.
*/
export interface ITableOfContentsRegistry {
/**
* Finds a table of contents model for a widget.
*
* ## Notes
*
* - If unable to find a table of contents model, the method return `undefined`.
*
* @param widget - widget
* @param configuration - Table of contents configuration
* @returns Table of contents model or undefined if not found
*/
getModel(
widget: Widget,
configuration?: TableOfContents.IConfig
): TableOfContents.Model | undefined;
/**
* Adds a table of contents factory to the registry.
*
* @param factory - table of contents factory
*/
add(factory: TableOfContents.IFactory): IDisposable;
}
/**
* Table of contents registry token.
*/
export const ITableOfContentsRegistry = new Token<ITableOfContentsRegistry>(
'@jupyterlab/toc:ITableOfContentsRegistry',
'A service to register table of content factory.'
);
/**
* Interface for the table of contents tracker
*/
export interface ITableOfContentsTracker {
/**
* Get the model associated with a given widget.
*
* @param widget Widget
*/
get(widget: Widget): TableOfContents.IModel<TableOfContents.IHeading> | null;
}
/**
* Table of contents tracker token.
*/
export const ITableOfContentsTracker = new Token<ITableOfContentsTracker>(
'@jupyterlab/toc:ITableOfContentsTracker',
'A widget tracker for table of contents.'
);
/**
* Namespace for table of contents interface
*/
export namespace TableOfContents {
/**
* Table of content model factory interface
*/
export interface IFactory<
W extends Widget = Widget,
H extends IHeading = IHeading
> {
/**
* Whether the factory can handle the widget or not.
*
* @param widget - widget
* @returns boolean indicating a ToC can be generated
*/
isApplicable: (widget: W) => boolean;
/**
* Create a new table of contents model for the widget
*
* @param widget - widget
* @param configuration - Table of contents configuration
* @returns The table of contents model
*/
createNew: (
widget: W,
configuration?: TableOfContents.IConfig
) => IModel<H>;
}
/**
* Table of Contents configuration
*
* #### Notes
* A document model may ignore some of those options.
*/
export interface IConfig extends JSONObject {
/**
* Base level for the highest headings
*/
baseNumbering: number;
/**
* Maximal depth of headings to display
*/
maximalDepth: number;
/**
* Whether to number first-level headings or not.
*/
numberingH1: boolean;
/**
* Whether to number headings in document or not.
*/
numberHeaders: boolean;
/**
* Whether to include cell outputs in headings or not.
*/
includeOutput: boolean;
/**
* Whether to synchronize heading collapse state between the ToC and the document or not.
*/
syncCollapseState: boolean;
}
/**
* Default table of content configuration
*/
export const defaultConfig: IConfig = {
baseNumbering: 1,
maximalDepth: 4,
numberingH1: true,
numberHeaders: false,
includeOutput: true,
syncCollapseState: false
};
/**
* Interface describing a heading.
*/
export interface IHeading {
/**
* Heading text.
*/
text: string;
/**
* HTML heading level.
*/
level: number;
/**
* Heading prefix.
*/
prefix?: string | null;
/**
* Dataset to add to the item node
*/
dataset?: Record<string, string>;
/**
* Whether the heading is collapsed or not
*/
collapsed?: boolean;
/**
* Whether the heading is marked to skip or not
*/
skip?: boolean;
}
/**
* Interface describing a widget table of contents model.
*/
export interface IModel<H extends IHeading> extends VDomRenderer.IModel {
/**
* Active heading
*/
readonly activeHeading: H | null;
/**
* Signal emitted when the active heading changes.
*/
readonly activeHeadingChanged: ISignal<IModel<H>, H | null>;
/**
* Signal emitted when a table of content section collapse state changes.
*
* If all headings state are set at the same time, the argument is null.
*/
readonly collapseChanged: ISignal<IModel<H>, H | null>;
/**
* Model configuration
*/
readonly configuration: IConfig;
/**
* Type of document supported by the model.
*
* #### Notes
* A `data-document-type` attribute with this value will be set
* on the tree view `.jp-TableOfContents-content[data-document-type="..."]`
*/
readonly documentType: string;
/**
* Returns the list of headings.
*
* @returns list of headings
*/
readonly headings: H[];
/**
* Signal emitted when the headings changes.
*/
readonly headingsChanged: ISignal<IModel<H>, void>;
/**
* Whether the model needs to be kept up to date or not.
*
* ### Notes
* This is set to `true` if the ToC panel is visible and
* to `false` if it is hidden. But some models may require
* to be always active; e.g. to add numbering in the document.
*/
isActive: boolean;
/**
* Set a new active heading.
*
* @param heading The new active heading
* @param emitSignal Whether to emit the activeHeadingChanged signal or not.
*/
setActiveHeading(heading: H | null, emitSignal?: boolean): void;
/**
* Model configuration setter.
*
* @param c New configuration
*/
setConfiguration(c: Partial<IConfig>): void;
/**
* List of configuration options supported by the model.
*/
readonly supportedOptions: (keyof IConfig)[];
/**
* Document title
*/
title?: string;
/**
* Callback on heading collapse.
*
* @param options.heading The heading to change state (all headings if not provided)
* @param options.collapsed The new collapsed status (toggle existing status if not provided)
*/
toggleCollapse: (options: { heading?: H; collapsed?: boolean }) => void;
}
/**
* Generic table of contents type
*/
export type Model = IModel<IHeading>;
/**
* Interface describing table of contents widget options.
*/
export interface IOptions {
/**
* Table of contents model.
*/
model?: IModel<IHeading>;
/**
* If no headings are present, a headline to display as a placeholder
*/
placeholderHeadline: string;
/**
* If no headings are present, text to display as a placeholder
*/
placeholderText: string;
}
/**
* Interface describing a toolbar item list
*/
export interface IToolbarItems
extends IObservableList<ToolbarRegistry.IToolbarItem> {}
}