-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfactory.ts
106 lines (93 loc) · 2.83 KB
/
factory.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IWidgetTracker } from '@jupyterlab/apputils';
import { ActivityMonitor, PathExt } from '@jupyterlab/coreutils';
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { Widget } from '@lumino/widgets';
import { TableOfContentsModel } from './model';
import { TableOfContents } from './tokens';
/**
* Timeout for throttling ToC rendering following model changes.
*
* @private
*/
const RENDER_TIMEOUT = 1000;
/**
* Abstract table of contents model factory for IDocumentWidget.
*/
export abstract class TableOfContentsFactory<W extends IDocumentWidget>
implements TableOfContents.IFactory<W>
{
/**
* Constructor
*
* @param tracker Widget tracker
*/
constructor(protected tracker: IWidgetTracker<W>) {}
/**
* Whether the factory can handle the widget or not.
*
* @param widget - widget
* @returns boolean indicating a ToC can be generated
*/
isApplicable(widget: Widget): boolean {
if (!this.tracker.has(widget)) {
return false;
}
return true;
}
/**
* 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
): TableOfContentsModel<TableOfContents.IHeading, W> {
const model = this._createNew(widget, configuration);
const context = widget.context;
const updateHeadings = () => {
model.refresh().catch(reason => {
console.error('Failed to update the table of contents.', reason);
});
};
const monitor = new ActivityMonitor({
signal: context.model.contentChanged,
timeout: RENDER_TIMEOUT
});
monitor.activityStopped.connect(updateHeadings);
const updateTitle = () => {
model.title = PathExt.basename(context.localPath);
};
context.pathChanged.connect(updateTitle);
context.ready
.then(() => {
updateTitle();
updateHeadings();
})
.catch(reason => {
console.error(`Failed to initiate headings for ${context.localPath}.`);
});
widget.disposed.connect(() => {
monitor.activityStopped.disconnect(updateHeadings);
context.pathChanged.disconnect(updateTitle);
});
return model;
}
/**
* Abstract table of contents model instantiation to allow
* override by real implementation to customize it. The public
* `createNew` contains the signal connections standards for IDocumentWidget
* when the model has been instantiated.
*
* @param widget
* @param configuration
*/
protected abstract _createNew(
widget: W,
configuration?: TableOfContents.IConfig
): TableOfContentsModel<TableOfContents.IHeading, W>;
}