/* * notion-enhancer core: menu * (c) 2021 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ 'use strict'; import { web, fs, registry, markdown } from '../../helpers.js'; for (let mod of await registry.get()) { for (let sheet of mod.css?.menu || []) { web.loadStyleset(`repo/${mod._dir}/${sheet}`); } } const components = { preview({ preview }) { if (!preview) return ''; const element = web.createElement( `` ); return element; }, name({ name, id, version }) { const element = web.createElement(``); return element; }, tags({ tags }) { if (!tags || !tags.length) return ''; const element = web.createElement( `` ); return element; }, description({ description }) { const element = web.createElement( `

${markdown.renderInline(description)}

` ); return element; }, authors({ authors }) { const element = web.createElement( `` ); return element; }, expand({ id }) { const element = web.createElement(`

settings & documentation

`); return element; }, toggle(id, { key, label, value }) { const element = web.createElement(``); return element; }, select(id, { key, label, values }) { const element = web.createElement(``); return element; }, text(id, { key, label, value }) { const element = web.createElement(``); element.querySelector('textarea').addEventListener('input', (ev) => { ev.target.style.removeProperty('--txt--scroll-height'); ev.target.style.setProperty('--txt--scroll-height', ev.target.scrollHeight + 'px'); }); return element; }, number(id, { key, label, value }) { const element = web.createElement(``); return element; }, file(id, { key, label, extensions }) { const accept = extensions && extensions.length ? ` accept="${extensions.join(',')}"` : ''; const element = web.createElement(``); element.querySelector('input[type="file"]').addEventListener('change', (ev) => { element.querySelector('.library--file_path').innerText = ev.target.files[0].name; }); return element; }, documentation_buttons({ _dir }) { const element = web.createElement(`

back to library view source code

`); return element; }, }, generators = { summary_card(mod) { const article = web.createElement('
'), body = web.createElement('
'); article.append(components.preview(mod)); body.append(components.name(mod)); body.append(components.tags(mod)); body.append(components.description(mod)); body.append(components.authors(mod)); body.append(components.expand(mod)); article.append(body); return article; }, full_card(mod) { const article = web.createElement('
'), body = web.createElement('
'); article.append(components.preview(mod)); body.append(components.name(mod)); body.append(components.tags(mod)); body.append(components.description(mod)); body.append(components.authors(mod)); article.append(body); if (mod.options && mod.options.length) { const options = web.createElement(`
`); mod.options.forEach((opt) => options.append(components[opt.type](mod.id, opt))); article.append(options); } return article; }, async documentation(mod) { const content = (await fs.isFile(`repo/${mod._dir}/README.md`)) ? markdown.render(await fs.getText(`repo/${mod._dir}/README.md`)) : '', article = web.createElement( `
${content}
` ); return article; }, }, tabs = { library: { title: document.querySelector('header [data-target="library"]'), async container() { document.querySelector('[data-target][data-active]').removeAttribute('data-active'); this.title.dataset.active = true; const $container = document.querySelector('[data-container]'); $container.dataset.container = 'library'; $container.innerHTML = ''; for (let mod of await registry.get()) $container.append(generators.summary_card(mod)); }, }, mod: { title: document.querySelector('header [data-target="library"]'), async container(mod) { document.querySelector('[data-target][data-active]').removeAttribute('data-active'); this.title.dataset.active = true; const $container = document.querySelector('[data-container]'); $container.dataset.container = 'page'; $container.innerHTML = ''; $container.append(components.documentation_buttons(mod)); $container.append(generators.full_card(mod)); $container.append(await generators.documentation(mod)); }, }, }; tabs.library.title.addEventListener('click', (ev) => tabs.library.container()); (async () => { const search = new Map( location.search .slice(1) .split('&') .map((query) => query.split('=')) ), mod = (await registry.get()).find((mod) => mod.id === search.get('mod')); if (mod) { tabs.mod.container(mod); } else tabs.library.container(); })(); // registry.errors().then((err) => { // document.querySelector('[data-section="alerts"]').innerHTML = JSON.stringify(err); // });