Fix dynamic offline mode and offline path

This commit is contained in:
Folyd 2024-06-22 23:22:46 -07:00
parent 3558333752
commit f2705bc5e0
4 changed files with 26 additions and 10 deletions

View file

@ -8,7 +8,12 @@ import {
REDIRECT_URL,
} from "./constants.js";
export default class RustSearchOmnibox {
export async function getBaseUrl() {
let isOfflineMode = await settings.isOfflineMode;
return isOfflineMode ? await settings.offlineDocPath : 'https://doc.rust-lang.org/';
}
export class RustSearchOmnibox {
static async run({
omnibox,
stdSearcher,
@ -278,12 +283,11 @@ export default class RustSearchOmnibox {
result.push(errorIndex);
}
let isOfflineMode = await settings.isOfflineMode;
let baseUrl = isOfflineMode ? await settings.offlineDocPath : 'https://doc.rust-lang.org/';
let baseUrl = await getBaseUrl();
return result.map(errorCode => {
return {
content: `${baseUrl}error_codes/${errorCode}.html`,
description: `Open error code <match>${errorCode}</match> on ${isOfflineMode ? 'offline mode' : 'https://doc.rust-lang.org/error_codes/error-index.html'}`,
description: `Open error code <match>${errorCode}</match> on error codes index`,
};
});
},

View file

@ -23,7 +23,7 @@ import {
INDEX_UPDATE_URL,
RUST_RELEASE_README_URL,
} from "./constants.js";
import RustSearchOmnibox from "./lib.js";
import { RustSearchOmnibox, getBaseUrl } from "./lib.js";
async function start(omnibox) {
@ -84,7 +84,7 @@ async function start(omnibox) {
);
let nightlySearcher = new DocSearch("std", await IndexManager.getStdNightlyIndex(), "https://doc.rust-lang.org/nightly/");
let stdSearcher = new DocSearch("std", await IndexManager.getStdStableIndex(), isOfflineMode ? offlineDocPath : "https://doc.rust-lang.org/");
let stdSearcher = new DocSearch("std", await IndexManager.getStdStableIndex(), await getBaseUrl());
RustSearchOmnibox.run({
omnibox,
@ -101,16 +101,18 @@ async function start(omnibox) {
if (!omnibox.extensionMode) return;
chrome.storage.onChanged.addListener(changes => {
chrome.storage.onChanged.addListener(async changes => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log('storage key updated:', key);
switch (key) {
case "offline-mode": {
isOfflineMode = newValue;
stdSearcher.setRootPath(await getBaseUrl());
break;
}
case "offline-path": {
offlineDocPath = newValue;
stdSearcher.setRootPath(await getBaseUrl());
break;
}
case "index-std-stable": {

View file

@ -41,12 +41,18 @@ document.addEventListener('DOMContentLoaded', async function () {
const offlineDocPath = document.querySelector('.offline-doc-path');
offlineDocPath.value = await settings.offlineDocPath;
offlineDocPath.onchange = async function (event) {
let path = event.target.value;
if (await getPlatformOs() === "win") {
// Replace all "/" to "\" for Windows.
settings.offlineDocPath = event.target.value.replaceAll("/", "\\");
} else {
settings.offlineDocPath = event.target.value;
path = event.target.value.replaceAll("/", "\\");
}
if (!path.startsWith("file://")) {
// Prepend file:// to allow browser open the file url
path = "file://" + path;
}
event.target.value = path;
settings.offlineDocPath = path;
};
let crateRegistry = document.querySelector("select[name='crate-registry']");

View file

@ -9,6 +9,10 @@ export default class DocSearch extends DocSearchV2 {
this.name = name;
}
setRootPath(rootPath) {
this.rootPath = rootPath;
}
setSearchIndex(searchIndex) {
this.searchIndex = this.buildIndex(searchIndex);
}