@visuallyjs/browser-ui-svelte 1.1.3 → 1.2.0

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.
Files changed (39) hide show
  1. package/AreaChartComponent.svelte +24 -15
  2. package/BarChartComponent.svelte +24 -12
  3. package/BubbleChartComponent.svelte +23 -11
  4. package/ColumnChartComponent.svelte +23 -11
  5. package/ControlsComponent.svelte +12 -34
  6. package/DecoratorComponent.svelte +15 -23
  7. package/DiagramComponent.svelte +2 -2
  8. package/DiagramPaletteComponent.svelte +24 -31
  9. package/DiagramProvider.svelte +3 -1
  10. package/ExportControlsComponent.svelte +15 -33
  11. package/GaugeChartComponent.svelte +2 -7
  12. package/InspectorComponent.svelte +24 -36
  13. package/LineChartComponent.svelte +23 -13
  14. package/MiniviewComponent.svelte +30 -46
  15. package/PaletteComponent.svelte +28 -41
  16. package/PieChartComponent.svelte +23 -8
  17. package/SankeyChartComponent.svelte +35 -14
  18. package/ScatterChartComponent.svelte +23 -11
  19. package/ShapePaletteComponent.svelte +11 -22
  20. package/SurfaceProvider.svelte +3 -1
  21. package/XYChartComponent.svelte +33 -12
  22. package/charts/definitions.d.ts +119 -0
  23. package/charts/definitions.js +1 -0
  24. package/definitions.d.ts +30 -256
  25. package/diagram-store.d.ts +0 -5
  26. package/diagram-store.js +0 -15
  27. package/index.d.ts +4 -0
  28. package/index.js +4 -0
  29. package/package.json +1 -1
  30. package/surface-store.d.ts +0 -5
  31. package/surface-store.js +5 -15
  32. package/use-diagram.svelte.d.ts +8 -0
  33. package/use-diagram.svelte.js +18 -0
  34. package/use-surface.svelte.d.ts +7 -0
  35. package/use-surface.svelte.js +18 -0
  36. package/use-visuallyjs-update.svelte.d.ts +19 -0
  37. package/use-visuallyjs-update.svelte.js +37 -0
  38. package/use-zoom.svelte.d.ts +1 -3
  39. package/use-zoom.svelte.js +14 -11
package/definitions.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { DOMAttributes } from "svelte/elements";
2
- import { SurfaceOptions, Vertex, Group, Node, ControlsComponentButtons, DataGeneratorFunction, type BrowserElement, Base, Size, ObjectData, DropTargetInfo, PaletteMode, SvgExportUIOptions, ImageExportUIOptions, ShapeLibraryImpl, ModelOptions, DiagramOptions, ColumnChartOptions, BarChartOptions, CategoryValueChartOptions, Diagram, DiagramCell, NodeMapping, GroupMapping, PortMapping, BrowserUI, EdgeMapping, OnVertexAddedCallback, CanvasDropFilter, LineChartOptions, AreaChartOptions, PieChartOptions, ScatterChartOptions, BubbleChartOptions, GaugeChartOptions, SankeyOptions, FontSpec, BrowserUIModel, PreparedShape, ChartModelFilter, VisuallyJsDefaultJSON, PointXY, FixedElementConstraints } from "@visuallyjs/browser-ui";
3
1
  import { type Snippet } from "svelte";
2
+ import type { DOMAttributes } from "svelte/elements";
3
+ import { SurfaceOptions, Vertex, Group, Node, ControlsComponentButtons, DataGeneratorFunction, type BrowserElement, Base, Size, ObjectData, DropTargetInfo, PaletteMode, SvgExportUIOptions, ImageExportUIOptions, ShapeLibraryImpl, ModelOptions, DiagramOptions, Diagram, DiagramCell, NodeMapping, GroupMapping, PortMapping, BrowserUI, EdgeMapping, OnVertexAddedCallback, CanvasDropFilter, FontSpec, BrowserUIModel, PreparedShape, PointXY, FixedElementConstraints } from "@visuallyjs/browser-ui";
4
4
  /**
5
5
  * Render options for a SurfaceComponent. This interface is the same as SurfaceOptions but with several vanilla-only options removed.
6
6
  */
@@ -33,6 +33,12 @@ export interface SveltePortMapping extends Omit<PortMapping<any>, "templateIdRes
33
33
  */
34
34
  component?: any;
35
35
  }
36
+ /**
37
+ * Defines a value holder. This is a direct copy of the `RefObject` concept from React.
38
+ */
39
+ export type RefObject<T> = {
40
+ current: T | null;
41
+ };
36
42
  /**
37
43
  * Spec for a react component overlay. A ctx object is passed in to your component and you are expected to return JSX.
38
44
  */
@@ -151,6 +157,14 @@ export interface MiniviewComponentProps extends DOMAttributes<HTMLDivElement> {
151
157
  * Defaults to true, meaning a click on a node/group in the miniview will cause that node/group to be centered in the related surface.
152
158
  */
153
159
  clickToCenter?: boolean;
160
+ /**
161
+ * Defaults to true - the miniview will display a lasso as the user is using the lasso in the canvas
162
+ */
163
+ showLasso?: boolean;
164
+ /**
165
+ * Defaults to true - the miniview will add a CSS class to elements whose model object is in the current selection.
166
+ */
167
+ trackSelection?: boolean;
154
168
  /**
155
169
  * Optional class name to set on the miniview component's container
156
170
  */
@@ -274,17 +288,13 @@ export interface InspectorComponentProps extends DOMAttributes<HTMLDivElement> {
274
288
  * Optional class name to set on the component's root element.
275
289
  */
276
290
  className?: string;
277
- /**
278
- * Callback invoked when the inspector is cleared.
279
- * @internal
280
- */
281
- renderEmptyContainer: () => void;
282
291
  /**
283
292
  * Callback invoked when a new object has started to be edited.
284
293
  * @param obj
285
294
  * @param cb
295
+ * @deprecated From 1.2.0 onwards, use the `current` state object approach instead.
286
296
  */
287
- refresh: (obj: Base) => void;
297
+ refresh?: (obj: Base) => void;
288
298
  /**
289
299
  * Optional filter function that you can supply if you want to ignore certain objects in your dataset.
290
300
  */
@@ -298,6 +308,12 @@ export interface InspectorComponentProps extends DOMAttributes<HTMLDivElement> {
298
308
  * Whether or not to auto commit changes on blur or enter keypress. Defaults to true.
299
309
  */
300
310
  autoCommit?: boolean;
311
+ /**
312
+ * A $state ref that the inspector will apply 2-way binding to. Since 1.2.0 this is the preferred way to use an inspector, as it reduces the amount of boilerplate you need to configure.
313
+ * @since 1.2.0
314
+ */
315
+ current?: Base | null;
316
+ children?: Snippet;
301
317
  }
302
318
  /**
303
319
  * @group Props
@@ -470,256 +486,10 @@ export interface DiagramComponentProps extends DOMAttributes<HTMLDivElement> {
470
486
  * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
471
487
  */
472
488
  url?: string;
473
- }
474
- /**
475
- * @group Props
476
- */
477
- export interface ColumnChartComponentProps extends DOMAttributes<HTMLDivElement> {
478
- /**
479
- * Options for the chart
480
- */
481
- options: Omit<ColumnChartOptions, "data" | "url" | "model">;
482
- /**
483
- * Optional class name to set on the chart component's container
484
- */
485
- className?: string;
486
- /**
487
- * Optional data to load after the chart has been mounted.
488
- */
489
- data?: Array<ObjectData>;
490
- /**
491
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
492
- */
493
- url?: string;
494
- /**
495
- * Optional filter to apply to the data source.
496
- */
497
- dataSourceFilter?: ChartModelFilter;
498
- }
499
- /**
500
- * @group Props
501
- */
502
- export interface BarChartComponentProps extends DOMAttributes<HTMLDivElement> {
503
- /**
504
- * Options for the chart
505
- */
506
- options: Omit<BarChartOptions, "data" | "url" | "model">;
507
- /**
508
- * Optional class name to set on the chart component's container
509
- */
510
- className?: string;
511
- /**
512
- * Optional data to load after the chart has been mounted.
513
- */
514
- data?: Array<ObjectData>;
515
- /**
516
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
517
- */
518
- url?: string;
519
- /**
520
- * Optional filter to apply to the data source.
521
- */
522
- dataSourceFilter?: ChartModelFilter;
523
- }
524
- /**
525
- * @group Props
526
- */
527
- export interface XYChartComponentProps extends DOMAttributes<HTMLDivElement> {
528
- /**
529
- * Options for the chart
530
- */
531
- options: Omit<CategoryValueChartOptions, "data" | "url" | "model">;
532
- /**
533
- * Optional class name to set on the chart component's container
534
- */
535
- className?: string;
536
- /**
537
- * Optional data to load after the chart has been mounted.
538
- */
539
- data?: Array<ObjectData>;
540
- /**
541
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
542
- */
543
- url?: string;
544
- }
545
- /**
546
- * @group Props
547
- */
548
- export interface LineChartComponentProps extends DOMAttributes<HTMLDivElement> {
549
- /**
550
- * Options for the chart
551
- */
552
- options: Omit<LineChartOptions, "data" | "url" | "model">;
553
- /**
554
- * Optional class name to set on the chart component's container
555
- */
556
- className?: string;
557
- /**
558
- * Optional data to load after the chart has been mounted.
559
- */
560
- data?: Array<ObjectData>;
561
- /**
562
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
563
- */
564
- url?: string;
565
- /**
566
- * Optional filter to apply to the data source.
567
- */
568
- dataSourceFilter?: ChartModelFilter;
569
- }
570
- /**
571
- * @group Props
572
- */
573
- export interface AreaChartComponentProps extends DOMAttributes<HTMLDivElement> {
574
- /**
575
- * Options for the chart
576
- */
577
- options: Omit<AreaChartOptions, "data" | "url" | "model">;
578
- /**
579
- * Optional class name to set on the chart component's container
580
- */
581
- className?: string;
582
- /**
583
- * Optional data to load after the chart has been mounted.
584
- */
585
- data?: Array<ObjectData>;
586
- /**
587
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
588
- */
589
- url?: string;
590
- /**
591
- * Optional filter to apply to the data source.
592
- */
593
- dataSourceFilter?: ChartModelFilter;
594
- }
595
- /**
596
- * @group Props
597
- */
598
- export interface PieChartComponentProps extends DOMAttributes<HTMLDivElement> {
599
- /**
600
- * Options for the chart
601
- */
602
- options: Omit<PieChartOptions, "data" | "url" | "model">;
603
- /**
604
- * Optional class name to set on the chart component's container
605
- */
606
- className?: string;
607
- /**
608
- * Optional data to load after the chart has been mounted.
609
- */
610
- data?: Array<ObjectData>;
611
- /**
612
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
613
- */
614
- url?: string;
615
- /**
616
- * Optional filter to apply to the data source.
617
- */
618
- dataSourceFilter?: ChartModelFilter;
619
- }
620
- /**
621
- * @group Props
622
- */
623
- export interface ScatterChartComponentProps extends DOMAttributes<HTMLDivElement> {
624
- /**
625
- * Options for the chart
626
- */
627
- options: Omit<ScatterChartOptions, "data" | "url" | "model">;
628
- /**
629
- * Optional class name to set on the chart component's container
630
- */
631
- className?: string;
632
- /**
633
- * Optional data to load after the chart has been mounted.
634
- */
635
- data?: Array<ObjectData>;
636
- /**
637
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
638
- */
639
- url?: string;
640
- /**
641
- * Optional filter to apply to the data source.
642
- */
643
- dataSourceFilter?: ChartModelFilter;
644
- }
645
- /**
646
- * @group Props
647
- */
648
- export interface BubbleChartComponentProps extends DOMAttributes<HTMLDivElement> {
649
- /**
650
- * Options for the chart
651
- */
652
- options: Omit<BubbleChartOptions, "data" | "url" | "model">;
653
- /**
654
- * Optional class name to set on the chart component's container
655
- */
656
- className?: string;
657
- /**
658
- * Optional data to load after the chart has been mounted.
659
- */
660
- data?: Array<ObjectData>;
661
- /**
662
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
663
- */
664
- url?: string;
665
- /**
666
- * Optional filter to apply to the data source.
667
- */
668
- dataSourceFilter?: ChartModelFilter;
669
- }
670
- /**
671
- * @group Props
672
- */
673
- export interface GaugeChartComponentProps extends DOMAttributes<HTMLDivElement> {
674
- /**
675
- * Options for the chart
676
- */
677
- options: Omit<GaugeChartOptions, "data" | "url" | "model">;
678
- /**
679
- * Optional class name to set on the chart component's container
680
- */
681
- className?: string;
682
- /**
683
- * Optional data to load after the chart has been mounted.
684
- */
685
- data?: Array<ObjectData>;
686
489
  /**
687
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
688
- */
689
- url?: string;
690
- }
691
- /**
692
- * @group Props
693
- */
694
- export interface SankeyChartComponentProps extends DOMAttributes<HTMLDivElement> {
695
- /**
696
- * Options for the chart
697
- */
698
- options: Omit<SankeyOptions, "data" | "url" | "model">;
699
- /**
700
- * Optional class name to set on the chart component's container
701
- */
702
- className?: string;
703
- /**
704
- * Optional data to load into the chart (in CSV format)
705
- */
706
- csvData?: string;
707
- /**
708
- * Optional data to load into the chart (in VisuallyJs default json format)
709
- */
710
- jsonData?: VisuallyJsDefaultJSON;
711
- /**
712
- * Optional url from which to load data after the component has been mounted. If you provide this and also data, this will take precedence.
713
- */
714
- url?: string;
715
- /**
716
- * Whether the chart is interactive.
717
- */
718
- interactive?: boolean;
719
- /**
720
- * Optional property to pivot the sankey chart on.
490
+ * @internal
721
491
  */
722
- pivot?: string;
492
+ children?: Snippet;
723
493
  }
724
494
  /**
725
495
  * Props for the diagram palette.
@@ -838,4 +608,8 @@ export interface DecoratorComponentProps extends DOMAttributes<HTMLDivElement> {
838
608
  * Optional constraints when using fixed placement.
839
609
  */
840
610
  constraints?: FixedElementConstraints;
611
+ /**
612
+ * @internal
613
+ */
614
+ children?: Snippet;
841
615
  }
@@ -23,8 +23,3 @@ export declare function internal_createDiagramContext(): DiagramRef;
23
23
  * @internal
24
24
  */
25
25
  export declare function internal_getDiagramContext(doNotCreate: boolean): DiagramRef;
26
- /**
27
- * Hook to expose the current Surface. This hook is asynchronous and returns a Promise.
28
- * @group Hooks
29
- */
30
- export declare function useDiagram(): Promise<Diagram>;
package/diagram-store.js CHANGED
@@ -41,18 +41,3 @@ export function internal_getDiagramContext(doNotCreate) {
41
41
  }
42
42
  return ctx;
43
43
  }
44
- /**
45
- * Hook to expose the current Surface. This hook is asynchronous and returns a Promise.
46
- * @group Hooks
47
- */
48
- export function useDiagram() {
49
- const ref = internal_getDiagramContext(true);
50
- return new Promise((resolve, reject) => {
51
- if (ref != null) {
52
- ref.listen((s) => resolve(s));
53
- }
54
- else {
55
- reject();
56
- }
57
- });
58
- }
package/index.d.ts CHANGED
@@ -11,7 +11,11 @@ export * from './diagram-store';
11
11
  export * from './surface-store';
12
12
  export * from './inspector-store';
13
13
  export * from './definitions';
14
+ export * from './charts/definitions';
14
15
  export * from './use-zoom.svelte';
16
+ export * from './use-surface.svelte';
17
+ export * from './use-diagram.svelte';
18
+ export * from './use-visuallyjs-update.svelte';
15
19
  import { BrowserElement, BrowserUIModel, BrowserUIOptions, TemplateRenderer, ModelOptions } from "@visuallyjs/browser-ui";
16
20
  /**
17
21
  * Extension of VisuallyJsModel suitable for use with the Svelte integration.
package/index.js CHANGED
@@ -11,7 +11,11 @@ export * from './diagram-store';
11
11
  export * from './surface-store';
12
12
  export * from './inspector-store';
13
13
  export * from './definitions';
14
+ export * from './charts/definitions';
14
15
  export * from './use-zoom.svelte';
16
+ export * from './use-surface.svelte';
17
+ export * from './use-diagram.svelte';
18
+ export * from './use-visuallyjs-update.svelte';
15
19
  import { BrowserUIModel, log } from "@visuallyjs/browser-ui";
16
20
  /**
17
21
  * Extension of VisuallyJsModel suitable for use with the Svelte integration.
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@visuallyjs/browser-ui-svelte","version":"1.1.3","description":"VisuallyJS Svelte integration for Browser UI renderer","module":"index.js","main":"index.js","types":"index.d.ts","svelte":"index.js","files":["**/*.d.ts","**/*.js","SurfaceComponent.svelte","DecoratorComponent.svelte","DefaultGroupComponent.svelte","DefaultNodeComponent.svelte","MiniviewComponent.svelte","ControlsComponent.svelte","SurfaceProvider.svelte","PaletteComponent.svelte","InspectorComponent.svelte","EdgeTypePickerComponent.svelte","ShapePaletteComponent.svelte","ExportControlsComponent.svelte","ShapeComponent.svelte","DiagramComponent.svelte","BarChartComponent.svelte","ColumnChartComponent.svelte","XYChartComponent.svelte","LineChartComponent.svelte","AreaChartComponent.svelte","PieChartComponent.svelte","WrapperComponent.svelte","DiagramProvider.svelte","DiagramPaletteComponent.svelte","ColorPickerComponent.svelte","ScatterChartComponent.svelte","BubbleChartComponent.svelte","GaugeChartComponent.svelte","SankeyChartComponent.svelte"],"author":"VisuallyJs <hello@visuallyjs.com> (https://visuallyjs.com)","license":"Commercial","dependencies":{"@visuallyjs/browser-ui":"1.1.3"},"homepage":"https://visuallyjs.com/svelte","bugs":"https://github.com/visuallyjs/visuallyjs/issues"}
1
+ {"name":"@visuallyjs/browser-ui-svelte","version":"1.2.0","description":"VisuallyJS Svelte integration for Browser UI renderer","module":"index.js","main":"index.js","types":"index.d.ts","svelte":"index.js","files":["**/*.d.ts","**/*.js","SurfaceComponent.svelte","DecoratorComponent.svelte","DefaultGroupComponent.svelte","DefaultNodeComponent.svelte","MiniviewComponent.svelte","ControlsComponent.svelte","SurfaceProvider.svelte","PaletteComponent.svelte","InspectorComponent.svelte","EdgeTypePickerComponent.svelte","ShapePaletteComponent.svelte","ExportControlsComponent.svelte","ShapeComponent.svelte","DiagramComponent.svelte","BarChartComponent.svelte","ColumnChartComponent.svelte","XYChartComponent.svelte","LineChartComponent.svelte","AreaChartComponent.svelte","PieChartComponent.svelte","WrapperComponent.svelte","DiagramProvider.svelte","DiagramPaletteComponent.svelte","ColorPickerComponent.svelte","ScatterChartComponent.svelte","BubbleChartComponent.svelte","GaugeChartComponent.svelte","SankeyChartComponent.svelte"],"author":"VisuallyJs <hello@visuallyjs.com> (https://visuallyjs.com)","license":"Commercial","dependencies":{"@visuallyjs/browser-ui":"1.2.0"},"homepage":"https://visuallyjs.com/svelte","bugs":"https://github.com/visuallyjs/visuallyjs/issues"}
@@ -29,11 +29,6 @@ export declare function internal_createSurfaceContext(): SurfaceRef;
29
29
  * @internal
30
30
  */
31
31
  export declare function internal_getSurfaceContext(doNotCreate: boolean): SurfaceRef;
32
- /**
33
- * Hook to expose the current Surface. This hook is asynchronous and returns a Promise.
34
- * @group Hooks
35
- */
36
- export declare function useSurface(): Promise<Surface>;
37
32
  /**
38
33
  * Hook to expose the current model. This hook is asynchronous and returns a Promise.
39
34
  * @group Hooks
package/surface-store.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { getContext, setContext } from "svelte";
2
+ import { internal_getDiagramContext } from "./index";
2
3
  /**
3
4
  * Key for the surface context
4
5
  * @internal
@@ -41,31 +42,20 @@ export function internal_getSurfaceContext(doNotCreate) {
41
42
  }
42
43
  return ctx;
43
44
  }
44
- /**
45
- * Hook to expose the current Surface. This hook is asynchronous and returns a Promise.
46
- * @group Hooks
47
- */
48
- export function useSurface() {
49
- const ref = internal_getSurfaceContext(true);
50
- return new Promise((resolve, reject) => {
51
- if (ref != null) {
52
- ref.listen((s) => resolve(s));
53
- }
54
- else {
55
- reject();
56
- }
57
- });
58
- }
59
45
  /**
60
46
  * Hook to expose the current model. This hook is asynchronous and returns a Promise.
61
47
  * @group Hooks
62
48
  */
63
49
  export function useVisuallyJsModel() {
64
50
  const ref = internal_getSurfaceContext(true);
51
+ const diagramRef = internal_getDiagramContext(true);
65
52
  return new Promise((resolve, reject) => {
66
53
  if (ref != null) {
67
54
  ref.listen((s) => resolve(s.model));
68
55
  }
56
+ else if (diagramRef != null) {
57
+ diagramRef.listen((d) => resolve(d.model));
58
+ }
69
59
  else {
70
60
  reject();
71
61
  }
@@ -0,0 +1,8 @@
1
+ import { Diagram } from '@visuallyjs/browser-ui';
2
+ /**
3
+ *
4
+ * @group Hooks
5
+ */
6
+ export declare function useDiagram(): {
7
+ readonly current: Diagram | null;
8
+ };
@@ -0,0 +1,18 @@
1
+ import { internal_getDiagramContext } from "./diagram-store";
2
+ /**
3
+ *
4
+ * @group Hooks
5
+ */
6
+ export function useDiagram() {
7
+ const ctx = internal_getDiagramContext(true);
8
+ let diagram = $state(null);
9
+ if (ctx != null) {
10
+ ctx.listen((s) => diagram = s);
11
+ }
12
+ // Return an object with a getter to maintain reactivity across function boundaries
13
+ return {
14
+ get current() {
15
+ return diagram;
16
+ }
17
+ };
18
+ }
@@ -0,0 +1,7 @@
1
+ import { Surface } from '@visuallyjs/browser-ui';
2
+ import { RefObject } from "./definitions";
3
+ /**
4
+ * Creates a reactive zoom state for a VisuallyJS UI
5
+ * @group Hooks
6
+ */
7
+ export declare function useSurface(): RefObject<Surface>;
@@ -0,0 +1,18 @@
1
+ import { internal_getSurfaceContext } from "./surface-store";
2
+ /**
3
+ * Creates a reactive zoom state for a VisuallyJS UI
4
+ * @group Hooks
5
+ */
6
+ export function useSurface() {
7
+ const ctx = internal_getSurfaceContext(true);
8
+ let surface = $state(null);
9
+ if (ctx != null) {
10
+ ctx.listen((s) => surface = s);
11
+ }
12
+ // Return an object with a getter to maintain reactivity across function boundaries
13
+ return {
14
+ get current() {
15
+ return surface;
16
+ }
17
+ };
18
+ }
@@ -0,0 +1,19 @@
1
+ import { BrowserUISvelteModel } from "./index";
2
+ /**
3
+ * Simple hook that responds to all update events in the model, including when the model is cleared. You pass in a function that takes the model as argument, and that should be invoked when an update/clear event occurs. The function is also invoked when the initial model reference is obtained.
4
+ *
5
+ * ```jsx
6
+ * <script>
7
+ * import { useVisuallyJsUpdate } from "@visuallyjs/browser-ui-svelte"
8
+ *
9
+ * let count = $state(0)
10
+ * useVisuallyJsUpdate((model) => count = model.getNodeCount())
11
+ * </script>
12
+ *
13
+ * <h1>{count}</h1>
14
+ * ```
15
+ *
16
+ * @param callback Callback to invoke when an update occurs.
17
+ * @group Hooks
18
+ */
19
+ export declare function useVisuallyJsUpdate(callback: (model: BrowserUISvelteModel) => any): void;
@@ -0,0 +1,37 @@
1
+ import { EVENT_DATA_UPDATED, EVENT_GRAPH_CLEARED } from "@visuallyjs/browser-ui";
2
+ import { useDiagram } from "./index";
3
+ import { useSurface } from "./use-surface.svelte";
4
+ /**
5
+ * Simple hook that responds to all update events in the model, including when the model is cleared. You pass in a function that takes the model as argument, and that should be invoked when an update/clear event occurs. The function is also invoked when the initial model reference is obtained.
6
+ *
7
+ * ```jsx
8
+ * <script>
9
+ * import { useVisuallyJsUpdate } from "@visuallyjs/browser-ui-svelte"
10
+ *
11
+ * let count = $state(0)
12
+ * useVisuallyJsUpdate((model) => count = model.getNodeCount())
13
+ * </script>
14
+ *
15
+ * <h1>{count}</h1>
16
+ * ```
17
+ *
18
+ * @param callback Callback to invoke when an update occurs.
19
+ * @group Hooks
20
+ */
21
+ export function useVisuallyJsUpdate(callback) {
22
+ const ui = useSurface();
23
+ const diagram = useDiagram();
24
+ $effect(() => {
25
+ const vjs = ui.current?.model || diagram.current?.model;
26
+ if (vjs != null) {
27
+ const fn = () => callback(vjs);
28
+ vjs.bind(EVENT_DATA_UPDATED, fn);
29
+ vjs.bind(EVENT_GRAPH_CLEARED, fn);
30
+ fn();
31
+ return (() => {
32
+ vjs.unbind(EVENT_DATA_UPDATED, fn);
33
+ vjs.unbind(EVENT_GRAPH_CLEARED, fn);
34
+ });
35
+ }
36
+ });
37
+ }
@@ -1,9 +1,7 @@
1
- import { BrowserUI } from '@visuallyjs/browser-ui';
2
1
  /**
3
2
  * Creates a reactive zoom state for a VisuallyJS UI
4
- * @param ui The Surface instance provided via props.
5
3
  * @group Hooks
6
4
  */
7
- export declare function useZoom(ui: BrowserUI): {
5
+ export declare function useZoom(): {
8
6
  readonly current: number;
9
7
  };
@@ -1,21 +1,24 @@
1
1
  import { EVENT_ZOOM } from '@visuallyjs/browser-ui';
2
+ import { useSurface } from "./use-surface.svelte";
2
3
  /**
3
4
  * Creates a reactive zoom state for a VisuallyJS UI
4
- * @param ui The Surface instance provided via props.
5
5
  * @group Hooks
6
6
  */
7
- export function useZoom(ui) {
8
- let zoom = $state(ui.getZoom());
7
+ export function useZoom() {
8
+ let zoom = $state(1);
9
+ let uiRef = useSurface();
9
10
  // Manage event listener lifecycle
10
11
  $effect(() => {
11
- const handler = (p) => {
12
- zoom = p.zoom;
13
- };
14
- ui.bind(EVENT_ZOOM, handler);
15
- // Auto-cleanup when the component/effect is destroyed
16
- return () => {
17
- ui.unbind(EVENT_ZOOM, handler);
18
- };
12
+ if (uiRef.current != null) {
13
+ const handler = (p) => {
14
+ zoom = p.zoom;
15
+ };
16
+ uiRef.current.bind(EVENT_ZOOM, handler);
17
+ // Auto-cleanup when the component/effect is destroyed
18
+ return () => {
19
+ uiRef.current.unbind(EVENT_ZOOM, handler);
20
+ };
21
+ }
19
22
  });
20
23
  // Return an object with a getter to maintain reactivity across function boundaries
21
24
  return {