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, REDIRECT_URL,
} from "./constants.js"; } 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({ static async run({
omnibox, omnibox,
stdSearcher, stdSearcher,
@ -278,12 +283,11 @@ export default class RustSearchOmnibox {
result.push(errorIndex); result.push(errorIndex);
} }
let isOfflineMode = await settings.isOfflineMode; let baseUrl = await getBaseUrl();
let baseUrl = isOfflineMode ? await settings.offlineDocPath : 'https://doc.rust-lang.org/';
return result.map(errorCode => { return result.map(errorCode => {
return { return {
content: `${baseUrl}error_codes/${errorCode}.html`, 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, INDEX_UPDATE_URL,
RUST_RELEASE_README_URL, RUST_RELEASE_README_URL,
} from "./constants.js"; } from "./constants.js";
import RustSearchOmnibox from "./lib.js"; import { RustSearchOmnibox, getBaseUrl } from "./lib.js";
async function start(omnibox) { 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 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({ RustSearchOmnibox.run({
omnibox, omnibox,
@ -101,16 +101,18 @@ async function start(omnibox) {
if (!omnibox.extensionMode) return; if (!omnibox.extensionMode) return;
chrome.storage.onChanged.addListener(changes => { chrome.storage.onChanged.addListener(async changes => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) { for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log('storage key updated:', key); console.log('storage key updated:', key);
switch (key) { switch (key) {
case "offline-mode": { case "offline-mode": {
isOfflineMode = newValue; isOfflineMode = newValue;
stdSearcher.setRootPath(await getBaseUrl());
break; break;
} }
case "offline-path": { case "offline-path": {
offlineDocPath = newValue; offlineDocPath = newValue;
stdSearcher.setRootPath(await getBaseUrl());
break; break;
} }
case "index-std-stable": { case "index-std-stable": {

View file

@ -41,12 +41,18 @@ document.addEventListener('DOMContentLoaded', async function () {
const offlineDocPath = document.querySelector('.offline-doc-path'); const offlineDocPath = document.querySelector('.offline-doc-path');
offlineDocPath.value = await settings.offlineDocPath; offlineDocPath.value = await settings.offlineDocPath;
offlineDocPath.onchange = async function (event) { offlineDocPath.onchange = async function (event) {
let path = event.target.value;
if (await getPlatformOs() === "win") { if (await getPlatformOs() === "win") {
// Replace all "/" to "\" for Windows. // Replace all "/" to "\" for Windows.
settings.offlineDocPath = event.target.value.replaceAll("/", "\\"); path = event.target.value.replaceAll("/", "\\");
} else {
settings.offlineDocPath = event.target.value;
} }
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']"); let crateRegistry = document.querySelector("select[name='crate-registry']");

View file

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