@editframe/elements 0.26.3-beta.0 → 0.26.4-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 (132) hide show
  1. package/package.json +2 -2
  2. package/scripts/build-css.js +3 -3
  3. package/tsdown.config.ts +1 -1
  4. package/src/elements/ContextProxiesController.ts +0 -124
  5. package/src/elements/CrossUpdateController.ts +0 -22
  6. package/src/elements/EFAudio.browsertest.ts +0 -706
  7. package/src/elements/EFAudio.ts +0 -56
  8. package/src/elements/EFCaptions.browsertest.ts +0 -1960
  9. package/src/elements/EFCaptions.ts +0 -823
  10. package/src/elements/EFImage.browsertest.ts +0 -120
  11. package/src/elements/EFImage.ts +0 -113
  12. package/src/elements/EFMedia/AssetIdMediaEngine.test.ts +0 -224
  13. package/src/elements/EFMedia/AssetIdMediaEngine.ts +0 -110
  14. package/src/elements/EFMedia/AssetMediaEngine.browsertest.ts +0 -140
  15. package/src/elements/EFMedia/AssetMediaEngine.ts +0 -385
  16. package/src/elements/EFMedia/BaseMediaEngine.browsertest.ts +0 -400
  17. package/src/elements/EFMedia/BaseMediaEngine.ts +0 -505
  18. package/src/elements/EFMedia/BufferedSeekingInput.browsertest.ts +0 -386
  19. package/src/elements/EFMedia/BufferedSeekingInput.ts +0 -430
  20. package/src/elements/EFMedia/JitMediaEngine.browsertest.ts +0 -226
  21. package/src/elements/EFMedia/JitMediaEngine.ts +0 -256
  22. package/src/elements/EFMedia/audioTasks/makeAudioBufferTask.browsertest.ts +0 -679
  23. package/src/elements/EFMedia/audioTasks/makeAudioBufferTask.ts +0 -117
  24. package/src/elements/EFMedia/audioTasks/makeAudioFrequencyAnalysisTask.ts +0 -246
  25. package/src/elements/EFMedia/audioTasks/makeAudioInitSegmentFetchTask.browsertest.ts +0 -59
  26. package/src/elements/EFMedia/audioTasks/makeAudioInitSegmentFetchTask.ts +0 -27
  27. package/src/elements/EFMedia/audioTasks/makeAudioInputTask.browsertest.ts +0 -55
  28. package/src/elements/EFMedia/audioTasks/makeAudioInputTask.ts +0 -53
  29. package/src/elements/EFMedia/audioTasks/makeAudioSeekTask.chunkboundary.regression.browsertest.ts +0 -207
  30. package/src/elements/EFMedia/audioTasks/makeAudioSeekTask.ts +0 -72
  31. package/src/elements/EFMedia/audioTasks/makeAudioSegmentFetchTask.ts +0 -32
  32. package/src/elements/EFMedia/audioTasks/makeAudioSegmentIdTask.ts +0 -29
  33. package/src/elements/EFMedia/audioTasks/makeAudioTasksVideoOnly.browsertest.ts +0 -95
  34. package/src/elements/EFMedia/audioTasks/makeAudioTimeDomainAnalysisTask.ts +0 -184
  35. package/src/elements/EFMedia/shared/AudioSpanUtils.ts +0 -129
  36. package/src/elements/EFMedia/shared/BufferUtils.ts +0 -342
  37. package/src/elements/EFMedia/shared/GlobalInputCache.ts +0 -77
  38. package/src/elements/EFMedia/shared/MediaTaskUtils.ts +0 -44
  39. package/src/elements/EFMedia/shared/PrecisionUtils.ts +0 -46
  40. package/src/elements/EFMedia/shared/RenditionHelpers.browsertest.ts +0 -246
  41. package/src/elements/EFMedia/shared/RenditionHelpers.ts +0 -56
  42. package/src/elements/EFMedia/shared/ThumbnailExtractor.ts +0 -227
  43. package/src/elements/EFMedia/tasks/makeMediaEngineTask.browsertest.ts +0 -167
  44. package/src/elements/EFMedia/tasks/makeMediaEngineTask.ts +0 -88
  45. package/src/elements/EFMedia/videoTasks/MainVideoInputCache.ts +0 -76
  46. package/src/elements/EFMedia/videoTasks/ScrubInputCache.ts +0 -61
  47. package/src/elements/EFMedia/videoTasks/makeScrubVideoBufferTask.ts +0 -114
  48. package/src/elements/EFMedia/videoTasks/makeScrubVideoInitSegmentFetchTask.ts +0 -35
  49. package/src/elements/EFMedia/videoTasks/makeScrubVideoInputTask.ts +0 -52
  50. package/src/elements/EFMedia/videoTasks/makeScrubVideoSeekTask.ts +0 -124
  51. package/src/elements/EFMedia/videoTasks/makeScrubVideoSegmentFetchTask.ts +0 -44
  52. package/src/elements/EFMedia/videoTasks/makeScrubVideoSegmentIdTask.ts +0 -32
  53. package/src/elements/EFMedia/videoTasks/makeUnifiedVideoSeekTask.ts +0 -370
  54. package/src/elements/EFMedia/videoTasks/makeVideoBufferTask.ts +0 -109
  55. package/src/elements/EFMedia.browsertest.ts +0 -872
  56. package/src/elements/EFMedia.ts +0 -341
  57. package/src/elements/EFSourceMixin.ts +0 -60
  58. package/src/elements/EFSurface.browsertest.ts +0 -151
  59. package/src/elements/EFSurface.ts +0 -142
  60. package/src/elements/EFTemporal.browsertest.ts +0 -215
  61. package/src/elements/EFTemporal.ts +0 -800
  62. package/src/elements/EFThumbnailStrip.browsertest.ts +0 -585
  63. package/src/elements/EFThumbnailStrip.media-engine.browsertest.ts +0 -714
  64. package/src/elements/EFThumbnailStrip.ts +0 -906
  65. package/src/elements/EFTimegroup.browsertest.ts +0 -934
  66. package/src/elements/EFTimegroup.ts +0 -882
  67. package/src/elements/EFVideo.browsertest.ts +0 -1482
  68. package/src/elements/EFVideo.ts +0 -564
  69. package/src/elements/EFWaveform.ts +0 -547
  70. package/src/elements/FetchContext.browsertest.ts +0 -401
  71. package/src/elements/FetchMixin.ts +0 -38
  72. package/src/elements/SampleBuffer.ts +0 -94
  73. package/src/elements/TargetController.browsertest.ts +0 -230
  74. package/src/elements/TargetController.ts +0 -224
  75. package/src/elements/TimegroupController.ts +0 -26
  76. package/src/elements/durationConverter.ts +0 -35
  77. package/src/elements/parseTimeToMs.ts +0 -9
  78. package/src/elements/printTaskStatus.ts +0 -16
  79. package/src/elements/renderTemporalAudio.ts +0 -108
  80. package/src/elements/updateAnimations.browsertest.ts +0 -1884
  81. package/src/elements/updateAnimations.ts +0 -217
  82. package/src/elements/util.ts +0 -24
  83. package/src/gui/ContextMixin.browsertest.ts +0 -860
  84. package/src/gui/ContextMixin.ts +0 -562
  85. package/src/gui/Controllable.browsertest.ts +0 -258
  86. package/src/gui/Controllable.ts +0 -41
  87. package/src/gui/EFConfiguration.ts +0 -40
  88. package/src/gui/EFControls.browsertest.ts +0 -389
  89. package/src/gui/EFControls.ts +0 -195
  90. package/src/gui/EFDial.browsertest.ts +0 -84
  91. package/src/gui/EFDial.ts +0 -172
  92. package/src/gui/EFFilmstrip.browsertest.ts +0 -712
  93. package/src/gui/EFFilmstrip.ts +0 -1349
  94. package/src/gui/EFFitScale.ts +0 -152
  95. package/src/gui/EFFocusOverlay.ts +0 -79
  96. package/src/gui/EFPause.browsertest.ts +0 -202
  97. package/src/gui/EFPause.ts +0 -73
  98. package/src/gui/EFPlay.browsertest.ts +0 -202
  99. package/src/gui/EFPlay.ts +0 -73
  100. package/src/gui/EFPreview.ts +0 -74
  101. package/src/gui/EFResizableBox.browsertest.ts +0 -79
  102. package/src/gui/EFResizableBox.ts +0 -898
  103. package/src/gui/EFScrubber.ts +0 -151
  104. package/src/gui/EFTimeDisplay.browsertest.ts +0 -237
  105. package/src/gui/EFTimeDisplay.ts +0 -55
  106. package/src/gui/EFToggleLoop.ts +0 -35
  107. package/src/gui/EFTogglePlay.ts +0 -70
  108. package/src/gui/EFWorkbench.ts +0 -115
  109. package/src/gui/PlaybackController.ts +0 -527
  110. package/src/gui/TWMixin.css +0 -6
  111. package/src/gui/TWMixin.ts +0 -61
  112. package/src/gui/TargetOrContextMixin.ts +0 -185
  113. package/src/gui/currentTimeContext.ts +0 -5
  114. package/src/gui/durationContext.ts +0 -3
  115. package/src/gui/efContext.ts +0 -6
  116. package/src/gui/fetchContext.ts +0 -5
  117. package/src/gui/focusContext.ts +0 -7
  118. package/src/gui/focusedElementContext.ts +0 -5
  119. package/src/gui/playingContext.ts +0 -5
  120. package/src/otel/BridgeSpanExporter.ts +0 -150
  121. package/src/otel/setupBrowserTracing.ts +0 -73
  122. package/src/otel/tracingHelpers.ts +0 -251
  123. package/src/transcoding/cache/RequestDeduplicator.test.ts +0 -170
  124. package/src/transcoding/cache/RequestDeduplicator.ts +0 -65
  125. package/src/transcoding/cache/URLTokenDeduplicator.test.ts +0 -182
  126. package/src/transcoding/cache/URLTokenDeduplicator.ts +0 -101
  127. package/src/transcoding/types/index.ts +0 -312
  128. package/src/transcoding/utils/MediaUtils.ts +0 -63
  129. package/src/transcoding/utils/UrlGenerator.ts +0 -68
  130. package/src/transcoding/utils/constants.ts +0 -36
  131. package/src/utils/LRUCache.test.ts +0 -274
  132. package/src/utils/LRUCache.ts +0 -696
@@ -1,898 +0,0 @@
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
- }