Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjennings committed Nov 23, 2023
1 parent 476abc3 commit 5e07901
Show file tree
Hide file tree
Showing 25 changed files with 355 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default defineConfig({
title: 'Svelte Pixi',
customCss: ['./src/tailwind.css'],
expressiveCode: false,

social: {
github: 'https://github.com/mattjennings/svelte-pixi',
},
Expand Down
3 changes: 3 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
"typescript": "^5.2.0"
},
"devDependencies": {
"@types/dashify": "^1.0.3",
"dashify": "^2.0.0",
"estree-util-visit": "^2.0.0",
"magic-string": "^0.30.5",
"marked": "^10.0.0",
"recast": "^0.23.4",
"svelte-pixi": "workspace:../dist",
"unist-util-visit-parents": "^6.0.1",
Expand Down
240 changes: 240 additions & 0 deletions docs/src/components/ComponentAPI.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
---
import { ComponentParser } from 'sveld'
import path from 'path'
import sveltePreprocess from 'svelte-preprocess'
import * as svelte from 'svelte/compiler'
import fs from 'fs'
import dashify from 'dashify'
import { Code } from 'astro:components'
import { marked } from 'marked'
const __dirname = path.dirname(new URL(import.meta.url).pathname)
const componentPath = path.resolve(
__dirname,
'../../../src/lib',
Astro.props.component,
)
const src = await fs.promises.readFile(componentPath, 'utf-8')
const filename = path.basename(componentPath, '.svelte')
const { code } = await svelte.preprocess(src, sveltePreprocess(), {
filename,
})
const data = new ComponentParser({
verbose: false,
}).parseSvelteComponent(code, {
filePath: filename,
moduleName: filename,
})
const props = data.props
.sort((a, b) => a.name.localeCompare(b.name))
.filter((prop) => !prop.name.startsWith('get'))
const slots = data.slots.sort((a, b) => {
if (a.default) return -1
if (b.default) return 1
return a.name!.localeCompare(b.name!)
})
const events = data.events.sort((a, b) => a.name.localeCompare(b.name))
const typedefs = data.typedefs.sort((a, b) => a.name.localeCompare(b.name))
const rest_props = data.rest_props
function formatCodeType(type: any) {
if (!type || type === 'undefined') return ''
return type.replace(
/[{}]/g,
(c: string) => ({ '{': '{', '}': '}' })[c],
)
// .replace(/\|/g, '|')
}
function formatCodeValue(value: any) {
if (!value) {
return ''
}
return value
.replace(/[{}]/g, (c: string) => ({ '{': '{', '}': '}' })[c])
.replace(/`/g, '\\`')
.replace(/\|/g, '|')
}
function formatDescription(description: string) {
if (
description === undefined ||
description === 'undefined' ||
description.trim().length === 0
)
return ''
return marked.parse(description, {
breaks: true,
})
}
function formatEventDetail(detail: any) {
if (detail === undefined || detail === 'undefined') return ''
return formatCodeType(detail.replace(/\n/g, ' '))
}
function getTypeDefs(def: any) {
if (def.typedefs.length === 0) return ''
return def.typedefs.map((typedef: any) => typedef.ts).join('\n\n')
}
---

<style>
td {
@apply text-sm;
}
td code {
@apply !mt-0;
}

table div {
@apply !mt-0;
}
</style>

{
typedefs.length > 0 && (
<>
<h3>Types</h3>
<div class="text-sm md:text-base">
<Code code={getTypeDefs(data)} lang="typescript" />
</div>
</>
)
}

{
props.length > 0 && (
<>
<h3 id="props">
<a href="#props">Props</a>
</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{props.map((prop) => (
<tr>
<td class="align-top">
<div class="pt-2">{prop.name}</div>
</td>
<td>
<div class="flex gap-1 pt-1">
<div class="inline-flex flex-wrap gap-1">
{prop.type &&
prop.type
.split(/\s?\|\s?/g)
.map((type) => formatCodeType(type))
.filter(Boolean)
.map((type) => (
<code title="Prop type" class="whitespace-nowrap">
{type}
</code>
))}
</div>
{prop.value && prop.kind !== 'function' && (
<div class="inline-flex">
={' '}
<code
title="Default value"
class="ml-1 whitespace-nowrap"
>
{formatCodeValue(prop.value)}
</code>
</div>
)}
</div>
<p
class="align-top"
set:html={
'description' in prop
? formatDescription(prop.description!)
: ''
}
/>
</td>
</tr>
))}
</tbody>
</table>
{rest_props && (
<p>
Additional props are passed on to{' '}
<a href={`/api/components/${dashify(rest_props.name)}`}>
{rest_props.name}
</a>
</p>
)}
</>
)
}

{
slots.length > 0 && (
<>
<h3 id="slots">
<a href="#slots">Slots</a>
</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Props</th>
<th>Fallback</th>
</tr>
</thead>
<tbody>
{slots.map((slot) => (
<tr>
<td>{slot.name === '__default__' ? 'default' : slot.name}</td>
<td>{slot.slot_props && <code>{slot.slot_props}</code>}</td>
<td>{slot.fallback && <code>{slot.fallback}</code>}</td>
</tr>
))}
</tbody>
</table>
</>
)
}

{
events.length > 0 && (
<>
<h3 id="events">
<a href="#events">Events</a>
</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
{events.map((event) => (
<tr>
<td>{event.name}</td>
<td>
{event.type && <code>{formatCodeValue(event.type)}</code>}
</td>
<td>
{'detail' in event && event.detail && (
<code>{formatEventDetail(event.detail)}</code>
)}
</td>
</tr>
))}
</tbody>
</table>
</>
)
}
7 changes: 6 additions & 1 deletion docs/src/content/docs/api/components/animated-sprite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: AnimatedSprite
pixi: https://pixijs.download/release/docs/PIXI.AnimatedSprite.html
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

A simple way to display an animation depicted by a list of textures.

Expand Down Expand Up @@ -57,4 +58,8 @@ I recommend using spritesheets created by TexturePacker ([they have a great tuto
scale={3}
on:loop={changeAnimation}
/>
```
```

## API

<ComponentAPI component="AnimatedSprite.svelte" />
7 changes: 5 additions & 2 deletions docs/src/content/docs/api/components/application.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
title: Application
pixi: https://pixijs.download/release/docs/PIXI.Application.html
---


import ComponentAPI from '$lib/components/ComponentAPI.astro'

Just like `PIXI.Application`, it sets up the [Renderer](/docs/components/renderer), [Ticker](/docs/components/ticker) and root [Container](/docs/components/container). If you wish, you can manually render those components instead.

Expand Down Expand Up @@ -85,3 +84,7 @@ If you want to customize the element that the canvas is rendered into, you can u
```

See [Render on Demand](/docs/advanced/optimizing-performance#render-on-demand) for more information.

## API

<ComponentAPI component="Application.svelte" />
5 changes: 5 additions & 0 deletions docs/src/content/docs/api/components/assets-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: AssetsLoader
sidebar:
badge: 'New'
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

Creates and loads assets using [PIXI.Assets](https://pixijs.download/release/docs/PIXI.Assets.html) by creating a bundle from the `assets` prop.

Expand Down Expand Up @@ -57,3 +58,7 @@ Note that `PIXI.Assets` can only be initialized once.
</Application>
{/await}
```

## API

<ComponentAPI component="AssetsLoader.svelte" />
4 changes: 4 additions & 0 deletions docs/src/content/docs/api/components/bitmap-text.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: BitmapText
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

Renders a line or multiple lines of text using bitmap font.

Expand Down Expand Up @@ -34,3 +35,6 @@ A BitmapText can only be created when the font is loaded.
/>
</AssetsLoader>
```
## API

<ComponentAPI component="BitmapText.svelte" />
5 changes: 5 additions & 0 deletions docs/src/content/docs/api/components/container.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Container
pixi: https://pixijs.download/release/docs/PIXI.Container.html
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

A Container is the base component for all components that render to the screen. It can be used on its own, but components such as Sprite or Graphics will be composed of Containers (and will provide context for [getContainer](/docs/utilities/get-container))

Expand All @@ -19,3 +20,7 @@ When child components are rendered inside, their coordinates become local to the
<Text text="World" x={-10} y={0} style={{ fill: 'white' }} />
</Container>
```

## API

<ComponentAPI component="Container.svelte" />
5 changes: 5 additions & 0 deletions docs/src/content/docs/api/components/graphics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Graphics
pixi: https://pixijs.download/release/docs/PIXI.Graphics.html
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

Graphics can be used for any kind of drawing. Use the `draw` prop to interact with the PIXI.Graphics API.

Expand Down Expand Up @@ -31,3 +32,7 @@ Graphics can be used for any kind of drawing. Use the `draw` prop to interact wi
}}
/>
```

## API

<ComponentAPI component="Graphics.svelte" />
5 changes: 5 additions & 0 deletions docs/src/content/docs/api/components/html-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pixi: https://pixijs.download/release/docs/HTMLText.html
sidebar:
badge: 'New'
---
import ComponentAPI from '$lib/components/ComponentAPI.astro'

Renders text with support for styling via HTML tags. The rendering can vary across browsers and platforms.

Expand All @@ -22,3 +23,7 @@ Renders text with support for styling via HTML tags. The rendering can vary acro
}}
/>
```

## API

<ComponentAPI component="HTMLText.svelte" />
Loading

0 comments on commit 5e07901

Please sign in to comment.