From 1d7949ded6524324826c5b4bee890ad16027ca1d Mon Sep 17 00:00:00 2001 From: venashial Date: Tue, 29 Mar 2022 22:06:43 -0700 Subject: [PATCH] Add component API to built docs + Add Checkbox, CheckboxList, & CheckboxVirtualList --- package.json | 3 +- plugins/sveld.js | 47 +++++++++---- pnpm-lock.yaml | 18 +++-- src/lib/components/Button.svelte | 3 + src/lib/components/Checkbox.svelte | 66 +++++++++++++++++++ src/lib/components/CheckboxList.svelte | 47 +++++++++++++ src/lib/components/CheckboxVirtualList.svelte | 56 ++++++++++++++++ src/lib/components/Input.svelte | 53 +++++++++++++++ .../_internal/components/Example.svelte | 3 +- src/routes/_internal/layout/page.svelte | 16 +++-- src/routes/components/Checkbox.md | 20 ++++++ src/routes/components/CheckboxList.md | 33 ++++++++++ src/routes/components/CheckboxVirtualList.md | 23 +++++++ src/routes/components/NavRow.md | 4 +- svelte.config.js | 38 +++-------- 15 files changed, 374 insertions(+), 56 deletions(-) create mode 100644 src/lib/components/Checkbox.svelte create mode 100644 src/lib/components/CheckboxList.svelte create mode 100644 src/lib/components/CheckboxVirtualList.svelte create mode 100644 src/lib/components/Input.svelte create mode 100644 src/routes/components/Checkbox.md create mode 100644 src/routes/components/CheckboxList.md create mode 100644 src/routes/components/CheckboxVirtualList.md diff --git a/package.json b/package.json index 4098fd1d4..76938daa9 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "@poppanator/sveltekit-svg": "^0.3.1", "@sveltejs/adapter-static": "^1.0.0-next.29", "@sveltejs/kit": "next", - "@types/prismjs": "^1.26.0", "@typescript-eslint/eslint-plugin": "^5.10.1", "@typescript-eslint/parser": "^5.10.1", "autoprefixer": "^10.4.2", @@ -69,7 +68,9 @@ }, "homepage": "https://omorphia.modrinth.com", "dependencies": { + "lodash.uniqueid": "^4.0.1", "sanitize.css": "^13.0.0", + "svelte-tiny-virtual-list": "^2.0.0", "svelte-use-click-outside": "^1.0.0", "throttle-debounce": "^3.0.1" } diff --git a/plugins/sveld.js b/plugins/sveld.js index 2a048322d..e5f5561c2 100644 --- a/plugins/sveld.js +++ b/plugins/sveld.js @@ -1,6 +1,8 @@ -import { ComponentParser } from 'sveld' +import {ComponentParser} from 'sveld' import sveltePreprocess from 'svelte-preprocess' import * as svelte from 'svelte/compiler' +import fs from 'fs/promises' +import path from 'path' export default function sveld() { return { @@ -9,22 +11,43 @@ export default function sveld() { if (id.endsWith('?raw&sveld')) { const raw = JSON.parse(src.split('export default ')[1]) - let { code } = await svelte.preprocess(raw, sveltePreprocess({ postcss: true }), { - filename: id - }) - const data = new ComponentParser({ - verbose: false - }).parseSvelteComponent(code, { - filePath: id, - moduleName: id - }) - + const data = await parseRaw(raw, id) return { code: `export default ${JSON.stringify(data)}`, map: null } } - } + }, + // This generates a `COMPONENT_API.json` with sveld in the `/_app` folder on build, which is used by the docs about components (only when built statically) + async generateBundle(options, bundle) { + const output = {}; + + const componentFiles = await fs.readdir(path.resolve('./src/lib/components')) + + for (const fileName of componentFiles) { + const filePath = path.resolve('./src/lib/components', fileName) + const raw = (await fs.readFile(filePath)).toString() + output[fileName] = await parseRaw(raw, filePath) + } + + this.emitFile({ + type: 'asset', + fileName: 'COMPONENT_API.json', + source: JSON.stringify(output), + }) + }, } +} + +async function parseRaw(raw, filePath) { + let { code } = await svelte.preprocess(raw, sveltePreprocess({ postcss: true }), { + filename: filePath + }) + return new ComponentParser({ + verbose: false + }).parseSvelteComponent(code, { + filePath, + moduleName: filePath + }) } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7876c8e41..db6895298 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,7 +7,6 @@ specifiers: '@poppanator/sveltekit-svg': ^0.3.1 '@sveltejs/adapter-static': ^1.0.0-next.29 '@sveltejs/kit': next - '@types/prismjs': ^1.26.0 '@typescript-eslint/eslint-plugin': ^5.10.1 '@typescript-eslint/parser': ^5.10.1 autoprefixer: ^10.4.2 @@ -15,6 +14,7 @@ specifiers: eslint: ^7.32.0 eslint-config-prettier: ^8.3.0 eslint-plugin-svelte3: ^3.2.1 + lodash.uniqueid: ^4.0.1 mdsvex: ^0.10.5 mdsvexamples: ^0.0.8 nodemon: ^2.0.15 @@ -32,6 +32,7 @@ specifiers: svelte: ^3.44.0 svelte-check: ^2.2.6 svelte-preprocess: ^4.10.1 + svelte-tiny-virtual-list: ^2.0.0 svelte-use-click-outside: ^1.0.0 svelte2tsx: ^0.5.5 throttle-debounce: ^3.0.1 @@ -40,7 +41,9 @@ specifiers: unplugin-icons: ^0.13.3 dependencies: + lodash.uniqueid: 4.0.1 sanitize.css: 13.0.0 + svelte-tiny-virtual-list: 2.0.0 svelte-use-click-outside: 1.0.0 throttle-debounce: 3.0.1 @@ -51,7 +54,6 @@ devDependencies: '@poppanator/sveltekit-svg': 0.3.1_svelte@3.46.4 '@sveltejs/adapter-static': 1.0.0-next.29 '@sveltejs/kit': 1.0.0-next.303_svelte@3.46.4 - '@types/prismjs': 1.26.0 '@typescript-eslint/eslint-plugin': 5.14.0_e3f5f4efe2bd492e36eb6c1c619dfc98 '@typescript-eslint/parser': 5.14.0_eslint@7.32.0+typescript@4.6.2 autoprefixer: 10.4.2_postcss@8.4.8 @@ -410,10 +412,6 @@ packages: resolution: {integrity: sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==} dev: true - /@types/prismjs/1.26.0: - resolution: {integrity: sha512-ZTaqn/qSqUuAq1YwvOFQfVW1AR/oQJlLSZVustdjwI+GZ8kr0MSHBj0tsXPW1EqHubx50gtBEjbPGsdZwQwCjQ==} - dev: true - /@types/pug/2.0.6: resolution: {integrity: sha512-SnHmG9wN1UVmagJOnyo/qkk0Z7gejYxOYYmaAwr5u2yFYfsupN3sg10kyzN8Hep/2zbHxCnsumxOoRIRMBwKCg==} dev: true @@ -2095,6 +2093,10 @@ packages: resolution: {integrity: sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=} dev: true + /lodash.uniqueid/4.0.1: + resolution: {integrity: sha1-MmjyanyI5PSxdY1nknGBTjH6WyY=} + dev: false + /lower-case/2.0.2: resolution: {integrity: sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==} dependencies: @@ -3603,6 +3605,10 @@ packages: typescript: 4.6.2 dev: true + /svelte-tiny-virtual-list/2.0.0: + resolution: {integrity: sha512-6HxqgUaBICWV/Mm/x1Mr1L6G6EYJM3rcYhRWqYzKpv59e3s9rWifOwP7fLNKtblr16l2Gec0iBtUCOCPiYDmeQ==} + dev: false + /svelte-use-click-outside/1.0.0: resolution: {integrity: sha512-tOWeMPxeIoW9RshS0WbogRhdYdbxcJV0ebkzSh1lwR7Ihl0hSZMmB4YyCHHoXJK4xcbxCCFh0AnQ1vkzGZfLVQ==} dev: false diff --git a/src/lib/components/Button.svelte b/src/lib/components/Button.svelte index abc6c2fa5..b3858b8c9 100644 --- a/src/lib/components/Button.svelte +++ b/src/lib/components/Button.svelte @@ -1,4 +1,7 @@ + +
+ + +
+ + \ No newline at end of file diff --git a/src/lib/components/CheckboxList.svelte b/src/lib/components/CheckboxList.svelte new file mode 100644 index 000000000..08fbe8e39 --- /dev/null +++ b/src/lib/components/CheckboxList.svelte @@ -0,0 +1,47 @@ + + +
+ {#each options as option} + handleChange(e, option.value)}> + {#if option.icon} + + {/if} + {option.label} + + {/each} +
+ + \ No newline at end of file diff --git a/src/lib/components/CheckboxVirtualList.svelte b/src/lib/components/CheckboxVirtualList.svelte new file mode 100644 index 000000000..7351e3b31 --- /dev/null +++ b/src/lib/components/CheckboxVirtualList.svelte @@ -0,0 +1,56 @@ + + + +
+ {@const option = options[index]} + handleChange(e, option.value)}> + {#if option.icon} + + {/if} + {option.label} + +
+
+ + \ No newline at end of file diff --git a/src/lib/components/Input.svelte b/src/lib/components/Input.svelte new file mode 100644 index 000000000..26ccbba14 --- /dev/null +++ b/src/lib/components/Input.svelte @@ -0,0 +1,53 @@ + + +{#if label} +
+ {label} +
+{/if} +{#if type === 'text'} + +{:else if type === 'textarea'} +