This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
/
workspace-center.js
343 lines (304 loc) · 11.1 KB
/
workspace-center.js
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
'use strict';
const TextEditor = require('./text-editor');
const PaneContainer = require('./pane-container');
// Essential: Represents the workspace at the center of the entire window.
module.exports = class WorkspaceCenter {
constructor(params) {
params.location = 'center';
this.paneContainer = new PaneContainer(params);
this.didActivate = params.didActivate;
this.paneContainer.onDidActivatePane(() => this.didActivate(this));
this.paneContainer.onDidChangeActivePane(pane => {
params.didChangeActivePane(this, pane);
});
this.paneContainer.onDidChangeActivePaneItem(item => {
params.didChangeActivePaneItem(this, item);
});
this.paneContainer.onDidDestroyPaneItem(item =>
params.didDestroyPaneItem(item)
);
}
destroy() {
this.paneContainer.destroy();
}
serialize() {
return this.paneContainer.serialize();
}
deserialize(state, deserializerManager) {
this.paneContainer.deserialize(state, deserializerManager);
}
activate() {
this.getActivePane().activate();
}
getLocation() {
return 'center';
}
setDraggingItem() {
// No-op
}
/*
Section: Event Subscription
*/
// Essential: Invoke the given callback with all current and future text
// editors in the workspace center.
//
// * `callback` {Function} to be called with current and future text editors.
// * `editor` An {TextEditor} that is present in {::getTextEditors} at the time
// of subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeTextEditors(callback) {
for (let textEditor of this.getTextEditors()) {
callback(textEditor);
}
return this.onDidAddTextEditor(({ textEditor }) => callback(textEditor));
}
// Essential: Invoke the given callback with all current and future panes items
// in the workspace center.
//
// * `callback` {Function} to be called with current and future pane items.
// * `item` An item that is present in {::getPaneItems} at the time of
// subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observePaneItems(callback) {
return this.paneContainer.observePaneItems(callback);
}
// Essential: Invoke the given callback when the active pane item changes.
//
// Because observers are invoked synchronously, it's important not to perform
// any expensive operations via this method. Consider
// {::onDidStopChangingActivePaneItem} to delay operations until after changes
// stop occurring.
//
// * `callback` {Function} to be called when the active pane item changes.
// * `item` The active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActivePaneItem(callback) {
return this.paneContainer.onDidChangeActivePaneItem(callback);
}
// Essential: Invoke the given callback when the active pane item stops
// changing.
//
// Observers are called asynchronously 100ms after the last active pane item
// change. Handling changes here rather than in the synchronous
// {::onDidChangeActivePaneItem} prevents unneeded work if the user is quickly
// changing or closing tabs and ensures critical UI feedback, like changing the
// highlighted tab, gets priority over work that can be done asynchronously.
//
// * `callback` {Function} to be called when the active pane item stopts
// changing.
// * `item` The active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidStopChangingActivePaneItem(callback) {
return this.paneContainer.onDidStopChangingActivePaneItem(callback);
}
// Essential: Invoke the given callback with the current active pane item and
// with all future active pane items in the workspace center.
//
// * `callback` {Function} to be called when the active pane item changes.
// * `item` The current active pane item.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActivePaneItem(callback) {
return this.paneContainer.observeActivePaneItem(callback);
}
// Extended: Invoke the given callback when a pane is added to the workspace
// center.
//
// * `callback` {Function} to be called panes are added.
// * `event` {Object} with the following keys:
// * `pane` The added pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPane(callback) {
return this.paneContainer.onDidAddPane(callback);
}
// Extended: Invoke the given callback before a pane is destroyed in the
// workspace center.
//
// * `callback` {Function} to be called before panes are destroyed.
// * `event` {Object} with the following keys:
// * `pane` The pane to be destroyed.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onWillDestroyPane(callback) {
return this.paneContainer.onWillDestroyPane(callback);
}
// Extended: Invoke the given callback when a pane is destroyed in the
// workspace center.
//
// * `callback` {Function} to be called panes are destroyed.
// * `event` {Object} with the following keys:
// * `pane` The destroyed pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidDestroyPane(callback) {
return this.paneContainer.onDidDestroyPane(callback);
}
// Extended: Invoke the given callback with all current and future panes in the
// workspace center.
//
// * `callback` {Function} to be called with current and future panes.
// * `pane` A {Pane} that is present in {::getPanes} at the time of
// subscription or that is added at some later time.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observePanes(callback) {
return this.paneContainer.observePanes(callback);
}
// Extended: Invoke the given callback when the active pane changes.
//
// * `callback` {Function} to be called when the active pane changes.
// * `pane` A {Pane} that is the current return value of {::getActivePane}.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActivePane(callback) {
return this.paneContainer.onDidChangeActivePane(callback);
}
// Extended: Invoke the given callback with the current active pane and when
// the active pane changes.
//
// * `callback` {Function} to be called with the current and future active#
// panes.
// * `pane` A {Pane} that is the current return value of {::getActivePane}.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
observeActivePane(callback) {
return this.paneContainer.observeActivePane(callback);
}
// Extended: Invoke the given callback when a pane item is added to the
// workspace center.
//
// * `callback` {Function} to be called when pane items are added.
// * `event` {Object} with the following keys:
// * `item` The added pane item.
// * `pane` {Pane} containing the added item.
// * `index` {Number} indicating the index of the added item in its pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPaneItem(callback) {
return this.paneContainer.onDidAddPaneItem(callback);
}
// Extended: Invoke the given callback when a pane item is about to be
// destroyed, before the user is prompted to save it.
//
// * `callback` {Function} to be called before pane items are destroyed.
// * `event` {Object} with the following keys:
// * `item` The item to be destroyed.
// * `pane` {Pane} containing the item to be destroyed.
// * `index` {Number} indicating the index of the item to be destroyed in
// its pane.
//
// Returns a {Disposable} on which `.dispose` can be called to unsubscribe.
onWillDestroyPaneItem(callback) {
return this.paneContainer.onWillDestroyPaneItem(callback);
}
// Extended: Invoke the given callback when a pane item is destroyed.
//
// * `callback` {Function} to be called when pane items are destroyed.
// * `event` {Object} with the following keys:
// * `item` The destroyed item.
// * `pane` {Pane} containing the destroyed item.
// * `index` {Number} indicating the index of the destroyed item in its
// pane.
//
// Returns a {Disposable} on which `.dispose` can be called to unsubscribe.
onDidDestroyPaneItem(callback) {
return this.paneContainer.onDidDestroyPaneItem(callback);
}
// Extended: Invoke the given callback when a text editor is added to the
// workspace center.
//
// * `callback` {Function} to be called when panes are added.
// * `event` {Object} with the following keys:
// * `textEditor` {TextEditor} that was added.
// * `pane` {Pane} containing the added text editor.
// * `index` {Number} indicating the index of the added text editor in its
// pane.
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddTextEditor(callback) {
return this.onDidAddPaneItem(({ item, pane, index }) => {
if (item instanceof TextEditor) {
callback({ textEditor: item, pane, index });
}
});
}
/*
Section: Pane Items
*/
// Essential: Get all pane items in the workspace center.
//
// Returns an {Array} of items.
getPaneItems() {
return this.paneContainer.getPaneItems();
}
// Essential: Get the active {Pane}'s active item.
//
// Returns an pane item {Object}.
getActivePaneItem() {
return this.paneContainer.getActivePaneItem();
}
// Essential: Get all text editors in the workspace center.
//
// Returns an {Array} of {TextEditor}s.
getTextEditors() {
return this.getPaneItems().filter(item => item instanceof TextEditor);
}
// Essential: Get the active item if it is an {TextEditor}.
//
// Returns an {TextEditor} or `undefined` if the current active item is not an
// {TextEditor}.
getActiveTextEditor() {
const activeItem = this.getActivePaneItem();
if (activeItem instanceof TextEditor) {
return activeItem;
}
}
// Save all pane items.
saveAll() {
this.paneContainer.saveAll();
}
confirmClose(options) {
return this.paneContainer.confirmClose(options);
}
/*
Section: Panes
*/
// Extended: Get all panes in the workspace center.
//
// Returns an {Array} of {Pane}s.
getPanes() {
return this.paneContainer.getPanes();
}
// Extended: Get the active {Pane}.
//
// Returns a {Pane}.
getActivePane() {
return this.paneContainer.getActivePane();
}
// Extended: Make the next pane active.
activateNextPane() {
return this.paneContainer.activateNextPane();
}
// Extended: Make the previous pane active.
activatePreviousPane() {
return this.paneContainer.activatePreviousPane();
}
paneForURI(uri) {
return this.paneContainer.paneForURI(uri);
}
paneForItem(item) {
return this.paneContainer.paneForItem(item);
}
// Destroy (close) the active pane.
destroyActivePane() {
const activePane = this.getActivePane();
if (activePane != null) {
activePane.destroy();
}
}
};