This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·146 lines (129 loc) · 5.81 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
/* brackets-quicklib */
define(function (require, exports, module) {
"use strict";
var JSONfile = require('text!sources.json');
var Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
EditorManager = brackets.getModule("editor/EditorManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
FileUtils = brackets.getModule("file/FileUtils"),
Dialogs = brackets.getModule("widgets/Dialogs");
// get extension path, create dataObjectbject with all data from our JSON file
var sourcesObject = JSON.parse(JSONfile);
var libraryNames = Object.getOwnPropertyNames(sourcesObject);
var localPath = ExtensionUtils.getModulePath(module);
// enable/disable prepend a comment with scripts name
var isEnabled = true;
// enable/disable usage of minified version
var outputMinified = true;
// function that inserts our content/snippet into the document
function insertFunction(content) {
var editor = EditorManager.getCurrentFullEditor();
var insertionPos = editor.getCursorPos();
if (editor) {
editor.document.batchOperation(function() {
editor.document.replaceRange(content, insertionPos);
});
}
else {
console.error("EditorManager error (insertFunction)");
}
}
//construct the string to be put in the document
function prepareString(id) {
// object that stores all needed elements for content insertion
var dataObject = {
name : id,
comment : "<!-- " + id + " -->",
url : null,
css: null,
jscript: null,
outputStr: "",
sendString: function() {
if (isEnabled == true) {
this.outputStr += this.comment + "\n";
}
if (this.css != null ){
this.outputStr += "<link rel=\"stylesheet\" href=\"" + this.url + "" + this.css + "\">\n";
}
if (this.jscript != null ) {
this.outputStr += "<script src=\"" + this.url + "" + this.jscript + "\"></script>";
}
return this.outputStr;
}
};
var dataHolder = sourcesObject[id];
if (dataHolder.hasOwnProperty('url') ) {
dataObject.url = dataHolder['url'];
} // set url
if (dataHolder.hasOwnProperty('script') ) {
dataObject.jscript = dataHolder['script'];
} // set JS
if (dataHolder.hasOwnProperty('stylesheet') ) {
dataObject.css = dataHolder['stylesheet'];
} // set CSS
var result = dataObject.sendString();
console.log(result);
if ( outputMinified !== true ) {
result = result.replace(/.min/,"");
}
insertFunction(result);
}
// contruct extension 'quickLib' menu with the use of all data gathered from sources.json
function constructMenu() {
$.each(libraryNames, function(i, name) {
var cleanName = name.replace(/[.-\s]/g,"").toUpperCase();
var menuVariable = "MNU_" + cleanName.toUpperCase();
CommandManager.register(name, menuVariable, function(id) {
prepareString(name);
});
menu.addMenuItem(menuVariable);
});
}
//opens the JSON file with all the CDN data
function openJSON() {
CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN, {fullPath: localPath + "sources.json", paneId: "first-pane"});
}
// Menus -> quickLib : display the "About Extension" modal
function aboutModal() {
var displayAbout = "<img style=\"float: left; margin:11px 5px 0px 0px; padding:0;\" src=\"styles/images/brackets_icon.svg\" alt=\"logo\" width=\"20\" height=\"20\">";
displayAbout += "<h3 style=\"margin-bottom:-5px;\">quickLib</h3></span>\n<small>version: 1.0.4</small><br><br>\n";
displayAbout += "<span style=\"letter-spacing: 1px;\">Quick & simple last-version snippet insert for all resources on ";
displayAbout += "<a href=\"https://developers.google.com/speed/libraries/\">Google Hosted Libraries</a>.<hr>";
displayAbout += "<p>Ͽ Author: Kopitar Anže</p><p>Ͽ GitHub: <a href=\"https://github.com/kopitar/brackets-quicklib\" >https://github.com/kopitar/brackets-quicklib</a></p>";
displayAbout += "Ͽ Contact: [email protected]<br><hr>";
// show modal dialog with "About Extension" information
Dialogs.showModalDialog('a',"About Extension", displayAbout);
}
// extension main menu
Menus.addMenu('quickLib','quicklib.main');
var menu = Menus.getMenu('quicklib.main');
// Allow enable and disable adding comments to inserted content
var MNU_COMMENTS = CommandManager.register('Add Comment', "comments.quicklib", function() {
this.setChecked(!this.getChecked());
isEnabled = this.getChecked();
});
// switch betweem normal and minified version
var MNU_SCRIPT = CommandManager.register('Minified version', "scriptoutput.quicklib", function() {
this.setChecked(!this.getChecked());
outputMinified = this.getChecked();
console.log(outputMinified);
});
// menu option for opening sources.json
var MNU_JSON = "openjson.quicklib";
CommandManager.register("Open CDN source...", MNU_JSON, openJSON );
// About Extension menu item
var MNU_ABOUT = "about.quicklib";
CommandManager.register("About Extension", MNU_ABOUT, aboutModal );
// construct the menu for this extension, register all commands & show it
constructMenu();
menu.addMenuDivider();
// divider -> Enable/Disable comment prepending -> About Extension
menu.addMenuItem(MNU_COMMENTS);
MNU_COMMENTS.setChecked(isEnabled);
menu.addMenuItem(MNU_SCRIPT);
MNU_SCRIPT.setChecked(isEnabled);
menu.addMenuItem(MNU_JSON);
menu.addMenuItem(MNU_ABOUT);
});