-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use as const to fix TS errors * add FileDetails.svelte * add FileDetails.test.ts * remove pr trigger from gh-pages.yml workflow
- Loading branch information
Showing
8 changed files
with
212 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
name: GitHub Pages | ||
|
||
on: | ||
pull_request: | ||
branches: [main] | ||
push: | ||
branches: [main] | ||
workflow_dispatch: | ||
|
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<script lang="ts"> | ||
import hljs from 'highlight.js/lib/common' | ||
import 'highlight.js/styles/vs2015.css' | ||
export let files: { | ||
title: string | ||
content: string | ||
language?: string | ||
node?: HTMLDetailsElement | null | ||
}[] = [] | ||
export let toggle_all_btn_title: string = 'Toggle all' | ||
export let default_lang: string = 'typescript' | ||
function toggle_all() { | ||
const any_open = files.some((file) => file.node?.open) | ||
for (const file of files) { | ||
if (!file.node) continue | ||
file.node.open = !any_open | ||
} | ||
files = [...files] // trigger reactivity | ||
} | ||
</script> | ||
|
||
{#if files?.length > 1} | ||
<button on:click={toggle_all} title={toggle_all_btn_title}> | ||
{files.some((file) => file.node?.open) ? 'Close' : 'Open'} all | ||
</button> | ||
{/if} | ||
|
||
<ol> | ||
{#each files as file, idx (file.title)} | ||
{@const { title, content, language = default_lang } = file} | ||
<li> | ||
<details bind:this={file.node}> | ||
<summary> | ||
<slot name="title" {idx} {...file}> | ||
<code>{title.split('/').at(-1)}</code> | ||
</slot> | ||
</summary> | ||
|
||
<pre><code>{@html hljs.highlight(content, { language }).value}</code></pre> | ||
</details> | ||
</li> | ||
{/each} | ||
</ol> | ||
|
||
<style> | ||
button { | ||
float: right; | ||
} | ||
ol { | ||
padding: 0 1em; | ||
} | ||
ol > li { | ||
margin: 1ex 0; | ||
} | ||
</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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { FileDetails } from '$lib' | ||
import { tick } from 'svelte' | ||
import { expect, test } from 'vitest' | ||
import { doc_query } from '.' | ||
|
||
const files = [ | ||
{ title: `file1`, content: `content1` }, | ||
{ title: `file2`, content: `content2` }, | ||
] | ||
const click = new MouseEvent(`click`) | ||
|
||
test(`FileDetails renders all files passed`, async () => { | ||
new FileDetails({ target: document.body, props: { files } }) | ||
await tick() | ||
|
||
const details = document.querySelectorAll(`li > details`) | ||
expect(details.length).toBe(files.length) | ||
|
||
// check items are wrapped in ordered list | ||
const ol = doc_query(`ol`) | ||
expect(ol.children.length).toBe(files.length) | ||
}) | ||
|
||
test(`FileDetails renders file titles and contents`, async () => { | ||
new FileDetails({ target: document.body, props: { files } }) | ||
await tick() | ||
|
||
const titles = document.querySelectorAll(`summary`) | ||
expect(titles.length).toBe(files.length) | ||
|
||
const contents = document.querySelectorAll(`pre > code`) | ||
|
||
for (const [idx, src] of contents.entries()) { | ||
expect(src.textContent).toBe(files[idx].content) | ||
} | ||
}) | ||
|
||
test(`FileDetails opens and closes files when the toggle button is clicked`, async () => { | ||
const files = [ | ||
{ title: `file1`, content: `content1`, open: false }, | ||
{ title: `file2`, content: `content2`, open: false }, | ||
] | ||
|
||
new FileDetails({ | ||
target: document.body, | ||
props: { files }, | ||
}) | ||
await tick() | ||
|
||
const details = document.querySelectorAll(`details`) | ||
for (const detail of details) { | ||
expect(detail.open).toBe(false) | ||
} | ||
|
||
const btn = doc_query(`button`) | ||
btn.dispatchEvent(click) | ||
await tick() | ||
|
||
for (const detail of details) { | ||
expect(detail.open).toBe(true) | ||
} | ||
|
||
btn.dispatchEvent(click) | ||
await tick() | ||
|
||
for (const detail of details) { | ||
expect(detail.open).toBe(false) | ||
} | ||
}) | ||
|
||
test(`toggle all button opens and closes all details`, async () => { | ||
const files = [ | ||
{ title: `file1`, content: `content1`, open: false }, | ||
{ title: `file2`, content: `content2`, open: false }, | ||
{ title: `file3`, content: `content3`, open: false }, | ||
] | ||
|
||
const toggle_all_btn_title = `toggle all` | ||
new FileDetails({ | ||
target: document.body, | ||
props: { files, toggle_all_btn_title }, | ||
}) | ||
|
||
await tick() | ||
|
||
const details = document.querySelectorAll(`details`) | ||
|
||
const btn = doc_query(`button[title='${toggle_all_btn_title}']`) | ||
|
||
// open all details | ||
btn.dispatchEvent(click) | ||
await tick() | ||
|
||
// all details elements should now be open | ||
for (const detail of details) { | ||
expect(detail.open).toBe(true) | ||
} | ||
|
||
// Click the toggle button again to close all details | ||
btn.dispatchEvent(click) | ||
await tick() | ||
|
||
// all details elements should now be closed | ||
for (const detail of details) { | ||
expect(detail.open).toBe(false) | ||
} | ||
|
||
// Open some of the details | ||
details[0].open = true | ||
details[1].open = true | ||
await tick() | ||
|
||
// Click the toggle button to close all details | ||
btn.dispatchEvent(click) | ||
await tick() | ||
|
||
// All details elements should now be closed | ||
for (const detail of details) { | ||
expect(detail.open).toBe(false) | ||
} | ||
}) |
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