/* * 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}`); } } // why a tagged template? because it syntax highlights const html = (html, ...templates) => html.map((str) => str + (templates.shift() || '')).join(''); const views = {}; views.components = { card: { preview: ({ preview = '' }) => web.createElement( html`` ), name: ({ name, id, version }) => web.createElement( html`` ), tags: ({ tags = [] }) => web.createElement( html`` ), description: ({ description }) => html`

${markdown.renderInline(description)}

`, authors: ({ authors }) => html``, async expand({ id }) { const element = web.createElement( `

${await fs.getText( 'icons/fontawesome/long-arrow-alt-right.svg' )} settings & documentation

` ); return element; }, }, options: { toggle(id, { key, label, value }) { const element = web.createElement(``); return element; }, async 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; }, }, }; const components = { preview: ({ preview = '' }) => web.createElement( html`` ), name: ({ name, id, version }) => web.createElement(``), 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 }) => html``, async expand({ id }) { const element = web.createElement( `

${await fs.getText( 'icons/fontawesome/long-arrow-alt-right.svg' )} settings & documentation

` ); return element; }, toggle(id, { key, label, value }) { const element = web.createElement(``); return element; }, async 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; }, async 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; }, async documentation_buttons({ _dir }) { const element = web.createElement(`

${await fs.getText( 'icons/fontawesome/long-arrow-alt-left.svg' )} back to library ${await fs.getText( 'icons/fontawesome/code.svg' )} view source code

`); return element; }, }, generators = { async summary_card(mod) { const article = web.createElement('
'), body = web.createElement('
'); article.append(await components.preview(mod)); body.append(await components.name(mod)); body.append(await components.tags(mod)); body.append(await components.description(mod)); body.append(await components.authors(mod)); body.append(await components.expand(mod)); article.append(body); return article; }, async full_card(mod) { const article = web.createElement('
'), body = web.createElement('
'); article.append(await components.preview(mod)); body.append(await components.name(mod)); body.append(await components.tags(mod)); body.append(await components.description(mod)); body.append(await components.authors(mod)); article.append(body); if (mod.options && mod.options.length) { const options = web.createElement(`
`); ( await Promise.all(mod.options.map((opt) => components[opt.type](mod.id, opt))) ).map((opt) => options.append(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(await 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(await components.documentation_buttons(mod)); $container.append(await 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); // });