Skip to content

Commit

Permalink
Add searchState compatible js
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jun 20, 2024
1 parent 2b40082 commit 7a0793b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion extension/content-script-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions extension/index-setter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class IndexSetter {
storage.setItem('index-std-nightly', index);
}

static setDescShards(crate, shards) {
storage.setItem(`index-desc-shards-${crate}`, shards);
}

static setBookIndex(index) {
storage.setItem('index-book', index);
}
Expand Down
5 changes: 3 additions & 2 deletions extension/script/add-search-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
return list;
}
async function loadDescShard(crates) {
if (!window.searchState.descShards) return;
if (!window.searchState.descShards) return null;

let crateDescsShard = {};
for (let crate of crates) {
Expand All @@ -35,6 +35,7 @@
}

console.log('load desc shard:', crateDescsShard);
return crateDescsShard;
}
async function sendSearchIndex() {
if (location.hostname === "docs.rs") { // docs.rs pages
Expand Down Expand Up @@ -91,11 +92,11 @@
STD_CRATES.forEach(crate => {
searchIndex[crate] = rawSearchIndex[crate];
});
await loadDescShard(STD_CRATES);
window.postMessage({
direction: `rust-search-extension:std`,
message: {
searchIndex,
descShards: await loadDescShard(STD_CRATES),
},
}, "*");
}
Expand Down
7 changes: 7 additions & 0 deletions extension/script/doc-rust-lang-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ window.addEventListener("message", function (event) {
event.data &&
event.data.direction === "rust-search-extension:std") {
let searchIndex = event.data.message.searchIndex;
let descShards = event.data.message.descShards;
if (TARGET === 'stable') {
rse.IndexSetter.setStdStableIndex(searchIndex);
if (descShards) {
rse.IndexSetter.setDescShards("std-stable", descShards);
}
} else {
rse.IndexSetter.setStdNightlyIndex(searchIndex);
if (descShards) {
rse.IndexSetter.setDescShards("std-nightly", descShards);
}
}
let now = new Date();
let version = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
Expand Down
30 changes: 30 additions & 0 deletions extension/search/docs/desc-shard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import CrateDocManager from "../../crate-manager.js";
import IndexManager from "../../index-manager.js";

class DescShardManager {
constructor() {
// A crate -> desc shard map.
this.descShards = new Map();
}

async initDescShards() {
for (const crate of Object.keys(await CrateDocManager.getCrates())) {
const descShards = await IndexManager.getDescShards(crate);
this.descShards.set(crate, descShards);
}
}


// Load a single desc shard.
// Compatible with librustdoc main.js.
async loadDesc({ descShard, descIndex }) {
let crateDescShard = this.descShards.get(descShard.crate);
if (!crateDescShard || crateDescShard.length === 0) {
return null;
}
return crateDescShard[descShard.shard][descIndex];
}
}

const searchState = new DescShardManager();
export default searchState;

0 comments on commit 7a0793b

Please sign in to comment.