This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.js
336 lines (283 loc) · 12.9 KB
/
main.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 2, maxerr: 50 */
/*global $, define, brackets, Mustache */
/*
* HTML Skeleton
* Created 2014-2015 Triangle717
* <http://le717.github.io/>
*
* Licensed under The MIT License
* <http://opensource.org/licenses/MIT/>
*/
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
CommandManager = brackets.getModule("command/CommandManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
EditorManager = brackets.getModule("editor/EditorManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileUtils = brackets.getModule("file/FileUtils"),
ImageViewer = brackets.getModule("editor/ImageViewer"),
LanguageManager = brackets.getModule("language/LanguageManager"),
Menus = brackets.getModule("command/Menus"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
ImageFiles = LanguageManager.getLanguage("image")._fileExtensions.concat("svg"),
Strings = require("strings"),
SvgSize = require("src/SvgSize"),
IndentSize = require("src/IndentSize"),
skeletonLogo = require.toUrl("img/HTML-Skeleton.svg"),
toolbarButtonHTML = require("text!htmlContent/toolbarButton.html"),
skeletonDialogHTML = require("text!htmlContent/mainDialog.html");
var indentUnits = "",
localizedDialog = Mustache.render(skeletonDialogHTML, Strings),
localizedButton = Mustache.render(toolbarButtonHTML, Strings);
// HTML snippets roster
var skeletonBones = {
image : "<img alt='' width='size-x' height='size-y' src='src-url'>",
inStyle : "<style></style>",
inScript : "<script></script>",
extStyle : "<link rel='stylesheet' href=''>",
viewport : "<meta name='viewport' content='width=device-width, initial-scale=1.0'>",
extScript: "<script src=''></script>",
basiSkel : "<!DOCTYPE html>\n<html lang=''>\n<head>\nindent-size<meta charset='utf-8'>\n" +
"indent-size<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
"indent-size<title></title>\n</head>\n\n<body>\nindent-size\n</body>\n</html>\n",
fullSkel : "<!DOCTYPE html>\n<html lang=''>\n<head>\nindent-size<meta charset='utf-8'>\n" +
"indent-size<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
"indent-size<title></title>\nindent-size<link rel='stylesheet' href=''>\n" +
"</head>\n\n<body>\nindent-size<script src=''></script>\n</body>\n</html>\n"
};
// Get user's indentation settings
// If the user ever changes their preferences,
// we need to make sure we stay up-to-date
PreferencesManager.on("change", function () {
// Do NOT attempt to assign `indentUnits` directly to the function.
// It will completely break otherwise
var tempVar = IndentSize.getIndentation();
indentUnits = tempVar;
});
/**
* @private
* Insert the selected elements into the document.
* @param elements The elements to be inserted into the document.
* @returns {Boolean} Always true.
*/
function _insertSelectedElements(elements) {
// Get the document in the full editor
var editor = EditorManager.getCurrentFullEditor();
if (editor) {
// Get the elements from the list in reverse so everything is added in the proper order
var cursor = editor.getCursorPos();
elements.reverse().forEach(function (value) {
editor.document.batchOperation(function () {
// Do a regex search for the `indent-size` keyword
// and replace it with the user's indent settings
// Also replace all single quotes with double quotes
value = value.replace(/indent-size/g, indentUnits).replace(/'/g, "\"");
// Insert the selected elements at the current cursor position
editor.document.replaceRange(value, cursor);
});
});
}
return true;
}
/**
* @private
* Get skeleton choices.
*/
function _getSelectedElements() {
var imgWidthInput = document.querySelector(".html-skeleton-form .img-width"),
imgHeightInput = document.querySelector(".html-skeleton-form .img-height"),
selections = [],
optionIDs = ["#basic-skeleton", "#viewport", "#ext-style",
"#in-style", "#ext-script", "#in-script", "#full-skeleton"
];
// For each option that is checked, keep track of the corresponding element
optionIDs.forEach(function (value) {
if (document.querySelector(".html-skeleton-form " + value).checked) {
selections.push(skeletonBones[document.querySelector(".html-skeleton-form " + value).value]);
}
});
// The picture/image box was checked
if (document.querySelector(".html-skeleton-form #img").checked) {
// Get the width/height values from the input fields
var imgWidth = imgWidthInput.value,
imgHeight = imgHeightInput.value;
imgWidth = imgWidth !== "" ? imgWidth : 0;
imgHeight = imgHeight !== "" ? imgHeight : 0;
// Mark the image tag for addition in document,
// replacing the placeholder values with actual ones
var imgFilledIn = skeletonBones.image;
imgFilledIn = imgFilledIn.replace(/src-url/, document.querySelector(".html-skeleton-img .img-src").textContent);
imgFilledIn = imgFilledIn.replace(/size-x/, imgWidth);
imgFilledIn = imgFilledIn.replace(/size-y/, imgHeight);
selections.push(imgFilledIn);
}
// Finally, add the selected elements to the document
_insertSelectedElements(selections);
}
/**
* @private
* Create a usable, valid path to the user's selected image
* relative to document into which it being inserted.
* @param {String} image The full path to a user-selected image.
* @param {Boolean} [uiDisplay=false] If true, perform additional changes suitable for UI display.
* @return {String} A usable, valid path to the image.
*/
function _createImageURL(image, uiDisplay) {
if (uiDisplay === undefined) {
uiDisplay = false;
}
// Get the directory to the file the image is being inserted into
// and just the file name of the image
var curDir = EditorManager.getCurrentFullEditor().document.file.parentPath,
fileName = FileUtils.getBaseName(image);
// If this is a saved document and image and document are in the same folder
if (!DocumentManager.getCurrentDocument().isUntitled() &&
curDir.toLowerCase() === image.replace(fileName, "").toLowerCase()) {
// Use only the image file name
image = fileName;
}
// Try to make the path relative
image = ProjectManager.makeProjectRelativeIfPossible(image);
// If desired, if the path is longer than 50 characters split it up for better displaying
if (uiDisplay && image.length > 50) {
image = image.substring(0, 51) + "<br>" + image.substring(51, image.length);
}
return image;
}
/**
* @private
* Update image width/height input fields.
* @param imageWidth {String} The image width.
* @param imageHeight {String} The image height.
* @return {Boolean} true.
*/
function _updateSizeInput(imgWidth, imgHeight) {
document.querySelector(".html-skeleton-form .img-width").value = imgWidth;
document.querySelector(".html-skeleton-form .img-height").value = imgHeight;
return true;
}
/**
* @private
* Display the user selected image.
* @param imagePath {String} Absolute path to image file.
*/
function _displayImage(imagePath) {
var shortImagePath = "",
isSvgImage = false,
isSupported = false,
imgCheckBox = document.querySelector(".html-skeleton-form #img"),
$imgPreview = $(".html-skeleton-img .image-preview"),
imgErrorText = document.querySelector(".html-skeleton-img .img-error-text"),
$imgPathDisplay = $(".html-skeleton-img .img-src");
// Check if the image is supported and if it is an SVG image
isSupported = LanguageManager.getLanguageForPath(imagePath).getId() === "image";
isSvgImage = FileUtils.getFileExtension(imagePath) === "svg" ? true : false;
// The Image check box was not checked before now. Since the user has opened an image,
// let's assume the user wants to use it and check the box for them.
if (!imgCheckBox.checked) {
imgCheckBox.checked = true;
}
// Quickly remove the size constraints to get an accurate image size
$imgPreview.removeClass("html-skeleton-img-container");
// Trim the file path for nice displaying
shortImagePath = _createImageURL(imagePath, true);
// The image is an unsupported file type
if (!isSupported && !isSvgImage) {
// Update display for image and display extension logo
$(".html-skeleton-img").css("position", "relative");
$imgPreview.addClass("html-skeleton-img-container");
$imgPathDisplay.css("color", "red");
$imgPreview.removeClass("html-skeleton-img-shadow");
$imgPathDisplay.html(shortImagePath);
imgErrorText.innerHTML = "<br>is not supported for previewing!";
$imgPreview.attr("src", skeletonLogo);
_updateSizeInput("", "");
return false;
// The image is a supported file type
} else if (isSupported || isSvgImage) {
// Clear possible CSS applied from previewing an unsupported image
imgErrorText.innerHTML = "";
$imgPathDisplay.css("color", "");
// Position and add small shadow to container
$(".html-skeleton-img").css("position", "relative");
$imgPreview.addClass("html-skeleton-img-shadow");
// Show the file path and display the image
$imgPathDisplay.html(shortImagePath);
$imgPreview.attr("src", imagePath);
$imgPreview.addClass("html-skeleton-img-container");
}
// Get the image width and height
$imgPreview.one("load", function () {
if (isSupported && !isSvgImage) {
var imgWidth = $imgPreview.prop("naturalWidth"),
imgHeight = $imgPreview.prop("naturalHeight");
_updateSizeInput(imgWidth, imgHeight);
// Special routine for SVG graphics only
} else if (isSvgImage) {
SvgSize.get(imagePath).then(function (sizes) {
_updateSizeInput(sizes.width, sizes.height);
});
}
});
return;
}
/**
* @private
* Open the file browse dialog for the user to select an image.
* @param {Object} e DOM event.
*/
function _showFileDialog(e) {
FileSystem.showOpenDialog(false, false, Strings.FILE_DIALOG_TITLE,
"", ImageFiles, function (cancel, selected) {
if (!cancel && selected && selected.length > 0) {
_displayImage(selected[0]);
}
});
e.preventDefault();
e.stopPropagation();
}
/**
* Display HTML Skeleton dialog box
*/
function displaySkeletonDialog() {
var skeletonDialog = Dialogs.showModalDialogUsingTemplate(localizedDialog),
$dialog = skeletonDialog.getElement(),
$doneButton = $(".dialog-button[data-button-id='ok']", $dialog);
// Display logo (and any user images) using Brackets' ImageViewer
new ImageViewer.ImageView(FileSystem.getFileForPath(skeletonLogo), $(".html-skeleton-img"));
document.querySelector(".html-skeleton-img .image-preview").classList.add("html-skeleton-img-container");
// Hide image stats
var imageTip = document.querySelector(".html-skeleton-img .image-tip"),
imageScale = document.querySelector(".html-skeleton-img .image-scale");
imageTip.parentNode.removeChild(imageTip);
imageScale.parentNode.removeChild(imageScale);
// If the Browse button is clicked, proceed to open the browse dialog
$(".dialog-button[data-button-id='browse']", $dialog).on("click", function (e) {
_showFileDialog(e);
});
// Upon closing the dialog, run function to gather and apply choices
$doneButton.on("click", _getSelectedElements);
}
/**
* @private
* Load the extension after Brackets itself has finished loading
*/
AppInit.appReady(function () {
// Define the extension ID and CSS
var EXTENSION_ID = "le717.html-skeleton";
ExtensionUtils.loadStyleSheet(module, "css/style.css");
// Get the user's indentation settings
indentUnits = IndentSize.getIndentation();
// Create a menu item in the Edit menu
CommandManager.register(Strings.INSERT_HTML_ELEMENTS, EXTENSION_ID, displaySkeletonDialog);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuItem(EXTENSION_ID);
// Create toolbar icon
$(localizedButton).appendTo("#main-toolbar > .buttons")
.on("click", displaySkeletonDialog);
});
});