-
Notifications
You must be signed in to change notification settings - Fork 268
/
PreferencesRendering.svelte
114 lines (106 loc) · 3.83 KB
/
PreferencesRendering.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<script lang="ts">
import type { IConfigurationPropertyRecordedSchema } from '../../../../main/src/plugin/configuration-registry';
import PreferencesRenderingItem from './PreferencesRenderingItem.svelte';
import SettingsPage from './SettingsPage.svelte';
import Route from '../../Route.svelte';
import { isDefaultScope, isPropertyValidInContext } from './Util';
import type { ContextUI } from '../context/context';
import { context } from '/@/stores/context';
import { onDestroy, onMount } from 'svelte';
import { type Unsubscriber } from 'svelte/store';
export let properties: IConfigurationPropertyRecordedSchema[] = [];
export let key: string;
// Context variables
let contextsUnsubscribe: Unsubscriber;
let globalContext: ContextUI;
// Search and matching records
let updateSearchValueTimeout: NodeJS.Timeout;
let matchingRecords: Map<string, IConfigurationPropertyRecordedSchema[]>;
export let searchValue = '';
$: searchValue;
$: matchingRecords = properties
.filter(property => property.parentId.startsWith(key) && isDefaultScope(property.scope) && !property.hidden)
.filter(property => isPropertyValidInContext(property.when, globalContext))
.filter(
property =>
!searchValue ||
matchValue(property.title, searchValue) ||
(property.description && matchValue(property.description, searchValue)) ||
(property.markdownDescription && matchValue(property.markdownDescription, searchValue)),
)
.reduce((map, property) => {
if (!map.has(property.parentId)) {
map.set(property.parentId, []);
}
map.get(property.parentId)?.push(property);
return map;
}, new Map<string, IConfigurationPropertyRecordedSchema[]>());
onMount(async () => {
contextsUnsubscribe = context.subscribe(value => {
globalContext = value;
});
});
onDestroy(() => {
if (contextsUnsubscribe) {
contextsUnsubscribe();
}
});
function matchValue(text: string, searchValue: string): boolean {
if (!text) {
return false;
}
return text.toLowerCase().indexOf(searchValue.toLowerCase()) >= 0;
}
function updateSearchValue(event: any) {
clearTimeout(updateSearchValueTimeout);
updateSearchValueTimeout = setTimeout(() => (searchValue = event.target.value), 500);
}
</script>
<Route path="/" breadcrumb="{key}">
<SettingsPage title="Preferences">
<div class="mt-4" slot="header">
<div
class="flex items-center text-gray-700 rounded-sm rounded-lg border-2 border-charcoal-900 focus-within:border-violet-500">
<input
on:input="{e => updateSearchValue(e)}"
class="w-full bg-charcoal-900 py-1 px-3 outline-0 text-sm"
name="search"
type="text"
placeholder="Search preferences"
id="input-search"
aria-label="input-search" />
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-4 h-4 mr-2 bg-charcoal-900"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
</div>
</div>
<div class="flex flex-col space-y-5">
{#if matchingRecords.size === 0}
<div>No Settings Found</div>
{:else}
{#each [...matchingRecords.keys()].sort((a, b) => a.localeCompare(b)) as configSection}
{@const records = matchingRecords.get(configSection)}
{#if records}
<div>
<div class="first-letter:uppercase">{records.at(0)?.title}</div>
{#each records as configItem}
<div class="bg-charcoal-600 rounded-md mt-2 ml-2">
<PreferencesRenderingItem record="{configItem}" />
</div>
{/each}
</div>
{/if}
{/each}
{/if}
</div>
</SettingsPage>
</Route>