Skip to content

Commit

Permalink
Integrate PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Jan 22, 2025
1 parent 6366caa commit 02f4dee
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 46 deletions.
51 changes: 12 additions & 39 deletions frontend/viewer/src/lib/DictionaryEntry.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
<script lang="ts" context="module">
const vernacularColors = [
'text-emerald-400 dark:text-emerald-300',
'text-fuchsia-600 dark:text-fuchsia-300',
'text-lime-600 dark:text-lime-200',
] as const;
const analysisColors = [
'text-blue-500 dark:text-blue-300',
'text-yellow-500 dark:text-yellow-200',
'text-rose-500 dark:text-rose-400',
] as const;
</script>
<script lang="ts">
import type { IEntry } from '$lib/dotnet-types';
import {useWritingSystemService} from './writing-system-service';
Expand All @@ -21,35 +8,21 @@
$: lines = entry.senses.length > 1 ? entry.senses.length + 1 : 1;
const writingSystemService = useWritingSystemService();
const wsColor = (() => {
const vernacularColor: Record<string, typeof vernacularColors[number]> = {};
const analysisColor: Record<string, typeof analysisColors[number]> = {};
writingSystemService.vernacular.forEach((ws, i) => {
vernacularColor[ws.wsId] = vernacularColors[i % vernacularColors.length];
});
writingSystemService.analysis.forEach((ws, i) => {
analysisColor[ws.wsId] = analysisColors[i % analysisColors.length];
});
return (ws: string, type: 'vernacular' | 'analysis') => {
const colors = type === 'vernacular' ? vernacularColor : analysisColor;
return colors[ws];
};
})();
const wsService = useWritingSystemService();
$: headwords = writingSystemService.vernacular
.map(ws => ({ws: ws.wsId, value: writingSystemService.headword(entry)}))
$: headwords = wsService.vernacular
.map(ws => ({ws: ws.wsId, value: wsService.headword(entry)}))
.filter(({value}) => !!value);
const partsOfSpeech = usePartsOfSpeech(writingSystemService);
const partsOfSpeech = usePartsOfSpeech();
</script>

<div>
<strong class="inline-flex gap-1">
{#each headwords as {ws, value}, i}
{#if value}
{#if i > 0}/{/if}
<span class={wsColor(ws, 'vernacular')}>{value}</span>
<span class={wsService.wsColor(ws, 'vernacular')}>{value}</span>
{/if}
{/each}
</strong>
Expand All @@ -63,32 +36,32 @@
<i>{partOfSpeech}</i>
{/if}
<span>
{#each writingSystemService.analysis as ws}
{#each wsService.analysis as ws}
{#if sense.gloss[ws.wsId] || sense.definition[ws.wsId]}
<span class="ml-0.5">
<sub class="-mr-0.5">{ws.abbreviation}</sub>
{#if sense.gloss[ws.wsId]}
<span class={wsColor(ws.wsId, 'analysis')}>{sense.gloss[ws.wsId]}</span>;
<span class={wsService.wsColor(ws.wsId, 'analysis')}>{sense.gloss[ws.wsId]}</span>;
{/if}
{#if sense.definition[ws.wsId]}
<span class={wsColor(ws.wsId, 'analysis')}>{sense.definition[ws.wsId]}</span>
<span class={wsService.wsColor(ws.wsId, 'analysis')}>{sense.definition[ws.wsId]}</span>
{/if}
</span>
{/if}
{/each}
</span>
{#each sense.exampleSentences as example (example.id)}
{@const usedVernacular = writingSystemService.vernacular.filter(ws => !!example.sentence[ws.wsId])}
{@const usedAnalysis = writingSystemService.analysis.filter(ws => !!example.translation[ws.wsId])}
{@const usedVernacular = wsService.vernacular.filter(ws => !!example.sentence[ws.wsId])}
{@const usedAnalysis = wsService.analysis.filter(ws => !!example.translation[ws.wsId])}
{#if usedVernacular.length || usedAnalysis.length}
<span class="-mr-0.5">[</span>
{#each usedVernacular as ws}
<span class={wsColor(ws.wsId, 'vernacular')}>{example.sentence[ws.wsId]}</span>
<span class={wsService.wsColor(ws.wsId, 'vernacular')}>{example.sentence[ws.wsId]}</span>
<span></span><!-- standardizes whitespace between texts -->
{/each}
<span></span>
{#each usedAnalysis as ws}
<span class={wsColor(ws.wsId, 'analysis')}>{example.translation[ws.wsId]}</span>
<span class={wsService.wsColor(ws.wsId, 'analysis')}>{example.translation[ws.wsId]}</span>
<span></span><!-- standardizes whitespace between texts -->
{/each}
<span class="-ml-0.5">]</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
export let sense: ISense;
export let readonly: boolean = false;
const writingSystemService = useWritingSystemService();
const partsOfSpeech = usePartsOfSpeech(writingSystemService);
const partsOfSpeech = usePartsOfSpeech();
const semanticDomains = useSemanticDomains();
const currentView = useCurrentView();
</script>
Expand Down
5 changes: 3 additions & 2 deletions frontend/viewer/src/lib/parts-of-speech.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {derived, type Readable, type Writable, writable} from 'svelte/store';
import type {IPartOfSpeech} from '$lib/dotnet-types';
import {useLexboxApi} from './services/service-provider';
import type {WritingSystemService} from './writing-system-service';
import {useWritingSystemService} from './writing-system-service';

type LabeledPartOfSpeech = IPartOfSpeech & {label: string};

let partsOfSpeechStore: Writable<IPartOfSpeech[] | null> | null = null;

export function usePartsOfSpeech(writingSystemService: WritingSystemService): Readable<LabeledPartOfSpeech[]> {
export function usePartsOfSpeech(): Readable<LabeledPartOfSpeech[]> {
const writingSystemService = useWritingSystemService();
if (partsOfSpeechStore === null) {
partsOfSpeechStore = writable<IPartOfSpeech[] | null>([], (set) => {
useLexboxApi().getPartsOfSpeech().then(partsOfSpeech => {
Expand Down
52 changes: 48 additions & 4 deletions frontend/viewer/src/lib/writing-system-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {IEntry, IExampleSentence, IMultiString, ISense, IWritingSystem, IWritingSystems} from '$lib/dotnet-types';
import {getContext, setContext} from 'svelte';
import {getContext, onDestroy, setContext} from 'svelte';
import {derived, get, type Readable} from 'svelte/store';
import type {WritingSystemSelection} from './config-types';
import {firstTruthy} from './utils';
Expand All @@ -14,12 +14,20 @@ export function useWritingSystemService(): WritingSystemService {
if (!writingSystemServiceContext) throw new Error('Writing system context is not initialized. Are you in the context of a project?');
const writingSystemService = get(writingSystemServiceContext);
if (!writingSystemService) throw new Error('Writing system service is not initialized.');
const unsub = writingSystemServiceContext.subscribe((service) => {
if (service !== writingSystemService) console.warn('Writing system service changed unexpectedly.');
});
onDestroy(unsub);
return writingSystemService;
}

export class WritingSystemService {

constructor(private readonly writingSystems: IWritingSystems) { }
private readonly wsColors: WritingSystemColors;

constructor(private readonly writingSystems: IWritingSystems) {
this.wsColors = calcWritingSystemColors(writingSystems);
}

allWritingSystems(selection: Extract<WritingSystemSelection, 'vernacular-analysis' | 'analysis-vernacular'> = 'vernacular-analysis'): IWritingSystem[] {
return this.pickWritingSystems(selection);
Expand Down Expand Up @@ -47,9 +55,9 @@ export class WritingSystemService {
ws = ws ?? 'vernacular-analysis';
switch (ws) {
case 'vernacular-analysis':
return [...new Set([...this.writingSystems.vernacular, ...this.writingSystems.analysis].sort())];
return [...this.writingSystems.vernacular, ...this.writingSystems.analysis];
case 'analysis-vernacular':
return [...new Set([...this.writingSystems.analysis, ...this.writingSystems.vernacular].sort())];
return [...this.writingSystems.analysis, ...this.writingSystems.vernacular];
case 'first-analysis':
return [this.writingSystems.analysis[0]];
case 'first-vernacular':
Expand Down Expand Up @@ -129,7 +137,43 @@ export class WritingSystemService {
return this.first(example.sentence, this.vernacular) || this.first(example.translation, this.analysis) || '';
}

wsColor(ws: string, type: 'vernacular' | 'analysis'): string {
const colors = this.wsColors[type];
return colors[ws];
}

private first(value: IMultiString, writingSystems: IWritingSystem[]): string | undefined {
return firstTruthy(writingSystems, ws => value[ws.wsId]);
}
}

type WritingSystemColors = {
vernacular: Record<string, typeof vernacularColors[number]>;
analysis: Record<string, typeof analysisColors[number]>;
}

function calcWritingSystemColors(writingSystems: IWritingSystems): WritingSystemColors {
const wsColors = {
vernacular: {} as Record<string, typeof vernacularColors[number]>,
analysis: {} as Record<string, typeof analysisColors[number]>,
};
writingSystems.vernacular.forEach((ws, i) => {
wsColors.vernacular[ws.wsId] = vernacularColors[i % vernacularColors.length];
});
writingSystems.analysis.forEach((ws, i) => {
wsColors.analysis[ws.wsId] = analysisColors[i % analysisColors.length];
});
return wsColors;
}

const vernacularColors = [
'text-emerald-400 dark:text-emerald-300',
'text-fuchsia-600 dark:text-fuchsia-300',
'text-lime-600 dark:text-lime-200',
] as const;

const analysisColors = [
'text-blue-500 dark:text-blue-300',
'text-yellow-500 dark:text-yellow-200',
'text-rose-500 dark:text-rose-400',
] as const;

0 comments on commit 02f4dee

Please sign in to comment.