@hayro_o7/labyrinth 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,7 +60,11 @@ npm install labyrinth
60
60
  autoPlay={false}
61
61
  showGrid={true}
62
62
  colors={customColors}
63
+ onControls={(controls) => (labyrinthControls = controls)}
63
64
  />
65
+
66
+ <button on:click={() => labyrinthControls?.play()}>Play</button>
67
+ <button on:click={() => labyrinthControls?.pause()}>Pause</button>
64
68
  ```
65
69
 
66
70
  ## API Reference
@@ -91,6 +95,7 @@ Generates a perfect maze (exactly one path between any two points) using the Rec
91
95
  | `legend` | `boolean` | `true` | Toggle the legend panel |
92
96
  | `stepCount` | `boolean` | `true` | Toggle the step counter |
93
97
  | `colors` | `ColorScheme` | default theme | Custom color scheme (see below) |
98
+ | `onControls` | `(controls: LabyrinthControls) => void` | `undefined` | Receive imperative control methods |
94
99
 
95
100
  ### Algorithms
96
101
 
@@ -147,6 +152,30 @@ const customColors: ColorScheme = {
147
152
 
148
153
  All color properties are optional - only specify the ones you want to override.
149
154
 
155
+ ### Imperative Controls
156
+
157
+ You can access imperative controls (play/pause/reset/step) by providing `onControls`:
158
+
159
+ ```svelte
160
+ <script lang="ts">
161
+ import { Labyrinth, generateLabyrinth } from '@hayro_o7/labyrinth';
162
+ import type { LabyrinthControls } from '@hayro_o7/labyrinth';
163
+
164
+ const graph = generateLabyrinth(20, 20);
165
+ let controls: LabyrinthControls | null = null;
166
+ </script>
167
+
168
+ <Labyrinth {graph} onControls={(c) => (controls = c)} />
169
+
170
+ <div class="external-controls">
171
+ <button on:click={() => controls?.play()}>Play</button>
172
+ <button on:click={() => controls?.pause()}>Pause</button>
173
+ <button on:click={() => controls?.reset()}>Reset</button>
174
+ <button on:click={() => controls?.stepBackward()}>Step Back</button>
175
+ <button on:click={() => controls?.stepForward()}>Step Forward</button>
176
+ </div>
177
+ ```
178
+
150
179
  ### Types
151
180
 
152
181
  ```typescript
@@ -194,6 +223,14 @@ interface ColorScheme {
194
223
  buttonsdisabled?: string;
195
224
  buttonstext?: string;
196
225
  }
226
+
227
+ interface LabyrinthControls {
228
+ play: () => void;
229
+ pause: () => void;
230
+ reset: () => void;
231
+ stepForward: () => void;
232
+ stepBackward: () => void;
233
+ }
197
234
  ```
198
235
 
199
236
  ## Development
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import type { Graph, AlgorithmType, PathStep } from '../types';
2
+ import type { Graph, AlgorithmType, PathStep, LabyrinthControls } from '../types';
3
3
  import { dijkstra } from '../algorithms/dijkstra';
4
4
  import { astar } from '../algorithms/astar';
5
5
 
@@ -13,11 +13,13 @@
13
13
  startNode?: string;
14
14
  endNode?: string;
15
15
  autoPlay?: boolean;
16
+ buttons?: boolean;
16
17
  legend?: boolean;
17
18
  stepCount?: boolean;
18
19
  animationSpeed?: number;
19
20
  showGrid?: boolean;
20
21
  colors?: ColorScheme;
22
+ onControls?: (controls: LabyrinthControls) => void;
21
23
  }
22
24
 
23
25
  let {
@@ -28,13 +30,14 @@
28
30
  startNode = '0,0',
29
31
  endNode,
30
32
  autoPlay = false,
33
+ buttons = true,
31
34
  legend = true,
32
35
  stepCount = true,
33
36
  animationSpeed = 50,
34
37
  showGrid = true,
35
- colors
38
+ colors,
39
+ onControls
36
40
  }: Props = $props();
37
-
38
41
  const defaultColors: Required<ColorScheme> = {
39
42
  start: '#22c55e',
40
43
  end: '#ef4444',
@@ -182,6 +185,18 @@
182
185
 
183
186
  const width = $derived(graph.width * cellSize);
184
187
  const height = $derived(graph.height * cellSize);
188
+
189
+ const controlApi: LabyrinthControls = {
190
+ play,
191
+ pause,
192
+ reset,
193
+ stepForward,
194
+ stepBackward
195
+ };
196
+
197
+ $effect(() => {
198
+ onControls?.(controlApi);
199
+ });
185
200
  </script>
186
201
 
187
202
  <div class="labyrinth-container" style={cssVars}>
@@ -254,11 +269,13 @@
254
269
  </svg>
255
270
 
256
271
  <div class="controls">
257
- <button onclick={play} disabled={isPlaying}>Play</button>
258
- <button onclick={pause} disabled={!isPlaying}>Pause</button>
259
- <button onclick={reset}>Reset</button>
260
- <button onclick={stepBackward} disabled={isPlaying || currentStepIndex === 0}>Step Back</button>
261
- <button onclick={stepForward} disabled={isPlaying || currentStepIndex === steps.length}>Step Forward</button>
272
+ {#if buttons}
273
+ <button onclick={play} disabled={isPlaying}>Play</button>
274
+ <button onclick={pause} disabled={!isPlaying}>Pause</button>
275
+ <button onclick={reset}>Reset</button>
276
+ <button onclick={stepBackward} disabled={isPlaying || currentStepIndex === 0}>Step Back</button>
277
+ <button onclick={stepForward} disabled={isPlaying || currentStepIndex === steps.length}>Step Forward</button>
278
+ {/if}
262
279
  {#if stepCount}
263
280
  <span class="step-counter">
264
281
  Step: {currentStepIndex} / {steps.length}
@@ -1,4 +1,4 @@
1
- import type { Graph, AlgorithmType } from '../types';
1
+ import type { Graph, AlgorithmType, LabyrinthControls } from '../types';
2
2
  import type { ColorScheme } from '../types';
3
3
  interface Props {
4
4
  graph: Graph;
@@ -8,11 +8,13 @@ interface Props {
8
8
  startNode?: string;
9
9
  endNode?: string;
10
10
  autoPlay?: boolean;
11
+ buttons?: boolean;
11
12
  legend?: boolean;
12
13
  stepCount?: boolean;
13
14
  animationSpeed?: number;
14
15
  showGrid?: boolean;
15
16
  colors?: ColorScheme;
17
+ onControls?: (controls: LabyrinthControls) => void;
16
18
  }
17
19
  declare const Labyrinth: import("svelte").Component<Props, {}, "">;
18
20
  type Labyrinth = ReturnType<typeof Labyrinth>;
package/dist/index.d.ts CHANGED
@@ -2,4 +2,4 @@ export { generateLabyrinth } from './labyrinth-generator';
2
2
  export { dijkstra } from './algorithms/dijkstra';
3
3
  export { astar } from './algorithms/astar';
4
4
  export { default as Labyrinth } from './components/Labyrinth.svelte';
5
- export type { Graph, GraphNode, Point, Cell, PathStep, AlgorithmResult, AlgorithmType, ColorScheme } from './types';
5
+ export type { Graph, GraphNode, Point, Cell, PathStep, AlgorithmResult, AlgorithmType, ColorScheme, LabyrinthControls } from './types';
package/dist/types.d.ts CHANGED
@@ -54,3 +54,10 @@ export interface ColorScheme {
54
54
  buttonsdisabled?: string;
55
55
  buttonstext?: string;
56
56
  }
57
+ export interface LabyrinthControls {
58
+ play: () => void;
59
+ pause: () => void;
60
+ reset: () => void;
61
+ stepForward: () => void;
62
+ stepBackward: () => void;
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hayro_o7/labyrinth",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",