@editframe/elements 0.15.0-beta.18 → 0.15.0-beta.19

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.
@@ -0,0 +1,133 @@
1
+ import { LitElement } from "lit";
2
+ import { customElement, state } from "lit/decorators.js";
3
+ import { createRef } from "lit/directives/ref.js";
4
+
5
+ @customElement("ef-fit-scale")
6
+ export class EFFitScale extends LitElement {
7
+ containerRef = createRef<HTMLDivElement>();
8
+ contentRef = createRef<HTMLSlotElement>();
9
+
10
+ createRenderRoot() {
11
+ Object.assign(this.style, {
12
+ display: "grid",
13
+ width: "100%",
14
+ height: "100%",
15
+ gridTemplateColumns: "100%",
16
+ gridTemplateRows: "100%",
17
+ overflow: "hidden",
18
+ boxSizing: "border-box",
19
+ contain: "strict",
20
+ position: "relative",
21
+ });
22
+ this.id = `${this.uniqueId}`;
23
+ return this;
24
+ }
25
+
26
+ uniqueId = Math.random().toString(36).substring(2, 15);
27
+
28
+ @state()
29
+ private scale = 1;
30
+
31
+ private animationFrameId?: number;
32
+
33
+ get contentChild() {
34
+ const firstElement = this.children[0];
35
+ if (!firstElement) return null;
36
+
37
+ let current: Element = firstElement;
38
+ while (current) {
39
+ if (current instanceof HTMLSlotElement) {
40
+ const assigned = current.assignedElements()[0];
41
+ if (!assigned) break;
42
+ current = assigned;
43
+ continue;
44
+ }
45
+
46
+ const display = window.getComputedStyle(current).display;
47
+ if (display !== "contents" && display !== "none") {
48
+ return current as HTMLElement;
49
+ }
50
+ const firstChild = current.children[0];
51
+ if (!firstChild) break;
52
+ current = firstChild;
53
+ }
54
+ return firstElement as HTMLElement; // Fallback to first element if no non-contents found
55
+ }
56
+
57
+ get scaleInfo() {
58
+ if (!this.contentChild) {
59
+ return {
60
+ scale: 1,
61
+ containerWidth: 0,
62
+ containerHeight: 0,
63
+ contentWidth: 0,
64
+ contentHeight: 0,
65
+ };
66
+ }
67
+
68
+ const containerWidth = this.clientWidth;
69
+ const containerHeight = this.clientHeight;
70
+ const contentWidth = this.contentChild.clientWidth;
71
+ const contentHeight = this.contentChild.clientHeight;
72
+
73
+ const containerRatio = containerWidth / containerHeight;
74
+ const contentRatio = contentWidth / contentHeight;
75
+
76
+ const scale =
77
+ containerRatio > contentRatio
78
+ ? containerHeight / contentHeight
79
+ : containerWidth / contentWidth;
80
+
81
+ return {
82
+ scale,
83
+ containerWidth,
84
+ containerHeight,
85
+ contentWidth,
86
+ contentHeight,
87
+ };
88
+ }
89
+
90
+ setScale = () => {
91
+ if (this.isConnected) {
92
+ const { scale } = this.scaleInfo;
93
+ if (this.contentChild) {
94
+ const containerRect = this.getBoundingClientRect();
95
+ const contentRect = this.contentChild.getBoundingClientRect();
96
+
97
+ const unscaledWidth = contentRect.width / this.scale;
98
+ const unscaledHeight = contentRect.height / this.scale;
99
+ const scaledWidth = unscaledWidth * scale;
100
+ const scaledHeight = unscaledHeight * scale;
101
+ const translateX = (containerRect.width - scaledWidth) / 2;
102
+ const translateY = (containerRect.height - scaledHeight) / 2;
103
+
104
+ // Use toFixed to avoid floating point precision issues
105
+ // this will update every frame with sub-pixel changes if we don't pin it down
106
+ Object.assign(this.contentChild.style, {
107
+ transform: `translate(${translateX.toFixed(4)}px, ${translateY.toFixed(4)}px) scale(${scale.toFixed(4)})`,
108
+ transformOrigin: "top left",
109
+ });
110
+ this.scale = scale;
111
+ }
112
+ this.animationFrameId = requestAnimationFrame(this.setScale);
113
+ }
114
+ };
115
+
116
+ connectedCallback(): void {
117
+ super.connectedCallback();
118
+ this.animationFrameId = requestAnimationFrame(this.setScale);
119
+ }
120
+
121
+ disconnectedCallback(): void {
122
+ super.disconnectedCallback();
123
+ if (this.animationFrameId) {
124
+ cancelAnimationFrame(this.animationFrameId);
125
+ }
126
+ }
127
+ }
128
+
129
+ declare global {
130
+ interface HTMLElementTagNameMap {
131
+ "ef-fit-scale": EFFitScale;
132
+ }
133
+ }
@@ -1,5 +1,5 @@
1
1
  import { LitElement, type PropertyValueMap, css, html } from "lit";
2
- import { customElement, eventOptions, state } from "lit/decorators.js";
2
+ import { customElement, eventOptions } from "lit/decorators.js";
3
3
  import { createRef, ref } from "lit/directives/ref.js";
4
4
 
5
5
  import { ContextMixin } from "./ContextMixin.js";
@@ -17,67 +17,19 @@ export class EFWorkbench extends ContextMixin(TWMixin(LitElement)) {
17
17
  `,
18
18
  ];
19
19
 
20
- stageRef = createRef<HTMLDivElement>();
21
- canvasRef = createRef<HTMLSlotElement>();
20
+ focusOverlay = createRef<HTMLDivElement>();
22
21
 
23
22
  @eventOptions({ passive: false, capture: true })
24
23
  handleStageWheel(event: WheelEvent) {
25
24
  event.preventDefault();
26
25
  }
27
26
 
28
- focusOverlay = createRef<HTMLDivElement>();
29
-
30
- @state()
31
- private stageScale = 1;
32
-
33
- setStageScale = () => {
34
- if (this.isConnected && !this.rendering) {
35
- const canvasElement = this.canvasRef.value;
36
- const stageElement = this.stageRef.value;
37
- const canvasChild = canvasElement?.assignedElements()[0];
38
- if (stageElement && canvasElement && canvasChild) {
39
- // Determine the appropriate scale factor to make the canvas fit into
40
- canvasElement.style.width = `${canvasChild.clientWidth}px`;
41
- canvasElement.style.height = `${canvasChild.clientHeight}px`;
42
- const stageWidth = stageElement.clientWidth;
43
- const stageHeight = stageElement.clientHeight;
44
- const canvasWidth = canvasElement.clientWidth;
45
- const canvasHeight = canvasElement.clientHeight;
46
- const stageRatio = stageWidth / stageHeight;
47
- const canvasRatio = canvasWidth / canvasHeight;
48
-
49
- if (stageRatio > canvasRatio) {
50
- const scale = stageHeight / canvasHeight;
51
- if (this.stageScale !== scale) {
52
- canvasElement.style.transform = `scale(${scale})`;
53
- }
54
- this.stageScale = scale;
55
- } else {
56
- const scale = stageWidth / canvasWidth;
57
- if (this.stageScale !== scale) {
58
- canvasElement.style.transform = `scale(${scale})`;
59
- }
60
- this.stageScale = scale;
61
- }
62
- }
63
- }
64
- if (this.isConnected) {
65
- requestAnimationFrame(this.setStageScale);
66
- }
67
- };
68
-
69
27
  connectedCallback(): void {
70
- // Set the body and html to 100% width and height to avoid scaling issues
71
- // this is a hack to avoid scaling issues when the stage is scaled and during output rendering
72
- // What I've discovered recently is that having the workbench be so smart as to determine
73
- // how it should be displayed at render time causes problems. Much of that has been extracted
74
- // but this is a quick fix to avoid scaling issues.
75
28
  document.body.style.width = "100%";
76
29
  document.body.style.height = "100%";
77
30
  document.documentElement.style.width = "100%";
78
31
  document.documentElement.style.height = "100%";
79
32
  super.connectedCallback();
80
- requestAnimationFrame(this.setStageScale);
81
33
  }
82
34
 
83
35
  disconnectedCallback(): void {
@@ -121,11 +73,7 @@ export class EFWorkbench extends ContextMixin(TWMixin(LitElement)) {
121
73
  render() {
122
74
  if (this.rendering) {
123
75
  return html`
124
- <slot
125
- ${ref(this.canvasRef)}
126
- class="fixed inset-0 h-full w-full"
127
- name="canvas"
128
- ></slot>
76
+ <slot class="fixed inset-0 h-full w-full" name="canvas"></slot>
129
77
  `;
130
78
  }
131
79
  return html`
@@ -134,15 +82,12 @@ export class EFWorkbench extends ContextMixin(TWMixin(LitElement)) {
134
82
  style="grid-template-rows: 1fr 300px; grid-template-columns: 100%;"
135
83
  >
136
84
  <div
137
- ${ref(this.stageRef)}
138
- class="relative grid h-full w-full justify-center overflow-hidden place-content-center place-items-center"
85
+ class="relative h-full w-full overflow-hidden"
139
86
  @wheel=${this.handleStageWheel}
140
87
  >
141
- <slot
142
- ${ref(this.canvasRef)}
143
- name="canvas"
144
- class="inline-block"
145
- ></slot>
88
+ <ef-fit-scale class="h-full grid place-content-center">
89
+ <slot name="canvas" class="contents"></slot>
90
+ </ef-fit-scale>
146
91
  <div
147
92
  class="border border-blue-500 bg-blue-200 bg-opacity-20 absolute"
148
93
  ${ref(this.focusOverlay)}