forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
171 lines (146 loc) · 3.51 KB
/
widget.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Message
} from '@phosphor/messaging';
import {
Widget
} from '@phosphor/widgets';
import {
CodeEditor
} from './';
/**
* The class name added to an editor widget that has a primary selection.
*/
const HAS_SELECTION_CLASS = 'jp-mod-has-primary-selection';
/**
* A widget which hosts a code editor.
*/
export
class CodeEditorWrapper extends Widget {
/**
* Construct a new code editor widget.
*/
constructor(options: CodeEditorWrapper.IOptions) {
super();
const editor = this._editor = options.factory({
host: this.node,
model: options.model,
uuid: options.uuid,
wordWrap: options.wordWrap,
readOnly: options.readOnly,
selectionStyle: options.selectionStyle
});
editor.model.selections.changed.connect(this._onSelectionsChanged, this);
}
/**
* Get the editor wrapped by the widget.
*/
get editor(): CodeEditor.IEditor {
return this._editor;
}
/**
* Get the model used by the widget.
*/
get model(): CodeEditor.IModel {
return this._editor.model;
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
if (this.isDisposed) {
return;
}
super.dispose();
this._editor.dispose();
this._editor = null;
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this._editor.focus();
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
if (this.isVisible) {
this._editor.refresh();
}
}
/**
* A message handler invoked on an `'after-show'` message.
*/
protected onAfterShow(msg: Message): void {
this._editor.refresh();
}
/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(msg: Widget.ResizeMessage): void {
if (msg.width >= 0 && msg.height >= 0) {
this._editor.setSize(msg);
} else {
this._editor.resizeToFit();
}
}
/**
* Handle a change in model selections.
*/
private _onSelectionsChanged(): void {
const { start, end } = this._editor.getSelection();
if (start.column !== end.column || start.line !== end.line) {
this.addClass(HAS_SELECTION_CLASS);
} else {
this.removeClass(HAS_SELECTION_CLASS);
}
}
private _editor: CodeEditor.IEditor = null;
}
/**
* The namespace for the `CodeEditorWrapper` statics.
*/
export
namespace CodeEditorWrapper {
/**
* The options used to initialize a code editor widget.
*/
export
interface IOptions {
/**
* A code editor factory.
*
* #### Notes
* The widget needs a factory and a model instead of a `CodeEditor.IEditor`
* object because it needs to provide its own node as the host.
*/
factory: CodeEditor.Factory;
/**
* The model used to initialize the code editor.
*/
model: CodeEditor.IModel;
/**
* The desired uuid for the editor.
*/
uuid?: string;
/**
* Whether line numbers should be displayed. Defaults to `false`.
*/
lineNumbers?: boolean;
/**
* Set to false for horizontal scrolling. Defaults to `true`.
*/
wordWrap?: boolean;
/**
* Whether the editor is read-only. Defaults to `false`.
*/
readOnly?: boolean;
/**
* The default selection style for the editor.
*/
selectionStyle?: CodeEditor.ISelectionStyle;
}
}