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
/
Copy pathmain.js
219 lines (151 loc) · 6.53 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
/* jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 2, maxerr: 50 */
/* global define, brackets, $, require, Mustache */
/*
HTML Skeleton
Created 2014 Triangle717
<http://Triangle717.WordPress.com/>
Licensed under The MIT License
<http://opensource.org/licenses/MIT/>
*/
/* ------- Begin Module Importing ------- */
define(function (require, exports, module) {
"use strict";
// Import the required Brackets modules
var AppInit = brackets.getModule("utils/AppInit"),
CommandManager = brackets.getModule("command/CommandManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
Document = brackets.getModule("document/Document"),
EditorManager = brackets.getModule("editor/EditorManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
ImageViewer = brackets.getModule("editor/ImageViewer"),
Menus = brackets.getModule("command/Menus"),
ProjectManager = brackets.getModule("project/ProjectManager"),
// Import dialog localization
Strings = require("strings"),
// Pull in the extension dialog
skellyDialogHtml = require("text!htmlContent/mainDialog.html"),
// Grab the logo to display in the dialog
skellyLogo = require.toUrl("img/HTML-Skeleton.svg"),
EXTENSION_ID = "le717.html-skeleton";
/* ------- End Module Importing ------- */
/* ------- Begin Available HTML Elements ------- */
// Assign a variable for 2 space indentation for easier coding
// FUTURE Replace with Sprint 37 preferences system
var twoSpaceIndent = "\u0020\u0020";
// Placeholder variables for image size
var $imgWidth = 0,
$imgHeight = 0;
var skellyBones = [
// Only the head and body tags + title and meta
'<!DOCTYPE html>\n<html lang="">\n<head>\n' + twoSpaceIndent +
'<meta charset="UTF-8">\n' + twoSpaceIndent + '<title></title>\n' +
'\n</head>\n\n<body>\n' + twoSpaceIndent + '\n</body>\n</html>\n',
// External stylesheet
'<link rel="stylesheet" href="">',
// Inline stylesheet
'<style></style>',
// External script
'<script src=""></script>',
// Inline script
'<script></script>',
// Full HTML skeleton
'<!DOCTYPE html>\n<html lang="">\n<head>\n' + twoSpaceIndent +
'<meta charset="UTF-8">\n' + twoSpaceIndent + '<title></title>\n' +
twoSpaceIndent + '<link rel="stylesheet" href="">' + '\n</head>\n\n<body>\n' +
twoSpaceIndent + '<script src=""></script>\n</body>\n</html>\n'
];
// Picture/Image
var imageCode = '<img src="" alt="" width="size-x" height="size-y" />';
/* ------- End Available HTML Elements ------- */
/* ------- Begin HTML Element Adding ------- */
function _insertAllTheCodes(finalElements) {
/* Inter the selected elements into the document */
// Get the last active editor because the dialog steals focus
var editor = EditorManager.getActiveEditor();
if (editor) {
// Get the cursor position
var cursor = editor.getCursorPos();
// Get the elements from the list in reverse so everything is added in the proper order
finalElements.reverse().forEach(function (value) {
// Wrap the actions in a `batchOperation` call, per guidelines
editor.document.batchOperation(function() {
// Insert the selected elements at the current cursor position
editor.document.replaceRange(value, cursor);
});
});
}
}
/* ------- End HTML Element Adding ------- */
/* ------- Begin HTML Element Choices ------- */
function _getOptions() {
/* Get element choices */
var imageCodeNew,
// Stores the elements to be added
finalElements = [],
// Store all the option IDs for easier access
optionIDs = ["#head-body", "#extern-style-tag", "#inline-style-tag",
"#extern-script-tag", "#inline-script-tag", "#full-skelly"
],
// Shortcuts to the image size input boxes
$imgWidthID = $("#img-width"),
$imgHeightID = $("#img-height");
// For each option that is checked, add the corresponding element
// to `finalElements` for addition in document
optionIDs.forEach(function (value, index) {
if ($(value + ":checked").val() === "on") {
finalElements.push(skellyBones[index]);
}
});
// The picture/image box is checked
if ($("#img-tag:checked").val() === "on") {
// The width box was filled out, use that value
if ($imgWidthID.val()) {
$imgWidth = $imgWidthID.val();
} else {
// The width box was empty, reset to 0
$imgWidth = 0;
}
// The height box was filled out, use that value
if ($imgHeightID.val()) {
$imgHeight = $imgHeightID.val();
} else {
// The height box was empty, reset to 0
$imgHeight = 0;
}
// Add the image tag to `finalElements` for addition in document,
// replacing the invalid values with valid ones
imageCodeNew = imageCode.replace(/size-x/, $imgWidth);
imageCodeNew = imageCodeNew.replace(/size-y/, $imgHeight);
finalElements.push(imageCodeNew);
}
// Finally, run process to add the selected elements
_insertAllTheCodes(finalElements);
}
/* ------- End HTML Element Choices ------- */
/* ------- Begin HTML Skeleton Dialog Box ------- */
function _showSkellyDialog() {
/* Display the HTML Skeleton box */
var localized = Mustache.render(skellyDialogHtml, Strings);
var skellyDialog = Dialogs.showModalDialogUsingTemplate(localized),
$doneButton = skellyDialog.getElement().find('.dialog-button[data-button-id="ok"]');
// Display logo using Bracket's viewer
ImageViewer.render(skellyLogo, $(".html-skeleton-image"));
// Hide image stats
$("#img-tip").remove();
$("#img-scale").remove();
// Upon closing the dialog, run function to gather and apply choices
$doneButton.on("click", _getOptions);
}
/* ------- End HTML Skeleton Dialog Box ------- */
/* ------- Begin Extension Initialization ------- */
AppInit.appReady(function () {
/* Load the extension after Brackets itself has finished loading */
// Load extension CSS
ExtensionUtils.loadStyleSheet(module, "css/style.css");
// Create a menu item in the Edit menu
CommandManager.register(Strings.INSERT_HTML_ELEMENTS, EXTENSION_ID, _showSkellyDialog);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuItem(EXTENSION_ID);
});
});
/* ------- End Extension Initialization ------- */