Improve workspace navigation, search, and tab actions
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { tick } from "svelte";
|
||||
import type { OutlineItem } from "$lib/workspace/outline";
|
||||
import Icon from "$lib/ui/Icon.svelte";
|
||||
import IconButton from "$lib/ui/IconButton.svelte";
|
||||
import "$lib/ui/find.css";
|
||||
|
||||
type Props = {
|
||||
items: OutlineItem[];
|
||||
@@ -7,22 +11,73 @@
|
||||
};
|
||||
|
||||
let { items, onJump }: Props = $props();
|
||||
let searchOpen = $state(false);
|
||||
let query = $state("");
|
||||
let pane: HTMLElement;
|
||||
let searchInput = $state<HTMLInputElement>();
|
||||
const visibleItems = $derived(items.filter(item => item.text.toLocaleLowerCase().includes(query.toLocaleLowerCase())));
|
||||
|
||||
async function openSearch() {
|
||||
searchOpen = true;
|
||||
await tick();
|
||||
searchInput?.focus();
|
||||
searchInput?.select();
|
||||
}
|
||||
|
||||
function closeSearch() {
|
||||
searchOpen = false;
|
||||
query = "";
|
||||
pane.focus();
|
||||
}
|
||||
|
||||
function keydown(event: KeyboardEvent) {
|
||||
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && event.key.toLowerCase() === "f") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
void openSearch();
|
||||
} else if (event.key === "Escape" && searchOpen) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
closeSearch();
|
||||
} else if (event.key === "Enter" && event.target === searchInput && visibleItems[0]) {
|
||||
event.preventDefault();
|
||||
onJump(visibleItems[0].line);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="outline">
|
||||
<div class="pane-label">Outline</div>
|
||||
<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions (The outline accepts focus for pane-local search shortcuts.) -->
|
||||
<section class="outline" aria-label="Outline" tabindex="0" bind:this={pane} onkeydown={keydown}>
|
||||
<div class="pane-label">
|
||||
<span>Outline</span>
|
||||
<IconButton icon="search" label="Find in outline (Ctrl+F)" active={searchOpen} onclick={() => void openSearch()} />
|
||||
</div>
|
||||
{#if searchOpen}
|
||||
<div class="rs-find-bar outline-search">
|
||||
<div class="rs-find-row">
|
||||
<div class="rs-find-field">
|
||||
<Icon icon="search" />
|
||||
<input bind:this={searchInput} bind:value={query} aria-label="Find in outline" placeholder="Find in outline" />
|
||||
</div>
|
||||
<IconButton icon="close" label="Close outline find (Escape)" onclick={closeSearch} />
|
||||
</div>
|
||||
<span class="match-count" role="status">{visibleItems.length} of {items.length} headings or symbols</span>
|
||||
</div>
|
||||
{/if}
|
||||
{#if items.length === 0}
|
||||
<p class="placeholder">No headings or symbols in this file.</p>
|
||||
{:else if visibleItems.length === 0}
|
||||
<p class="placeholder">No matching headings or symbols.</p>
|
||||
{:else}
|
||||
<ul>
|
||||
{#each items as item (item.line + item.text)}
|
||||
{#each visibleItems as item (item.line + item.text)}
|
||||
<li style:padding-left="{8 + (item.level - 1) * 12}px">
|
||||
<button type="button" onclick={() => onJump(item.line)}>{item.text}</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<style>
|
||||
.outline {
|
||||
@@ -33,7 +88,10 @@
|
||||
}
|
||||
|
||||
.pane-label {
|
||||
padding: 6px 10px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 2px 8px 2px 10px;
|
||||
font-size: 11px;
|
||||
letter-spacing: 0.06em;
|
||||
text-transform: uppercase;
|
||||
@@ -42,6 +100,11 @@
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.outline-search { border-bottom: 1px solid var(--rs-border); }
|
||||
.outline-search :global(.rs-find-row) { flex-wrap: nowrap; }
|
||||
.outline-search :global(.rs-find-field) { flex-basis: 0; }
|
||||
.match-count { color: var(--rs-text-dim); font-size: 11px; }
|
||||
|
||||
.placeholder {
|
||||
margin: 12px;
|
||||
color: var(--rs-text-dim);
|
||||
@@ -54,7 +117,7 @@
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
button {
|
||||
li button {
|
||||
width: 100%;
|
||||
border-radius: var(--rs-control-radius);
|
||||
border: 0;
|
||||
@@ -67,7 +130,7 @@
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
li button:hover {
|
||||
background: var(--rs-bg-active);
|
||||
color: var(--rs-text-strong);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user