-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
117 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,41 @@ | ||
import type { GroupedPageMap } from "kitbook"; | ||
import type { GroupedPageMap, KitbookSettings } from 'kitbook' | ||
import { pagesStore } from '../modules/hmrUpdatedModules' | ||
|
||
// import { derived } from 'svelte/store' | ||
|
||
/** | ||
* The default modules glob import used by Kitbook is this | ||
* ```js | ||
* const modules = import.meta.glob(['/src/**\/*.{md,svx,svelte,variants.ts}', '/README.md']); | ||
* ``` | ||
* `{md,svx}` = Kitbook Story Files | ||
* | ||
* | ||
* `{svelte,variants.ts}` = Automatically create a default Story for each component w/ variants automatically being populated by colocated files `*.variants.ts` files (Foo.svelte and Foo.variants.ts; +page.svelte and _page.variants.ts) | ||
*/ | ||
export function layoutLoad({ pages }: | ||
{ | ||
pages: GroupedPageMap, | ||
}) { | ||
export function layoutLoad({ pages: initialPages, settings, variantsTemplate }: | ||
{ | ||
pages: GroupedPageMap | ||
settings: KitbookSettings | ||
variantsTemplate: string | ||
}) { | ||
return async () => { | ||
if (!Object.keys(pages).length) throw new Error('No pages found. Did you import layoutLoad into your Kitbook layout.ts file and you have at least a README.md or one +page.svelte, +layout.svelte, *.svelte, *.md, or *.svx file in your project?') | ||
if (!Object.keys(initialPages).length) | ||
throw new Error('No pages found. Did you import layoutLoad into your Kitbook layout.ts file and you have at least a README.md or one +page.svelte, +layout.svelte, *.svelte, *.md, or *.svx file in your project?') | ||
|
||
// console.log({ initialPages }) | ||
// const pages = derived(pagesStore, ($pagesStore, set) => { | ||
// console.log({ $pagesStore }) | ||
// if (Object.keys($pagesStore).length) { | ||
// console.log('setting pages') | ||
// set($pagesStore) | ||
// } | ||
// }, initialPages) | ||
|
||
return { | ||
pages, | ||
}; | ||
pages: initialPages, | ||
pagesStore, | ||
settings, | ||
variantsTemplate, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,58 @@ | ||
<script lang="ts"> | ||
import { type SvelteComponent, getContext, setContext } from 'svelte' | ||
import { type SvelteComponent, setContext } from 'svelte' | ||
import type { Writable } from 'svelte/store' | ||
import type { GroupedPage, GroupedPageMap, LoadedModules, Variant } from '../kitbook-types' | ||
import ErrorBoundary from '../components/errorBoundary/ErrorBoundary.js' | ||
export let data: { | ||
pages: GroupedPageMap | ||
pagesStore: Writable<GroupedPageMap> | ||
page: GroupedPage | ||
pageKey: string | ||
loadedModules: LoadedModules | ||
storyId: string | ||
variant?: Variant<SvelteComponent> | ||
variantIdx?: string // string is better because it works as an index also and doesn't give false negative on '0' | ||
editedProps?: Record<string, any> | ||
// error?: string; | ||
} | ||
let { variant } = data | ||
const pagesStore = getContext<Writable<GroupedPageMap>>('pages-store') | ||
let { variant, pagesStore } = data | ||
$: page = $pagesStore[data.pageKey] | ||
$: if (page?.loadVariants?.loadModule && data.variantIdx) { | ||
page.loadVariants.loadModule().then((module) => { | ||
variant = module?.variants[data.variantIdx] || {} | ||
}).catch((error) => { | ||
console.error(error) | ||
}) | ||
page.loadVariants | ||
.loadModule() | ||
.then((module) => { | ||
variant = module?.variants[data.variantIdx] || {} | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
} | ||
const isStory = !!data.storyId | ||
if (isStory) { | ||
// `Story` components check the `sandboxId` context to know whether to show when in the sandbox - this is passed to the sandbox originally using the `storyId` query param in iframe url | ||
setContext<string>('sandboxId', data.storyId) | ||
setContext<Record<string, any>>('sandboxProps', data.editedProps || {}) | ||
} | ||
for (const { key, context } of data.variant?.contexts || []) | ||
setContext(key, context) | ||
for (const { key, context } of data.variant?.contexts || []) setContext(key, context) | ||
</script> | ||
|
||
<div class="absolute inset-0 overflow-auto"> | ||
{#if isStory} | ||
<div id="sandbox" style="display: contents;"> | ||
<svelte:component this={data.loadedModules.svx} /> | ||
<ErrorBoundary onError={console.error}> | ||
<div slot="before"> | ||
{#if Object.keys(variant.props).length === 0} | ||
<b>Kitbook tip</b>: Your component may need props passed in. Create a "{data.page | ||
?.name}.variants.ts" file with at least variant. In the future Kitbook will try to | ||
automatically supply default props, but until then they must be supplied manually. | ||
{/if} | ||
</div> | ||
{:else} | ||
<ErrorBoundary onError={console.error}> | ||
<div slot="before"> | ||
{#if Object.keys(variant.props).length === 0} | ||
<b>Kitbook tip</b>: Your component may need props passed in. Create a "{data.page?.name}.variants.ts" file with at least variant. In the future Kitbook will try to | ||
automatically supply default props, but until then they must be supplied manually. | ||
{#if variant?.slots} | ||
{@const defaultSlotContent = variant.slots.default} | ||
<svelte:component this={data.loadedModules.component} {...variant.props}> | ||
{#if typeof defaultSlotContent === 'string'} | ||
{@html defaultSlotContent} | ||
{:else} | ||
<svelte:component this={defaultSlotContent} /> | ||
{/if} | ||
</div> | ||
{#if variant?.slots} | ||
{@const defaultSlotContent = variant.slots.default} | ||
<svelte:component this={data.loadedModules.component} {...variant.props}> | ||
{#if typeof defaultSlotContent === 'string'} | ||
{@html defaultSlotContent} | ||
{:else} | ||
<svelte:component this={defaultSlotContent} /> | ||
{/if} | ||
</svelte:component> | ||
{:else} | ||
<svelte:component this={data.loadedModules.component} {...variant.props} /> | ||
{/if} | ||
</ErrorBoundary> | ||
{/if} | ||
</svelte:component> | ||
{:else} | ||
<svelte:component this={data.loadedModules.component} {...variant.props} /> | ||
{/if} | ||
</ErrorBoundary> | ||
</div> | ||
|
||
<style> | ||
#sandbox > :global(:not(.show-in-sandbox)) { | ||
display: none; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ const red = '\x1B[31m' | |
const bold = '\x1B[1m' | ||
const reset = '\x1B[0m' | ||
|
||
const LATEST_VERSION_WITH_ROUTES_UPDATE = '[email protected].48^' | ||
const LATEST_VERSION_WITH_ROUTES_UPDATE = '[email protected].50^' | ||
const FILE_WITH_NOTICE = '[...file]/+page.svelte' | ||
|
||
export function initKitbook({ routesDirectory, kitbookRoute }: KitbookSettings) { | ||
|
@@ -36,7 +36,7 @@ export function initKitbook({ routesDirectory, kitbookRoute }: KitbookSettings) | |
const src = 'node_modules/kitbook/dist/routes' | ||
const destination = kitbookDirectory | ||
fs.cpSync(src, destination, { recursive: true, filter: excludeDocFiles }) | ||
console.log(`${bold}${green}[Kitbook] Added Kitbook route files to ${kitbookDirectory}. Don't edit these. They will be automatically updated by Kitbook in future versions when needed.\n${reset}`) | ||
console.info(`${bold}${green}[Kitbook] Added Kitbook route files to ${kitbookDirectory}. Don't edit these. They will be automatically updated by Kitbook in future versions when needed.\n${reset}`) | ||
} | ||
catch (e) { | ||
console.error(`${bold}${red}[Kitbook] Error copying in needed routes: ${e}\n${reset}`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 3 additions & 13 deletions
16
packages/kitbook/src/lib/plugins/vite/virtual/virtual-module.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
declare module 'virtual:kitbook-modules' { | ||
import type { GroupedPageMap } from 'kitbook' | ||
declare module 'virtual:kitbook' { | ||
import type { GroupedPageMap, KitbookSettings, Variant } from 'kitbook' | ||
|
||
export const pages: GroupedPageMap | ||
} | ||
|
||
declare module 'virtual:kitbook-settings' { | ||
import type { KitbookSettings } from 'kitbook' | ||
|
||
export const settings: KitbookSettings | ||
} | ||
|
||
declare module 'virtual:kitbook-templates' { | ||
import type { KitbookSettings } from 'kitbook' | ||
|
||
export const variants: string | ||
export const variantsTemplate: string | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// @ts-ignore - virtual import | ||
import { pages, settings, variantsTemplate } from 'virtual:kitbook' | ||
import { layoutLoad } from 'kitbook' | ||
import { pages } from 'virtual:kitbook-modules' | ||
import type { LayoutLoad } from './$types' | ||
|
||
export const load = layoutLoad({ pages }) satisfies LayoutLoad | ||
export const load = layoutLoad({ pages, settings, variantsTemplate }) satisfies LayoutLoad |
Oops, something went wrong.
fcb6f8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
kitbook – ./packages/kitbook
kitbook-git-main-polylingual.vercel.app
kitbook.vercel.app
kitbook-polylingual.vercel.app
fcb6f8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
kitbook-template – ./packages/template
kitbook-template-git-main-polylingual.vercel.app
kitbook-template-polylingual.vercel.app
kitbook-template.vercel.app