Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
<script lang="ts">
|
||||
import IconButton from "$lib/ui/IconButton.svelte";
|
||||
import { workspaceSearch, type SearchHit } from "$lib/workspace/api";
|
||||
import type { TreeEntry } from "$lib/workspace/types";
|
||||
|
||||
type Props = {
|
||||
onOpen: (entry: TreeEntry, line: number) => void;
|
||||
searchRequest?: { text: string; id: number } | null;
|
||||
};
|
||||
|
||||
let { onOpen, searchRequest }: Props = $props();
|
||||
let query = $state("");
|
||||
let hits = $state<SearchHit[]>([]);
|
||||
let error = $state<string | null>(null);
|
||||
|
||||
let generation = 0;
|
||||
$effect(() => { if (searchRequest) { query = searchRequest.text; void run(searchRequest.text); } });
|
||||
|
||||
async function run(value = query) {
|
||||
const version = ++generation;
|
||||
const q = value.trim();
|
||||
if (!q) {
|
||||
hits = [];
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const results = await workspaceSearch(q);
|
||||
if (version !== generation) return;
|
||||
hits = results;
|
||||
error = null;
|
||||
} catch (err) {
|
||||
if (version !== generation) return;
|
||||
error = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="pane">
|
||||
<div class="pane-label">Search</div>
|
||||
<form
|
||||
class="bar"
|
||||
onsubmit={(event) => {
|
||||
event.preventDefault();
|
||||
void run();
|
||||
}}
|
||||
>
|
||||
<input bind:value={query} placeholder="Search files and notes" />
|
||||
<IconButton icon="search" label="Search workspace" type="submit" />
|
||||
</form>
|
||||
{#if error}
|
||||
<p class="error">{error}</p>
|
||||
{/if}
|
||||
<ul>
|
||||
{#each hits as hit (hit.rel_path + hit.line + hit.text)}
|
||||
<li>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() =>
|
||||
onOpen(
|
||||
{
|
||||
name: hit.rel_path.split("/").pop() ?? hit.rel_path,
|
||||
rel_path: hit.rel_path,
|
||||
kind: "file",
|
||||
},
|
||||
hit.line,
|
||||
)}
|
||||
>
|
||||
<span class="path">{hit.rel_path}:{hit.line}</span>
|
||||
<span class="text">{hit.text}</span>
|
||||
</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.pane {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.pane-label {
|
||||
padding: 6px 10px;
|
||||
font-size: 11px;
|
||||
letter-spacing: 0.06em;
|
||||
text-transform: uppercase;
|
||||
color: var(--rs-text-dim);
|
||||
border-bottom: 1px solid var(--rs-border);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.bar {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
border-radius: 5px;
|
||||
border: 1px solid var(--rs-border);
|
||||
background: var(--rs-bg);
|
||||
padding: 4px 8px;
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0 0 12px;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
border-radius: var(--rs-control-radius);
|
||||
border: 0;
|
||||
background: transparent;
|
||||
text-align: left;
|
||||
padding: 6px 10px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: var(--rs-bg-active);
|
||||
}
|
||||
|
||||
.path {
|
||||
display: block;
|
||||
color: var(--rs-accent);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.text {
|
||||
display: block;
|
||||
color: var(--rs-text-dim);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.error {
|
||||
margin: 8px 10px;
|
||||
color: var(--rs-error);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user