Skip to content

Commit

Permalink
Integrate omnibox search into manage page
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed May 27, 2024
1 parent 3879fc9 commit 55d0057
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion core
74 changes: 41 additions & 33 deletions extension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getPlatformOs() {
});
}

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

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!`;
const omnibox = new Omnibox({ defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() });
const omnibox = new Omnibox({ el, defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() });

let formatDoc = (index, doc) => {
let content = doc.href;
Expand Down Expand Up @@ -427,6 +427,40 @@ async function start() {

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

// Skip following code if `el` provide, which mean this function run in webpage.
if (el) return;

// DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
// https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
// Rustc:* action is exclusive to rustc docs event
case "rustc:check": {
sendResponse({
version: rustcSearcher.version,
});
break;
}
case "rustc:add": {
if (message.searchIndex) {
rustcSearcher.setSearchIndex(message.searchIndex);
rustcSearcher.setVersion(message.version);
sendResponse(true);
} else {
sendResponse(false);
}
break;
}
case "open-url": {
if (message.url) {
Omnibox.navigateToUrl(message.url);
}
break;
}
}
return true;
});

chrome.storage.onChanged.addListener(changes => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log('storage key updated:', key);
Expand Down Expand Up @@ -520,6 +554,10 @@ async function start() {
}
});

await checkAutoUpdate();
}

async function checkAutoUpdate() {
if (await settings.autoUpdate) {
let version = await storage.getItem('auto-update-version');
let now = new Date();
Expand All @@ -532,37 +570,6 @@ async function start() {
Omnibox.navigateToUrl(INDEX_UPDATE_URL);
await storage.setItem('auto-update-version', `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`);
}

// DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
// https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
// Rustc:* action is exclusive to rustc docs event
case "rustc:check": {
sendResponse({
version: rustcSearcher.version,
});
break;
}
case "rustc:add": {
if (message.searchIndex) {
rustcSearcher.setSearchIndex(message.searchIndex);
rustcSearcher.setVersion(message.version);
sendResponse(true);
} else {
sendResponse(false);
}
break;
}
case "open-url": {
if (message.url) {
Omnibox.navigateToUrl(message.url);
}
break;
}
}
return true;
});
}

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


export { start };
3 changes: 3 additions & 0 deletions extension/manage/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { STATS_PATTERNS } from "../../statistics.js";
import Statistics from "../../statistics.js";
import { start } from "../../main.js";

const TYPE_OTHER = "other";
const CHART_COLOR = "rgba(249, 188, 45, 0.5)";
Expand Down Expand Up @@ -306,4 +307,6 @@ async function renderYearList() {
const yearAgo = moment().startOf('day').subtract(1, 'year').valueOf();
await renderCharts(now, yearAgo);
await renderYearList();

await start("#omnibox");
})();
1 change: 1 addition & 0 deletions extension/search/docs/crate-doc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { c } from "../../core/index.js";
import DocSearch from "./base.js";
import CrateDocManager from "../../crate-manager.js";

// A DocSearch dedicated to a single crate based on the search-index.
class SingleCrateDocSearch extends DocSearch {
Expand Down
3 changes: 2 additions & 1 deletion manage/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
<script type="text/javascript" src="./js/d3.min.js"></script>
<script type="text/javascript" src="./js/moment.min.js"></script>
<script type="text/javascript" src="./js/charts.js"></script>

<link rel="stylesheet" href="../core/omnibox.css">
<link rel="stylesheet" href="./css/calendar-heatmap.css">
<link rel="stylesheet" href="./css/charts.css">
{% endblock head %}

{% block content %}
<div id="omnibox"></div>
<div style="flex-direction: column;" class="flex-layout">
<div class="chart-heatmap"></div>
</div>
Expand Down

0 comments on commit 55d0057

Please sign in to comment.