Spaces:
Running
Running
Tobias Brugger commited on
Commit ·
3e97cdf
1
Parent(s): 16b5aab
automatically select correct term
Browse files- src/lib/DatasetViewer.svelte +57 -2
src/lib/DatasetViewer.svelte
CHANGED
|
@@ -28,6 +28,10 @@
|
|
| 28 |
let selectedTerm1 = $state<string | null>(null);
|
| 29 |
let selectedTerm2 = $state<string | null>(null);
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
// UI state
|
| 32 |
let loading = $state(false);
|
| 33 |
let error = $state("");
|
|
@@ -63,6 +67,8 @@
|
|
| 63 |
equivalencyScores = [];
|
| 64 |
selectedTerm1 = null;
|
| 65 |
selectedTerm2 = null;
|
|
|
|
|
|
|
| 66 |
hasLoaded = false;
|
| 67 |
|
| 68 |
try {
|
|
@@ -92,8 +98,14 @@
|
|
| 92 |
hasLoaded = true;
|
| 93 |
|
| 94 |
// Auto-select first term if available
|
| 95 |
-
if (data1.length > 0)
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
} catch (e) {
|
| 98 |
error = e instanceof Error ? e.message : "Failed to load jurisdictions";
|
| 99 |
console.error("Jurisdiction loading error:", e);
|
|
@@ -128,6 +140,49 @@
|
|
| 128 |
return translationMap.get(normalized) || null;
|
| 129 |
}
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
// Check if a term should be highlighted based on the selected term in the other jurisdiction
|
| 132 |
function shouldHighlightTerm(
|
| 133 |
term: string,
|
|
|
|
| 28 |
let selectedTerm1 = $state<string | null>(null);
|
| 29 |
let selectedTerm2 = $state<string | null>(null);
|
| 30 |
|
| 31 |
+
// Track previous selected terms for detecting user-initiated changes (loop prevention)
|
| 32 |
+
let prevSelectedTerm1 = $state<string | null>(null);
|
| 33 |
+
let prevSelectedTerm2 = $state<string | null>(null);
|
| 34 |
+
|
| 35 |
// UI state
|
| 36 |
let loading = $state(false);
|
| 37 |
let error = $state("");
|
|
|
|
| 67 |
equivalencyScores = [];
|
| 68 |
selectedTerm1 = null;
|
| 69 |
selectedTerm2 = null;
|
| 70 |
+
prevSelectedTerm1 = null;
|
| 71 |
+
prevSelectedTerm2 = null;
|
| 72 |
hasLoaded = false;
|
| 73 |
|
| 74 |
try {
|
|
|
|
| 98 |
hasLoaded = true;
|
| 99 |
|
| 100 |
// Auto-select first term if available
|
| 101 |
+
if (data1.length > 0) {
|
| 102 |
+
selectedTerm1 = data1[0].term;
|
| 103 |
+
prevSelectedTerm1 = data1[0].term;
|
| 104 |
+
}
|
| 105 |
+
if (data2.length > 0) {
|
| 106 |
+
selectedTerm2 = data2[0].term;
|
| 107 |
+
prevSelectedTerm2 = data2[0].term;
|
| 108 |
+
}
|
| 109 |
} catch (e) {
|
| 110 |
error = e instanceof Error ? e.message : "Failed to load jurisdictions";
|
| 111 |
console.error("Jurisdiction loading error:", e);
|
|
|
|
| 140 |
return translationMap.get(normalized) || null;
|
| 141 |
}
|
| 142 |
|
| 143 |
+
// Find a term in a data array by its normalized form
|
| 144 |
+
function findTermInData(
|
| 145 |
+
normalizedTarget: string,
|
| 146 |
+
data: TermDefinition[]
|
| 147 |
+
): string | null {
|
| 148 |
+
const found = data.find(
|
| 149 |
+
(item) => normalizeTermForComparison(item.term) === normalizedTarget
|
| 150 |
+
);
|
| 151 |
+
return found?.term || null;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
// Auto-select direct translation when selectedTerm1 changes (user-initiated)
|
| 155 |
+
$effect(() => {
|
| 156 |
+
const current = selectedTerm1;
|
| 157 |
+
if (current && current !== prevSelectedTerm1 && data2.length > 0) {
|
| 158 |
+
const translation = getDirectTranslation(current);
|
| 159 |
+
if (translation) {
|
| 160 |
+
const matchingTerm = findTermInData(translation, data2);
|
| 161 |
+
if (matchingTerm && matchingTerm !== selectedTerm2) {
|
| 162 |
+
selectedTerm2 = matchingTerm;
|
| 163 |
+
prevSelectedTerm2 = matchingTerm;
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
prevSelectedTerm1 = current;
|
| 167 |
+
}
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
+
// Auto-select direct translation when selectedTerm2 changes (user-initiated)
|
| 171 |
+
$effect(() => {
|
| 172 |
+
const current = selectedTerm2;
|
| 173 |
+
if (current && current !== prevSelectedTerm2 && data1.length > 0) {
|
| 174 |
+
const translation = getDirectTranslation(current);
|
| 175 |
+
if (translation) {
|
| 176 |
+
const matchingTerm = findTermInData(translation, data1);
|
| 177 |
+
if (matchingTerm && matchingTerm !== selectedTerm1) {
|
| 178 |
+
selectedTerm1 = matchingTerm;
|
| 179 |
+
prevSelectedTerm1 = matchingTerm;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
prevSelectedTerm2 = current;
|
| 183 |
+
}
|
| 184 |
+
});
|
| 185 |
+
|
| 186 |
// Check if a term should be highlighted based on the selected term in the other jurisdiction
|
| 187 |
function shouldHighlightTerm(
|
| 188 |
term: string,
|