Skip to content

Commit 0bfb6dc

Browse files
authored
Merge pull request #242 from pathsim/refactor/codebase-cleanup
codebase cleanup
2 parents a4d3026 + 89906d8 commit 0bfb6dc

14 files changed

Lines changed: 212 additions & 484 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ src/
5252
│ ├── export/ # Export utilities
5353
│ │ └── svg/ # SVG graph export (renderer, types)
5454
│ ├── nodes/ # Node type system
55-
│ │ ├── features/ # Node feature flags
5655
│ │ ├── generated/ # Auto-generated from PathSim
5756
│ │ └── shapes/ # Node shape definitions
5857
│ ├── plotting/ # Plot system

scripts/extract.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class TypeScriptGenerator:
783783
def __init__(self, output_dir: Path):
784784
self.output_dir = output_dir
785785

786-
def write_blocks(self, blocks: dict, config: dict) -> None:
786+
def write_blocks(self, blocks: dict, config: dict, import_paths: dict[str, str] | None = None) -> None:
787787
"""Write blocks.ts file."""
788788
lines = [
789789
"// Auto-generated by scripts/extract.py - DO NOT EDIT",
@@ -820,6 +820,14 @@ def write_blocks(self, blocks: dict, config: dict) -> None:
820820
lines.append("};")
821821
lines.append("")
822822

823+
# Import paths map (block name -> Python import path)
824+
if import_paths:
825+
lines.append("export const blockImportPaths: Record<string, string> = {")
826+
for block_name, path in sorted(import_paths.items()):
827+
lines.append(f' "{block_name}": "{path}",')
828+
lines.append("};")
829+
lines.append("")
830+
823831
output_path = self.output_dir / "nodes" / "generated" / "blocks.ts"
824832
output_path.parent.mkdir(parents=True, exist_ok=True)
825833
output_path.write_text("\n".join(lines), encoding="utf-8")
@@ -1014,7 +1022,7 @@ def main():
10141022
print("\nExtracting blocks...")
10151023
blocks, block_config, block_import_paths = BlockExtractor(config).extract_all()
10161024
if extract_all or args.blocks:
1017-
generator.write_blocks(blocks, block_config)
1025+
generator.write_blocks(blocks, block_config, block_import_paths)
10181026
print(f" Total: {len(blocks)} blocks")
10191027

10201028
if extract_all or args.events or args.registry:

src/lib/components/FlowCanvas.svelte

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
} from '@xyflow/svelte';
1414
import '@xyflow/svelte/dist/style.css';
1515
16+
import { isInputFocused } from '$lib/utils/focus';
1617
import BaseNode from './nodes/BaseNode.svelte';
1718
import EventNode from './nodes/EventNode.svelte';
1819
import AnnotationNode from './nodes/AnnotationNode.svelte';
@@ -63,12 +64,7 @@
6364
6465
// Keyboard shortcuts for node manipulation
6566
function handleKeydown(event: KeyboardEvent) {
66-
// Ignore if typing in an input field or code editor
67-
const isInputFocused =
68-
event.target instanceof HTMLInputElement ||
69-
event.target instanceof HTMLTextAreaElement ||
70-
(event.target as HTMLElement)?.closest?.('.cm-editor');
71-
if (isInputFocused) return;
67+
if (isInputFocused(event)) return;
7268
7369
// Handle Delete key (SvelteFlow's deleteKeyCode doesn't work reliably for 'Delete')
7470
if (event.key === 'Delete') {

src/lib/components/edges/ArrowEdge.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { BaseEdge, getSmoothStepPath, type EdgeProps } from '@xyflow/svelte';
33
import { hoveredHandle, selectedNodeHighlight } from '$lib/stores/hoveredHandle';
4+
import { onDestroy } from 'svelte';
45
56
let {
67
id,
@@ -20,11 +21,16 @@
2021
2122
// Check if this edge is connected to the hovered handle
2223
let hovered = $state<{ nodeId: string; handleId: string; color?: string } | null>(null);
23-
hoveredHandle.subscribe((h) => (hovered = h));
24+
const unsubscribeHovered = hoveredHandle.subscribe((h) => (hovered = h));
2425
2526
// Check if this edge is connected to a selected node
2627
let selectedNode = $state<{ nodeId: string; color?: string } | null>(null);
27-
selectedNodeHighlight.subscribe((s) => (selectedNode = s));
28+
const unsubscribeSelected = selectedNodeHighlight.subscribe((s) => (selectedNode = s));
29+
30+
onDestroy(() => {
31+
unsubscribeHovered();
32+
unsubscribeSelected();
33+
});
2834
2935
const isHoverHighlighted = $derived(() => {
3036
if (!hovered) return false;

src/lib/nodes/features/index.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/lib/nodes/features/registry.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/lib/nodes/features/types.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/lib/nodes/generated/blocks.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,68 @@ export const blockConfig: Record<string, string[]> = {
13421342
Recording: ["Scope", "Spectrum"],
13431343
Chemical: ["Process", "Bubbler4", "Splitter", "GLC"],
13441344
};
1345+
1346+
export const blockImportPaths: Record<string, string> = {
1347+
"ADC": "pathsim.blocks",
1348+
"Abs": "pathsim.blocks",
1349+
"Adder": "pathsim.blocks",
1350+
"Amplifier": "pathsim.blocks",
1351+
"AntiWindupPID": "pathsim.blocks",
1352+
"Bubbler4": "pathsim_chem.tritium",
1353+
"ButterworthBandpassFilter": "pathsim.blocks",
1354+
"ButterworthBandstopFilter": "pathsim.blocks",
1355+
"ButterworthHighpassFilter": "pathsim.blocks",
1356+
"ButterworthLowpassFilter": "pathsim.blocks",
1357+
"ChirpPhaseNoiseSource": "pathsim.blocks",
1358+
"Clip": "pathsim.blocks",
1359+
"ClockSource": "pathsim.blocks",
1360+
"Constant": "pathsim.blocks",
1361+
"Cos": "pathsim.blocks",
1362+
"Counter": "pathsim.blocks",
1363+
"CounterDown": "pathsim.blocks",
1364+
"CounterUp": "pathsim.blocks",
1365+
"DAC": "pathsim.blocks",
1366+
"Delay": "pathsim.blocks",
1367+
"Differentiator": "pathsim.blocks",
1368+
"DynamicalSystem": "pathsim.blocks",
1369+
"Exp": "pathsim.blocks",
1370+
"FIR": "pathsim.blocks",
1371+
"Function": "pathsim.blocks",
1372+
"GLC": "pathsim_chem.tritium",
1373+
"GaussianPulseSource": "pathsim.blocks",
1374+
"Integrator": "pathsim.blocks",
1375+
"Interface": "pathsim.blocks",
1376+
"LUT": "pathsim.blocks",
1377+
"LUT1D": "pathsim.blocks",
1378+
"Log": "pathsim.blocks",
1379+
"Log10": "pathsim.blocks",
1380+
"Mod": "pathsim.blocks",
1381+
"Multiplier": "pathsim.blocks",
1382+
"ODE": "pathsim.blocks",
1383+
"PID": "pathsim.blocks",
1384+
"PinkNoise": "pathsim.blocks",
1385+
"Pow": "pathsim.blocks",
1386+
"Process": "pathsim_chem.tritium",
1387+
"PulseSource": "pathsim.blocks",
1388+
"RandomNumberGenerator": "pathsim.blocks",
1389+
"Relay": "pathsim.blocks",
1390+
"SampleHold": "pathsim.blocks",
1391+
"Scope": "pathsim.blocks",
1392+
"Sin": "pathsim.blocks",
1393+
"SinusoidalSource": "pathsim.blocks",
1394+
"Source": "pathsim.blocks",
1395+
"Spectrum": "pathsim.blocks",
1396+
"Splitter": "pathsim_chem.tritium",
1397+
"Sqrt": "pathsim.blocks",
1398+
"SquareWaveSource": "pathsim.blocks",
1399+
"StateSpace": "pathsim.blocks",
1400+
"StepSource": "pathsim.blocks",
1401+
"Subsystem": "pathsim.blocks",
1402+
"Switch": "pathsim.blocks",
1403+
"Tan": "pathsim.blocks",
1404+
"Tanh": "pathsim.blocks",
1405+
"TransferFunctionNumDen": "pathsim.blocks",
1406+
"TransferFunctionZPG": "pathsim.blocks",
1407+
"TriangleWaveSource": "pathsim.blocks",
1408+
"WhiteNoise": "pathsim.blocks",
1409+
};

src/lib/nodes/types.ts

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* Node type definitions
33
*
4-
* Re-exports types from centralized location for backwards compatibility.
5-
* New code should import from '$lib/types' directly.
4+
* Re-exports types from centralized locations for convenience.
65
*/
76

8-
// Re-export all node-related types from centralized location
7+
// Re-export all node-related types
98
export type {
109
PortDirection,
1110
PortDefinition,
@@ -21,50 +20,12 @@ export type {
2120
Annotation
2221
} from '$lib/types/nodes';
2322

24-
// Re-export simulation types used in this module
23+
// Re-export simulation types
2524
export type { SolverType, SimulationSettings } from '$lib/types/simulation';
2625

2726
// Re-export schema types
2827
export type { GraphFile } from '$lib/types/schema';
2928
export { GRAPH_FILE_VERSION } from '$lib/types/schema';
3029

31-
// Import extracted defaults from PathSim (generated by scripts/extract-simulation.py)
32-
import { extractedSimulationParams, uiOnlyParams } from '$lib/simulation/generated/simulation';
33-
import type { SolverType, SimulationSettings } from '$lib/types/simulation';
34-
35-
// Helper to get default value from extracted params
36-
function getExtractedDefault(key: string): string {
37-
const param = extractedSimulationParams[key];
38-
return param?.default ?? '';
39-
}
40-
41-
// Default values for simulation settings (used as placeholders and code gen fallback)
42-
// Values extracted from PathSim via scripts/extract-simulation.py
43-
export const DEFAULT_SIMULATION_SETTINGS: SimulationSettings = {
44-
duration: getExtractedDefault('duration'),
45-
dt: getExtractedDefault('dt'),
46-
solver: (getExtractedDefault('solver') || 'SSPRK22') as SolverType,
47-
adaptive: true,
48-
atol: getExtractedDefault('atol'),
49-
rtol: getExtractedDefault('rtol'),
50-
ftol: getExtractedDefault('ftol'),
51-
dt_min: getExtractedDefault('dt_min'),
52-
dt_max: getExtractedDefault('dt_max'),
53-
ghostTraces: parseInt(uiOnlyParams.ghostTraces?.default || '0'),
54-
plotResults: uiOnlyParams.plotResults?.default === 'true'
55-
};
56-
57-
// Initial empty settings (defaults shown as placeholders)
58-
export const INITIAL_SIMULATION_SETTINGS: SimulationSettings = {
59-
duration: '',
60-
dt: '',
61-
solver: 'SSPRK22', // Solver always has a value (selected from matrix)
62-
adaptive: true,
63-
atol: '',
64-
rtol: '',
65-
ftol: '',
66-
dt_min: '',
67-
dt_max: '',
68-
ghostTraces: 0,
69-
plotResults: true
70-
};
30+
// Re-export simulation defaults
31+
export { DEFAULT_SIMULATION_SETTINGS, INITIAL_SIMULATION_SETTINGS } from '$lib/simulation/defaults';

0 commit comments

Comments
 (0)