Skip to content

Commit

Permalink
Fix error when missing context value
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Nov 27, 2024
1 parent 5a7c5d7 commit 765f04b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
47 changes: 25 additions & 22 deletions src/lib/components/common/HighlightGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
export let open = false;
export let highlights: [string, H][] = [];
export let getHref: (id: string) => string;
export let showAll = false;
let clientWidth = 1000;
$: isSmall = clientWidth < remToPx(27);
Expand Down Expand Up @@ -46,28 +47,30 @@
{$_("search.matchingResults", { values: { n: highlights.length } })}
</slot>
</div>
<div class="right">
<Button
minW={false}
size="small"
ghost
on:click={collapseAll}
title={$_("search.collapseAll")}
>
<Fold16 height={12} width={12} />
{#if !isSmall}{$_("search.collapseAll")}{/if}
</Button>
<Button
minW={false}
size="small"
ghost
on:click={expandAll}
title={$_("search.expandAll")}
>
<Unfold16 height={12} width={12} />
{#if !isSmall}{$_("search.expandAll")}{/if}
</Button>
</div>
{#if showAll}
<div class="right">
<Button
minW={false}
size="small"
ghost
on:click={collapseAll}
title={$_("search.collapseAll")}
>
<Fold16 height={12} width={12} />
{#if !isSmall}{$_("search.collapseAll")}{/if}
</Button>
<Button
minW={false}
size="small"
ghost
on:click={expandAll}
title={$_("search.expandAll")}
>
<Unfold16 height={12} width={12} />
{#if !isSmall}{$_("search.expandAll")}{/if}
</Button>
</div>
{/if}
</summary>
<ul>
{#each highlights as [id, highlight]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
</svelte:fragment>
</HighlightGroup>
</Story>

<Story name="With All Controls">
<HighlightGroup {highlights} {getHref} showAll>
<svelte:fragment let:id let:highlight>
<Highlight title={id} segments={highlight} />
</svelte:fragment>
</HighlightGroup>
</Story>
5 changes: 3 additions & 2 deletions src/lib/components/documents/NoteHighlights.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
export let open = true;
const { subscribe } =
getContext<Writable<{ allOpen: boolean }>>("highlightState");
$: subscribe((state) => {
getContext<Writable<{ allOpen: boolean }>>("highlightState") ?? {};
$: subscribe?.((state) => {
open = state.allOpen;
});
Expand All @@ -49,6 +49,7 @@
{highlights}
getHref={noteHref}
bind:open
showAll={Boolean(subscribe)}
on:collapseAll
on:expandAll
>
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/documents/PageHighlights.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
export let open = true;
const { subscribe } =
getContext<Writable<{ allOpen: boolean }>>("highlightState");
$: subscribe((state) => {
getContext<Writable<{ allOpen: boolean }>>("highlightState") ?? {};
$: subscribe?.((state) => {
open = state.allOpen;
});
$: highlights = Object.entries(document.highlights ?? {});
Expand All @@ -43,6 +43,7 @@
{highlights}
getHref={pageHref}
bind:open
showAll={Boolean(subscribe)}
on:collapseAll
on:expandAll
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script context="module" lang="ts">
import type { Document } from "$lib/api/types";
import { setContext } from "svelte";
import { writable } from "svelte/store";
import { Story } from "@storybook/addon-svelte-csf";
import type { Document } from "$lib/api/types";
import NoteHighlights from "../NoteHighlights.svelte";
import search from "@/test/fixtures/documents/search-highlight.json";
Expand All @@ -15,6 +18,13 @@
tags: ["autodocs"],
parameters: { layout: "fullscreen" },
};
setContext(
"highlightState",
writable({
allOpen: true,
}),
);
</script>

<Story name="closed">
Expand Down
13 changes: 11 additions & 2 deletions src/lib/components/documents/stories/PageHighlights.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script context="module" lang="ts">
import type { Document } from "$lib/api/types";
import { setContext } from "svelte";
import { writable } from "svelte/store";
import { Story } from "@storybook/addon-svelte-csf";
import type { Document } from "$lib/api/types";
import PageHighlights from "../PageHighlights.svelte";
import search from "@/test/fixtures/documents/search-highlight.json";
Expand All @@ -17,6 +19,13 @@
tags: ["autodocs"],
parameters: { layout: "fullscreen" },
};
setContext(
"highlightState",
writable({
allOpen: true,
}),
);
</script>

<Story name="closed">
Expand Down

0 comments on commit 765f04b

Please sign in to comment.