Upload main.js
Browse files
main.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import "./style.css";
|
| 2 |
+
|
| 3 |
+
async function loadProvincesLocal() {
|
| 4 |
+
const res = await fetch("./provinces.json", { cache: "no-store" });
|
| 5 |
+
if (!res.ok) throw new Error("Không tải được provinces.json");
|
| 6 |
+
return res.json();
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
function makeWorker(url) {
|
| 10 |
+
return new Worker(new URL(url, import.meta.url), { type: "module" });
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
async function boot() {
|
| 14 |
+
const el = document.querySelector("#app");
|
| 15 |
+
el.innerHTML = `
|
| 16 |
+
<div style="padding:16px;font-family:system-ui">
|
| 17 |
+
<h2>CAD2MAP (Local-only)</h2>
|
| 18 |
+
<div id="prov">Đang tải provinces...</div>
|
| 19 |
+
<input id="file" type="file" />
|
| 20 |
+
<div id="log" style="margin-top:12px;white-space:pre-wrap"></div>
|
| 21 |
+
</div>
|
| 22 |
+
`;
|
| 23 |
+
|
| 24 |
+
const log = (s) => (document.querySelector("#log").textContent += s + "\n");
|
| 25 |
+
|
| 26 |
+
// 1) Provinces local-only
|
| 27 |
+
try {
|
| 28 |
+
const p = await loadProvincesLocal();
|
| 29 |
+
document.querySelector("#prov").textContent = `Provinces: ${p.length} (từ provinces.json, không cloud)`;
|
| 30 |
+
} catch (e) {
|
| 31 |
+
document.querySelector("#prov").textContent = "Lỗi tải provinces (local)";
|
| 32 |
+
log(String(e));
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// 2) Worker (parse client-side)
|
| 36 |
+
const dxfWorker = makeWorker("./workers/dxf-parser-worker.js");
|
| 37 |
+
|
| 38 |
+
document.querySelector("#file").addEventListener("change", async (ev) => {
|
| 39 |
+
const f = ev.target.files?.[0];
|
| 40 |
+
if (!f) return;
|
| 41 |
+
|
| 42 |
+
const buf = await f.arrayBuffer();
|
| 43 |
+
const id = crypto.randomUUID();
|
| 44 |
+
dxfWorker.postMessage({ id, input: { fileName: f.name, buffer: buf } }, [buf]);
|
| 45 |
+
|
| 46 |
+
dxfWorker.onmessage = (msgEv) => {
|
| 47 |
+
const { success, error } = msgEv.data || {};
|
| 48 |
+
if (!success) log("DXF worker: " + (error || "fail"));
|
| 49 |
+
else log("DXF worker: OK");
|
| 50 |
+
};
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
boot();
|