Skip to content

Commit 55d0057

Browse files
committed
Integrate omnibox search into manage page
1 parent 3879fc9 commit 55d0057

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

core

extension/main.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function getPlatformOs() {
3737
});
3838
}
3939

40-
async function start() {
40+
async function start(el) {
4141
// All dynamic setting items. Those items will been updated
4242
// in chrome.storage.onchange listener callback.
4343
let isOfflineMode = await settings.isOfflineMode;
@@ -110,7 +110,7 @@ async function start() {
110110
let rustcSearcher = new RustcSearch();
111111

112112
const defaultSuggestion = `Search std ${c.match("docs")}, external ${c.match("docs")} (~,@), ${c.match("crates")} (!), ${c.match("attributes")} (#), ${c.match("books")} (%), clippy ${c.match("lints")} (>), and ${c.match("error codes")}, etc in your address bar instantly!`;
113-
const omnibox = new Omnibox({ defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() });
113+
const omnibox = new Omnibox({ el, defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() });
114114

115115
let formatDoc = (index, doc) => {
116116
let content = doc.href;
@@ -427,6 +427,40 @@ async function start() {
427427

428428
omnibox.addNoCacheQueries("/", "!", "@", ":");
429429

430+
// Skip following code if `el` provide, which mean this function run in webpage.
431+
if (el) return;
432+
433+
// DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
434+
// https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
435+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
436+
switch (message.action) {
437+
// Rustc:* action is exclusive to rustc docs event
438+
case "rustc:check": {
439+
sendResponse({
440+
version: rustcSearcher.version,
441+
});
442+
break;
443+
}
444+
case "rustc:add": {
445+
if (message.searchIndex) {
446+
rustcSearcher.setSearchIndex(message.searchIndex);
447+
rustcSearcher.setVersion(message.version);
448+
sendResponse(true);
449+
} else {
450+
sendResponse(false);
451+
}
452+
break;
453+
}
454+
case "open-url": {
455+
if (message.url) {
456+
Omnibox.navigateToUrl(message.url);
457+
}
458+
break;
459+
}
460+
}
461+
return true;
462+
});
463+
430464
chrome.storage.onChanged.addListener(changes => {
431465
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
432466
console.log('storage key updated:', key);
@@ -520,6 +554,10 @@ async function start() {
520554
}
521555
});
522556

557+
await checkAutoUpdate();
558+
}
559+
560+
async function checkAutoUpdate() {
523561
if (await settings.autoUpdate) {
524562
let version = await storage.getItem('auto-update-version');
525563
let now = new Date();
@@ -532,37 +570,6 @@ async function start() {
532570
Omnibox.navigateToUrl(INDEX_UPDATE_URL);
533571
await storage.setItem('auto-update-version', `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`);
534572
}
535-
536-
// DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
537-
// https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
538-
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
539-
switch (message.action) {
540-
// Rustc:* action is exclusive to rustc docs event
541-
case "rustc:check": {
542-
sendResponse({
543-
version: rustcSearcher.version,
544-
});
545-
break;
546-
}
547-
case "rustc:add": {
548-
if (message.searchIndex) {
549-
rustcSearcher.setSearchIndex(message.searchIndex);
550-
rustcSearcher.setVersion(message.version);
551-
sendResponse(true);
552-
} else {
553-
sendResponse(false);
554-
}
555-
break;
556-
}
557-
case "open-url": {
558-
if (message.url) {
559-
Omnibox.navigateToUrl(message.url);
560-
}
561-
break;
562-
}
563-
}
564-
return true;
565-
});
566573
}
567574

568575
const chromeAction = chrome.action || chrome.browserAction;
@@ -578,4 +585,5 @@ chrome.runtime.onInstalled.addListener(async ({ previousVersion, reason }) => {
578585
}
579586
});
580587

588+
581589
export { start };

extension/manage/js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { STATS_PATTERNS } from "../../statistics.js";
22
import Statistics from "../../statistics.js";
3+
import { start } from "../../main.js";
34

45
const TYPE_OTHER = "other";
56
const CHART_COLOR = "rgba(249, 188, 45, 0.5)";
@@ -306,4 +307,6 @@ async function renderYearList() {
306307
const yearAgo = moment().startOf('day').subtract(1, 'year').valueOf();
307308
await renderCharts(now, yearAgo);
308309
await renderYearList();
310+
311+
await start("#omnibox");
309312
})();

extension/search/docs/crate-doc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { c } from "../../core/index.js";
22
import DocSearch from "./base.js";
3+
import CrateDocManager from "../../crate-manager.js";
34

45
// A DocSearch dedicated to a single crate based on the search-index.
56
class SingleCrateDocSearch extends DocSearch {

manage/templates/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
<script type="text/javascript" src="./js/d3.min.js"></script>
66
<script type="text/javascript" src="./js/moment.min.js"></script>
77
<script type="text/javascript" src="./js/charts.js"></script>
8-
8+
<link rel="stylesheet" href="../core/omnibox.css">
99
<link rel="stylesheet" href="./css/calendar-heatmap.css">
1010
<link rel="stylesheet" href="./css/charts.css">
1111
{% endblock head %}
1212

1313
{% block content %}
14+
<div id="omnibox"></div>
1415
<div style="flex-direction: column;" class="flex-layout">
1516
<div class="chart-heatmap"></div>
1617
</div>

0 commit comments

Comments
 (0)