-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
123 lines (81 loc) · 4.4 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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, XMLHttpRequest */
/** Simple extension that adds a "File > Hello World" menu item */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
PanelManager = brackets.getModule("view/PanelManager"),
NativeApp = brackets.getModule("utils/NativeApp"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
EditorManager = brackets.getModule("editor/EditorManager");
ExtensionUtils.loadStyleSheet(module, "style.css");
var SEARCHIN_PANEL = "searchin-panel";
var searchPanel = $('<div id="'+SEARCHIN_PANEL+'"></div>');
var panel = PanelManager.createBottomPanel(SEARCHIN_PANEL, searchPanel);
function createListMDN(dom, html) {
var container = $(".search-results-container", dom).get(0);
if ($(".column-container", container).size() > 0) {
$(".column-container", container).each(function() {
// Link
var link = $(this).find("h4").html();
// Description
var description = $(this).find("p").html();
html += '<div class="searchin-item"><div class="searchin-item-title">'+link+'</div><div class="searchin-item-description">'+description+'</div></div>';
});
} else {
html += '<div class="searchin-noresult">No results</div>';
}
return html;
}
function createListWebPlatform(dom, html) {
var container = $(".mw-search-results", dom).first();
if ($(container).children("li").size() > 0) {
$(container).children("li").each(function() {
// Link
var a = $(this).find(".mw-search-result-heading a");
var link = '<a href="http://docs.webplatform.org'+$(a).attr('href')+'" title="'+$(a).attr('title')+'">'+$(a).html()+'</a>';
// Description
var description = $(this).find(".searchresult").html();
html += '<div class="searchin-item"><div class="searchin-item-title">'+link+'</div><div class="searchin-item-description">'+description+'</div></div>';
});
} else {
html += '<div class="searchin-noresult">No results</div>';
}
return html;
}
function search(url, title, createListCB) {
var editor = EditorManager.getCurrentFullEditor();
if (editor.hasSelection()) {
var text = editor.getSelectedText(),
xhr = new XMLHttpRequest();
url = url + text;
xhr.open('GET', url, false);
xhr.send(null);
var dom = $(xhr.responseText);
var html = '<div class="searchin-toolbar"><a href="#" class="searchin-close">×</a><div class="searchin-search">Search "'+text+'" on '+title+'</div>';
html += '<a href="'+url+'" class="searchin-more">More on '+title+'</a>';
html += '</div><div class="searchin-results">';
html = createListCB(dom, html);
html += "</div>";
panel.$panel.html(html);
$(".searchin-close", panel.$panel).click(function() { panel.hide(); });
panel.show();
}
}
function searchMDN() {
search("https://developer.mozilla.org/search?q=", "MDN", createListMDN);
}
function searchWebPlatform() {
search("http://docs.webplatform.org/w/index.php?search=", "WebPlatform", createListWebPlatform);
}
var menu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
menu.addMenuDivider(Menus.LAST, Menus.LAST);
var SEARCHIN_MDN = "searchin.MDN";
CommandManager.register("Search in MDN", SEARCHIN_MDN, searchMDN);
menu.addMenuItem(SEARCHIN_MDN);
var SEARCHIN_WEBPLATFORM = "searchin.WebPlatform";
CommandManager.register("Search in WebPlatform", SEARCHIN_WEBPLATFORM, searchWebPlatform);
menu.addMenuItem(SEARCHIN_WEBPLATFORM);
});