-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
184 lines (158 loc) · 7.82 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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* 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 */
/** Extension that supports inline WebPlatform Docs */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Async = brackets.getModule("utils/Async"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
FileUtils = brackets.getModule("file/FileUtils"),
EditorManager = brackets.getModule("editor/EditorManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
Menus = brackets.getModule("command/Menus"),
CSSUtils = brackets.getModule("language/CSSUtils"),
NativeApp = brackets.getModule("utils/NativeApp");
var CSSPropDocCache = {};
var CSSPropsServerURL = "http://ec2-184-73-148-225.compute-1.amazonaws.com/css.json";
// Local modules
var InlineWPDViewer = require("InlineWPDViewer");
var KeyboardPrefs = JSON.parse(require("text!keyboard.json"));
function _addFontDeclaration(url) {
var attributes = {
type: "text/css",
rel: "stylesheet",
href: url
};
var $link = $("<link/>").attr(attributes);
$link.appendTo("head");
}
function _getRemoteCSSPropsHash() {
var result = new $.Deferred();
// TODO: Change when proxy server supports this
result.resolve("");
return result.promise();
}
function _getRemoteCSSPropDetails() {
$.get(CSSPropsServerURL, function (data) {
if (data.hasOwnProperty("HASH") && data.HASH !== CSSPropDocCache.HASH) {
CSSPropDocCache = data;
NativeFileSystem.requestNativeFileSystem(brackets.app.getApplicationSupportDirectory(), function (fs) {
fs.root.getDirectory("wpd_data", {create: true}, function (dirEntry) {
dirEntry.getFile("cssPropsCache", {create: true}, function (fileEntry) {
FileUtils.writeText(fileEntry, JSON.stringify(data)).done(function () {
}).fail(function (err) {
console.log("Error writing css property cache");
});
}, function (error) {
console.log(error);
});
}, function (error) {
console.log(error);
});
}, function (error) {
console.log(error);
});
}
});
}
function _initCSSPropDocCache() {
var appSupportDir = brackets.app.getApplicationSupportDirectory();
NativeFileSystem.requestNativeFileSystem(appSupportDir, function (fs) {
var folder = "wpd_data";
fs.root.getDirectory(folder, {create: true}, function (dirEntry) {
dirEntry.getFile("cssPropsCache", {create: false}, function (fileEntry) {
FileUtils.readAsText(fileEntry).done(function (text, modTime) {
CSSPropDocCache = JSON.parse(text);
_getRemoteCSSPropsHash().done(function (hash) {
if (hash !== CSSPropDocCache.HASH.toString()) {
_getRemoteCSSPropDetails();
}
});
}).fail(function (error) {
console.log("Problems occurred reading cssPropsCache from filesystem. Updating from server.");
_getRemoteCSSPropDetails();
});
}, function (error) {
console.log("cssPropsCache not found. Updating from server.");
_getRemoteCSSPropDetails();
});
}, function (error) {
console.log("Problems occurred reading wpd_data folder. Updating properties from server.");
_getRemoteCSSPropDetails();
});
}, function (error) {
console.log("Problems occurred accessing native filesystem. Updating properties from server.");
_getRemoteCSSPropDetails();
});
}
/**
* When the cursor is on a CSS property, look up the definition of the property on webplatform.org
* and view the results in an InlineWPDViewer.
*/
function handleShowCSSDocs() {
var editor = EditorManager.getFocusedEditor();
if (!editor) {
return;
}
// Only provide a WPD viewer when cursor is in CSS content
if (editor.getModeForSelection() !== "css") {
return;
}
// Only provide WPD viewer if the selection is within a single line
var sel = editor.getSelection(false);
if (sel.start.line !== sel.end.line) {
return;
}
var cssInfo = CSSUtils.getInfoAtPos(editor, editor.getSelection().start);
if (cssInfo && cssInfo.name) {
var cssPropName = cssInfo.name;
if (CSSPropDocCache && CSSPropDocCache.hasOwnProperty("PROPERTIES")) {
// TODO: Objects should be keyed off the name w/o the css/properties/ prefix
var cssPropDetails = CSSPropDocCache.PROPERTIES["css/properties/" + cssPropName];
if (cssPropDetails) {
var wpdViewer = new InlineWPDViewer(cssPropName, cssPropDetails);
wpdViewer.load(editor);
editor.addInlineWidget(editor.getCursorPos(), wpdViewer);
}
}
}
}
ExtensionUtils.loadStyleSheet(module, "style.css");
_addFontDeclaration('http://fonts.googleapis.com/css?family=Gudea');
_addFontDeclaration('http://fonts.googleapis.com/css?family=Bitter:700');
var SHOW_CSS_DOCS_CMD = "inlinewpd.showCSSDocs";
var showCSSDocsCmd = CommandManager.register("Show CSS Docs", SHOW_CSS_DOCS_CMD,
handleShowCSSDocs);
showCSSDocsCmd.setEnabled(true);
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuItem(SHOW_CSS_DOCS_CMD, KeyboardPrefs.showCSSDocs);
var editor_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
// Add the Commands as MenuItems of the Editor context menu
if (editor_cmenu) {
editor_cmenu.addMenuDivider();
editor_cmenu.addMenuItem(SHOW_CSS_DOCS_CMD);
}
_initCSSPropDocCache();
});