@theia/toolbar 1.45.1 → 1.46.0-next.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +32 -32
  2. package/lib/browser/abstract-toolbar-contribution.d.ts +16 -16
  3. package/lib/browser/abstract-toolbar-contribution.js +68 -68
  4. package/lib/browser/application-shell-with-toolbar-override.d.ts +15 -15
  5. package/lib/browser/application-shell-with-toolbar-override.js +101 -101
  6. package/lib/browser/codicons.d.ts +1 -1
  7. package/lib/browser/codicons.js +20 -20
  8. package/lib/browser/font-awesome-icons.d.ts +1 -1
  9. package/lib/browser/font-awesome-icons.js +20 -20
  10. package/lib/browser/package.spec.js +18 -18
  11. package/lib/browser/toolbar-command-contribution.d.ts +25 -25
  12. package/lib/browser/toolbar-command-contribution.js +211 -211
  13. package/lib/browser/toolbar-command-quick-input-service.d.ts +19 -19
  14. package/lib/browser/toolbar-command-quick-input-service.js +112 -112
  15. package/lib/browser/toolbar-constants.d.ts +23 -23
  16. package/lib/browser/toolbar-constants.js +75 -75
  17. package/lib/browser/toolbar-controller.d.ts +34 -34
  18. package/lib/browser/toolbar-controller.js +186 -186
  19. package/lib/browser/toolbar-defaults.d.ts +3 -3
  20. package/lib/browser/toolbar-defaults.js +60 -60
  21. package/lib/browser/toolbar-frontend-module.d.ts +4 -4
  22. package/lib/browser/toolbar-frontend-module.js +25 -25
  23. package/lib/browser/toolbar-icon-selector-dialog.d.ts +65 -65
  24. package/lib/browser/toolbar-icon-selector-dialog.js +235 -235
  25. package/lib/browser/toolbar-interfaces.d.ts +45 -45
  26. package/lib/browser/toolbar-interfaces.js +42 -42
  27. package/lib/browser/toolbar-preference-contribution.d.ts +9 -9
  28. package/lib/browser/toolbar-preference-contribution.js +34 -34
  29. package/lib/browser/toolbar-preference-schema.d.ts +5 -5
  30. package/lib/browser/toolbar-preference-schema.js +73 -73
  31. package/lib/browser/toolbar-storage-provider.d.ts +47 -47
  32. package/lib/browser/toolbar-storage-provider.js +357 -357
  33. package/lib/browser/toolbar.d.ts +56 -56
  34. package/lib/browser/toolbar.js +380 -380
  35. package/package.json +11 -11
  36. package/src/browser/abstract-toolbar-contribution.tsx +53 -53
  37. package/src/browser/application-shell-with-toolbar-override.ts +98 -98
  38. package/src/browser/codicons.ts +18 -18
  39. package/src/browser/font-awesome-icons.ts +18 -18
  40. package/src/browser/package.spec.ts +19 -19
  41. package/src/browser/style/toolbar.css +255 -255
  42. package/src/browser/toolbar-command-contribution.ts +211 -211
  43. package/src/browser/toolbar-command-quick-input-service.ts +86 -86
  44. package/src/browser/toolbar-constants.ts +79 -79
  45. package/src/browser/toolbar-controller.ts +185 -185
  46. package/src/browser/toolbar-defaults.ts +58 -58
  47. package/src/browser/toolbar-frontend-module.ts +30 -30
  48. package/src/browser/toolbar-icon-selector-dialog.tsx +296 -296
  49. package/src/browser/toolbar-interfaces.ts +76 -76
  50. package/src/browser/toolbar-preference-contribution.ts +38 -38
  51. package/src/browser/toolbar-preference-schema.ts +75 -75
  52. package/src/browser/toolbar-storage-provider.ts +352 -352
  53. package/src/browser/toolbar.tsx +424 -424
@@ -1,358 +1,358 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2022 Ericsson and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
- return c > 3 && r && Object.defineProperty(target, key, r), r;
22
- };
23
- var __metadata = (this && this.__metadata) || function (k, v) {
24
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.ToolbarStorageProvider = exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE = void 0;
28
- const jsoncParser = require("jsonc-parser");
29
- const core_1 = require("@theia/core");
30
- const inversify_1 = require("@theia/core/shared/inversify");
31
- const monaco_text_model_service_1 = require("@theia/monaco/lib/browser/monaco-text-model-service");
32
- const monaco_workspace_1 = require("@theia/monaco/lib/browser/monaco-workspace");
33
- const monaco = require("@theia/monaco-editor-core");
34
- const frontend_application_state_1 = require("@theia/core/lib/browser/frontend-application-state");
35
- const browser_1 = require("@theia/editor/lib/browser");
36
- const file_service_1 = require("@theia/filesystem/lib/browser/file-service");
37
- const promise_util_1 = require("@theia/core/lib/common/promise-util");
38
- const uri_1 = require("@theia/core/lib/common/uri");
39
- const toolbar_interfaces_1 = require("./toolbar-interfaces");
40
- const toolbar_constants_1 = require("./toolbar-constants");
41
- const toolbar_preference_schema_1 = require("./toolbar-preference-schema");
42
- exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE = 'There was an error reading your toolbar.json file. Please check if it is corrupt'
43
- + ' by right-clicking the toolbar and selecting "Customize Toolbar". You can also reset it to its defaults by selecting'
44
- + ' "Restore Toolbar Defaults"';
45
- let ToolbarStorageProvider = class ToolbarStorageProvider {
46
- constructor() {
47
- this._ready = new promise_util_1.Deferred();
48
- this.toDispose = new core_1.DisposableCollection();
49
- this.toolbarItemsUpdatedEmitter = new core_1.Emitter();
50
- this.onToolbarItemsChanged = this.toolbarItemsUpdatedEmitter.event;
51
- }
52
- get ready() {
53
- return this._ready.promise;
54
- }
55
- init() {
56
- this.doInit();
57
- }
58
- async doInit() {
59
- const reference = await this.textModelService.createModelReference(this.USER_TOOLBAR_URI);
60
- this.model = reference.object;
61
- this.toDispose.push(reference);
62
- this.toDispose.push(core_1.Disposable.create(() => this.model = undefined));
63
- this.readConfiguration();
64
- if (this.model) {
65
- this.toDispose.push(this.model.onDidChangeContent(() => this.readConfiguration()));
66
- this.toDispose.push(this.model.onDirtyChanged(() => this.readConfiguration()));
67
- this.toDispose.push(this.model.onDidChangeValid(() => this.readConfiguration()));
68
- }
69
- this.toDispose.push(this.toolbarItemsUpdatedEmitter);
70
- await this.appState.reachedState('ready');
71
- this.monacoWorkspace = this.lateInjector(monaco_workspace_1.MonacoWorkspace);
72
- this.editorManager = this.lateInjector(browser_1.EditorManager);
73
- this._ready.resolve();
74
- }
75
- readConfiguration() {
76
- if (!this.model || this.model.dirty) {
77
- return;
78
- }
79
- try {
80
- if (this.model.valid) {
81
- const content = this.model.getText();
82
- this.toolbarItems = this.parseContent(content);
83
- }
84
- else {
85
- this.toolbarItems = undefined;
86
- }
87
- this.toolbarItemsUpdatedEmitter.fire();
88
- }
89
- catch (e) {
90
- console.error(`Failed to load toolbar config from '${this.USER_TOOLBAR_URI}'.`, e);
91
- }
92
- }
93
- async removeItem(position) {
94
- if (this.toolbarItems) {
95
- const { alignment, groupIndex, itemIndex } = position;
96
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
97
- modifiedConfiguration.items[alignment][groupIndex].splice(itemIndex, 1);
98
- const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
99
- return this.writeToFile([], sanitizedConfiguration);
100
- }
101
- return false;
102
- }
103
- async addItem(command, alignment) {
104
- var _a, _b;
105
- if (this.toolbarItems) {
106
- const itemFromCommand = {
107
- id: command.id,
108
- command: command.id,
109
- icon: command.iconClass,
110
- };
111
- const groupIndex = (_a = this.toolbarItems) === null || _a === void 0 ? void 0 : _a.items[alignment].length;
112
- if (groupIndex) {
113
- const lastItemIndex = (_b = this.toolbarItems) === null || _b === void 0 ? void 0 : _b.items[alignment][groupIndex - 1].length;
114
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
115
- modifiedConfiguration.items[alignment][groupIndex - 1].push(itemFromCommand);
116
- return !!lastItemIndex && this.writeToFile([], modifiedConfiguration);
117
- }
118
- return this.addItemToEmptyColumn(itemFromCommand, alignment);
119
- }
120
- return false;
121
- }
122
- async swapValues(oldPosition, newPosition, direction) {
123
- var _a;
124
- if (this.toolbarItems) {
125
- const { alignment, groupIndex, itemIndex } = oldPosition;
126
- const draggedItem = (_a = this.toolbarItems) === null || _a === void 0 ? void 0 : _a.items[alignment][groupIndex][itemIndex];
127
- const newItemIndex = direction === 'location-right' ? newPosition.itemIndex + 1 : newPosition.itemIndex;
128
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
129
- if (newPosition.alignment === oldPosition.alignment && newPosition.groupIndex === oldPosition.groupIndex) {
130
- modifiedConfiguration.items[newPosition.alignment][newPosition.groupIndex].splice(newItemIndex, 0, draggedItem);
131
- if (newPosition.itemIndex > oldPosition.itemIndex) {
132
- modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex, 1);
133
- }
134
- else {
135
- modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex + 1, 1);
136
- }
137
- }
138
- else {
139
- modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex, 1);
140
- modifiedConfiguration.items[newPosition.alignment][newPosition.groupIndex].splice(newItemIndex, 0, draggedItem);
141
- }
142
- const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
143
- return this.writeToFile([], sanitizedConfiguration);
144
- }
145
- return false;
146
- }
147
- async addItemToEmptyColumn(item, alignment) {
148
- if (this.toolbarItems) {
149
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
150
- modifiedConfiguration.items[alignment].push([item]);
151
- return this.writeToFile([], modifiedConfiguration);
152
- }
153
- return false;
154
- }
155
- async moveItemToEmptySpace(oldPosition, newAlignment, centerPosition) {
156
- const { alignment: oldAlignment, itemIndex: oldItemIndex } = oldPosition;
157
- let oldGroupIndex = oldPosition.groupIndex;
158
- if (this.toolbarItems) {
159
- const draggedItem = this.toolbarItems.items[oldAlignment][oldGroupIndex][oldItemIndex];
160
- const newGroupIndex = this.toolbarItems.items[oldAlignment].length;
161
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
162
- if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.LEFT) {
163
- modifiedConfiguration.items[newAlignment].push([draggedItem]);
164
- }
165
- else if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.CENTER) {
166
- if (centerPosition === 'left') {
167
- modifiedConfiguration.items[newAlignment].unshift([draggedItem]);
168
- if (newAlignment === oldAlignment) {
169
- oldGroupIndex = oldGroupIndex + 1;
170
- }
171
- }
172
- else if (centerPosition === 'right') {
173
- modifiedConfiguration.items[newAlignment].splice(newGroupIndex + 1, 0, [draggedItem]);
174
- }
175
- }
176
- else if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.RIGHT) {
177
- modifiedConfiguration.items[newAlignment].unshift([draggedItem]);
178
- if (newAlignment === oldAlignment) {
179
- oldGroupIndex = oldGroupIndex + 1;
180
- }
181
- }
182
- modifiedConfiguration.items[oldAlignment][oldGroupIndex].splice(oldItemIndex, 1);
183
- const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
184
- return this.writeToFile([], sanitizedConfiguration);
185
- }
186
- return false;
187
- }
188
- async insertGroup(position, insertDirection) {
189
- if (this.toolbarItems) {
190
- const { alignment, groupIndex, itemIndex } = position;
191
- const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
192
- const originalColumn = modifiedConfiguration.items[alignment];
193
- if (originalColumn) {
194
- const existingGroup = originalColumn[groupIndex];
195
- const existingGroupLength = existingGroup.length;
196
- let poppedGroup = [];
197
- let numItemsToRemove;
198
- if (insertDirection === 'left' && itemIndex !== 0) {
199
- numItemsToRemove = existingGroupLength - itemIndex;
200
- poppedGroup = existingGroup.splice(itemIndex, numItemsToRemove);
201
- originalColumn.splice(groupIndex, 1, existingGroup, poppedGroup);
202
- }
203
- else if (insertDirection === 'right' && itemIndex !== existingGroupLength - 1) {
204
- numItemsToRemove = itemIndex + 1;
205
- poppedGroup = existingGroup.splice(0, numItemsToRemove);
206
- originalColumn.splice(groupIndex, 1, poppedGroup, existingGroup);
207
- }
208
- const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
209
- return this.writeToFile([], sanitizedConfiguration);
210
- }
211
- }
212
- return false;
213
- }
214
- removeEmptyGroupsFromToolbar(toolbarItems) {
215
- if (toolbarItems) {
216
- const modifiedConfiguration = (0, core_1.deepClone)(toolbarItems);
217
- const columns = [toolbar_interfaces_1.ToolbarAlignment.LEFT, toolbar_interfaces_1.ToolbarAlignment.CENTER, toolbar_interfaces_1.ToolbarAlignment.RIGHT];
218
- columns.forEach(column => {
219
- const groups = toolbarItems.items[column];
220
- groups.forEach((group, index) => {
221
- if (group.length === 0) {
222
- modifiedConfiguration.items[column].splice(index, 1);
223
- }
224
- });
225
- });
226
- return modifiedConfiguration;
227
- }
228
- return undefined;
229
- }
230
- async clearAll() {
231
- if (this.model) {
232
- const textModel = this.model.textEditorModel;
233
- await this.monacoWorkspace.applyBackgroundEdit(this.model, [
234
- {
235
- range: textModel.getFullModelRange(),
236
- // eslint-disable-next-line no-null/no-null
237
- text: null,
238
- forceMoveMarkers: false,
239
- },
240
- ]);
241
- }
242
- this.toolbarItemsUpdatedEmitter.fire();
243
- return true;
244
- }
245
- async writeToFile(path, value, insertion = false) {
246
- if (this.model) {
247
- try {
248
- const content = this.model.getText().trim();
249
- const textModel = this.model.textEditorModel;
250
- const editOperations = [];
251
- const { insertSpaces, tabSize, defaultEOL } = textModel.getOptions();
252
- for (const edit of jsoncParser.modify(content, path, value, {
253
- isArrayInsertion: insertion,
254
- formattingOptions: {
255
- insertSpaces,
256
- tabSize,
257
- eol: defaultEOL === monaco.editor.DefaultEndOfLine.LF ? '\n' : '\r\n',
258
- },
259
- })) {
260
- const start = textModel.getPositionAt(edit.offset);
261
- const end = textModel.getPositionAt(edit.offset + edit.length);
262
- editOperations.push({
263
- range: monaco.Range.fromPositions(start, end),
264
- // eslint-disable-next-line no-null/no-null
265
- text: edit.content || null,
266
- forceMoveMarkers: false,
267
- });
268
- }
269
- await this.monacoWorkspace.applyBackgroundEdit(this.model, editOperations, false);
270
- await this.model.save();
271
- return true;
272
- }
273
- catch (e) {
274
- const message = core_1.nls.localize('theia/toolbar/failedUpdate', "Failed to update the value of '{0}' in '{1}'.", path.join('.'), this.USER_TOOLBAR_URI.path.toString());
275
- this.messageService.error(core_1.nls.localize('theia/toolbar/jsonError', exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE));
276
- console.error(`${message}`, e);
277
- return false;
278
- }
279
- }
280
- return false;
281
- }
282
- parseContent(fileContent) {
283
- const rawConfig = this.parse(fileContent);
284
- if (!(0, toolbar_preference_schema_1.isToolbarPreferences)(rawConfig)) {
285
- return undefined;
286
- }
287
- return rawConfig;
288
- }
289
- parse(fileContent) {
290
- let strippedContent = fileContent.trim();
291
- if (!strippedContent) {
292
- return undefined;
293
- }
294
- strippedContent = jsoncParser.stripComments(strippedContent);
295
- return jsoncParser.parse(strippedContent);
296
- }
297
- async openOrCreateJSONFile(state, doOpen = false) {
298
- const fileExists = await this.fileService.exists(this.USER_TOOLBAR_URI);
299
- let doWriteStateToFile = false;
300
- if (fileExists) {
301
- const fileContent = await this.fileService.read(this.USER_TOOLBAR_URI);
302
- if (fileContent.value.trim() === '') {
303
- doWriteStateToFile = true;
304
- }
305
- }
306
- else {
307
- await this.fileService.create(this.USER_TOOLBAR_URI);
308
- doWriteStateToFile = true;
309
- }
310
- if (doWriteStateToFile) {
311
- await this.writeToFile([], state);
312
- }
313
- this.readConfiguration();
314
- if (doOpen) {
315
- const widget = await this.editorManager.open(this.USER_TOOLBAR_URI);
316
- return widget;
317
- }
318
- return undefined;
319
- }
320
- dispose() {
321
- this.toDispose.dispose();
322
- }
323
- };
324
- __decorate([
325
- (0, inversify_1.inject)(frontend_application_state_1.FrontendApplicationStateService),
326
- __metadata("design:type", frontend_application_state_1.FrontendApplicationStateService)
327
- ], ToolbarStorageProvider.prototype, "appState", void 0);
328
- __decorate([
329
- (0, inversify_1.inject)(monaco_text_model_service_1.MonacoTextModelService),
330
- __metadata("design:type", monaco_text_model_service_1.MonacoTextModelService)
331
- ], ToolbarStorageProvider.prototype, "textModelService", void 0);
332
- __decorate([
333
- (0, inversify_1.inject)(file_service_1.FileService),
334
- __metadata("design:type", file_service_1.FileService)
335
- ], ToolbarStorageProvider.prototype, "fileService", void 0);
336
- __decorate([
337
- (0, inversify_1.inject)(core_1.MessageService),
338
- __metadata("design:type", core_1.MessageService)
339
- ], ToolbarStorageProvider.prototype, "messageService", void 0);
340
- __decorate([
341
- (0, inversify_1.inject)(toolbar_interfaces_1.LateInjector),
342
- __metadata("design:type", Function)
343
- ], ToolbarStorageProvider.prototype, "lateInjector", void 0);
344
- __decorate([
345
- (0, inversify_1.inject)(toolbar_constants_1.UserToolbarURI),
346
- __metadata("design:type", uri_1.default)
347
- ], ToolbarStorageProvider.prototype, "USER_TOOLBAR_URI", void 0);
348
- __decorate([
349
- (0, inversify_1.postConstruct)(),
350
- __metadata("design:type", Function),
351
- __metadata("design:paramtypes", []),
352
- __metadata("design:returntype", void 0)
353
- ], ToolbarStorageProvider.prototype, "init", null);
354
- ToolbarStorageProvider = __decorate([
355
- (0, inversify_1.injectable)()
356
- ], ToolbarStorageProvider);
357
- exports.ToolbarStorageProvider = ToolbarStorageProvider;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2022 Ericsson and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.ToolbarStorageProvider = exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE = void 0;
28
+ const jsoncParser = require("jsonc-parser");
29
+ const core_1 = require("@theia/core");
30
+ const inversify_1 = require("@theia/core/shared/inversify");
31
+ const monaco_text_model_service_1 = require("@theia/monaco/lib/browser/monaco-text-model-service");
32
+ const monaco_workspace_1 = require("@theia/monaco/lib/browser/monaco-workspace");
33
+ const monaco = require("@theia/monaco-editor-core");
34
+ const frontend_application_state_1 = require("@theia/core/lib/browser/frontend-application-state");
35
+ const browser_1 = require("@theia/editor/lib/browser");
36
+ const file_service_1 = require("@theia/filesystem/lib/browser/file-service");
37
+ const promise_util_1 = require("@theia/core/lib/common/promise-util");
38
+ const uri_1 = require("@theia/core/lib/common/uri");
39
+ const toolbar_interfaces_1 = require("./toolbar-interfaces");
40
+ const toolbar_constants_1 = require("./toolbar-constants");
41
+ const toolbar_preference_schema_1 = require("./toolbar-preference-schema");
42
+ exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE = 'There was an error reading your toolbar.json file. Please check if it is corrupt'
43
+ + ' by right-clicking the toolbar and selecting "Customize Toolbar". You can also reset it to its defaults by selecting'
44
+ + ' "Restore Toolbar Defaults"';
45
+ let ToolbarStorageProvider = class ToolbarStorageProvider {
46
+ constructor() {
47
+ this._ready = new promise_util_1.Deferred();
48
+ this.toDispose = new core_1.DisposableCollection();
49
+ this.toolbarItemsUpdatedEmitter = new core_1.Emitter();
50
+ this.onToolbarItemsChanged = this.toolbarItemsUpdatedEmitter.event;
51
+ }
52
+ get ready() {
53
+ return this._ready.promise;
54
+ }
55
+ init() {
56
+ this.doInit();
57
+ }
58
+ async doInit() {
59
+ const reference = await this.textModelService.createModelReference(this.USER_TOOLBAR_URI);
60
+ this.model = reference.object;
61
+ this.toDispose.push(reference);
62
+ this.toDispose.push(core_1.Disposable.create(() => this.model = undefined));
63
+ this.readConfiguration();
64
+ if (this.model) {
65
+ this.toDispose.push(this.model.onDidChangeContent(() => this.readConfiguration()));
66
+ this.toDispose.push(this.model.onDirtyChanged(() => this.readConfiguration()));
67
+ this.toDispose.push(this.model.onDidChangeValid(() => this.readConfiguration()));
68
+ }
69
+ this.toDispose.push(this.toolbarItemsUpdatedEmitter);
70
+ await this.appState.reachedState('ready');
71
+ this.monacoWorkspace = this.lateInjector(monaco_workspace_1.MonacoWorkspace);
72
+ this.editorManager = this.lateInjector(browser_1.EditorManager);
73
+ this._ready.resolve();
74
+ }
75
+ readConfiguration() {
76
+ if (!this.model || this.model.dirty) {
77
+ return;
78
+ }
79
+ try {
80
+ if (this.model.valid) {
81
+ const content = this.model.getText();
82
+ this.toolbarItems = this.parseContent(content);
83
+ }
84
+ else {
85
+ this.toolbarItems = undefined;
86
+ }
87
+ this.toolbarItemsUpdatedEmitter.fire();
88
+ }
89
+ catch (e) {
90
+ console.error(`Failed to load toolbar config from '${this.USER_TOOLBAR_URI}'.`, e);
91
+ }
92
+ }
93
+ async removeItem(position) {
94
+ if (this.toolbarItems) {
95
+ const { alignment, groupIndex, itemIndex } = position;
96
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
97
+ modifiedConfiguration.items[alignment][groupIndex].splice(itemIndex, 1);
98
+ const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
99
+ return this.writeToFile([], sanitizedConfiguration);
100
+ }
101
+ return false;
102
+ }
103
+ async addItem(command, alignment) {
104
+ var _a, _b;
105
+ if (this.toolbarItems) {
106
+ const itemFromCommand = {
107
+ id: command.id,
108
+ command: command.id,
109
+ icon: command.iconClass,
110
+ };
111
+ const groupIndex = (_a = this.toolbarItems) === null || _a === void 0 ? void 0 : _a.items[alignment].length;
112
+ if (groupIndex) {
113
+ const lastItemIndex = (_b = this.toolbarItems) === null || _b === void 0 ? void 0 : _b.items[alignment][groupIndex - 1].length;
114
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
115
+ modifiedConfiguration.items[alignment][groupIndex - 1].push(itemFromCommand);
116
+ return !!lastItemIndex && this.writeToFile([], modifiedConfiguration);
117
+ }
118
+ return this.addItemToEmptyColumn(itemFromCommand, alignment);
119
+ }
120
+ return false;
121
+ }
122
+ async swapValues(oldPosition, newPosition, direction) {
123
+ var _a;
124
+ if (this.toolbarItems) {
125
+ const { alignment, groupIndex, itemIndex } = oldPosition;
126
+ const draggedItem = (_a = this.toolbarItems) === null || _a === void 0 ? void 0 : _a.items[alignment][groupIndex][itemIndex];
127
+ const newItemIndex = direction === 'location-right' ? newPosition.itemIndex + 1 : newPosition.itemIndex;
128
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
129
+ if (newPosition.alignment === oldPosition.alignment && newPosition.groupIndex === oldPosition.groupIndex) {
130
+ modifiedConfiguration.items[newPosition.alignment][newPosition.groupIndex].splice(newItemIndex, 0, draggedItem);
131
+ if (newPosition.itemIndex > oldPosition.itemIndex) {
132
+ modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex, 1);
133
+ }
134
+ else {
135
+ modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex + 1, 1);
136
+ }
137
+ }
138
+ else {
139
+ modifiedConfiguration.items[oldPosition.alignment][oldPosition.groupIndex].splice(oldPosition.itemIndex, 1);
140
+ modifiedConfiguration.items[newPosition.alignment][newPosition.groupIndex].splice(newItemIndex, 0, draggedItem);
141
+ }
142
+ const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
143
+ return this.writeToFile([], sanitizedConfiguration);
144
+ }
145
+ return false;
146
+ }
147
+ async addItemToEmptyColumn(item, alignment) {
148
+ if (this.toolbarItems) {
149
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
150
+ modifiedConfiguration.items[alignment].push([item]);
151
+ return this.writeToFile([], modifiedConfiguration);
152
+ }
153
+ return false;
154
+ }
155
+ async moveItemToEmptySpace(oldPosition, newAlignment, centerPosition) {
156
+ const { alignment: oldAlignment, itemIndex: oldItemIndex } = oldPosition;
157
+ let oldGroupIndex = oldPosition.groupIndex;
158
+ if (this.toolbarItems) {
159
+ const draggedItem = this.toolbarItems.items[oldAlignment][oldGroupIndex][oldItemIndex];
160
+ const newGroupIndex = this.toolbarItems.items[oldAlignment].length;
161
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
162
+ if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.LEFT) {
163
+ modifiedConfiguration.items[newAlignment].push([draggedItem]);
164
+ }
165
+ else if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.CENTER) {
166
+ if (centerPosition === 'left') {
167
+ modifiedConfiguration.items[newAlignment].unshift([draggedItem]);
168
+ if (newAlignment === oldAlignment) {
169
+ oldGroupIndex = oldGroupIndex + 1;
170
+ }
171
+ }
172
+ else if (centerPosition === 'right') {
173
+ modifiedConfiguration.items[newAlignment].splice(newGroupIndex + 1, 0, [draggedItem]);
174
+ }
175
+ }
176
+ else if (newAlignment === toolbar_interfaces_1.ToolbarAlignment.RIGHT) {
177
+ modifiedConfiguration.items[newAlignment].unshift([draggedItem]);
178
+ if (newAlignment === oldAlignment) {
179
+ oldGroupIndex = oldGroupIndex + 1;
180
+ }
181
+ }
182
+ modifiedConfiguration.items[oldAlignment][oldGroupIndex].splice(oldItemIndex, 1);
183
+ const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
184
+ return this.writeToFile([], sanitizedConfiguration);
185
+ }
186
+ return false;
187
+ }
188
+ async insertGroup(position, insertDirection) {
189
+ if (this.toolbarItems) {
190
+ const { alignment, groupIndex, itemIndex } = position;
191
+ const modifiedConfiguration = (0, core_1.deepClone)(this.toolbarItems);
192
+ const originalColumn = modifiedConfiguration.items[alignment];
193
+ if (originalColumn) {
194
+ const existingGroup = originalColumn[groupIndex];
195
+ const existingGroupLength = existingGroup.length;
196
+ let poppedGroup = [];
197
+ let numItemsToRemove;
198
+ if (insertDirection === 'left' && itemIndex !== 0) {
199
+ numItemsToRemove = existingGroupLength - itemIndex;
200
+ poppedGroup = existingGroup.splice(itemIndex, numItemsToRemove);
201
+ originalColumn.splice(groupIndex, 1, existingGroup, poppedGroup);
202
+ }
203
+ else if (insertDirection === 'right' && itemIndex !== existingGroupLength - 1) {
204
+ numItemsToRemove = itemIndex + 1;
205
+ poppedGroup = existingGroup.splice(0, numItemsToRemove);
206
+ originalColumn.splice(groupIndex, 1, poppedGroup, existingGroup);
207
+ }
208
+ const sanitizedConfiguration = this.removeEmptyGroupsFromToolbar(modifiedConfiguration);
209
+ return this.writeToFile([], sanitizedConfiguration);
210
+ }
211
+ }
212
+ return false;
213
+ }
214
+ removeEmptyGroupsFromToolbar(toolbarItems) {
215
+ if (toolbarItems) {
216
+ const modifiedConfiguration = (0, core_1.deepClone)(toolbarItems);
217
+ const columns = [toolbar_interfaces_1.ToolbarAlignment.LEFT, toolbar_interfaces_1.ToolbarAlignment.CENTER, toolbar_interfaces_1.ToolbarAlignment.RIGHT];
218
+ columns.forEach(column => {
219
+ const groups = toolbarItems.items[column];
220
+ groups.forEach((group, index) => {
221
+ if (group.length === 0) {
222
+ modifiedConfiguration.items[column].splice(index, 1);
223
+ }
224
+ });
225
+ });
226
+ return modifiedConfiguration;
227
+ }
228
+ return undefined;
229
+ }
230
+ async clearAll() {
231
+ if (this.model) {
232
+ const textModel = this.model.textEditorModel;
233
+ await this.monacoWorkspace.applyBackgroundEdit(this.model, [
234
+ {
235
+ range: textModel.getFullModelRange(),
236
+ // eslint-disable-next-line no-null/no-null
237
+ text: null,
238
+ forceMoveMarkers: false,
239
+ },
240
+ ]);
241
+ }
242
+ this.toolbarItemsUpdatedEmitter.fire();
243
+ return true;
244
+ }
245
+ async writeToFile(path, value, insertion = false) {
246
+ if (this.model) {
247
+ try {
248
+ const content = this.model.getText().trim();
249
+ const textModel = this.model.textEditorModel;
250
+ const editOperations = [];
251
+ const { insertSpaces, tabSize, defaultEOL } = textModel.getOptions();
252
+ for (const edit of jsoncParser.modify(content, path, value, {
253
+ isArrayInsertion: insertion,
254
+ formattingOptions: {
255
+ insertSpaces,
256
+ tabSize,
257
+ eol: defaultEOL === monaco.editor.DefaultEndOfLine.LF ? '\n' : '\r\n',
258
+ },
259
+ })) {
260
+ const start = textModel.getPositionAt(edit.offset);
261
+ const end = textModel.getPositionAt(edit.offset + edit.length);
262
+ editOperations.push({
263
+ range: monaco.Range.fromPositions(start, end),
264
+ // eslint-disable-next-line no-null/no-null
265
+ text: edit.content || null,
266
+ forceMoveMarkers: false,
267
+ });
268
+ }
269
+ await this.monacoWorkspace.applyBackgroundEdit(this.model, editOperations, false);
270
+ await this.model.save();
271
+ return true;
272
+ }
273
+ catch (e) {
274
+ const message = core_1.nls.localize('theia/toolbar/failedUpdate', "Failed to update the value of '{0}' in '{1}'.", path.join('.'), this.USER_TOOLBAR_URI.path.toString());
275
+ this.messageService.error(core_1.nls.localize('theia/toolbar/jsonError', exports.TOOLBAR_BAD_JSON_ERROR_MESSAGE));
276
+ console.error(`${message}`, e);
277
+ return false;
278
+ }
279
+ }
280
+ return false;
281
+ }
282
+ parseContent(fileContent) {
283
+ const rawConfig = this.parse(fileContent);
284
+ if (!(0, toolbar_preference_schema_1.isToolbarPreferences)(rawConfig)) {
285
+ return undefined;
286
+ }
287
+ return rawConfig;
288
+ }
289
+ parse(fileContent) {
290
+ let strippedContent = fileContent.trim();
291
+ if (!strippedContent) {
292
+ return undefined;
293
+ }
294
+ strippedContent = jsoncParser.stripComments(strippedContent);
295
+ return jsoncParser.parse(strippedContent);
296
+ }
297
+ async openOrCreateJSONFile(state, doOpen = false) {
298
+ const fileExists = await this.fileService.exists(this.USER_TOOLBAR_URI);
299
+ let doWriteStateToFile = false;
300
+ if (fileExists) {
301
+ const fileContent = await this.fileService.read(this.USER_TOOLBAR_URI);
302
+ if (fileContent.value.trim() === '') {
303
+ doWriteStateToFile = true;
304
+ }
305
+ }
306
+ else {
307
+ await this.fileService.create(this.USER_TOOLBAR_URI);
308
+ doWriteStateToFile = true;
309
+ }
310
+ if (doWriteStateToFile) {
311
+ await this.writeToFile([], state);
312
+ }
313
+ this.readConfiguration();
314
+ if (doOpen) {
315
+ const widget = await this.editorManager.open(this.USER_TOOLBAR_URI);
316
+ return widget;
317
+ }
318
+ return undefined;
319
+ }
320
+ dispose() {
321
+ this.toDispose.dispose();
322
+ }
323
+ };
324
+ __decorate([
325
+ (0, inversify_1.inject)(frontend_application_state_1.FrontendApplicationStateService),
326
+ __metadata("design:type", frontend_application_state_1.FrontendApplicationStateService)
327
+ ], ToolbarStorageProvider.prototype, "appState", void 0);
328
+ __decorate([
329
+ (0, inversify_1.inject)(monaco_text_model_service_1.MonacoTextModelService),
330
+ __metadata("design:type", monaco_text_model_service_1.MonacoTextModelService)
331
+ ], ToolbarStorageProvider.prototype, "textModelService", void 0);
332
+ __decorate([
333
+ (0, inversify_1.inject)(file_service_1.FileService),
334
+ __metadata("design:type", file_service_1.FileService)
335
+ ], ToolbarStorageProvider.prototype, "fileService", void 0);
336
+ __decorate([
337
+ (0, inversify_1.inject)(core_1.MessageService),
338
+ __metadata("design:type", core_1.MessageService)
339
+ ], ToolbarStorageProvider.prototype, "messageService", void 0);
340
+ __decorate([
341
+ (0, inversify_1.inject)(toolbar_interfaces_1.LateInjector),
342
+ __metadata("design:type", Function)
343
+ ], ToolbarStorageProvider.prototype, "lateInjector", void 0);
344
+ __decorate([
345
+ (0, inversify_1.inject)(toolbar_constants_1.UserToolbarURI),
346
+ __metadata("design:type", uri_1.default)
347
+ ], ToolbarStorageProvider.prototype, "USER_TOOLBAR_URI", void 0);
348
+ __decorate([
349
+ (0, inversify_1.postConstruct)(),
350
+ __metadata("design:type", Function),
351
+ __metadata("design:paramtypes", []),
352
+ __metadata("design:returntype", void 0)
353
+ ], ToolbarStorageProvider.prototype, "init", null);
354
+ ToolbarStorageProvider = __decorate([
355
+ (0, inversify_1.injectable)()
356
+ ], ToolbarStorageProvider);
357
+ exports.ToolbarStorageProvider = ToolbarStorageProvider;
358
358
  //# sourceMappingURL=toolbar-storage-provider.js.map