2025-04-30 11:53:56 +00:00
|
|
|
|
const XLSX = require("xlsx");
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// TODO: Replace deprecated @electron/remote with contextBridge‑based IPC in production.
|
2025-04-30 11:53:56 +00:00
|
|
|
|
const electron = require("@electron/remote");
|
2025-05-02 13:50:50 +00:00
|
|
|
|
const { ipcRenderer } = require("electron");
|
|
|
|
|
const path = require("path");
|
2022-08-04 03:00:20 +00:00
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Supported file extensions
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2025-04-30 11:53:56 +00:00
|
|
|
|
const EXTENSIONS =
|
|
|
|
|
"xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html|numbers".split(
|
2025-05-02 13:50:50 +00:00
|
|
|
|
"|",
|
2025-04-30 11:53:56 +00:00
|
|
|
|
);
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// DOM references
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
const dropContainer = document.getElementById("drop-container");
|
|
|
|
|
const fileStatus = document.getElementById("fileStatus");
|
|
|
|
|
const exportBtn = document.getElementById("exportBtn");
|
2025-04-30 11:53:56 +00:00
|
|
|
|
const spinnerOverlay = document.getElementById("spinner-overlay");
|
2025-05-02 13:50:50 +00:00
|
|
|
|
const htmlout = document.getElementById("htmlout");
|
|
|
|
|
const onError = document.getElementById("onError");
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// State & helpers
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
let currentWorkbook = null; // SheetJS workbook in memory
|
|
|
|
|
const isSpreadsheet = (ext) => EXTENSIONS.includes(ext.toLowerCase());
|
|
|
|
|
const nextPaint = () => new Promise(requestAnimationFrame);
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Open external links in default browser (security)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2025-04-30 11:53:56 +00:00
|
|
|
|
document.addEventListener("click", (e) => {
|
|
|
|
|
if (e.target.tagName === "A" && e.target.href.startsWith("http")) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
electron.shell.openExternal(e.target.href);
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-08-04 03:00:20 +00:00
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Export logic – uses cached workbook (no DOM traversal)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
async function exportWorkbookAsFile() {
|
|
|
|
|
if (!currentWorkbook) return displayError("No workbook loaded!");
|
|
|
|
|
// -- 1. use electron save as dialog to get file path
|
|
|
|
|
const { filePath, canceled } = await electron.dialog.showSaveDialog({
|
2025-04-30 11:53:56 +00:00
|
|
|
|
title: "Save file as",
|
|
|
|
|
filters: [{ name: "Spreadsheets", extensions: EXTENSIONS }],
|
2022-08-04 03:00:20 +00:00
|
|
|
|
});
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 2. if canceled or no file path, return
|
|
|
|
|
if (canceled || !filePath) return;
|
|
|
|
|
// -- 3. write workbook to file
|
|
|
|
|
try {
|
|
|
|
|
XLSX.writeFile(currentWorkbook, filePath);
|
|
|
|
|
electron.dialog.showMessageBox({ message: `Exported to ${filePath}` });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// -- 4. if error, display error
|
|
|
|
|
displayError(`Failed to export: ${err.message}`);
|
|
|
|
|
}
|
2025-04-28 19:07:18 +00:00
|
|
|
|
}
|
2025-05-02 13:50:50 +00:00
|
|
|
|
exportBtn.addEventListener("click", exportWorkbookAsFile);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Render workbook --> HTML tables
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2025-04-28 19:07:18 +00:00
|
|
|
|
function renderWorkbookToTables(wb) {
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 1. convert each sheet to HTML
|
|
|
|
|
const html = wb.SheetNames.map((name) => {
|
|
|
|
|
const sheet = wb.Sheets[name];
|
|
|
|
|
const table = XLSX.utils.sheet_to_html(sheet, { id: `${name}-tbl` });
|
|
|
|
|
// -- 2. wrap in details element
|
|
|
|
|
return `<details class="sheetjs-sheet-container">
|
|
|
|
|
<summary class="sheetjs-sheet-name">${name}</summary>
|
|
|
|
|
<div class="sheetjs-tab-content">${table}</div>
|
|
|
|
|
</details>`;
|
|
|
|
|
}).join(""); // -- 3. join into single string
|
|
|
|
|
// -- 4. render to DOM
|
|
|
|
|
htmlout.innerHTML = html; // single write → single re‑flow of the DOM
|
2022-08-04 03:00:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Generic UI helpers
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
const displayError = (msg) => (onError ? ((onError.textContent = msg), (onError.hidden = false)) : console.error(msg));
|
|
|
|
|
const hideDropUI = () => dropContainer && (dropContainer.style.display = "none");
|
|
|
|
|
const showDropUI = () => dropContainer && (dropContainer.style.display = "block");
|
|
|
|
|
const hideExportBtn = () => (exportBtn.disabled = true);
|
|
|
|
|
const showExportBtn = () => (exportBtn.disabled = false);
|
|
|
|
|
const showSpinner = () => (spinnerOverlay.style.display = "flex");
|
|
|
|
|
const hideSpinner = () => (spinnerOverlay.style.display = "none");
|
|
|
|
|
const hideOutputUI = () => (htmlout.innerHTML = "");
|
|
|
|
|
const hideLoadedFileUI = () => (fileStatus.innerHTML = "");
|
|
|
|
|
const getLoadedFileUI = (fileName) => `<div class="file-loaded">
|
|
|
|
|
<span class="file-name text-muted text-small">${fileName}</span>
|
|
|
|
|
<button type="button" class="unload-btn">Unload</button>
|
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
function showLoadedFileUI(fileName) {
|
|
|
|
|
fileStatus.innerHTML = getLoadedFileUI(fileName);
|
|
|
|
|
hideDropUI();
|
|
|
|
|
showExportBtn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Event delegation for unload button – avoids per‑render listener leaks
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
fileStatus.addEventListener("click", (e) => {
|
|
|
|
|
if (e.target.classList.contains("unload-btn")) {
|
|
|
|
|
hideLoadedFileUI();
|
|
|
|
|
hideExportBtn();
|
|
|
|
|
showDropUI();
|
|
|
|
|
hideOutputUI();
|
|
|
|
|
currentWorkbook = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// File‑open dialog handler
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2022-08-04 03:00:20 +00:00
|
|
|
|
async function handleReadBtn() {
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 1. show file open dialog to get the file path
|
|
|
|
|
const { filePaths, canceled } = await electron.dialog.showOpenDialog({
|
2025-04-30 11:53:56 +00:00
|
|
|
|
title: "Select a file",
|
2025-04-28 19:07:18 +00:00
|
|
|
|
filters: [{ name: "Spreadsheets", extensions: EXTENSIONS }],
|
2025-04-30 11:53:56 +00:00
|
|
|
|
properties: ["openFile"],
|
2022-08-04 03:00:20 +00:00
|
|
|
|
});
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 2. if canceled or no file path, return
|
|
|
|
|
if (canceled || !filePaths.length) return;
|
|
|
|
|
// -- 3. if multiple files selected, return error
|
|
|
|
|
if (filePaths.length !== 1) return displayError("Please choose a single file.");
|
|
|
|
|
|
2025-04-28 19:07:18 +00:00
|
|
|
|
showSpinner();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
await nextPaint(); // ensure spinner paints
|
2025-04-28 19:07:18 +00:00
|
|
|
|
try {
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 4. read the first selected file
|
|
|
|
|
const filePath = filePaths[0];
|
|
|
|
|
currentWorkbook = XLSX.readFile(filePath);
|
|
|
|
|
renderWorkbookToTables(currentWorkbook);
|
|
|
|
|
showLoadedFileUI(path.basename(filePath));
|
2025-04-28 19:07:18 +00:00
|
|
|
|
} finally {
|
|
|
|
|
hideSpinner();
|
2025-04-30 11:53:56 +00:00
|
|
|
|
hideDropUI();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 5. reset error UI state
|
|
|
|
|
onError && (onError.hidden = true);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Drag‑and‑drop + file input
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
function addListener(id, evt, fn) {
|
2025-04-28 19:07:18 +00:00
|
|
|
|
const el = document.getElementById(id);
|
2025-05-02 13:50:50 +00:00
|
|
|
|
if (el) el.addEventListener(evt, fn);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
function attachFileListeners() {
|
|
|
|
|
// file input element
|
2025-04-30 11:53:56 +00:00
|
|
|
|
addListener("readIn", "change", (e) => {
|
2025-04-28 19:07:18 +00:00
|
|
|
|
showSpinner();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
nextPaint().then(() => readFile(e.target.files));
|
2025-04-28 19:07:18 +00:00
|
|
|
|
});
|
2025-04-30 11:53:56 +00:00
|
|
|
|
addListener("readBtn", "click", handleReadBtn);
|
2025-05-02 13:50:50 +00:00
|
|
|
|
|
|
|
|
|
// drag‑and‑drop (applied to whole window for simplicity)
|
|
|
|
|
const onDrag = (e) => {
|
2025-04-30 11:53:56 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.dataTransfer.dropEffect = "copy";
|
2025-04-28 19:07:18 +00:00
|
|
|
|
};
|
2025-05-02 13:50:50 +00:00
|
|
|
|
|
|
|
|
|
["dragenter", "dragover"].forEach((t) =>
|
|
|
|
|
document.body.addEventListener(t, onDrag, { passive: false })
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-01 10:15:07 +00:00
|
|
|
|
document.body.addEventListener(
|
2025-04-30 11:53:56 +00:00
|
|
|
|
"drop",
|
|
|
|
|
(e) => {
|
|
|
|
|
e.preventDefault();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
readFile(e.dataTransfer.files).catch((err) => displayError(err.message));
|
2025-04-30 11:53:56 +00:00
|
|
|
|
},
|
2025-05-02 13:50:50 +00:00
|
|
|
|
{ passive: false }
|
2025-04-30 11:53:56 +00:00
|
|
|
|
);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Read File from input or DnD
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2022-08-04 03:00:20 +00:00
|
|
|
|
async function readFile(files) {
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 1. if no files, return
|
|
|
|
|
if (!files || !files.length) return;
|
|
|
|
|
// -- 2. get the first file
|
|
|
|
|
const file = files[0];
|
|
|
|
|
// -- 3. if not a spreadsheet, return error
|
|
|
|
|
const ext = path.extname(file.name).slice(1);
|
|
|
|
|
if (!isSpreadsheet(ext)) return displayError(`Unsupported file type .${ext}`);
|
|
|
|
|
|
2025-04-28 19:07:18 +00:00
|
|
|
|
showSpinner();
|
|
|
|
|
try {
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// -- 4. read the file
|
|
|
|
|
const data = await file.arrayBuffer();
|
|
|
|
|
currentWorkbook = XLSX.read(data);
|
|
|
|
|
// -- 5. render the workbook to tables
|
|
|
|
|
renderWorkbookToTables(currentWorkbook);
|
|
|
|
|
// -- 6. show the loaded file UI
|
|
|
|
|
showLoadedFileUI(file.name);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
} finally {
|
|
|
|
|
hideSpinner();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// reset error UI state
|
|
|
|
|
onError && (onError.hidden = true);
|
2025-04-28 19:07:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 13:50:50 +00:00
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Init
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
attachFileListeners();
|
|
|
|
|
// the file-opened event is sent from the main process when a file is opened using "open with"
|
|
|
|
|
ipcRenderer.on("file-opened", async (_e, filePath) => {
|
2025-04-30 18:15:48 +00:00
|
|
|
|
showSpinner();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
await nextPaint(); // ensure spinner paints
|
|
|
|
|
currentWorkbook = XLSX.readFile(filePath);
|
|
|
|
|
renderWorkbookToTables(currentWorkbook);
|
|
|
|
|
showLoadedFileUI(path.basename(filePath));
|
2025-04-30 18:15:48 +00:00
|
|
|
|
hideSpinner();
|
|
|
|
|
hideDropUI();
|
|
|
|
|
showExportBtn();
|
2025-05-02 13:50:50 +00:00
|
|
|
|
});
|