all repos — h3 @ 0165fca9a3e522e99dc0b53acd440398dcf68493

A tiny, extremely minimalist JavaScript microframework.

Simplified site.
h3rald h3rald@h3rald.com
Sun, 19 Apr 2020 11:12:27 +0200
commit

0165fca9a3e522e99dc0b53acd440398dcf68493

parent

a1cd2c943df6d809a9c09c61a86b48b53504e9b2

4 files changed, 42 insertions(+), 30 deletions(-)

jump to
M docs/example/assets/js/h3.jsdocs/example/assets/js/h3.js

@@ -106,7 +106,10 @@ // Assume single child text node

this.children = [new VNode({ type: "#text", value: data })]; return; } - if (typeof data !== "function" && (typeof data !== "object" || data === null)) { + if ( + typeof data !== "function" && + (typeof data !== "object" || data === null) + ) { console.log(data); throw new Error( "[VNode] The second argument of a VNode constructor must be an object, an array or a string."

@@ -608,8 +611,14 @@

let store = null; let router = null; -h3.init = ({ element, routes, modules, preStart, postStart }) => { - if (!(element instanceof Element)) { +h3.init = (config) => { + let { element, routes, modules, preStart, postStart } = config; + if (!routes) { + // Assume config is a component object, define default route + routes = { "/": config }; + } + element = element || document.body; + if (!(element && element instanceof Element)) { throw new Error("[h3.init] Invalid element specified."); } // Initialize store
M docs/js/app.jsdocs/js/app.js

@@ -11,32 +11,23 @@ api: "API",

about: "About", }; -const pages = (store) => { - store.on("$init", () => ({ pages: {} })); - store.on("pages/set", (state, data) => ({ - pages: { [data.id]: data.content, ...state.pages }, - })); -}; +const pages = {}; const Page = () => { const id = h3.route.path.slice(1); const ids = Object.keys(labels); const md = ids.includes(id) ? `md/${id}.md` : `md/overview.md`; - if (!h3.state.pages[id]) { + if (!pages[id]) { (async () => { const response = await fetch(md); const text = await response.text(); - h3.dispatch("pages/set", { - id, - content: marked(DOMPurify.sanitize(text)), - }); + pages[id] = marked(DOMPurify.sanitize(text)); h3.redraw(); })(); } const menu = ids.map((p) => h3("a", { href: `#/${p}` }, labels[p])); - const html = h3.state.pages[id]; - const content = html - ? h3("div.content", { $html: html }) + const content = pages[id] + ? h3("div.content", { $html: pages[id] }) : h3("div.spinner-container", h3("span.spinner")); return h3("div.page", [ h3("header.row", [

@@ -57,10 +48,4 @@ ]),

]); }; -h3.init({ - element: document.body, - modules: [pages], - routes: { - "/": Page, - }, -}); +h3.init(Page);
M docs/js/h3.jsdocs/js/h3.js

@@ -106,7 +106,10 @@ // Assume single child text node

this.children = [new VNode({ type: "#text", value: data })]; return; } - if (typeof data !== "function" && (typeof data !== "object" || data === null)) { + if ( + typeof data !== "function" && + (typeof data !== "object" || data === null) + ) { console.log(data); throw new Error( "[VNode] The second argument of a VNode constructor must be an object, an array or a string."

@@ -608,8 +611,14 @@

let store = null; let router = null; -h3.init = ({ element, routes, modules, preStart, postStart }) => { - if (!(element instanceof Element)) { +h3.init = (config) => { + let { element, routes, modules, preStart, postStart } = config; + if (!routes) { + // Assume config is a component object, define default route + routes = { "/": config }; + } + element = element || document.body; + if (!(element && element instanceof Element)) { throw new Error("[h3.init] Invalid element specified."); } // Initialize store
M h3.jsh3.js

@@ -106,7 +106,10 @@ // Assume single child text node

this.children = [new VNode({ type: "#text", value: data })]; return; } - if (typeof data !== "function" && (typeof data !== "object" || data === null)) { + if ( + typeof data !== "function" && + (typeof data !== "object" || data === null) + ) { console.log(data); throw new Error( "[VNode] The second argument of a VNode constructor must be an object, an array or a string."

@@ -608,8 +611,14 @@

let store = null; let router = null; -h3.init = ({ element, routes, modules, preStart, postStart }) => { - if (!(element instanceof Element)) { +h3.init = (config) => { + let { element, routes, modules, preStart, postStart } = config; + if (!routes) { + // Assume config is a component object, define default route + routes = { "/": config }; + } + element = element || document.body; + if (!(element && element instanceof Element)) { throw new Error("[h3.init] Invalid element specified."); } // Initialize store