@editframe/elements 0.20.4-beta.0 → 0.23.6-beta.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 (183) hide show
  1. package/dist/DelayedLoadingState.js +0 -27
  2. package/dist/EF_FRAMEGEN.d.ts +5 -3
  3. package/dist/EF_FRAMEGEN.js +49 -11
  4. package/dist/_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js +7 -0
  5. package/dist/attachContextRoot.d.ts +1 -0
  6. package/dist/attachContextRoot.js +9 -0
  7. package/dist/elements/ContextProxiesController.d.ts +1 -2
  8. package/dist/elements/EFAudio.js +5 -9
  9. package/dist/elements/EFCaptions.d.ts +1 -3
  10. package/dist/elements/EFCaptions.js +112 -129
  11. package/dist/elements/EFImage.js +6 -7
  12. package/dist/elements/EFMedia/AssetIdMediaEngine.js +2 -5
  13. package/dist/elements/EFMedia/AssetMediaEngine.js +36 -33
  14. package/dist/elements/EFMedia/BaseMediaEngine.js +57 -73
  15. package/dist/elements/EFMedia/BufferedSeekingInput.d.ts +1 -1
  16. package/dist/elements/EFMedia/BufferedSeekingInput.js +134 -78
  17. package/dist/elements/EFMedia/JitMediaEngine.js +9 -19
  18. package/dist/elements/EFMedia/audioTasks/makeAudioBufferTask.js +7 -13
  19. package/dist/elements/EFMedia/audioTasks/makeAudioFrequencyAnalysisTask.js +2 -3
  20. package/dist/elements/EFMedia/audioTasks/makeAudioInitSegmentFetchTask.js +1 -1
  21. package/dist/elements/EFMedia/audioTasks/makeAudioInputTask.js +6 -5
  22. package/dist/elements/EFMedia/audioTasks/makeAudioSeekTask.js +1 -3
  23. package/dist/elements/EFMedia/audioTasks/makeAudioSegmentFetchTask.js +1 -1
  24. package/dist/elements/EFMedia/audioTasks/makeAudioSegmentIdTask.js +1 -1
  25. package/dist/elements/EFMedia/audioTasks/makeAudioTimeDomainAnalysisTask.js +1 -1
  26. package/dist/elements/EFMedia/shared/AudioSpanUtils.js +9 -25
  27. package/dist/elements/EFMedia/shared/BufferUtils.js +2 -17
  28. package/dist/elements/EFMedia/shared/GlobalInputCache.js +0 -24
  29. package/dist/elements/EFMedia/shared/PrecisionUtils.js +0 -21
  30. package/dist/elements/EFMedia/shared/ThumbnailExtractor.js +0 -17
  31. package/dist/elements/EFMedia/tasks/makeMediaEngineTask.js +1 -10
  32. package/dist/elements/EFMedia/videoTasks/MainVideoInputCache.d.ts +29 -0
  33. package/dist/elements/EFMedia/videoTasks/MainVideoInputCache.js +32 -0
  34. package/dist/elements/EFMedia/videoTasks/ScrubInputCache.js +1 -15
  35. package/dist/elements/EFMedia/videoTasks/makeScrubVideoBufferTask.js +1 -7
  36. package/dist/elements/EFMedia/videoTasks/makeScrubVideoInputTask.js +8 -5
  37. package/dist/elements/EFMedia/videoTasks/makeScrubVideoSeekTask.js +12 -13
  38. package/dist/elements/EFMedia/videoTasks/makeScrubVideoSegmentIdTask.js +1 -1
  39. package/dist/elements/EFMedia/videoTasks/makeUnifiedVideoSeekTask.js +134 -70
  40. package/dist/elements/EFMedia/videoTasks/makeVideoBufferTask.js +11 -18
  41. package/dist/elements/EFMedia.d.ts +19 -0
  42. package/dist/elements/EFMedia.js +44 -25
  43. package/dist/elements/EFSourceMixin.js +5 -7
  44. package/dist/elements/EFSurface.js +6 -9
  45. package/dist/elements/EFTemporal.browsertest.d.ts +11 -0
  46. package/dist/elements/EFTemporal.d.ts +10 -0
  47. package/dist/elements/EFTemporal.js +100 -41
  48. package/dist/elements/EFThumbnailStrip.js +23 -73
  49. package/dist/elements/EFTimegroup.browsertest.d.ts +3 -3
  50. package/dist/elements/EFTimegroup.d.ts +35 -14
  51. package/dist/elements/EFTimegroup.js +138 -181
  52. package/dist/elements/EFVideo.d.ts +16 -2
  53. package/dist/elements/EFVideo.js +156 -108
  54. package/dist/elements/EFWaveform.js +23 -40
  55. package/dist/elements/SampleBuffer.js +3 -7
  56. package/dist/elements/TargetController.js +5 -5
  57. package/dist/elements/durationConverter.js +4 -4
  58. package/dist/elements/renderTemporalAudio.d.ts +10 -0
  59. package/dist/elements/renderTemporalAudio.js +35 -0
  60. package/dist/elements/updateAnimations.js +19 -43
  61. package/dist/gui/ContextMixin.d.ts +5 -5
  62. package/dist/gui/ContextMixin.js +167 -162
  63. package/dist/gui/Controllable.browsertest.d.ts +0 -0
  64. package/dist/gui/Controllable.d.ts +15 -0
  65. package/dist/gui/Controllable.js +9 -0
  66. package/dist/gui/EFConfiguration.js +7 -7
  67. package/dist/gui/EFControls.browsertest.d.ts +11 -0
  68. package/dist/gui/EFControls.d.ts +18 -4
  69. package/dist/gui/EFControls.js +70 -28
  70. package/dist/gui/EFDial.browsertest.d.ts +0 -0
  71. package/dist/gui/EFDial.d.ts +18 -0
  72. package/dist/gui/EFDial.js +141 -0
  73. package/dist/gui/EFFilmstrip.browsertest.d.ts +11 -0
  74. package/dist/gui/EFFilmstrip.d.ts +12 -2
  75. package/dist/gui/EFFilmstrip.js +214 -129
  76. package/dist/gui/EFFitScale.js +5 -8
  77. package/dist/gui/EFFocusOverlay.js +4 -4
  78. package/dist/gui/EFPause.browsertest.d.ts +0 -0
  79. package/dist/gui/EFPause.d.ts +23 -0
  80. package/dist/gui/EFPause.js +59 -0
  81. package/dist/gui/EFPlay.browsertest.d.ts +0 -0
  82. package/dist/gui/EFPlay.d.ts +23 -0
  83. package/dist/gui/EFPlay.js +59 -0
  84. package/dist/gui/EFPreview.d.ts +4 -0
  85. package/dist/gui/EFPreview.js +18 -9
  86. package/dist/gui/EFResizableBox.browsertest.d.ts +0 -0
  87. package/dist/gui/EFResizableBox.d.ts +34 -0
  88. package/dist/gui/EFResizableBox.js +547 -0
  89. package/dist/gui/EFScrubber.d.ts +9 -3
  90. package/dist/gui/EFScrubber.js +13 -13
  91. package/dist/gui/EFTimeDisplay.d.ts +7 -1
  92. package/dist/gui/EFTimeDisplay.js +8 -8
  93. package/dist/gui/EFToggleLoop.d.ts +9 -3
  94. package/dist/gui/EFToggleLoop.js +7 -5
  95. package/dist/gui/EFTogglePlay.d.ts +12 -4
  96. package/dist/gui/EFTogglePlay.js +26 -21
  97. package/dist/gui/EFWorkbench.js +5 -5
  98. package/dist/gui/PlaybackController.d.ts +67 -0
  99. package/dist/gui/PlaybackController.js +310 -0
  100. package/dist/gui/TWMixin.js +1 -1
  101. package/dist/gui/TWMixin2.js +1 -1
  102. package/dist/gui/TargetOrContextMixin.d.ts +10 -0
  103. package/dist/gui/TargetOrContextMixin.js +98 -0
  104. package/dist/gui/efContext.d.ts +2 -2
  105. package/dist/index.d.ts +5 -0
  106. package/dist/index.js +5 -1
  107. package/dist/otel/BridgeSpanExporter.d.ts +13 -0
  108. package/dist/otel/BridgeSpanExporter.js +87 -0
  109. package/dist/otel/setupBrowserTracing.d.ts +12 -0
  110. package/dist/otel/setupBrowserTracing.js +32 -0
  111. package/dist/otel/tracingHelpers.d.ts +34 -0
  112. package/dist/otel/tracingHelpers.js +112 -0
  113. package/dist/style.css +1 -1
  114. package/dist/transcoding/cache/RequestDeduplicator.js +0 -21
  115. package/dist/transcoding/cache/URLTokenDeduplicator.js +1 -21
  116. package/dist/transcoding/utils/UrlGenerator.js +2 -19
  117. package/dist/utils/LRUCache.js +6 -53
  118. package/package.json +13 -5
  119. package/src/elements/ContextProxiesController.ts +10 -10
  120. package/src/elements/EFAudio.ts +1 -0
  121. package/src/elements/EFCaptions.browsertest.ts +128 -56
  122. package/src/elements/EFCaptions.ts +60 -34
  123. package/src/elements/EFImage.browsertest.ts +1 -2
  124. package/src/elements/EFMedia/AssetMediaEngine.ts +65 -37
  125. package/src/elements/EFMedia/BaseMediaEngine.ts +110 -52
  126. package/src/elements/EFMedia/BufferedSeekingInput.ts +218 -101
  127. package/src/elements/EFMedia/JitMediaEngine.browsertest.ts +3 -0
  128. package/src/elements/EFMedia/audioTasks/makeAudioInputTask.ts +7 -3
  129. package/src/elements/EFMedia/audioTasks/makeAudioSeekTask.chunkboundary.regression.browsertest.ts +1 -1
  130. package/src/elements/EFMedia/videoTasks/MainVideoInputCache.ts +76 -0
  131. package/src/elements/EFMedia/videoTasks/makeScrubVideoInputTask.ts +16 -10
  132. package/src/elements/EFMedia/videoTasks/makeScrubVideoSeekTask.ts +7 -1
  133. package/src/elements/EFMedia/videoTasks/makeUnifiedVideoSeekTask.ts +222 -116
  134. package/src/elements/EFMedia.browsertest.ts +8 -15
  135. package/src/elements/EFMedia.ts +54 -8
  136. package/src/elements/EFSurface.browsertest.ts +2 -6
  137. package/src/elements/EFSurface.ts +1 -0
  138. package/src/elements/EFTemporal.browsertest.ts +58 -1
  139. package/src/elements/EFTemporal.ts +140 -4
  140. package/src/elements/EFThumbnailStrip.browsertest.ts +2 -8
  141. package/src/elements/EFThumbnailStrip.ts +1 -0
  142. package/src/elements/EFTimegroup.browsertest.ts +16 -15
  143. package/src/elements/EFTimegroup.ts +281 -275
  144. package/src/elements/EFVideo.browsertest.ts +162 -74
  145. package/src/elements/EFVideo.ts +229 -101
  146. package/src/elements/FetchContext.browsertest.ts +7 -2
  147. package/src/elements/TargetController.browsertest.ts +1 -0
  148. package/src/elements/TargetController.ts +1 -0
  149. package/src/elements/renderTemporalAudio.ts +108 -0
  150. package/src/elements/updateAnimations.browsertest.ts +181 -6
  151. package/src/elements/updateAnimations.ts +6 -6
  152. package/src/gui/ContextMixin.browsertest.ts +274 -27
  153. package/src/gui/ContextMixin.ts +230 -175
  154. package/src/gui/Controllable.browsertest.ts +258 -0
  155. package/src/gui/Controllable.ts +41 -0
  156. package/src/gui/EFControls.browsertest.ts +294 -80
  157. package/src/gui/EFControls.ts +139 -28
  158. package/src/gui/EFDial.browsertest.ts +84 -0
  159. package/src/gui/EFDial.ts +172 -0
  160. package/src/gui/EFFilmstrip.browsertest.ts +712 -0
  161. package/src/gui/EFFilmstrip.ts +213 -23
  162. package/src/gui/EFPause.browsertest.ts +202 -0
  163. package/src/gui/EFPause.ts +73 -0
  164. package/src/gui/EFPlay.browsertest.ts +202 -0
  165. package/src/gui/EFPlay.ts +73 -0
  166. package/src/gui/EFPreview.ts +20 -5
  167. package/src/gui/EFResizableBox.browsertest.ts +79 -0
  168. package/src/gui/EFResizableBox.ts +898 -0
  169. package/src/gui/EFScrubber.ts +7 -5
  170. package/src/gui/EFTimeDisplay.browsertest.ts +19 -19
  171. package/src/gui/EFTimeDisplay.ts +3 -1
  172. package/src/gui/EFToggleLoop.ts +6 -5
  173. package/src/gui/EFTogglePlay.ts +30 -23
  174. package/src/gui/PlaybackController.ts +522 -0
  175. package/src/gui/TWMixin.css +3 -0
  176. package/src/gui/TargetOrContextMixin.ts +185 -0
  177. package/src/gui/efContext.ts +2 -2
  178. package/src/otel/BridgeSpanExporter.ts +150 -0
  179. package/src/otel/setupBrowserTracing.ts +73 -0
  180. package/src/otel/tracingHelpers.ts +251 -0
  181. package/test/cache-integration-verification.browsertest.ts +1 -1
  182. package/types.json +1 -1
  183. package/dist/elements/ContextProxiesController.js +0 -69
@@ -0,0 +1,898 @@
1
+ import { css, html, LitElement } from "lit";
2
+ import { customElement, property, state } from "lit/decorators.js";
3
+ import { styleMap } from "lit/directives/style-map.js";
4
+
5
+ // Constants
6
+ const DEFAULT_MIN_SIZE = 10;
7
+ const CENTER_RESIZE_MULTIPLIER = 2;
8
+
9
+ export interface BoxBounds {
10
+ x: number;
11
+ y: number;
12
+ width: number;
13
+ height: number;
14
+ }
15
+
16
+ type ResizeCorner = "top-left" | "top-right" | "bottom-left" | "bottom-right";
17
+ type ResizeSide = "top" | "right" | "bottom" | "left";
18
+ type ResizeHandle = ResizeCorner | ResizeSide;
19
+
20
+ interface Dimensions {
21
+ width: number;
22
+ height: number;
23
+ }
24
+
25
+ interface ResizeContext {
26
+ readonly initialBounds: BoxBounds;
27
+ readonly container: Dimensions;
28
+ readonly constraints: {
29
+ minSize: number;
30
+ aspectRatio?: number;
31
+ };
32
+ readonly movement: {
33
+ deltaX: number;
34
+ deltaY: number;
35
+ };
36
+ readonly modifiers: {
37
+ centerResize: boolean;
38
+ preserveAspectRatio: boolean;
39
+ };
40
+ }
41
+
42
+ // Pure calculation functions
43
+ function constrainMovementDeltas(
44
+ initialBounds: BoxBounds,
45
+ deltaX: number,
46
+ deltaY: number,
47
+ container: Dimensions,
48
+ ): { deltaX: number; deltaY: number } {
49
+ const maxLeftMovement = -initialBounds.x;
50
+ const maxRightMovement =
51
+ container.width - (initialBounds.x + initialBounds.width);
52
+ const maxUpMovement = -initialBounds.y;
53
+ const maxDownMovement =
54
+ container.height - (initialBounds.y + initialBounds.height);
55
+
56
+ return {
57
+ deltaX: Math.max(maxLeftMovement, Math.min(maxRightMovement, deltaX)),
58
+ deltaY: Math.max(maxUpMovement, Math.min(maxDownMovement, deltaY)),
59
+ };
60
+ }
61
+
62
+ function calculateNormalResize(
63
+ context: ResizeContext,
64
+ handle: ResizeHandle,
65
+ ): BoxBounds {
66
+ const { initialBounds, movement } = context;
67
+ const { deltaX, deltaY } = movement;
68
+
69
+ switch (handle) {
70
+ case "bottom-right":
71
+ return {
72
+ ...initialBounds,
73
+ width: initialBounds.width + deltaX,
74
+ height: initialBounds.height + deltaY,
75
+ };
76
+
77
+ case "top-left": {
78
+ const rightEdge = initialBounds.x + initialBounds.width;
79
+ const bottomEdge = initialBounds.y + initialBounds.height;
80
+ const newX = initialBounds.x + deltaX;
81
+ const newY = initialBounds.y + deltaY;
82
+
83
+ return {
84
+ x: newX,
85
+ y: newY,
86
+ width: rightEdge - newX,
87
+ height: bottomEdge - newY,
88
+ };
89
+ }
90
+
91
+ case "top-right": {
92
+ const bottomEdge = initialBounds.y + initialBounds.height;
93
+ const newY = initialBounds.y + deltaY;
94
+
95
+ return {
96
+ ...initialBounds,
97
+ y: newY,
98
+ width: initialBounds.width + deltaX,
99
+ height: bottomEdge - newY,
100
+ };
101
+ }
102
+
103
+ case "bottom-left": {
104
+ const rightEdge = initialBounds.x + initialBounds.width;
105
+ const newX = initialBounds.x + deltaX;
106
+
107
+ return {
108
+ ...initialBounds,
109
+ x: newX,
110
+ width: rightEdge - newX,
111
+ height: initialBounds.height + deltaY,
112
+ };
113
+ }
114
+
115
+ case "top": {
116
+ const bottomEdge = initialBounds.y + initialBounds.height;
117
+ const newY = initialBounds.y + deltaY;
118
+
119
+ return {
120
+ ...initialBounds,
121
+ y: newY,
122
+ height: bottomEdge - newY,
123
+ };
124
+ }
125
+
126
+ case "right":
127
+ return {
128
+ ...initialBounds,
129
+ width: initialBounds.width + deltaX,
130
+ };
131
+
132
+ case "bottom":
133
+ return {
134
+ ...initialBounds,
135
+ height: initialBounds.height + deltaY,
136
+ };
137
+
138
+ case "left": {
139
+ const rightEdge = initialBounds.x + initialBounds.width;
140
+ const newX = initialBounds.x + deltaX;
141
+
142
+ return {
143
+ ...initialBounds,
144
+ x: newX,
145
+ width: rightEdge - newX,
146
+ };
147
+ }
148
+
149
+ default:
150
+ return initialBounds;
151
+ }
152
+ }
153
+
154
+ function calculateAspectRatioResize(
155
+ context: ResizeContext,
156
+ handle: ResizeHandle,
157
+ ): BoxBounds {
158
+ const { initialBounds, movement, constraints } = context;
159
+ const { deltaX, deltaY } = movement;
160
+ if (!constraints.aspectRatio) {
161
+ return initialBounds;
162
+ }
163
+ const aspectRatio = constraints.aspectRatio;
164
+
165
+ const widthMovement = deltaX;
166
+ const heightMovement = deltaY * aspectRatio;
167
+
168
+ let movementValue: number;
169
+ switch (handle) {
170
+ case "bottom-right":
171
+ case "top-left":
172
+ movementValue = (widthMovement + heightMovement) / 2;
173
+ break;
174
+ case "top-right":
175
+ case "bottom-left":
176
+ movementValue = (widthMovement - heightMovement) / 2;
177
+ break;
178
+ default:
179
+ movementValue = widthMovement;
180
+ }
181
+
182
+ const baseWidth =
183
+ handle === "top-left" || handle === "bottom-left"
184
+ ? initialBounds.width - movementValue
185
+ : initialBounds.width + movementValue;
186
+
187
+ const width = Math.max(constraints.minSize, baseWidth);
188
+ const height = width / aspectRatio;
189
+
190
+ const newBounds: BoxBounds = { ...initialBounds, width, height };
191
+
192
+ // Adjust position for handles that move the origin
193
+ switch (handle) {
194
+ case "top-left":
195
+ newBounds.x = initialBounds.x + initialBounds.width - width;
196
+ newBounds.y = initialBounds.y + initialBounds.height - height;
197
+ break;
198
+ case "top-right":
199
+ newBounds.y = initialBounds.y + initialBounds.height - height;
200
+ break;
201
+ case "bottom-left":
202
+ newBounds.x = initialBounds.x + initialBounds.width - width;
203
+ break;
204
+ }
205
+
206
+ return newBounds;
207
+ }
208
+
209
+ function calculateCenterResize(
210
+ context: ResizeContext,
211
+ handle: ResizeHandle,
212
+ ): BoxBounds {
213
+ const { initialBounds, movement } = context;
214
+ const { deltaX, deltaY } = movement;
215
+
216
+ const centerX = initialBounds.x + initialBounds.width / 2;
217
+ const centerY = initialBounds.y + initialBounds.height / 2;
218
+
219
+ let widthChange = 0;
220
+ let heightChange = 0;
221
+
222
+ switch (handle) {
223
+ case "bottom-right":
224
+ widthChange = deltaX * CENTER_RESIZE_MULTIPLIER;
225
+ heightChange = deltaY * CENTER_RESIZE_MULTIPLIER;
226
+ break;
227
+ case "top-left":
228
+ widthChange = -deltaX * CENTER_RESIZE_MULTIPLIER;
229
+ heightChange = -deltaY * CENTER_RESIZE_MULTIPLIER;
230
+ break;
231
+ case "top-right":
232
+ widthChange = deltaX * CENTER_RESIZE_MULTIPLIER;
233
+ heightChange = -deltaY * CENTER_RESIZE_MULTIPLIER;
234
+ break;
235
+ case "bottom-left":
236
+ widthChange = -deltaX * CENTER_RESIZE_MULTIPLIER;
237
+ heightChange = deltaY * CENTER_RESIZE_MULTIPLIER;
238
+ break;
239
+ case "top":
240
+ case "bottom":
241
+ heightChange =
242
+ (handle === "bottom" ? deltaY : -deltaY) * CENTER_RESIZE_MULTIPLIER;
243
+ break;
244
+ case "left":
245
+ case "right":
246
+ widthChange =
247
+ (handle === "right" ? deltaX : -deltaX) * CENTER_RESIZE_MULTIPLIER;
248
+ break;
249
+ }
250
+
251
+ const newWidth = initialBounds.width + widthChange;
252
+ const newHeight = initialBounds.height + heightChange;
253
+
254
+ return {
255
+ x: centerX - newWidth / 2,
256
+ y: centerY - newHeight / 2,
257
+ width: newWidth,
258
+ height: newHeight,
259
+ };
260
+ }
261
+
262
+ function calculateCenterResizeWithAspectRatio(
263
+ context: ResizeContext,
264
+ handle: ResizeHandle,
265
+ ): BoxBounds {
266
+ const { initialBounds, movement, constraints } = context;
267
+ const { deltaX, deltaY } = movement;
268
+ if (!constraints.aspectRatio) {
269
+ return initialBounds;
270
+ }
271
+ const aspectRatio = constraints.aspectRatio;
272
+
273
+ const centerX = initialBounds.x + initialBounds.width / 2;
274
+ const centerY = initialBounds.y + initialBounds.height / 2;
275
+
276
+ let movementValue: number;
277
+ switch (handle) {
278
+ case "bottom-right":
279
+ movementValue = Math.max(deltaX, deltaY);
280
+ break;
281
+ case "top-left":
282
+ movementValue = -Math.max(-deltaX, -deltaY);
283
+ break;
284
+ case "top-right":
285
+ movementValue = Math.max(deltaX, -deltaY);
286
+ break;
287
+ case "bottom-left":
288
+ movementValue = Math.max(-deltaX, deltaY);
289
+ break;
290
+ case "top":
291
+ case "bottom":
292
+ movementValue = handle === "bottom" ? deltaY : -deltaY;
293
+ break;
294
+ case "left":
295
+ case "right":
296
+ movementValue = handle === "right" ? deltaX : -deltaX;
297
+ break;
298
+ default:
299
+ movementValue = Math.max(deltaX, deltaY);
300
+ }
301
+
302
+ const newWidth = Math.max(
303
+ constraints.minSize,
304
+ initialBounds.width + movementValue * CENTER_RESIZE_MULTIPLIER,
305
+ );
306
+ const newHeight = newWidth / aspectRatio;
307
+
308
+ return {
309
+ x: centerX - newWidth / 2,
310
+ y: centerY - newHeight / 2,
311
+ width: newWidth,
312
+ height: newHeight,
313
+ };
314
+ }
315
+
316
+ @customElement("ef-resizable-box")
317
+ export class EFResizableBox extends LitElement {
318
+ @property({ type: Object })
319
+ bounds: BoxBounds = { x: 0, y: 0, width: 100, height: 100 };
320
+
321
+ @state()
322
+ private containerWidth = 0;
323
+
324
+ @state()
325
+ private containerHeight = 0;
326
+
327
+ @property({ type: Number })
328
+ minSize = DEFAULT_MIN_SIZE;
329
+
330
+ @state()
331
+ private isDragging = false;
332
+
333
+ @state()
334
+ private dragMode: "move" | "resize" | null = null;
335
+
336
+ private interaction: {
337
+ startPoint: { x: number; y: number };
338
+ target: { mode: "move" | "resize"; handle?: ResizeHandle };
339
+ initialBounds: BoxBounds;
340
+ pointerId: number;
341
+ } | null = null;
342
+
343
+ private modifiers = { shift: false, alt: false };
344
+
345
+ static styles = css`
346
+ .box {
347
+ position: absolute;
348
+ border: 2px solid var(--ef-resizable-box-border-color, #3b82f6);
349
+ background-color: var(--ef-resizable-box-bg-color, rgba(59, 130, 246, 0.2));
350
+ cursor: grab;
351
+ }
352
+ .box.dragging {
353
+ border-color: var(--ef-resizable-box-dragging-border-color, #2563eb);
354
+ background-color: var(--ef-resizable-box-dragging-bg-color, rgba(37, 99, 235, 0.3));
355
+ }
356
+ .handle {
357
+ position: absolute;
358
+ background-color: var(--ef-resizable-box-handle-color, #3b82f6);
359
+ touch-action: none;
360
+ }
361
+ .top-left { top: -4px; left: -4px; width: 8px; height: 8px; cursor: nwse-resize; }
362
+ .top-right { top: -4px; right: -4px; width: 8px; height: 8px; cursor: nesw-resize; }
363
+ .bottom-left { bottom: -4px; left: -4px; width: 8px; height: 8px; cursor: nesw-resize; }
364
+ .bottom-right { bottom: -4px; right: -4px; width: 8px; height: 8px; cursor: nwse-resize; }
365
+ .top { top: -4px; left: 4px; right: 4px; height: 8px; cursor: ns-resize; }
366
+ .right { top: 4px; bottom: 4px; right: -4px; width: 8px; cursor: ew-resize; }
367
+ .bottom { bottom: -4px; left: 4px; right: 4px; height: 8px; cursor: ns-resize; }
368
+ .left { top: 4px; bottom: 4px; left: -4px; width: 8px; cursor: ew-resize; }
369
+ `;
370
+
371
+ private resizeObserver?: ResizeObserver;
372
+
373
+ connectedCallback() {
374
+ super.connectedCallback();
375
+ if (this.offsetParent) {
376
+ this.containerWidth = this.offsetParent.clientWidth;
377
+ this.containerHeight = this.offsetParent.clientHeight;
378
+ }
379
+ this.resizeObserver = new ResizeObserver(() => {
380
+ if (this.offsetParent) {
381
+ this.containerWidth = this.offsetParent.clientWidth;
382
+ this.containerHeight = this.offsetParent.clientHeight;
383
+ }
384
+ });
385
+ if (this.offsetParent) {
386
+ this.resizeObserver.observe(this.offsetParent);
387
+ }
388
+ }
389
+
390
+ private handlePointerDown(
391
+ e: PointerEvent,
392
+ mode: "move" | "resize",
393
+ handle?: ResizeHandle,
394
+ ) {
395
+ e.preventDefault();
396
+ e.stopPropagation();
397
+ this.isDragging = true;
398
+ this.dragMode = mode;
399
+
400
+ this.interaction = {
401
+ startPoint: { x: e.clientX, y: e.clientY },
402
+ target: { mode, handle },
403
+ initialBounds: { ...this.bounds },
404
+ pointerId: e.pointerId,
405
+ };
406
+ this.modifiers = { shift: e.shiftKey, alt: e.altKey };
407
+
408
+ document.addEventListener("pointermove", this.handlePointerMove);
409
+ document.addEventListener("pointerup", this.handlePointerUp);
410
+ }
411
+
412
+ private handlePointerMove = (e: PointerEvent) => {
413
+ if (
414
+ !this.isDragging ||
415
+ !this.interaction ||
416
+ e.pointerId !== this.interaction.pointerId
417
+ )
418
+ return;
419
+
420
+ const deltaX = e.clientX - this.interaction.startPoint.x;
421
+ const deltaY = e.clientY - this.interaction.startPoint.y;
422
+
423
+ this.modifiers = { shift: e.shiftKey, alt: e.altKey };
424
+
425
+ if (this.dragMode === "move") {
426
+ const constrainedMovement = constrainMovementDeltas(
427
+ this.interaction.initialBounds,
428
+ deltaX,
429
+ deltaY,
430
+ { width: this.containerWidth, height: this.containerHeight },
431
+ );
432
+ this.bounds = {
433
+ ...this.interaction.initialBounds,
434
+ x: this.interaction.initialBounds.x + constrainedMovement.deltaX,
435
+ y: this.interaction.initialBounds.y + constrainedMovement.deltaY,
436
+ };
437
+ } else if (this.dragMode === "resize" && this.interaction.target.handle) {
438
+ const context: ResizeContext = {
439
+ initialBounds: this.interaction.initialBounds,
440
+ container: { width: this.containerWidth, height: this.containerHeight },
441
+ constraints: {
442
+ minSize: this.minSize,
443
+ aspectRatio: this.modifiers.shift
444
+ ? this.interaction.initialBounds.width /
445
+ this.interaction.initialBounds.height
446
+ : undefined,
447
+ },
448
+ movement: { deltaX, deltaY },
449
+ modifiers: {
450
+ centerResize: this.modifiers.alt,
451
+ preserveAspectRatio: this.modifiers.shift,
452
+ },
453
+ };
454
+ this.bounds = this.calculateBoundsWithModeAwareConstraints(
455
+ context,
456
+ this.interaction.target.handle,
457
+ );
458
+ }
459
+
460
+ this.dispatchBoundsChange();
461
+ };
462
+
463
+ private handlePointerUp = () => {
464
+ this.isDragging = false;
465
+ this.dragMode = null;
466
+ this.interaction = null;
467
+ document.removeEventListener("pointermove", this.handlePointerMove);
468
+ document.removeEventListener("pointerup", this.handlePointerUp);
469
+ };
470
+
471
+ private calculateBoundsWithModeAwareConstraints(
472
+ context: ResizeContext,
473
+ handle: ResizeHandle,
474
+ ): BoxBounds {
475
+ const { modifiers, constraints, container, initialBounds } = context;
476
+
477
+ // For normal resize, use the simple delta constraint approach
478
+ if (!modifiers.centerResize && !modifiers.preserveAspectRatio) {
479
+ const constrainedMovement = this.constrainResizeDeltas(
480
+ initialBounds,
481
+ context.movement.deltaX,
482
+ context.movement.deltaY,
483
+ handle,
484
+ container,
485
+ constraints.minSize,
486
+ );
487
+
488
+ return calculateNormalResize(
489
+ {
490
+ ...context,
491
+ movement: constrainedMovement,
492
+ },
493
+ handle,
494
+ );
495
+ }
496
+
497
+ // For modifier-based resizes, calculate ideal bounds then constrain smartly
498
+ let idealBounds: BoxBounds;
499
+
500
+ if (modifiers.centerResize && modifiers.preserveAspectRatio) {
501
+ idealBounds = calculateCenterResizeWithAspectRatio(context, handle);
502
+ } else if (modifiers.centerResize) {
503
+ idealBounds = calculateCenterResize(context, handle);
504
+ } else {
505
+ idealBounds = calculateAspectRatioResize(context, handle);
506
+ }
507
+
508
+ // Smart constraint that preserves the resize mode's behavior
509
+ return this.constrainBoundsForMode(idealBounds, context, handle);
510
+ }
511
+
512
+ private constrainBoundsForMode(
513
+ idealBounds: BoxBounds,
514
+ context: ResizeContext,
515
+ handle: ResizeHandle,
516
+ ): BoxBounds {
517
+ const { container, constraints, modifiers } = context;
518
+
519
+ // Check if bounds are already valid
520
+ if (this.isValidBounds(idealBounds, container, constraints.minSize)) {
521
+ return idealBounds;
522
+ }
523
+
524
+ // For combined center + aspect ratio, need special handling
525
+ if (
526
+ modifiers.centerResize &&
527
+ modifiers.preserveAspectRatio &&
528
+ constraints.aspectRatio
529
+ ) {
530
+ return this.constrainCenterResizeWithAspectRatio(idealBounds, context);
531
+ }
532
+
533
+ // For aspect ratio modes, we need to scale the bounds proportionally
534
+ if (modifiers.preserveAspectRatio && constraints.aspectRatio) {
535
+ return this.constrainWithAspectRatio(idealBounds, context, handle);
536
+ }
537
+
538
+ // For center resize, we need to adjust from the center
539
+ if (modifiers.centerResize) {
540
+ return this.constrainCenterResize(idealBounds, context);
541
+ }
542
+
543
+ // Fallback to simple constraint
544
+ return this.simpleConstrainBounds(
545
+ idealBounds,
546
+ container,
547
+ constraints.minSize,
548
+ );
549
+ }
550
+
551
+ private isValidBounds(
552
+ bounds: BoxBounds,
553
+ container: Dimensions,
554
+ minSize: number,
555
+ ): boolean {
556
+ return (
557
+ bounds.x >= 0 &&
558
+ bounds.y >= 0 &&
559
+ bounds.width >= minSize &&
560
+ bounds.height >= minSize &&
561
+ bounds.x + bounds.width <= container.width &&
562
+ bounds.y + bounds.height <= container.height
563
+ );
564
+ }
565
+
566
+ private constrainWithAspectRatio(
567
+ idealBounds: BoxBounds,
568
+ context: ResizeContext,
569
+ handle: ResizeHandle,
570
+ ): BoxBounds {
571
+ const { container, constraints, initialBounds } = context;
572
+ if (!constraints.aspectRatio) {
573
+ return initialBounds;
574
+ }
575
+ const aspectRatio = constraints.aspectRatio;
576
+
577
+ // Calculate maximum allowed dimensions
578
+ const maxWidth = container.width - Math.max(0, idealBounds.x);
579
+ const maxHeight = container.height - Math.max(0, idealBounds.y);
580
+
581
+ // Find the largest size that fits both constraints
582
+ let constrainedWidth = Math.max(
583
+ constraints.minSize,
584
+ Math.min(maxWidth, idealBounds.width),
585
+ );
586
+ let constrainedHeight = constrainedWidth / aspectRatio;
587
+
588
+ // If height is too big, constrain by height instead
589
+ if (constrainedHeight > maxHeight) {
590
+ constrainedHeight = Math.max(constraints.minSize, maxHeight);
591
+ constrainedWidth = constrainedHeight * aspectRatio;
592
+ }
593
+
594
+ // Ensure we don't go smaller than minimum
595
+ if (constrainedWidth < constraints.minSize) {
596
+ constrainedWidth = constraints.minSize;
597
+ constrainedHeight = constrainedWidth / aspectRatio;
598
+ }
599
+
600
+ const result: BoxBounds = {
601
+ ...idealBounds,
602
+ width: constrainedWidth,
603
+ height: constrainedHeight,
604
+ };
605
+
606
+ // Adjust position for handles that move the origin
607
+ switch (handle) {
608
+ case "top-left":
609
+ result.x = initialBounds.x + initialBounds.width - constrainedWidth;
610
+ result.y = initialBounds.y + initialBounds.height - constrainedHeight;
611
+ break;
612
+ case "top-right":
613
+ result.y = initialBounds.y + initialBounds.height - constrainedHeight;
614
+ break;
615
+ case "bottom-left":
616
+ result.x = initialBounds.x + initialBounds.width - constrainedWidth;
617
+ break;
618
+ }
619
+
620
+ // Ensure position is within bounds
621
+ result.x = Math.max(0, Math.min(container.width - result.width, result.x));
622
+ result.y = Math.max(
623
+ 0,
624
+ Math.min(container.height - result.height, result.y),
625
+ );
626
+
627
+ return result;
628
+ }
629
+
630
+ private constrainCenterResize(
631
+ idealBounds: BoxBounds,
632
+ context: ResizeContext,
633
+ ): BoxBounds {
634
+ const { container, constraints, initialBounds } = context;
635
+
636
+ const centerX = initialBounds.x + initialBounds.width / 2;
637
+ const centerY = initialBounds.y + initialBounds.height / 2;
638
+
639
+ // Calculate maximum dimensions from center
640
+ const maxWidthFromCenter = Math.min(
641
+ centerX * 2,
642
+ (container.width - centerX) * 2,
643
+ );
644
+ const maxHeightFromCenter = Math.min(
645
+ centerY * 2,
646
+ (container.height - centerY) * 2,
647
+ );
648
+
649
+ const constrainedWidth = Math.max(
650
+ constraints.minSize,
651
+ Math.min(maxWidthFromCenter, idealBounds.width),
652
+ );
653
+ const constrainedHeight = Math.max(
654
+ constraints.minSize,
655
+ Math.min(maxHeightFromCenter, idealBounds.height),
656
+ );
657
+
658
+ return {
659
+ x: centerX - constrainedWidth / 2,
660
+ y: centerY - constrainedHeight / 2,
661
+ width: constrainedWidth,
662
+ height: constrainedHeight,
663
+ };
664
+ }
665
+
666
+ private constrainCenterResizeWithAspectRatio(
667
+ idealBounds: BoxBounds,
668
+ context: ResizeContext,
669
+ ): BoxBounds {
670
+ const { container, constraints, initialBounds } = context;
671
+ if (!constraints.aspectRatio) {
672
+ return initialBounds;
673
+ }
674
+ const aspectRatio = constraints.aspectRatio;
675
+
676
+ const centerX = initialBounds.x + initialBounds.width / 2;
677
+ const centerY = initialBounds.y + initialBounds.height / 2;
678
+
679
+ // Calculate maximum dimensions from center while maintaining aspect ratio
680
+ const maxWidthFromCenter = Math.min(
681
+ centerX * 2,
682
+ (container.width - centerX) * 2,
683
+ );
684
+ const maxHeightFromCenter = Math.min(
685
+ centerY * 2,
686
+ (container.height - centerY) * 2,
687
+ );
688
+
689
+ // Start with the ideal width, then constrain
690
+ let constrainedWidth = Math.max(
691
+ constraints.minSize,
692
+ Math.min(maxWidthFromCenter, idealBounds.width),
693
+ );
694
+ let constrainedHeight = constrainedWidth / aspectRatio;
695
+
696
+ // If height doesn't fit, constrain by height instead
697
+ if (constrainedHeight > maxHeightFromCenter) {
698
+ constrainedHeight = Math.max(constraints.minSize, maxHeightFromCenter);
699
+ constrainedWidth = constrainedHeight * aspectRatio;
700
+ }
701
+
702
+ // Ensure minimum size
703
+ if (constrainedWidth < constraints.minSize) {
704
+ constrainedWidth = constraints.minSize;
705
+ constrainedHeight = constrainedWidth / aspectRatio;
706
+ }
707
+
708
+ if (constrainedHeight < constraints.minSize) {
709
+ constrainedHeight = constraints.minSize;
710
+ constrainedWidth = constrainedHeight * aspectRatio;
711
+ }
712
+
713
+ return {
714
+ x: centerX - constrainedWidth / 2,
715
+ y: centerY - constrainedHeight / 2,
716
+ width: constrainedWidth,
717
+ height: constrainedHeight,
718
+ };
719
+ }
720
+
721
+ private simpleConstrainBounds(
722
+ bounds: BoxBounds,
723
+ container: Dimensions,
724
+ minSize: number,
725
+ ): BoxBounds {
726
+ return {
727
+ x: Math.max(0, Math.min(container.width - bounds.width, bounds.x)),
728
+ y: Math.max(0, Math.min(container.height - bounds.height, bounds.y)),
729
+ width: Math.max(
730
+ minSize,
731
+ Math.min(container.width - bounds.x, bounds.width),
732
+ ),
733
+ height: Math.max(
734
+ minSize,
735
+ Math.min(container.height - bounds.y, bounds.height),
736
+ ),
737
+ };
738
+ }
739
+
740
+ private constrainResizeDeltas(
741
+ initialBounds: BoxBounds,
742
+ deltaX: number,
743
+ deltaY: number,
744
+ handle: ResizeHandle,
745
+ container: Dimensions,
746
+ minSize: number,
747
+ ): { deltaX: number; deltaY: number } {
748
+ let constrainedDeltaX = deltaX;
749
+ let constrainedDeltaY = deltaY;
750
+
751
+ switch (handle) {
752
+ case "bottom-right":
753
+ // Can't make smaller than minSize, can't go beyond container
754
+ constrainedDeltaX = Math.max(
755
+ minSize - initialBounds.width,
756
+ Math.min(
757
+ container.width - initialBounds.x - initialBounds.width,
758
+ deltaX,
759
+ ),
760
+ );
761
+ constrainedDeltaY = Math.max(
762
+ minSize - initialBounds.height,
763
+ Math.min(
764
+ container.height - initialBounds.y - initialBounds.height,
765
+ deltaY,
766
+ ),
767
+ );
768
+ break;
769
+
770
+ case "top-left":
771
+ // Can't make smaller than minSize, can't go beyond 0
772
+ constrainedDeltaX = Math.max(
773
+ -initialBounds.x,
774
+ Math.min(initialBounds.width - minSize, deltaX),
775
+ );
776
+ constrainedDeltaY = Math.max(
777
+ -initialBounds.y,
778
+ Math.min(initialBounds.height - minSize, deltaY),
779
+ );
780
+ break;
781
+
782
+ case "top-right":
783
+ constrainedDeltaX = Math.max(
784
+ minSize - initialBounds.width,
785
+ Math.min(
786
+ container.width - initialBounds.x - initialBounds.width,
787
+ deltaX,
788
+ ),
789
+ );
790
+ constrainedDeltaY = Math.max(
791
+ -initialBounds.y,
792
+ Math.min(initialBounds.height - minSize, deltaY),
793
+ );
794
+ break;
795
+
796
+ case "bottom-left":
797
+ constrainedDeltaX = Math.max(
798
+ -initialBounds.x,
799
+ Math.min(initialBounds.width - minSize, deltaX),
800
+ );
801
+ constrainedDeltaY = Math.max(
802
+ minSize - initialBounds.height,
803
+ Math.min(
804
+ container.height - initialBounds.y - initialBounds.height,
805
+ deltaY,
806
+ ),
807
+ );
808
+ break;
809
+
810
+ case "right":
811
+ constrainedDeltaX = Math.max(
812
+ minSize - initialBounds.width,
813
+ Math.min(
814
+ container.width - initialBounds.x - initialBounds.width,
815
+ deltaX,
816
+ ),
817
+ );
818
+ break;
819
+
820
+ case "left":
821
+ constrainedDeltaX = Math.max(
822
+ -initialBounds.x,
823
+ Math.min(initialBounds.width - minSize, deltaX),
824
+ );
825
+ break;
826
+
827
+ case "bottom":
828
+ constrainedDeltaY = Math.max(
829
+ minSize - initialBounds.height,
830
+ Math.min(
831
+ container.height - initialBounds.y - initialBounds.height,
832
+ deltaY,
833
+ ),
834
+ );
835
+ break;
836
+
837
+ case "top":
838
+ constrainedDeltaY = Math.max(
839
+ -initialBounds.y,
840
+ Math.min(initialBounds.height - minSize, deltaY),
841
+ );
842
+ break;
843
+ }
844
+
845
+ return { deltaX: constrainedDeltaX, deltaY: constrainedDeltaY };
846
+ }
847
+
848
+ private dispatchBoundsChange() {
849
+ this.dispatchEvent(
850
+ new CustomEvent("bounds-change", {
851
+ detail: { bounds: this.bounds },
852
+ bubbles: true,
853
+ composed: true,
854
+ }),
855
+ );
856
+ }
857
+
858
+ render() {
859
+ const boxStyles = {
860
+ left: `${this.bounds.x}px`,
861
+ top: `${this.bounds.y}px`,
862
+ width: `${this.bounds.width}px`,
863
+ height: `${this.bounds.height}px`,
864
+ };
865
+
866
+ return html`
867
+ <div
868
+ class="box ${this.isDragging ? "dragging" : ""}"
869
+ style=${styleMap(boxStyles)}
870
+ @pointerdown=${(e: PointerEvent) => this.handlePointerDown(e, "move")}
871
+ >
872
+ ${this.renderHandles()}
873
+ <slot></slot>
874
+ </div>
875
+ `;
876
+ }
877
+
878
+ private renderHandles() {
879
+ const handles: ResizeHandle[] = [
880
+ "top-left",
881
+ "top-right",
882
+ "bottom-left",
883
+ "bottom-right",
884
+ "top",
885
+ "right",
886
+ "bottom",
887
+ "left",
888
+ ];
889
+ return handles.map(
890
+ (handle) => html`
891
+ <div
892
+ class="handle ${handle}"
893
+ @pointerdown=${(e: PointerEvent) => this.handlePointerDown(e, "resize", handle)}
894
+ ></div>
895
+ `,
896
+ );
897
+ }
898
+ }