-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathmain.js
More file actions
300 lines (262 loc) · 11.5 KB
/
Copy pathmain.js
File metadata and controls
300 lines (262 loc) · 11.5 KB
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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/* eslint-disable no-invalid-this */
/* global logger */
define(function (require, exports, module) {
const AppInit = require("utils/AppInit");
const CommandManager = require("command/CommandManager");
const Menus = require("command/Menus");
const Commands = require("command/Commands");
const WorkspaceManager = require("view/WorkspaceManager");
const Strings = require("strings");
const Mustache = require("thirdparty/mustache/mustache");
const Metrics = require("utils/Metrics");
const Driver = require("./driver");
const SnippetsList = require("./snippetsList");
const CodeHintIntegration = require("./codeHintIntegration");
const Helper = require("./helper");
const UIHelper = require("./UIHelper");
const SnippetsState = require("./snippetsState");
const SnippetCursorManager = require("./snippetCursorManager");
const Global = require("./global");
const snippetsPanelTpl = require("text!./htmlContent/snippets-panel.html");
// the html content of the panel will be stored in this variable
let $snippetsPanel;
const MY_COMMAND_ID = "custom_snippets";
const PANEL_ID = "customSnippets.panel";
const MENU_ITEM_NAME = Strings.CUSTOM_SNIPPETS_MENU_ITEM_NAME; // this name will appear as the menu item
const PANEL_MIN_SIZE = 340; // the minimum size more than which its height cannot be decreased
// this is to store the panel reference,
// as we only need to create this once. rest of the time we can just toggle the visibility of the panel
let customSnippetsPanel;
/**
* This function is called when the first time the custom snippets panel button is clicked
* this is responsible to create the custom snippets bottom panel and show that
* @private
*/
function _createPanel() {
customSnippetsPanel = WorkspaceManager.createBottomPanel(PANEL_ID, $snippetsPanel, PANEL_MIN_SIZE,
Strings.CUSTOM_SNIPPETS_PANEL_TITLE, {iconClass: "fa-solid fa-code"});
UIHelper.init(customSnippetsPanel);
customSnippetsPanel.show();
// also register the handlers
_registerHandlers();
$("#filter-snippets-input").val("");
UIHelper.initializeListViewToolbarTitle();
SnippetsList.showSnippetsList(); // to show the snippets list in the snippets panel
}
/**
* This function is responsible to toggle the visibility of the panel
* this is called every time (after the panel is created) to show/hide the panel
* @private
*/
function _togglePanelVisibility() {
if (customSnippetsPanel.isVisible()) {
customSnippetsPanel.hide();
CommandManager.get(MY_COMMAND_ID).setChecked(false);
} else {
customSnippetsPanel.show();
CommandManager.get(MY_COMMAND_ID).setChecked(true);
$("#filter-snippets-input").val("");
UIHelper.initializeListViewToolbarTitle();
SnippetsList.showSnippetsList(); // we just remake the snippets list UI to make sure it is always on point
}
}
/**
* This function is responsible to hide the panel
* this is called when user clicks on the 'cross' icon inside the panel itself and that is the reason,
* why we don't need to check whether the panel is visible or not
* @private
*/
function _hidePanel() {
customSnippetsPanel.hide();
CommandManager.get(MY_COMMAND_ID).setChecked(false);
}
/**
* This function is responsible to create the bottom panel, if not created
* if panel is already created, we just toggle its visibility
* this will be called when the custom snippets menu item is clicked from the menu bar
*/
function showCustomSnippetsPanel() {
// make sure that the panel is not created,
// if it is then we can just toggle its visibility
if (!customSnippetsPanel) {
_createPanel();
CommandManager.get(MY_COMMAND_ID).setChecked(true);
} else {
_togglePanelVisibility();
}
}
/**
* This function is responsible to add the Custom Snippets menu item to the menu bar
* @private
*/
function _addToMenu() {
const menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
menu.addMenuItem(MY_COMMAND_ID, "", Menus.BEFORE, Commands.FILE_EXTENSION_MANAGER);
}
/**
* This function is responsible to register all the required handlers
* @private
*/
function _registerHandlers() {
const $saveCustomSnippetBtn = $("#save-custom-snippet-btn");
const $cancelCustomSnippetBtn = $("#cancel-custom-snippet-btn");
const $abbrInput = $("#abbr-box");
const $descInput = $("#desc-box");
const $templateInput = $("#template-text-box");
const $fileExtnInput = $("#file-extn-box");
const $addSnippetBtn = $("#add-snippet-btn");
const $addNewSnippetBtn = $("#add-new-snippet-btn");
const $backToListMenuBtn = $("#back-to-list-menu-btn");
const $filterInput = $("#filter-snippets-input");
const $editAbbrInput = $("#edit-abbr-box");
const $editDescInput = $("#edit-desc-box");
const $editTemplateInput = $("#edit-template-text-box");
const $editFileExtnInput = $("#edit-file-extn-box");
const $saveEditSnippetBtn = $("#save-edit-snippet-btn");
const $cancelEditSnippetBtn = $("#cancel-edit-snippet-btn");
$addSnippetBtn.on("click", function () {
UIHelper.showAddSnippetMenu();
});
$addNewSnippetBtn.on("click", function () {
UIHelper.showAddSnippetMenu();
});
$backToListMenuBtn.on("click", function () {
UIHelper.showSnippetListMenu();
SnippetsList.showSnippetsList();
});
$saveCustomSnippetBtn.on("click", function () {
Driver.handleSaveBtnClick();
});
$cancelCustomSnippetBtn.on("click", function () {
UIHelper.showSnippetListMenu();
SnippetsList.showSnippetsList();
});
$abbrInput.on("input", Helper.toggleSaveButtonDisability);
$templateInput.on("input", Helper.toggleSaveButtonDisability);
$abbrInput.on("keydown", function (e) {
Helper.validateAbbrInput(e, this);
});
$abbrInput.on("paste", function (e) {
Helper.handleAbbrPaste(e, $(this));
});
$descInput.on("keydown", function (e) {
Helper.validateDescInput(e, this);
});
$descInput.on("paste", function (e) {
Helper.handleDescPaste(e, $(this));
});
$templateInput.on("keydown", function (e) {
Helper.handleTextareaTabKey(e, this);
});
$fileExtnInput.on("input", function () {
Helper.handleFileExtensionInput($(this));
});
$fileExtnInput.on("keypress", function (e) {
Helper.handleFileExtensionKeypress(e, this);
});
$fileExtnInput.on("paste", function (e) {
Helper.handleFileExtensionPaste(e, $(this));
});
$editAbbrInput.on("input", Helper.toggleEditSaveButtonDisability);
$editDescInput.on("input", Helper.toggleEditSaveButtonDisability);
$editTemplateInput.on("input", Helper.toggleEditSaveButtonDisability);
$editFileExtnInput.on("input", Helper.toggleEditSaveButtonDisability);
$editAbbrInput.on("keydown", function (e) {
Helper.validateAbbrInput(e, this);
});
$editAbbrInput.on("paste", function (e) {
Helper.handleAbbrPaste(e, $(this));
});
$editDescInput.on("keydown", function (e) {
Helper.validateDescInput(e, this);
});
$editDescInput.on("paste", function (e) {
Helper.handleDescPaste(e, $(this));
});
$editTemplateInput.on("keydown", function (e) {
Helper.handleTextareaTabKey(e, this);
});
$editFileExtnInput.on("input", function () {
Helper.handleFileExtensionInput($(this));
});
$editFileExtnInput.on("keypress", function (e) {
Helper.handleFileExtensionKeypress(e, this);
});
$editFileExtnInput.on("paste", function (e) {
Helper.handleFileExtensionPaste(e, $(this));
});
$saveEditSnippetBtn.on("click", function () {
Driver.handleEditSaveBtnClick();
});
$cancelEditSnippetBtn.on("click", function () {
Driver.handleCancelEditBtnClick();
});
// filter input event handler
$filterInput.on("keyup input", function (event) {
// if user presses 'esc' we clear the input field
if (event && event.key === "Escape") {
$(this).val("");
SnippetsList.showSnippetsList();
return;
}
SnippetsList.showSnippetsList();
});
}
// When the panel tab is closed externally (e.g. via the × button),
// update the menu checked state to stay in sync.
WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_HIDDEN, function (event, panelID) {
if (panelID === PANEL_ID && customSnippetsPanel) {
CommandManager.get(MY_COMMAND_ID).setChecked(false);
}
});
AppInit.appReady(function () {
CommandManager.register(MENU_ITEM_NAME, MY_COMMAND_ID, showCustomSnippetsPanel);
// Render template with localized strings
const renderedHtml = Mustache.render(snippetsPanelTpl, {Strings: Strings});
$snippetsPanel = $(renderedHtml);
_addToMenu();
CodeHintIntegration.init();
// load snippets from file storage
const _snippetsLoadedPromise = SnippetsState.loadSnippetsFromState()
.then(function () {
// track boot-time snippet count (only if user has snippets)
const snippetCount = Global.SnippetHintsList.length;
if (snippetCount > 0) {
const countRange = Metrics.getRangeName(snippetCount);
Metrics.countEvent(Metrics.EVENT_TYPE.EDITOR, "snipt", `boot.${countRange}`);
}
})
.catch(function (error) {
logger.reportError(error, "Custom Snippets: didn't load on app init");
});
SnippetCursorManager.registerHandlers();
// Expose modules for integration testing
if (brackets.test) {
brackets.test.CustomSnippetsGlobal = Global;
brackets.test.CustomSnippetsHelper = Helper;
brackets.test.CustomSnippetsCursorManager = SnippetCursorManager;
brackets.test.CustomSnippetsCodeHintHandler = CodeHintIntegration._CustomSnippetsHandler;
brackets.test.CustomSnippetsDriver = Driver;
brackets.test._customSnippetsLoadedPromise = _snippetsLoadedPromise;
}
});
});