@tldraw/editor 3.16.0-canary.c2c4563957ce → 3.16.0-canary.c360426d8b7a

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 (114) hide show
  1. package/dist-cjs/index.d.ts +59 -3
  2. package/dist-cjs/index.js +6 -2
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +2 -2
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +11 -1
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/editor/Editor.js +9 -4
  9. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  10. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js +4 -0
  11. package/dist-cjs/lib/editor/derivations/notVisibleShapes.js.map +2 -2
  12. package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js +4 -2
  13. package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js.map +2 -2
  14. package/dist-cjs/lib/editor/shapes/ShapeUtil.js +10 -0
  15. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  16. package/dist-cjs/lib/hooks/useCanvasEvents.js +15 -12
  17. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  18. package/dist-cjs/lib/hooks/useDocumentEvents.js +5 -5
  19. package/dist-cjs/lib/hooks/useDocumentEvents.js.map +2 -2
  20. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js +1 -2
  21. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js.map +2 -2
  22. package/dist-cjs/lib/hooks/useGestureEvents.js +1 -1
  23. package/dist-cjs/lib/hooks/useGestureEvents.js.map +2 -2
  24. package/dist-cjs/lib/hooks/useHandleEvents.js +3 -3
  25. package/dist-cjs/lib/hooks/useHandleEvents.js.map +2 -2
  26. package/dist-cjs/lib/hooks/useSelectionEvents.js +4 -4
  27. package/dist-cjs/lib/hooks/useSelectionEvents.js.map +2 -2
  28. package/dist-cjs/lib/license/LicenseManager.js +133 -38
  29. package/dist-cjs/lib/license/LicenseManager.js.map +2 -2
  30. package/dist-cjs/lib/license/LicenseProvider.js +36 -3
  31. package/dist-cjs/lib/license/LicenseProvider.js.map +2 -2
  32. package/dist-cjs/lib/license/Watermark.js +143 -75
  33. package/dist-cjs/lib/license/Watermark.js.map +3 -3
  34. package/dist-cjs/lib/primitives/geometry/Geometry2d.js +24 -2
  35. package/dist-cjs/lib/primitives/geometry/Geometry2d.js.map +2 -2
  36. package/dist-cjs/lib/primitives/geometry/Group2d.js +5 -1
  37. package/dist-cjs/lib/primitives/geometry/Group2d.js.map +2 -2
  38. package/dist-cjs/lib/utils/dom.js +12 -1
  39. package/dist-cjs/lib/utils/dom.js.map +2 -2
  40. package/dist-cjs/lib/utils/getPointerInfo.js +2 -2
  41. package/dist-cjs/lib/utils/getPointerInfo.js.map +2 -2
  42. package/dist-cjs/version.js +3 -3
  43. package/dist-cjs/version.js.map +1 -1
  44. package/dist-esm/index.d.mts +59 -3
  45. package/dist-esm/index.mjs +9 -3
  46. package/dist-esm/index.mjs.map +2 -2
  47. package/dist-esm/lib/TldrawEditor.mjs +3 -3
  48. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  49. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +12 -2
  50. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  51. package/dist-esm/lib/editor/Editor.mjs +9 -4
  52. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  53. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs +4 -0
  54. package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs.map +2 -2
  55. package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs +4 -2
  56. package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs.map +2 -2
  57. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +10 -0
  58. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  59. package/dist-esm/lib/hooks/useCanvasEvents.mjs +17 -13
  60. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  61. package/dist-esm/lib/hooks/useDocumentEvents.mjs +11 -6
  62. package/dist-esm/lib/hooks/useDocumentEvents.mjs.map +2 -2
  63. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs +2 -3
  64. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs.map +2 -2
  65. package/dist-esm/lib/hooks/useGestureEvents.mjs +2 -2
  66. package/dist-esm/lib/hooks/useGestureEvents.mjs.map +2 -2
  67. package/dist-esm/lib/hooks/useHandleEvents.mjs +9 -4
  68. package/dist-esm/lib/hooks/useHandleEvents.mjs.map +2 -2
  69. package/dist-esm/lib/hooks/useSelectionEvents.mjs +6 -5
  70. package/dist-esm/lib/hooks/useSelectionEvents.mjs.map +2 -2
  71. package/dist-esm/lib/license/LicenseManager.mjs +134 -39
  72. package/dist-esm/lib/license/LicenseManager.mjs.map +2 -2
  73. package/dist-esm/lib/license/LicenseProvider.mjs +36 -4
  74. package/dist-esm/lib/license/LicenseProvider.mjs.map +2 -2
  75. package/dist-esm/lib/license/Watermark.mjs +144 -76
  76. package/dist-esm/lib/license/Watermark.mjs.map +3 -3
  77. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +24 -2
  78. package/dist-esm/lib/primitives/geometry/Geometry2d.mjs.map +2 -2
  79. package/dist-esm/lib/primitives/geometry/Group2d.mjs +5 -1
  80. package/dist-esm/lib/primitives/geometry/Group2d.mjs.map +2 -2
  81. package/dist-esm/lib/utils/dom.mjs +12 -1
  82. package/dist-esm/lib/utils/dom.mjs.map +2 -2
  83. package/dist-esm/lib/utils/getPointerInfo.mjs +2 -2
  84. package/dist-esm/lib/utils/getPointerInfo.mjs.map +2 -2
  85. package/dist-esm/version.mjs +3 -3
  86. package/dist-esm/version.mjs.map +1 -1
  87. package/editor.css +8 -3
  88. package/package.json +7 -7
  89. package/src/index.ts +3 -0
  90. package/src/lib/TldrawEditor.tsx +3 -4
  91. package/src/lib/components/default-components/DefaultCanvas.tsx +8 -2
  92. package/src/lib/editor/Editor.test.ts +90 -0
  93. package/src/lib/editor/Editor.ts +16 -4
  94. package/src/lib/editor/derivations/notVisibleShapes.ts +6 -0
  95. package/src/lib/editor/managers/FocusManager/FocusManager.ts +6 -2
  96. package/src/lib/editor/shapes/ShapeUtil.ts +11 -0
  97. package/src/lib/hooks/useCanvasEvents.ts +17 -11
  98. package/src/lib/hooks/useDocumentEvents.ts +11 -6
  99. package/src/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.ts +2 -2
  100. package/src/lib/hooks/useGestureEvents.ts +2 -2
  101. package/src/lib/hooks/useHandleEvents.ts +9 -4
  102. package/src/lib/hooks/useSelectionEvents.ts +6 -5
  103. package/src/lib/license/LicenseManager.test.ts +719 -387
  104. package/src/lib/license/LicenseManager.ts +187 -49
  105. package/src/lib/license/LicenseProvider.tsx +69 -5
  106. package/src/lib/license/Watermark.tsx +151 -77
  107. package/src/lib/primitives/geometry/Geometry2d.test.ts +420 -0
  108. package/src/lib/primitives/geometry/Geometry2d.ts +29 -2
  109. package/src/lib/primitives/geometry/Group2d.ts +6 -1
  110. package/src/lib/test/InFrontOfTheCanvas.test.tsx +187 -0
  111. package/src/lib/utils/dom.test.ts +94 -0
  112. package/src/lib/utils/dom.ts +38 -1
  113. package/src/lib/utils/getPointerInfo.ts +2 -1
  114. package/src/version.ts +3 -3
@@ -3,7 +3,7 @@ import { memo, useRef } from 'react'
3
3
  import { useCanvasEvents } from '../hooks/useCanvasEvents'
4
4
  import { useEditor } from '../hooks/useEditor'
5
5
  import { usePassThroughWheelEvents } from '../hooks/usePassThroughWheelEvents'
6
- import { preventDefault, stopEventPropagation } from '../utils/dom'
6
+ import { markEventAsHandled, preventDefault } from '../utils/dom'
7
7
  import { runtime } from '../utils/runtime'
8
8
  import { watermarkDesktopSvg, watermarkMobileSvg } from '../watermarks'
9
9
  import { LicenseManager } from './LicenseManager'
@@ -28,12 +28,62 @@ export const Watermark = memo(function Watermark() {
28
28
  return (
29
29
  <>
30
30
  <LicenseStyles />
31
- <WatermarkInner src={isMobile ? WATERMARK_MOBILE_LOCAL_SRC : WATERMARK_DESKTOP_LOCAL_SRC} />
31
+ <WatermarkInner
32
+ src={isMobile ? WATERMARK_MOBILE_LOCAL_SRC : WATERMARK_DESKTOP_LOCAL_SRC}
33
+ isUnlicensed={licenseManagerState === 'unlicensed'}
34
+ />
32
35
  </>
33
36
  )
34
37
  })
35
38
 
36
- const WatermarkInner = memo(function WatermarkInner({ src }: { src: string }) {
39
+ const UnlicensedWatermark = memo(function UnlicensedWatermark({
40
+ isDebugMode,
41
+ isMobile,
42
+ }: {
43
+ isDebugMode: boolean
44
+ isMobile: boolean
45
+ }) {
46
+ const events = useCanvasEvents()
47
+ const ref = useRef<HTMLDivElement>(null)
48
+ usePassThroughWheelEvents(ref)
49
+
50
+ const url =
51
+ 'https://tldraw.dev/pricing?utm_source=dotcom&utm_medium=organic&utm_campaign=watermark'
52
+
53
+ return (
54
+ <div
55
+ ref={ref}
56
+ className={LicenseManager.className}
57
+ data-debug={isDebugMode}
58
+ data-mobile={isMobile}
59
+ data-unlicensed={true}
60
+ data-testid="tl-watermark-unlicensed"
61
+ draggable={false}
62
+ {...events}
63
+ >
64
+ <button
65
+ draggable={false}
66
+ role="button"
67
+ onPointerDown={(e) => {
68
+ markEventAsHandled(e)
69
+ preventDefault(e)
70
+ }}
71
+ title="The tldraw SDK requires a license key to work in production. You can get a free 100-day trial license at tldraw.dev/pricing."
72
+ onClick={() => runtime.openWindow(url, '_blank')}
73
+ >
74
+ Get a license for production
75
+ </button>
76
+ </div>
77
+ )
78
+ })
79
+
80
+ const WatermarkInner = memo(function WatermarkInner({
81
+ src,
82
+ isUnlicensed,
83
+ }: {
84
+ src: string
85
+ isUnlicensed: boolean
86
+ }) {
37
87
  const editor = useEditor()
38
88
  const isDebugMode = useValue('debug mode', () => editor.getInstanceState().isDebugMode, [editor])
39
89
  const isMobile = useValue('is mobile', () => editor.getViewportScreenBounds().width < 700, [
@@ -47,12 +97,17 @@ const WatermarkInner = memo(function WatermarkInner({ src }: { src: string }) {
47
97
  const maskCss = `url('${src}') center 100% / 100% no-repeat`
48
98
  const url = 'https://tldraw.dev/?utm_source=dotcom&utm_medium=organic&utm_campaign=watermark'
49
99
 
100
+ if (isUnlicensed) {
101
+ return <UnlicensedWatermark isDebugMode={isDebugMode} isMobile={isMobile} />
102
+ }
103
+
50
104
  return (
51
105
  <div
52
106
  ref={ref}
53
107
  className={LicenseManager.className}
54
108
  data-debug={isDebugMode}
55
109
  data-mobile={isMobile}
110
+ data-testid="tl-watermark-licensed"
56
111
  draggable={false}
57
112
  {...events}
58
113
  >
@@ -60,10 +115,10 @@ const WatermarkInner = memo(function WatermarkInner({ src }: { src: string }) {
60
115
  draggable={false}
61
116
  role="button"
62
117
  onPointerDown={(e) => {
63
- stopEventPropagation(e)
118
+ markEventAsHandled(e)
64
119
  preventDefault(e)
65
120
  }}
66
- title="made with tldraw"
121
+ title="Build infinite canvas applications with the tldraw SDK. Learn more at https://tldraw.dev."
67
122
  onClick={() => runtime.openWindow(url, '_blank')}
68
123
  style={{ mask: maskCss, WebkitMask: maskCss }}
69
124
  />
@@ -75,7 +130,8 @@ const LicenseStyles = memo(function LicenseStyles() {
75
130
  const editor = useEditor()
76
131
  const className = LicenseManager.className
77
132
 
78
- const CSS = `/* ------------------- SEE LICENSE -------------------
133
+ const CSS = `
134
+ /* ------------------- SEE LICENSE -------------------
79
135
  The tldraw watermark is part of tldraw's license. It is shown for unlicensed
80
136
  or "licensed-with-watermark" users. By using this library, you agree to
81
137
  preserve the watermark's behavior, keeping it visible, unobscured, and
@@ -84,87 +140,105 @@ available to user-interaction.
84
140
  To remove the watermark, please purchase a license at tldraw.dev.
85
141
  */
86
142
 
87
- .${className} {
88
- position: absolute;
89
- bottom: max(var(--tl-space-2), env(safe-area-inset-bottom));
90
- right: max(var(--tl-space-2), env(safe-area-inset-right));
91
- width: 96px;
92
- height: 32px;
93
- display: flex;
94
- align-items: center;
95
- justify-content: center;
96
- z-index: var(--tl-layer-watermark) !important;
97
- background-color: color-mix(in srgb, var(--tl-color-background) 62%, transparent);
98
- opacity: 1;
99
- border-radius: 5px;
100
- pointer-events: all;
101
- padding: 2px;
102
- box-sizing: content-box;
143
+ .${className} {
144
+ position: absolute;
145
+ bottom: max(var(--tl-space-2), env(safe-area-inset-bottom));
146
+ right: max(var(--tl-space-2), env(safe-area-inset-right));
147
+ width: 96px;
148
+ height: 32px;
149
+ display: flex;
150
+ align-items: center;
151
+ justify-content: center;
152
+ z-index: var(--tl-layer-watermark) !important;
153
+ background-color: color-mix(in srgb, var(--tl-color-background) 62%, transparent);
154
+ opacity: 1;
155
+ border-radius: 5px;
156
+ pointer-events: all;
157
+ padding: 2px;
158
+ box-sizing: content-box;
159
+ }
160
+
161
+ .${className} > button {
162
+ position: absolute;
163
+ width: 96px;
164
+ height: 32px;
165
+ pointer-events: all;
166
+ cursor: inherit;
167
+ color: var(--tl-color-text);
168
+ opacity: .38;
169
+ border: 0;
170
+ padding: 0;
171
+ background-color: currentColor;
172
+ }
173
+
174
+ .${className}[data-debug='true'] {
175
+ bottom: max(46px, env(safe-area-inset-bottom));
176
+ }
177
+
178
+ .${className}[data-mobile='true'] {
179
+ border-radius: 4px 0px 0px 4px;
180
+ right: max(-2px, calc(env(safe-area-inset-right) - 2px));
181
+ width: 8px;
182
+ height: 48px;
183
+ }
184
+
185
+ .${className}[data-mobile='true'] > button {
186
+ width: 8px;
187
+ height: 32px;
188
+ }
189
+
190
+ .${className}[data-unlicensed='true'] > button {
191
+ font-size: 100px;
192
+ position: absolute;
193
+ pointer-events: all;
194
+ cursor: pointer;
195
+ color: var(--tl-color-text);
196
+ opacity: 0.8;
197
+ border: 0;
198
+ padding: 0;
199
+ background-color: transparent;
200
+ font-size: 11px;
201
+ font-weight: 600;
202
+ text-align: center;
203
+ }
204
+
205
+ .${className}[data-mobile='true'][data-unlicensed='true'] > button {
206
+ display: none;
207
+ }
208
+
209
+ @media (hover: hover) {
210
+ .${className}[data-licensed='false'] > button {
211
+ pointer-events: none;
103
212
  }
104
213
 
105
- .${className} > button {
106
- position: absolute;
107
- width: 96px;
108
- height: 32px;
109
- pointer-events: all;
110
- cursor: inherit;
111
- color: var(--tl-color-text);
112
- opacity: .38;
113
- border: 0;
114
- padding: 0;
115
- background-color: currentColor;
214
+ .${className}[data-licensed='false']:hover {
215
+ background-color: var(--tl-color-background);
216
+ transition: background-color 0.2s ease-in-out;
217
+ transition-delay: 0.32s;
116
218
  }
117
219
 
118
- .${className}[data-debug='true'] {
119
- bottom: max(46px, env(safe-area-inset-bottom));
220
+ .${className}[data-licensed='false']:hover > button {
221
+ animation: ${className}_delayed_link 0.2s forwards ease-in-out;
222
+ animation-delay: 0.32s;
120
223
  }
121
224
 
122
- .${className}[data-mobile='true'] {
123
- border-radius: 4px 0px 0px 4px;
124
- right: max(-2px, calc(env(safe-area-inset-right) - 2px));
125
- width: 8px;
126
- height: 48px;
225
+ .${className}[data-licensed='false'] > button:focus-visible {
226
+ opacity: 1;
127
227
  }
228
+ }
128
229
 
129
- .${className}[data-mobile='true'] > button {
130
- width: 8px;
131
- height: 32px;
230
+ @keyframes ${className}_delayed_link {
231
+ 0% {
232
+ cursor: inherit;
233
+ opacity: .38;
234
+ pointer-events: none;
132
235
  }
133
-
134
- @media (hover: hover) {
135
- .${className} > button {
136
- pointer-events: none;
137
- }
138
-
139
- .${className}:hover {
140
- background-color: var(--tl-color-background);
141
- transition: background-color 0.2s ease-in-out;
142
- transition-delay: 0.32s;
143
- }
144
-
145
- .${className}:hover > button {
146
- animation: ${className}_delayed_link 0.2s forwards ease-in-out;
147
- animation-delay: 0.32s;
148
- }
149
-
150
- .${className} > button:focus-visible {
151
- opacity: 1;
152
- }
236
+ 100% {
237
+ cursor: pointer;
238
+ opacity: 1;
239
+ pointer-events: all;
153
240
  }
154
-
155
-
156
- @keyframes ${className}_delayed_link {
157
- 0% {
158
- cursor: inherit;
159
- opacity: .38;
160
- pointer-events: none;
161
- }
162
- 100% {
163
- cursor: pointer;
164
- opacity: 1;
165
- pointer-events: all;
166
- }
167
- }`
241
+ }`
168
242
 
169
243
  return <style nonce={editor.options.nonce}>{CSS}</style>
170
244
  })
@@ -1,6 +1,7 @@
1
1
  import { Mat } from '../Mat'
2
2
  import { Vec, VecLike } from '../Vec'
3
3
  import { Geometry2dFilters } from './Geometry2d'
4
+ import { Group2d } from './Group2d'
4
5
  import { Rectangle2d } from './Rectangle2d'
5
6
 
6
7
  describe('TransformedGeometry2d', () => {
@@ -36,6 +37,425 @@ describe('TransformedGeometry2d', () => {
36
37
  })
37
38
  })
38
39
 
40
+ describe('excludeFromShapeBounds', () => {
41
+ test('simple geometry with excludeFromShapeBounds flag', () => {
42
+ const rect = new Rectangle2d({
43
+ width: 100,
44
+ height: 50,
45
+ isFilled: true,
46
+ excludeFromShapeBounds: true,
47
+ })
48
+
49
+ // The bounds should still be calculated normally for simple geometry
50
+ const bounds = rect.bounds
51
+ expect(bounds.width).toBe(100)
52
+ expect(bounds.height).toBe(50)
53
+ expect(bounds.x).toBe(0)
54
+ expect(bounds.y).toBe(0)
55
+ })
56
+
57
+ test('group with excluded child geometry', () => {
58
+ const mainRect = new Rectangle2d({
59
+ width: 100,
60
+ height: 50,
61
+ isFilled: true,
62
+ })
63
+
64
+ const excludedRect = new Rectangle2d({
65
+ width: 200,
66
+ height: 100,
67
+ isFilled: true,
68
+ excludeFromShapeBounds: true,
69
+ })
70
+
71
+ const group = new Group2d({
72
+ children: [mainRect, excludedRect],
73
+ })
74
+
75
+ // The bounds should only include the non-excluded rectangle
76
+ const bounds = group.bounds
77
+ expect(bounds.width).toBe(100) // Only the main rectangle width
78
+ expect(bounds.height).toBe(50) // Only the main rectangle height
79
+ expect(bounds.x).toBe(0)
80
+ expect(bounds.y).toBe(0)
81
+ })
82
+
83
+ test('group with multiple excluded children', () => {
84
+ const rect1 = new Rectangle2d({
85
+ width: 50,
86
+ height: 50,
87
+ isFilled: true,
88
+ })
89
+
90
+ const rect2 = new Rectangle2d({
91
+ width: 100,
92
+ height: 30,
93
+ isFilled: true,
94
+ })
95
+
96
+ const excludedRect1 = new Rectangle2d({
97
+ width: 200,
98
+ height: 200,
99
+ isFilled: true,
100
+ excludeFromShapeBounds: true,
101
+ })
102
+
103
+ const excludedRect2 = new Rectangle2d({
104
+ width: 300,
105
+ height: 300,
106
+ isFilled: true,
107
+ excludeFromShapeBounds: true,
108
+ })
109
+
110
+ const group = new Group2d({
111
+ children: [rect1, excludedRect1, rect2, excludedRect2],
112
+ })
113
+
114
+ // The bounds should include both non-excluded rectangles
115
+ const bounds = group.bounds
116
+ expect(bounds.width).toBe(100) // Width of rect2 (larger of the two)
117
+ expect(bounds.height).toBe(50) // Height of rect1 (larger of the two)
118
+ expect(bounds.x).toBe(0)
119
+ expect(bounds.y).toBe(0)
120
+ })
121
+
122
+ test('group with all children excluded', () => {
123
+ const excludedRect1 = new Rectangle2d({
124
+ width: 100,
125
+ height: 50,
126
+ isFilled: true,
127
+ excludeFromShapeBounds: true,
128
+ })
129
+
130
+ const excludedRect2 = new Rectangle2d({
131
+ width: 200,
132
+ height: 100,
133
+ isFilled: true,
134
+ excludeFromShapeBounds: true,
135
+ })
136
+
137
+ const group = new Group2d({
138
+ children: [excludedRect1, excludedRect2],
139
+ })
140
+
141
+ // The bounds should be empty when all children are excluded
142
+ const bounds = group.bounds
143
+ expect(bounds.width).toBe(0)
144
+ expect(bounds.height).toBe(0)
145
+ expect(bounds.x).toBe(0)
146
+ expect(bounds.y).toBe(0)
147
+ })
148
+
149
+ test('nested groups with excluded geometry', () => {
150
+ const innerRect = new Rectangle2d({
151
+ width: 50,
152
+ height: 50,
153
+ isFilled: true,
154
+ })
155
+
156
+ const excludedRect = new Rectangle2d({
157
+ width: 200,
158
+ height: 200,
159
+ isFilled: true,
160
+ excludeFromShapeBounds: true,
161
+ })
162
+
163
+ const innerGroup = new Group2d({
164
+ children: [innerRect, excludedRect],
165
+ })
166
+
167
+ const outerRect = new Rectangle2d({
168
+ width: 100,
169
+ height: 30,
170
+ isFilled: true,
171
+ })
172
+
173
+ const outerGroup = new Group2d({
174
+ children: [innerGroup, outerRect],
175
+ })
176
+
177
+ // The bounds should include both the inner group (without excluded rect) and outer rect
178
+ const bounds = outerGroup.bounds
179
+ expect(bounds.width).toBe(100) // Width of outerRect (larger)
180
+ expect(bounds.height).toBe(50) // Height of innerRect (larger)
181
+ expect(bounds.x).toBe(0)
182
+ expect(bounds.y).toBe(0)
183
+ })
184
+
185
+ test('bounds calculation with transformed geometry', () => {
186
+ const rect = new Rectangle2d({
187
+ width: 50,
188
+ height: 50,
189
+ isFilled: true,
190
+ }).transform(Mat.Translate(100, 100))
191
+
192
+ const excludedRect = new Rectangle2d({
193
+ width: 200,
194
+ height: 200,
195
+ isFilled: true,
196
+ excludeFromShapeBounds: true,
197
+ }).transform(Mat.Translate(50, 50))
198
+
199
+ const group = new Group2d({
200
+ children: [rect, excludedRect],
201
+ })
202
+
203
+ // The bounds should only include the non-excluded rectangle
204
+ const bounds = group.bounds
205
+ // Verify that the excluded rectangle doesn't affect the bounds
206
+ // The bounds should be smaller than if the excluded rect was included
207
+ expect(bounds.width).toBeLessThan(200) // Should not include the excluded rect's width
208
+ expect(bounds.height).toBeLessThan(200) // Should not include the excluded rect's height
209
+ // The bounds should not be empty
210
+ expect(bounds.width).toBeGreaterThan(0)
211
+ expect(bounds.height).toBeGreaterThan(0)
212
+ })
213
+ })
214
+
215
+ describe('getBoundsVertices', () => {
216
+ test('basic geometry returns vertices when not excluded from bounds', () => {
217
+ const rect = new Rectangle2d({
218
+ width: 100,
219
+ height: 50,
220
+ isFilled: true,
221
+ })
222
+
223
+ const boundsVertices = rect.getBoundsVertices()
224
+ const vertices = rect.getVertices()
225
+
226
+ expect(boundsVertices).toEqual(vertices)
227
+ expect(boundsVertices.length).toBe(4)
228
+ expect(boundsVertices).toMatchObject([
229
+ { x: 0, y: 0, z: 1 },
230
+ { x: 100, y: 0, z: 1 },
231
+ { x: 100, y: 50, z: 1 },
232
+ { x: 0, y: 50, z: 1 },
233
+ ])
234
+ })
235
+
236
+ test('geometry excluded from shape bounds returns empty array', () => {
237
+ const rect = new Rectangle2d({
238
+ width: 100,
239
+ height: 50,
240
+ isFilled: true,
241
+ excludeFromShapeBounds: true,
242
+ })
243
+
244
+ const boundsVertices = rect.getBoundsVertices()
245
+ expect(boundsVertices).toEqual([])
246
+ })
247
+
248
+ test('cached boundsVertices property', () => {
249
+ const rect = new Rectangle2d({
250
+ width: 100,
251
+ height: 50,
252
+ isFilled: true,
253
+ })
254
+
255
+ // Access the cached property multiple times
256
+ const boundsVertices1 = rect.boundsVertices
257
+ const boundsVertices2 = rect.boundsVertices
258
+
259
+ // Should return the same reference (cached)
260
+ expect(boundsVertices1).toBe(boundsVertices2)
261
+ expect(boundsVertices1.length).toBe(4)
262
+ })
263
+ })
264
+
265
+ describe('TransformedGeometry2d getBoundsVertices', () => {
266
+ test('transforms bounds vertices correctly', () => {
267
+ const rect = new Rectangle2d({
268
+ width: 100,
269
+ height: 50,
270
+ isFilled: true,
271
+ })
272
+
273
+ const transformed = rect.transform(Mat.Translate(50, 100).scale(2, 2))
274
+ const boundsVertices = transformed.getBoundsVertices()
275
+
276
+ expect(boundsVertices).toMatchObject([
277
+ { x: 50, y: 100, z: 1 },
278
+ { x: 250, y: 100, z: 1 },
279
+ { x: 250, y: 200, z: 1 },
280
+ { x: 50, y: 200, z: 1 },
281
+ ])
282
+ })
283
+
284
+ test('transforms empty bounds vertices for excluded geometry', () => {
285
+ const rect = new Rectangle2d({
286
+ width: 100,
287
+ height: 50,
288
+ isFilled: true,
289
+ excludeFromShapeBounds: true,
290
+ })
291
+
292
+ const transformed = rect.transform(Mat.Translate(50, 100))
293
+ const boundsVertices = transformed.getBoundsVertices()
294
+
295
+ expect(boundsVertices).toEqual([])
296
+ })
297
+
298
+ test('nested transform preserves bounds vertices behavior', () => {
299
+ const rect = new Rectangle2d({
300
+ width: 100,
301
+ height: 50,
302
+ isFilled: true,
303
+ })
304
+
305
+ const transformed1 = rect.transform(Mat.Translate(10, 20))
306
+ const transformed2 = transformed1.transform(Mat.Scale(2, 2))
307
+ const boundsVertices = transformed2.getBoundsVertices()
308
+
309
+ expect(boundsVertices).toMatchObject([
310
+ { x: 20, y: 40, z: 1 },
311
+ { x: 220, y: 40, z: 1 },
312
+ { x: 220, y: 140, z: 1 },
313
+ { x: 20, y: 140, z: 1 },
314
+ ])
315
+ })
316
+ })
317
+
318
+ describe('Group2d getBoundsVertices', () => {
319
+ test('flattens children bounds vertices', () => {
320
+ const rect1 = new Rectangle2d({
321
+ width: 50,
322
+ height: 50,
323
+ isFilled: true,
324
+ })
325
+
326
+ const rect2 = new Rectangle2d({
327
+ width: 30,
328
+ height: 30,
329
+ isFilled: true,
330
+ }).transform(Mat.Translate(60, 60))
331
+
332
+ const group = new Group2d({
333
+ children: [rect1, rect2],
334
+ })
335
+
336
+ const boundsVertices = group.getBoundsVertices()
337
+
338
+ // Should include all vertices from both rectangles
339
+ expect(boundsVertices.length).toBe(8) // 4 vertices from each rectangle
340
+
341
+ // Check that we have vertices from both rectangles
342
+ expect(boundsVertices).toEqual(
343
+ expect.arrayContaining([
344
+ expect.objectContaining({ x: 0, y: 0 }), // rect1 vertices
345
+ expect.objectContaining({ x: 50, y: 0 }),
346
+ expect.objectContaining({ x: 50, y: 50 }),
347
+ expect.objectContaining({ x: 0, y: 50 }),
348
+ expect.objectContaining({ x: 60, y: 60 }), // rect2 vertices
349
+ expect.objectContaining({ x: 90, y: 60 }),
350
+ expect.objectContaining({ x: 90, y: 90 }),
351
+ expect.objectContaining({ x: 60, y: 90 }),
352
+ ])
353
+ )
354
+ })
355
+
356
+ test('excludes children marked as excluded from bounds', () => {
357
+ const rect1 = new Rectangle2d({
358
+ width: 50,
359
+ height: 50,
360
+ isFilled: true,
361
+ })
362
+
363
+ const rect2 = new Rectangle2d({
364
+ width: 100,
365
+ height: 100,
366
+ isFilled: true,
367
+ excludeFromShapeBounds: true,
368
+ })
369
+
370
+ const group = new Group2d({
371
+ children: [rect1, rect2],
372
+ })
373
+
374
+ const boundsVertices = group.getBoundsVertices()
375
+
376
+ // Should only include vertices from rect1, not rect2
377
+ expect(boundsVertices.length).toBe(4) // Only rect1's 4 vertices
378
+ expect(boundsVertices).toMatchObject([
379
+ { x: 0, y: 0, z: 1 },
380
+ { x: 50, y: 0, z: 1 },
381
+ { x: 50, y: 50, z: 1 },
382
+ { x: 0, y: 50, z: 1 },
383
+ ])
384
+ })
385
+
386
+ test('returns empty array when group itself is excluded from bounds', () => {
387
+ const rect1 = new Rectangle2d({
388
+ width: 50,
389
+ height: 50,
390
+ isFilled: true,
391
+ })
392
+
393
+ const rect2 = new Rectangle2d({
394
+ width: 30,
395
+ height: 30,
396
+ isFilled: true,
397
+ })
398
+
399
+ const group = new Group2d({
400
+ children: [rect1, rect2],
401
+ excludeFromShapeBounds: true,
402
+ })
403
+
404
+ const boundsVertices = group.getBoundsVertices()
405
+ expect(boundsVertices).toEqual([])
406
+ })
407
+
408
+ test('handles nested groups correctly', () => {
409
+ const rect1 = new Rectangle2d({
410
+ width: 50,
411
+ height: 50,
412
+ isFilled: true,
413
+ })
414
+
415
+ const rect2 = new Rectangle2d({
416
+ width: 30,
417
+ height: 30,
418
+ isFilled: true,
419
+ })
420
+
421
+ const innerGroup = new Group2d({
422
+ children: [rect2],
423
+ })
424
+
425
+ const outerGroup = new Group2d({
426
+ children: [rect1, innerGroup],
427
+ })
428
+
429
+ const boundsVertices = outerGroup.getBoundsVertices()
430
+
431
+ // Should include vertices from both rectangles
432
+ expect(boundsVertices.length).toBe(8) // 4 vertices from each rectangle
433
+ })
434
+
435
+ test('handles all children excluded from bounds', () => {
436
+ const rect1 = new Rectangle2d({
437
+ width: 50,
438
+ height: 50,
439
+ isFilled: true,
440
+ excludeFromShapeBounds: true,
441
+ })
442
+
443
+ const rect2 = new Rectangle2d({
444
+ width: 30,
445
+ height: 30,
446
+ isFilled: true,
447
+ excludeFromShapeBounds: true,
448
+ })
449
+
450
+ const group = new Group2d({
451
+ children: [rect1, rect2],
452
+ })
453
+
454
+ const boundsVertices = group.getBoundsVertices()
455
+ expect(boundsVertices).toEqual([])
456
+ })
457
+ })
458
+
39
459
  function expectApproxMatch(a: VecLike, b: VecLike) {
40
460
  expect(a.x).toBeCloseTo(b.x, 0.0001)
41
461
  expect(a.y).toBeCloseTo(b.y, 0.0001)