react-svg-canvas 0.1.4 → 0.1.5

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.
@@ -42,8 +42,8 @@ export declare function computeScore(target: SnapTarget, distance: number, dragS
42
42
  /**
43
43
  * Main snap computation for drag operations
44
44
  */
45
- export declare function computeSnap(context: DragSnapContext, objects: SnapSpatialObject[], viewBounds: Bounds, config: SnapConfiguration, getParent?: (id: string) => string | undefined): SnapResult;
45
+ export declare function computeSnap(context: DragSnapContext, objects: SnapSpatialObject[], viewBounds: Bounds, config: SnapConfiguration, getParent?: (id: string) => string | undefined, customTargets?: SnapTarget[]): SnapResult;
46
46
  /**
47
47
  * Compute snapping for resize operations
48
48
  */
49
- export declare function computeResizeSnap(context: ResizeSnapContext, objects: SnapSpatialObject[], viewBounds: Bounds, config: SnapConfiguration, getParent?: (id: string) => string | undefined): ResizeSnapResult;
49
+ export declare function computeResizeSnap(context: ResizeSnapContext, objects: SnapSpatialObject[], viewBounds: Bounds, config: SnapConfiguration, getParent?: (id: string) => string | undefined, customTargets?: SnapTarget[]): ResizeSnapResult;
@@ -304,7 +304,7 @@ function getDragSnapValuesForAxis(snapPoints, axis, grabPoint) {
304
304
  /**
305
305
  * Main snap computation for drag operations
306
306
  */
307
- export function computeSnap(context, objects, viewBounds, config, getParent) {
307
+ export function computeSnap(context, objects, viewBounds, config, getParent, customTargets) {
308
308
  if (!config.enabled) {
309
309
  return {
310
310
  snappedPosition: { x: context.draggedBounds.x, y: context.draggedBounds.y },
@@ -320,7 +320,9 @@ export function computeSnap(context, objects, viewBounds, config, getParent) {
320
320
  }
321
321
  // Generate all snap targets with geometric relevance filtering
322
322
  const excludeIds = new Set([context.draggedId]);
323
- const targets = generateAllSnapTargets(objects, excludeIds, viewBounds, config, context.draggedBounds);
323
+ const generatedTargets = generateAllSnapTargets(objects, excludeIds, viewBounds, config, context.draggedBounds);
324
+ // Merge with custom targets (e.g., from table grids)
325
+ const targets = customTargets ? [...generatedTargets, ...customTargets] : generatedTargets;
324
326
  // Get snap points from dragged bounds
325
327
  const draggedSnapPoints = getSnapPoints(context.draggedBounds);
326
328
  // Score all candidates
@@ -499,7 +501,7 @@ export function computeSnap(context, objects, viewBounds, config, getParent) {
499
501
  /**
500
502
  * Compute snapping for resize operations
501
503
  */
502
- export function computeResizeSnap(context, objects, viewBounds, config, getParent) {
504
+ export function computeResizeSnap(context, objects, viewBounds, config, getParent, customTargets) {
503
505
  if (!config.enabled) {
504
506
  return {
505
507
  snappedBounds: context.currentBounds,
@@ -514,7 +516,9 @@ export function computeResizeSnap(context, objects, viewBounds, config, getParen
514
516
  objectBoundsMap.set(obj.id, getAABB(obj));
515
517
  }
516
518
  const excludeIds = new Set([context.objectId]);
517
- const targets = generateAllSnapTargets(objects, excludeIds, viewBounds, config);
519
+ const generatedTargets = generateAllSnapTargets(objects, excludeIds, viewBounds, config);
520
+ // Merge with custom targets (e.g., from table grids)
521
+ const targets = customTargets ? [...generatedTargets, ...customTargets] : generatedTargets;
518
522
  // For resize, we snap the edges being resized
519
523
  const currentSnapPoints = getSnapPoints(context.currentBounds);
520
524
  // Determine which edges are being resized based on handle
@@ -2,12 +2,14 @@
2
2
  * React hook for snapping functionality
3
3
  */
4
4
  import type { Point, Bounds, ResizeHandle } from '../types';
5
- import type { SnapConfiguration, SnapSpatialObject, RotatedBounds, ActiveSnap, ActiveSnapEdge, ScoredCandidate } from './types';
5
+ import type { SnapConfiguration, SnapSpatialObject, SnapTarget, RotatedBounds, ActiveSnap, ActiveSnapEdge, ScoredCandidate } from './types';
6
6
  export interface UseSnappingOptions {
7
7
  objects: SnapSpatialObject[];
8
8
  config: SnapConfiguration;
9
9
  viewBounds: Bounds;
10
10
  getParent?: (id: string) => string | undefined;
11
+ /** Additional snap targets (e.g., from table grids, guides) */
12
+ customTargets?: SnapTarget[];
11
13
  }
12
14
  export interface SnapDragParams {
13
15
  bounds: RotatedBounds;
@@ -38,7 +38,7 @@ function normalizeDirection(delta) {
38
38
  * Hook for snapping during drag and resize operations
39
39
  */
40
40
  export function useSnapping(options) {
41
- const { objects, config, viewBounds, getParent } = options;
41
+ const { objects, config, viewBounds, getParent, customTargets } = options;
42
42
  // State for active snaps, candidates, and active snap edges
43
43
  const [activeSnaps, setActiveSnaps] = React.useState([]);
44
44
  const [allCandidates, setAllCandidates] = React.useState([]);
@@ -66,7 +66,7 @@ export function useSnapping(options) {
66
66
  delta
67
67
  };
68
68
  const finalExcludeIds = excludeIds || new Set([objectId]);
69
- const result = computeSnap(context, objectsRef.current, viewBounds, config, getParent);
69
+ const result = computeSnap(context, objectsRef.current, viewBounds, config, getParent, customTargets);
70
70
  setActiveSnaps(result.activeSnaps);
71
71
  setAllCandidates(result.candidates);
72
72
  setActiveSnapEdges(result.activeSnapEdges);
@@ -76,7 +76,7 @@ export function useSnapping(options) {
76
76
  candidates: result.candidates,
77
77
  activeSnapEdges: result.activeSnapEdges
78
78
  };
79
- }, [config, viewBounds, getParent]);
79
+ }, [config, viewBounds, getParent, customTargets]);
80
80
  const snapResize = React.useCallback((params) => {
81
81
  const { originalBounds, currentBounds, objectId, handle, delta, excludeIds } = params;
82
82
  const context = {
@@ -86,7 +86,7 @@ export function useSnapping(options) {
86
86
  handle,
87
87
  delta
88
88
  };
89
- const result = computeResizeSnap(context, objectsRef.current, viewBounds, config, getParent);
89
+ const result = computeResizeSnap(context, objectsRef.current, viewBounds, config, getParent, customTargets);
90
90
  setActiveSnaps(result.activeSnaps);
91
91
  setAllCandidates(result.candidates);
92
92
  return {
@@ -94,7 +94,7 @@ export function useSnapping(options) {
94
94
  activeSnaps: result.activeSnaps,
95
95
  candidates: result.candidates
96
96
  };
97
- }, [config, viewBounds, getParent]);
97
+ }, [config, viewBounds, getParent, customTargets]);
98
98
  const clearSnaps = React.useCallback(() => {
99
99
  setActiveSnaps([]);
100
100
  setAllCandidates([]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-svg-canvas",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "React library for building interactive SVG canvas applications with pan, zoom, selection, drag-and-drop, resize, and Figma-style snapping",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",