diff --git a/js/editor.js b/js/editor.js index 5b2b988..bb900cc 100644 --- a/js/editor.js +++ b/js/editor.js @@ -1,16 +1,12 @@ "use strict"; import * as Sentry from "@sentry/browser"; - import * as currency from "./currency.js"; +import { createHelpTables } from "./help_page.js"; import * as regex from "./regex.js"; -import helpOperators from "./help_operators.json"; -import helpShortcuts from "./help_shortcuts.json"; -import helpUnits from "./help_units.json"; +import showToast from "show-toast"; import { LocalStorage } from "web-browser-storage"; import { createUnit, unit, evaluate as mathjs_evaluate } from "mathjs"; - const storage = new LocalStorage(); -import showToast from "show-toast"; var _ = require("lodash"); let editor; @@ -87,60 +83,6 @@ function onOverlayClick() { focusEditor(); } -async function createHelpTables() { - // Operator Table - const operatorColumns = ["Name", "Operator", "Example"]; - const operatorsDataa = [...helpOperators.operators]; - const operatorTableHtml = createTable(operatorColumns, operatorsDataa); - document.getElementById("operator-table-container").innerHTML = - operatorTableHtml; - - // Keyboard shortcut table - const shortcutColumns = ["Action", "Shortcut"]; - const shortcutsData = [...helpShortcuts.shortcuts]; - const shortcutTableHtml = createTable(shortcutColumns, shortcutsData); - document.getElementById("shortcut-table-container").innerHTML = - shortcutTableHtml; - - // Units table - const unitColumns = ["Value", "Units"]; - const unitsData = [...helpUnits.units]; - const unitTableHtml = createTable(unitColumns, unitsData); - document.getElementById("unit-table-container").innerHTML = unitTableHtml; -} - -function createTable(columns, data) { - let table = ""; - - // Create table header - table += ""; - columns.forEach((column) => { - table += ``; - }); - table += ""; - - // Create table body - table += ""; - data.forEach((row) => { - table += ""; - columns.forEach((column) => { - table += `"; - }); - table += ""; - }); - table += "
${column}
`; - - if (Array.isArray(row[column])) { - row[column].forEach((item) => { - table += `${item}
`; - }); - } else { - table += `${row[column]}`; - } - table += "
"; - return table; -} - export async function setupEvaluator() { homeCurrency = await setupHomeCurrency(); createUnit(homeCurrency.toLowerCase()); diff --git a/js/help_page.js b/js/help_page.js new file mode 100644 index 0000000..0e6d443 --- /dev/null +++ b/js/help_page.js @@ -0,0 +1,60 @@ +// Help page related activities happen in this File. Including DOM modifications + +"use strict"; +import helpOperators from "./help_operators.json"; +import helpShortcuts from "./help_shortcuts.json"; +import helpUnits from "./help_units.json"; + +export async function createHelpTables() { + // Operator Table + const operatorColumns = ["Name", "Operator", "Example"]; + const operatorsDataa = [...helpOperators.operators]; + const operatorTableHtml = createTable(operatorColumns, operatorsDataa); + document.getElementById("operator-table-container").innerHTML = + operatorTableHtml; + + // Keyboard shortcut table + const shortcutColumns = ["Action", "Shortcut"]; + const shortcutsData = [...helpShortcuts.shortcuts]; + const shortcutTableHtml = createTable(shortcutColumns, shortcutsData); + document.getElementById("shortcut-table-container").innerHTML = + shortcutTableHtml; + + // Units table + const unitColumns = ["Value", "Units"]; + const unitsData = [...helpUnits.units]; + const unitTableHtml = createTable(unitColumns, unitsData); + document.getElementById("unit-table-container").innerHTML = unitTableHtml; +} + +function createTable(columns, data) { + let table = ""; + + // Create table header + table += ""; + columns.forEach((column) => { + table += ``; + }); + table += ""; + + // Create table body + table += ""; + data.forEach((row) => { + table += ""; + columns.forEach((column) => { + table += `"; + }); + table += ""; + }); + table += "
${column}
`; + + if (Array.isArray(row[column])) { + row[column].forEach((item) => { + table += `${item}
`; + }); + } else { + table += `${row[column]}`; + } + table += "
"; + return table; +} diff --git a/js/localize.js b/js/localize.js deleted file mode 100644 index e7dfb08..0000000 --- a/js/localize.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -export function localize() { - let strings = document.querySelectorAll("[data-localize]"); - - for (let s of strings) { - s.innerText = chrome.i18n.getMessage(s.dataset.localize); - } -} diff --git a/test/help_page.test.js b/test/help_page.test.js new file mode 100644 index 0000000..4e23e39 --- /dev/null +++ b/test/help_page.test.js @@ -0,0 +1,33 @@ +import { createHelpTables } from "../js/help_page"; +import { describe, it, expect } from "vitest"; +import path from "path"; +import fs from "fs"; + +describe("createHelpTables tests", () => { + const indexPath = path.resolve(__dirname, "../html/index.html"); + const indexHtml = fs.readFileSync(indexPath, "utf-8"); + document.body.innerHTML = indexHtml; + + createHelpTables(); + + it("should create shortcut tables contents", () => { + const shortcutTables = document.getElementById("shortcut-table-container"); + expect(shortcutTables.querySelectorAll("td")[4].outerHTML).toEqual( + "Open/Close calculator History" + ); + }); + + it("should create operator tables contents", () => { + const operatorTables = document.getElementById("operator-table-container"); + expect(operatorTables.querySelectorAll("td")[2].outerHTML).toEqual( + "2 + 3 = 5" + ); + }); + + it("should create unit tables contents", () => { + const unitTables = document.getElementById("unit-table-container"); + expect(unitTables.querySelectorAll("td")[5].outerHTML).toEqual( + "m2, sqin, sqft, sqyd, sqmi, sqrd, sqch, sqmil, acre, hectare" + ); + }); +});