mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-12 22:57:10 +00:00
Merge import to export.js
This commit is contained in:
parent
179c47d93b
commit
5e48da1cb6
3 changed files with 65 additions and 67 deletions
|
@ -5,6 +5,7 @@ import CrateDocManager from "../../crate-manager.js";
|
|||
import settings from "../../settings.js";
|
||||
|
||||
(async function () {
|
||||
// ============== export ==============
|
||||
document.querySelector(".btn-export").onclick = async (event) => {
|
||||
let target = event.target.parentElement;
|
||||
let data = Object.create(null)
|
||||
|
@ -33,7 +34,7 @@ import settings from "../../settings.js";
|
|||
list,
|
||||
};
|
||||
}
|
||||
let date = new Compat().normalizeDate(new Date());
|
||||
let date = Compat.normalizeDate(new Date());
|
||||
saveToFile(JSON.stringify(data), `rust-search-extension-${date}.json`, 'text/plain');
|
||||
};
|
||||
|
||||
|
@ -44,4 +45,66 @@ import settings from "../../settings.js";
|
|||
a.download = fileName;
|
||||
a.click();
|
||||
}
|
||||
})();
|
||||
})();
|
||||
|
||||
(function () {
|
||||
// ============== import ==============
|
||||
let json = null;
|
||||
let fileSelector = document.querySelector(".file-selector");
|
||||
fileSelector.onchange = function () {
|
||||
fileSelector.classList.remove("required");
|
||||
|
||||
let fileReader = new FileReader();
|
||||
fileReader.onload = () => {
|
||||
json = JSON.parse(fileReader.result);
|
||||
console.log("Imported JSON:", json);
|
||||
};
|
||||
fileReader.readAsText(this.files[0]);
|
||||
};
|
||||
|
||||
document.querySelector(".btn-import").onclick = async (event) => {
|
||||
if (!json) {
|
||||
fileSelector.classList.add("required");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!["settings", "history", "stats", "crates"].some(item => item in json)) {
|
||||
alert("Invalid json file");
|
||||
return;
|
||||
}
|
||||
|
||||
let target = event.target.parentElement;
|
||||
if (
|
||||
![".settings", ".search-history", ".search-statistics", ".crates"]
|
||||
.some(item => target.querySelector(item).checked)
|
||||
) {
|
||||
alert("Please select at least one category to import.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (json["settings"] && target.querySelector(".settings").checked) {
|
||||
let importedSettings = json["settings"];
|
||||
settings.autoUpdate = importedSettings["auto-update"];
|
||||
settings.crateRegistry = importedSettings["crate-registry"];
|
||||
settings.isOfflineMode = importedSettings["offline-mode"];
|
||||
settings.offlineDocPath = importedSettings["offline-path"];
|
||||
}
|
||||
if (json["history"] && target.querySelector(".search-history").checked) {
|
||||
await storage.setItem("history", json["history"]);
|
||||
}
|
||||
if (json["stats"] && target.querySelector(".search-statistics").checked) {
|
||||
await storage.setItem("statistics", json["stats"]);
|
||||
}
|
||||
if (json["crates"] && target.querySelector(".crates").checked) {
|
||||
let importedCrates = json["crates"];
|
||||
let catalog = await CrateDocManager.getCrates();
|
||||
for (let [name, searchIndex] of Object.entries(importedCrates["list"])) {
|
||||
await storage.setItem(name, searchIndex);
|
||||
}
|
||||
let crates = Object.assign(catalog, importedCrates["catalog"]);
|
||||
await storage.setItem("crates", crates);
|
||||
}
|
||||
|
||||
alert("Import success!")
|
||||
};
|
||||
})();
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
import storage from "../../core/storage.js";
|
||||
import CrateDocManager from "../../crate-manager.js";
|
||||
import settings from "../../settings.js";
|
||||
|
||||
(function () {
|
||||
let json = null;
|
||||
let fileSelector = document.querySelector(".file-selector");
|
||||
fileSelector.onchange = function () {
|
||||
fileSelector.classList.remove("required");
|
||||
|
||||
let fileReader = new FileReader();
|
||||
fileReader.onload = () => {
|
||||
json = JSON.parse(fileReader.result);
|
||||
console.log("Imported JSON:", json);
|
||||
};
|
||||
fileReader.readAsText(this.files[0]);
|
||||
};
|
||||
|
||||
document.querySelector(".btn-import").onclick = async (event) => {
|
||||
if (!json) {
|
||||
fileSelector.classList.add("required");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!["settings", "history", "stats", "crates"].some(item => item in json)) {
|
||||
alert("Invalid json file");
|
||||
return;
|
||||
}
|
||||
|
||||
let target = event.target.parentElement;
|
||||
if (
|
||||
![".settings", ".search-history", ".search-statistics", ".crates"]
|
||||
.some(item => target.querySelector(item).checked)
|
||||
) {
|
||||
alert("Please select at least one category to import.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (json["settings"] && target.querySelector(".settings").checked) {
|
||||
let importedSettings = json["settings"];
|
||||
settings.autoUpdate = importedSettings["auto-update"];
|
||||
settings.crateRegistry = importedSettings["crate-registry"];
|
||||
settings.isOfflineMode = importedSettings["offline-mode"];
|
||||
settings.offlineDocPath = importedSettings["offline-path"];
|
||||
}
|
||||
if (json["history"] && target.querySelector(".search-history").checked) {
|
||||
await storage.setItem("history", json["history"]);
|
||||
}
|
||||
if (json["stats"] && target.querySelector(".search-statistics").checked) {
|
||||
await storage.setItem("statistics", json["stats"]);
|
||||
}
|
||||
if (json["crates"] && target.querySelector(".crates").checked) {
|
||||
let importedCrates = json["crates"];
|
||||
let catalog = await CrateDocManager.getCrates();
|
||||
for (let [name, searchIndex] of Object.entries(importedCrates["list"])) {
|
||||
await storage.setItem(name, searchIndex);
|
||||
}
|
||||
let crates = Object.assign(catalog, importedCrates["catalog"]);
|
||||
await storage.setItem("crates", crates);
|
||||
}
|
||||
|
||||
alert("Import success!")
|
||||
};
|
||||
})();
|
|
@ -46,5 +46,4 @@
|
|||
|
||||
{% block js %}
|
||||
<script src="./js/export.js" type="module"></script>
|
||||
<script src="./js/import.js" type="module"></script>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue