Skip to content

Commit f5a7f6b

Browse files
Mark "ID field" on content and schema
1 parent bd81d29 commit f5a7f6b

14 files changed

Lines changed: 158 additions & 32 deletions

File tree

src/lib/components/app/button/content-button.svelte

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import Trash2 from 'lucide-svelte/icons/trash-2';
1010
1111
// Types
12-
import type { Content } from '$lib/types/types.svelte';
12+
import type { Content, Schema } from '$lib/types/types.svelte';
1313
1414
// Utilities
15-
import { cap, getQueryString } from '$lib/utilities/utilities.svelte';
15+
import { cap, checkHasIDField, getQueryString } from '$lib/utilities/utilities.svelte';
1616
1717
// Constants and locales
1818
import * as m from '$lib/paraglide/messages.js';
@@ -21,12 +21,14 @@
2121
import ConfirmToRemove from '$lib/components/app/popover/confirm-to-remove.svelte';
2222
import { deleteContentById } from '$lib/db/db';
2323
import { invalidateAll } from '$app/navigation';
24+
import IdCardLanyard from 'lucide-svelte/icons/id-card-lanyard';
2425
2526
interface Props {
2627
content: Content;
28+
schemas: Schema[];
2729
onclick: (_viewDocumentAs?: 'document' | 'documentsGroup') => void;
2830
}
29-
let { content, onclick }: Props = $props();
31+
let { content, schemas, onclick }: Props = $props();
3032
let badgeType = $derived(
3133
content.type === 'collection' ? 'string' : content.type === 'object' ? 'number' : 'boolean'
3234
);
@@ -75,7 +77,19 @@
7577
</Tooltip.Content>
7678
</Tooltip.Root>
7779
<div class="flex flex-col overflow-hidden text-left">
78-
<div class="whitespace-nowrap">{content.name}</div>
80+
<div class="flex items-center gap-1 whitespace-nowrap">
81+
{#if checkHasIDField($state.snapshot(schemas.find((schema) => schema.id === content.schemaId)!))}
82+
<Tooltip.Root>
83+
<Tooltip.Trigger>
84+
<IdCardLanyard class="h-5 w-5" />
85+
</Tooltip.Trigger>
86+
<Tooltip.Content side="bottom">
87+
<!-- TODO locales -->
88+
This content's schema contains ID field(s)
89+
</Tooltip.Content>
90+
</Tooltip.Root>
91+
{/if}{content.name}
92+
</div>
7993
<div class="whitespace-nowrap text-xs text-muted-foreground">
8094
{content.description}
8195
</div>

src/lib/components/app/content-table/content-table.svelte

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import type { FilterObject, JSONObject, Content } from '$lib/types/types.svelte';
2828
2929
// Utilities
30-
import { cap, getLowCMSTypeByConfig, includesAny } from '$lib/utilities/utilities.svelte';
30+
import {
31+
cap,
32+
checkHasIDField,
33+
getLowCMSTypeByConfig,
34+
includesAny
35+
} from '$lib/utilities/utilities.svelte';
3136
3237
// Constants and locales
3338
import * as m from '$lib/paraglide/messages.js';
@@ -56,7 +61,20 @@
5661
})
5762
: []
5863
);
59-
let entries = $derived(Object.entries(schema?.properties ?? {})) as [string, JSONSchema7][];
64+
let entries = $derived(
65+
Object.entries(schema?.properties ?? {}).sort((a, b) => {
66+
const isAIDField = checkHasIDField({ properties: a });
67+
const isBIDField = checkHasIDField({ properties: b });
68+
if (isAIDField && !isBIDField) {
69+
return -1;
70+
} else if (!isAIDField && isBIDField) {
71+
return 1;
72+
} else {
73+
return 0;
74+
}
75+
})
76+
) as [string, JSONSchema7][];
77+
console.log({ entries });
6078
6179
// State
6280
let checkedRowIndexes = $state<number[]>([]);

src/lib/components/app/editor/document-editor/document-editor.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ It could edit a single `document` or document inside a `collection`.
218218
placeholder={cap(m.select_x({ x: m.enumeration() }))}
219219
/>
220220
{:else if config.type === 'string'}
221-
{#if config['x-custom-string-type']}
221+
{#if config['x-string-custom-type']}
222222
<div class="mb-1 flex items-center gap-2">
223223
<Button
224224
variant="secondary"
225225
size="sm"
226226
onclick={() => {
227-
const xCustomStringType = config['x-custom-string-type'];
227+
const xCustomStringType = config['x-string-custom-type'];
228228
const value = xCustomStringType === 'ID - uuid' ? uuidv4() : nanoid(lengthOfNanoid);
229229
setValueByJsonPathsMutable(editedData, jsonPaths, { value });
230230
}}
231231
>
232-
Generate a "{config['x-custom-string-type']}"
232+
Generate a "{config['x-string-custom-type']}"
233233
</Button>
234-
{#if config['x-custom-string-type'] === 'ID - nanoid'}
234+
{#if config['x-string-custom-type'] === 'ID - nanoid'}
235235
<Label for="nanoid-length">Length</Label>
236236
<Input id="nanoid-length" class="h-6 w-fit" type="number" bind:value={lengthOfNanoid} />
237237
{/if}

src/lib/components/app/editor/schema-editor/add-field-button.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
</Popover.Trigger>
103103
<Popover.Content
104104
class="border-3 w-96 overflow-scroll"
105-
style="max-height: calc(50dvh - 60px); min-height: 320px"
105+
style="max-height: 70dvh; min-height: 320px"
106106
>
107107
<FieldEditor
108108
onConfirmFieldDetail={(field, schema) => {

src/lib/components/app/editor/schema-editor/edit-field-button.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
</Popover.Trigger>
7171
<Popover.Content
7272
class="border-3 w-96 overflow-scroll"
73-
style="max-height: calc(50dvh - 60px); min-height: 320px"
73+
style="max-height: 70dvh; min-height: 320px"
7474
>
7575
<button
7676
class="absolute right-2 z-30"

src/lib/components/app/editor/schema-editor/field-editor/keyword-map-editor/keyword-map-editor.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@
7878
{:else}
7979
{#if type === 'string'}
8080
<StringCustomTypeEditor bind:keywordObj {disabled} />
81-
<div class={keywordObj['x-custom-string-type'] ? 'opacity-60' : ''}>
81+
<div class={keywordObj['x-string-custom-type'] ? 'opacity-60' : ''}>
8282
<EnumEditor
8383
bind:keywordObj
8484
type="string"
85-
disabled={disabled || !!keywordObj['x-custom-string-type']}
85+
disabled={disabled || !!keywordObj['x-string-custom-type']}
8686
/>
8787
</div>
8888
{/if}

src/lib/components/app/editor/schema-editor/field-editor/keyword-map-editor/string-custom-type-editor.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
disabled?: boolean;
2020
}
2121
let { keywordObj = $bindable(), disabled }: Props = $props();
22-
let xCustomStringType = $derived(keywordObj['x-custom-string-type']);
22+
let xCustomStringType = $derived(keywordObj['x-string-custom-type']);
2323
</script>
2424

2525
<RemovableSelect
@@ -28,12 +28,12 @@
2828
options={JSON_SCHEMA.string.customTypes.map((t) => ({ label: t, value: t, withIcon: true }))}
2929
onSelectedChange={(item) => {
3030
const { value } = item as Selected<string>;
31-
keywordObj['x-custom-string-type'] = value;
31+
keywordObj['x-string-custom-type'] = value;
3232
}}
3333
{disabled}
3434
onRemove={() => {
3535
console.log({ keywordObj });
36-
delete keywordObj['x-custom-string-type'];
36+
delete keywordObj['x-string-custom-type'];
3737
delete keywordObj.enum;
3838
}}
3939
/>

src/lib/components/app/label/type-label.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<span>
1111
{config.type === 'array'
1212
? `${config.type} of ${(config!.items as JSONSchema7WithCustomKeyword)!.type}(s)`
13-
: config.type === 'string' && config['x-custom-string-type']
14-
? `${config.type} | ${config['x-custom-string-type']}`
13+
: config.type === 'string' && config['x-string-custom-type']
14+
? `${config.type} | ${config['x-string-custom-type']}`
1515
: config.type}
1616
</span>

src/lib/constants/constants.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const JSON_SCHEMA = {
114114
'null'
115115
] as JSONSchema7TypeName[],
116116
string: {
117-
keywords: ['enum', 'minLength', 'maxLength', 'pattern', 'x-custom-string-type'],
117+
keywords: ['enum', 'minLength', 'maxLength', 'pattern', 'x-string-custom-type'],
118118
customTypes: [
119119
'ID - uuid',
120120
'ID - nanoid'

src/lib/types/types.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type LowCMSType =
8484
export type JSONSchema7WithUnknown = JSONSchema7 & { type: 'unknown' };
8585

8686
export type JSONSchema7WithCustomKeyword = JSONSchema7 & {
87-
'x-custom-string-type'?: string;
87+
'x-string-custom-type'?: string;
8888
};
8989

9090
export type FilterObject = {

0 commit comments

Comments
 (0)