forked from MiguelCastillo/Brackets-Themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemeManager.js
206 lines (163 loc) · 7.1 KB
/
themeManager.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
/*
* Copyright (c) 2013 Miguel Castillo.
*
* Licensed under MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, CodeMirror */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem;
var Theme = require("theme");
var PREFERENCES_KEY = "extensions.brackets-editorthemes";
var preferences = PreferencesManager.getPreferenceStorage(PREFERENCES_KEY);
var menu = Menus.addMenu("Themes", "editortheme", Menus.BEFORE, Menus.AppMenuBar.HELP_MENU);
var themeManager = {
_selected: preferences.getValue("theme") || ["default"],
_mode: "",
// Hash for themes loaded and ready to be used.
_themes: {}
};
/**
* Handles updating codemirror with the current selection of themes.
*/
themeManager.applyThemes = function () {
var editor = EditorManager.getActiveEditor();
if (!editor || !editor._codeMirror) {
return;
}
var cm = editor._codeMirror,
newThemes = themeManager._selected.join(" "),
currentThemes = cm.getOption("theme"),
mode = cm.getDoc().getMode().name;
// CodeMirror treats json as javascript, so we gotta do
// an extra check just to make we are not feeding json
// into jshint/jslint. Let's leave that to json linters
if ( cm.getDoc().getMode().jsonMode ) {
mode = "json";
}
// Add the document mode the the body so that we can actually
// style based on document type
$("body").removeClass("doctype-" + themeManager._mode).addClass("doctype-" + mode);
themeManager._mode = mode;
// Make sure the menu is up to date
syncSelection(true);
// Check if the editor already has the theme applied...
if (currentThemes === newThemes) {
var mainEditor = EditorManager.getCurrentFullEditor();
if (editor !== mainEditor) {
setTimeout(function(){
EditorManager.resizeEditor(EditorManager.REFRESH_FORCE);
}, 100);
}
return;
}
var themes = {}, styleDeferred = [];
// Setup current and further documents to get the new theme...
CodeMirror.defaults.theme = newThemes;
cm.setOption("theme", newThemes);
// If the css has not yet been loaded, then we load it so that
// styling is properly applied to codemirror
$.each(themeManager._selected.slice(0), function (index, item) {
var _theme = themeManager._themes[item];
themes[item] = _theme;
if (!_theme.css) {
var deferred = $.Deferred();
_theme.css = ExtensionUtils.addLinkedStyleSheet(_theme.path + "/" + _theme.fileName, deferred);
styleDeferred.push(deferred);
}
});
return $.when.apply($, styleDeferred).always(function() {
// Make sure we update the preferences when a new theme is selected.
preferences.setValue("theme", themeManager._selected);
$('body').removeClass(currentThemes.replace(' ', ',')).addClass(newThemes.replace(' ', ','));
cm.refresh();
$(ExtensionUtils).trigger("Themes.themeChanged", [themes]);
});
};
/**
* Return all the files in the specified path
*/
themeManager.loadFiles = function (path) {
var result = $.Deferred();
function handleError(error) {
result.reject(error);
}
// Load up the content of the directory
function loadDirectoryContent(fs) {
fs.root.createReader().readEntries(function success(entries) {
var i, files = [];
for (i = 0; i < entries.length; i++) {
if (entries[i].isFile) {
files.push(entries[i].name);
}
}
result.resolve({
files: files,
path: path
});
}, handleError);
}
// Get directory reader handle
NativeFileSystem.requestNativeFileSystem(path, loadDirectoryContent, handleError);
return result.done(loadThemes).promise();
};
/**
* Create theme objects and add them to the global themes container.
*/
function loadThemes(themes) {
//
// Iterate through each name in the themes and make them theme objects
//
$.each(themes.files, function (index, themeFile) {
var _theme = new Theme({fileName: themeFile, path: themes.path});
themeManager._themes[_theme.name] = _theme;
// Create the command id used by the menu
var COMMAND_ID = "theme." + _theme.name;
// Register menu event...
CommandManager.register(_theme.displayName, COMMAND_ID, function () {
syncSelection(false);
themeManager._selected = [_theme.name];
themeManager.applyThemes();
this.setChecked(true);
});
// Add theme menu item
menu.addMenuItem(COMMAND_ID);
});
if (themes.files.length !== 0) {
menu.addMenuDivider();
}
}
function syncSelection(val) {
$.each(themeManager._selected, function (index, item) {
var command = CommandManager.get("theme." + item);
if (command) {
command.setChecked(val);
}
});
}
return themeManager;
});