<script lang="ts">
import { TanStackDevtools } from '@tanstack/svelte-devtools'
import {
HotkeysProvider,
createHotkey,
getFormatForDisplay,
getKeyboardLayout,
} from '@tanstack/svelte-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/svelte-hotkeys-devtools'
import OwnLayout from './OwnLayout.svelte'
const formatForDisplay = getFormatForDisplay()
const keyboard = getKeyboardLayout()
let germanFired = $state('none')
let everyLayoutFired = $state('none')
let physicalFired = $state('none')
createHotkey('Mod+Ö', () => (germanFired = 'Toggle comment'))
createHotkey('Mod+Shift+Ä', () => (germanFired = 'Add annotation'))
createHotkey('ß', () => (germanFired = 'Toggle sidebar'))
createHotkey('§', () => (germanFired = 'Show sections'))
createHotkey('Mod+K', () => (everyLayoutFired = 'Open command palette'))
createHotkey('Mod+Enter', () => (everyLayoutFired = 'Send'))
createHotkey('Escape', () => (everyLayoutFired = 'Dismiss'))
createHotkey('[KeyW]', () => (physicalFired = 'Move up'))
createHotkey('[KeyA]', () => (physicalFired = 'Move left'))
createHotkey('[KeyS]', () => (physicalFired = 'Move down'))
createHotkey('[KeyD]', () => (physicalFired = 'Move right'))
createHotkey('Mod+[KeyZ]', () => (physicalFired = 'Undo'))
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
</script>
<div class="app">
<header>
<h1>Keyboard Layouts</h1>
<p>
Type hotkeys with keys from other layouts, match keys by position, and
label them with the characters on the user's keyboard.
</p>
</header>
<main>
<section class="demo-section">
<h2>German keys</h2>
<p class="note">Switch your keyboard layout to German to press these.</p>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Ö')}</kbd> Toggle comment
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Shift+Ä')}</kbd> Add annotation
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('ß')}</kbd> Toggle sidebar
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('§')}</kbd> Show sections
</div>
</div>
<p role="status">Last fired: {germanFired}</p>
</section>
<section class="demo-section">
<h2>Keys on every layout</h2>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+K')}</kbd> Open command palette
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Enter')}</kbd> Send
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Escape')}</kbd> Dismiss
</div>
</div>
<p role="status">Last fired: {everyLayoutFired}</p>
</section>
<section class="demo-section">
<h2>Physical keys</h2>
<p class="note">
These labels show the characters on your keyboard. On French AZERTY, the
movement keys read Z Q S D.
</p>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('[KeyW]')}</kbd> Move up
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('[KeyA]')}</kbd> Move left
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('[KeyS]')}</kbd> Move down
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('[KeyD]')}</kbd> Move right
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+[KeyZ]')}</kbd> Undo
</div>
</div>
<p role="status">Last fired: {physicalFired}</p>
</section>
<section class="demo-section">
<h2>On-screen keyboard</h2>
<p class="note">
{keyboard.layout
? 'The top letter row of your keyboard layout.'
: 'No layout from the browser, so this is the US top row.'}
</p>
<p role="status">
Top row:
{#each ['KeyQ', 'KeyW', 'KeyE', 'KeyR', 'KeyT', 'KeyY', 'KeyU', 'KeyI', 'KeyO', 'KeyP'] as code (code)}
<kbd
>{(keyboard.layout?.get(code) ?? code.slice(3)).toUpperCase()}</kbd
>{' '}
{/each}
</p>
</section>
<HotkeysProvider defaultOptions={{ keyboardLayout: false }}>
<OwnLayout />
</HotkeysProvider>
</main>
</div>
{#if import.meta.env.DEV}
<TanStackDevtools plugins={devtoolsPlugins} />
{/if}