feat: Update total calculator count to include all calculators by modifying the migration script.
This commit is contained in:
@@ -1,20 +1,19 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
|
import { loadCalculators, searchCalculators, type CalculatorDef } from '$lib/data/calculatorLoader';
|
||||||
|
|
||||||
export let idPrefix = 'search';
|
export let idPrefix = 'search';
|
||||||
let query = '';
|
let query = '';
|
||||||
let focused = false;
|
let focused = false;
|
||||||
let selectedIndex = -1;
|
let selectedIndex = -1;
|
||||||
let lastQuery = '';
|
let lastQuery = '';
|
||||||
let searchCalculatorsFn: ((q: string) => any[]) | null = null;
|
let allCalcs: CalculatorDef[] = [];
|
||||||
|
|
||||||
$: if (query.length >= 1 && !searchCalculatorsFn) {
|
$: if (query.length >= 1 && allCalcs.length === 0) {
|
||||||
import('$lib/data/calculators').then(m => {
|
loadCalculators().then(data => { allCalcs = data; });
|
||||||
searchCalculatorsFn = m.searchCalculators;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$: results = (query.length >= 2 && searchCalculatorsFn) ? searchCalculatorsFn(query).slice(0, 8) : [];
|
$: results = (query.length >= 2 && allCalcs.length > 0) ? searchCalculators(allCalcs, query).slice(0, 8) : [];
|
||||||
$: listboxId = `${idPrefix}-listbox`;
|
$: listboxId = `${idPrefix}-listbox`;
|
||||||
$: inputId = `${idPrefix}-input`;
|
$: inputId = `${idPrefix}-input`;
|
||||||
$: isOpen = focused && results.length > 0;
|
$: isOpen = focused && results.length > 0;
|
||||||
|
|||||||
@@ -3,15 +3,15 @@
|
|||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { categories } from '$lib/data/stats';
|
import { categories } from '$lib/data/stats';
|
||||||
import type { CalculatorDef } from '$lib/data/calculators';
|
import { loadCalculators, type CalculatorDef } from '$lib/data/calculatorLoader';
|
||||||
|
|
||||||
let allCalculators: CalculatorDef[] = [];
|
let allCalculators: CalculatorDef[] = [];
|
||||||
let isLoaded = false;
|
let isLoaded = false;
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
if (isLoaded || !browser) return;
|
if (isLoaded || !browser) return;
|
||||||
const module = await import('$lib/data/calculators');
|
const data = await loadCalculators();
|
||||||
allCalculators = module.calculators;
|
allCalculators = data;
|
||||||
isLoaded = true;
|
isLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
43
hdyc-svelte/src/lib/data/calculatorLoader.ts
Normal file
43
hdyc-svelte/src/lib/data/calculatorLoader.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// Shared lazy loader – fetches /data/calculators.json exactly once.
|
||||||
|
// Because this is a plain fetch (not a JS dynamic import), Vite will NOT
|
||||||
|
// emit a modulepreload for it, keeping the homepage bundle small.
|
||||||
|
|
||||||
|
export interface CalculatorDef {
|
||||||
|
slug: string;
|
||||||
|
name: string;
|
||||||
|
category: string;
|
||||||
|
type: string;
|
||||||
|
teaser: string;
|
||||||
|
labels: { in1: string; in2: string };
|
||||||
|
factor?: number;
|
||||||
|
offset?: number;
|
||||||
|
hidden?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cache: CalculatorDef[] | null = null;
|
||||||
|
let pending: Promise<CalculatorDef[]> | null = null;
|
||||||
|
|
||||||
|
export async function loadCalculators(): Promise<CalculatorDef[]> {
|
||||||
|
if (cache) return cache;
|
||||||
|
if (pending) return pending;
|
||||||
|
|
||||||
|
pending = fetch('/data/calculators.json')
|
||||||
|
.then(r => r.json())
|
||||||
|
.then((data: CalculatorDef[]) => {
|
||||||
|
cache = data;
|
||||||
|
pending = null;
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
|
||||||
|
return pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function searchCalculators(calcs: CalculatorDef[], query: string): CalculatorDef[] {
|
||||||
|
const q = query.toLowerCase();
|
||||||
|
return calcs.filter(c =>
|
||||||
|
(c.name.toLowerCase().includes(q) ||
|
||||||
|
c.slug.includes(q) ||
|
||||||
|
c.labels.in1.toLowerCase().includes(q) ||
|
||||||
|
c.labels.in2.toLowerCase().includes(q)) && !c.hidden
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -82,4 +82,4 @@ export const categories: Record<string, { label: string; icon: string }> = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const totalCalculators = 2460;
|
export const totalCalculators = 3124;
|
||||||
|
|||||||
37133
hdyc-svelte/static/data/calculators.json
Normal file
37133
hdyc-svelte/static/data/calculators.json
Normal file
File diff suppressed because it is too large
Load Diff
14
migrate.py
14
migrate.py
@@ -1,11 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).resolve().parent
|
BASE_DIR = Path(__file__).resolve().parent
|
||||||
CALCLIST = BASE_DIR / 'calculators_list.md'
|
CALCLIST = BASE_DIR / 'calculators_list.md'
|
||||||
OUTPUT_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/calculators.ts'
|
OUTPUT_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/calculators.ts'
|
||||||
STATS_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/stats.ts'
|
STATS_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/stats.ts'
|
||||||
|
CALCULATORS_JSON = BASE_DIR / 'hdyc-svelte/static/data/calculators.json'
|
||||||
|
|
||||||
CATEGORY_KEYS = [
|
CATEGORY_KEYS = [
|
||||||
'length',
|
'length',
|
||||||
@@ -496,15 +498,21 @@ export function searchCalculators(query: string): CalculatorDef[] {
|
|||||||
print(f"Generated {len(calculators_ts_entries)} calculators into calculators.ts")
|
print(f"Generated {len(calculators_ts_entries)} calculators into calculators.ts")
|
||||||
|
|
||||||
# Generate stats.ts
|
# Generate stats.ts
|
||||||
active_count = len([e for e in calculators_ts_entries if not e.get('hidden')])
|
total_count = len(calculators_ts_entries)
|
||||||
stats_content = f"""// THIS FILE IS AUTO-GENERATED BY migrate.py
|
stats_content = f"""// THIS FILE IS AUTO-GENERATED BY migrate.py
|
||||||
export const categories: Record<string, {{ label: string; icon: string }}> = {json.dumps(CATEGORIES, indent=2, ensure_ascii=False)};
|
export const categories: Record<string, {{ label: string; icon: string }}> = {json.dumps(CATEGORIES, indent=2, ensure_ascii=False)};
|
||||||
|
|
||||||
export const totalCalculators = {active_count};
|
export const totalCalculators = {total_count};
|
||||||
"""
|
"""
|
||||||
with open(STATS_FILE, 'w', encoding='utf-8') as f:
|
with open(STATS_FILE, 'w', encoding='utf-8') as f:
|
||||||
f.write(stats_content)
|
f.write(stats_content)
|
||||||
print(f"Generated stats.ts with {active_count} active calculators")
|
print(f"Generated stats.ts with {total_count} total calculators")
|
||||||
|
|
||||||
|
# Generate calculators.json for true lazy loading
|
||||||
|
os.makedirs(os.path.dirname(CALCULATORS_JSON), exist_ok=True)
|
||||||
|
with open(CALCULATORS_JSON, 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(calculators_ts_entries, f, ensure_ascii=False, indent=2)
|
||||||
|
print(f"Generated calculators.json (Size: {os.path.getsize(CALCULATORS_JSON) // 1024}KB)")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
process()
|
process()
|
||||||
|
|||||||
Reference in New Issue
Block a user