Skip to content

Commit ad24e03

Browse files
authored
use DomElementStyles (#1810)
1 parent 0542d1a commit ad24e03

File tree

8 files changed

+28
-32
lines changed

8 files changed

+28
-32
lines changed

apps/studio/electron/preload/webview/elements/style.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
import type { DomElementStyles } from '@onlook/models/element';
12
import { jsonClone } from '@onlook/utility';
23
import { elementFromDomId } from '/common/helpers';
34

4-
export function getStyles(element: HTMLElement): {
5-
defined: Record<string, string>;
6-
computed: Record<string, string>;
7-
} {
5+
export function getStyles(element: HTMLElement): DomElementStyles {
86
const computed = getComputedStyle(element);
97
const inline = getInlineStyles(element);
108
const stylesheet = getStylesheetStyles(element);

apps/studio/src/lib/editor/engine/element/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ export class ElementManager {
8585
for (const domEl of domEls) {
8686
const adjustedRect = adaptRectToCanvas(domEl.rect, webview);
8787
const isComponent = !!domEl.instanceId;
88-
this.editorEngine.overlay.state.addClickRect(
89-
adjustedRect,
90-
{ ...domEl.styles?.computed, ...domEl.styles?.defined },
91-
isComponent,
92-
);
88+
this.editorEngine.overlay.state.addClickRect(adjustedRect, domEl.styles, isComponent);
9389
this.addSelectedElement(domEl);
9490
}
9591
}

apps/studio/src/lib/editor/engine/overlay/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DomElement } from '@onlook/models/element';
1+
import type { DomElement, DomElementStyles } from '@onlook/models/element';
22
import { reaction } from 'mobx';
33
import type { EditorEngine } from '..';
44
import type { RectDimensions } from './rect';
@@ -27,7 +27,7 @@ export class OverlayManager {
2727

2828
refreshOverlay = async () => {
2929
this.state.updateHoverRect(null);
30-
const newClickRects: { rect: RectDimensions; styles: Record<string, string> }[] = [];
30+
const newClickRects: { rect: RectDimensions; styles: DomElementStyles | null }[] = [];
3131
for (const selectedElement of this.editorEngine.elements.selected) {
3232
const webview = this.editorEngine.webviews.getWebview(selectedElement.webviewId);
3333
if (!webview) {
@@ -40,7 +40,7 @@ export class OverlayManager {
4040
continue;
4141
}
4242
const adaptedRect = adaptRectToCanvas(el.rect, webview);
43-
newClickRects.push({ rect: adaptedRect, styles: el.styles?.computed || {} });
43+
newClickRects.push({ rect: adaptedRect, styles: el.styles });
4444
}
4545
this.state.removeClickRects();
4646
for (const clickRect of newClickRects) {

apps/studio/src/lib/editor/engine/overlay/state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DomElementStyles } from '@onlook/models/element';
12
import { makeAutoObservable } from 'mobx';
23
import { nanoid } from 'nanoid/non-secure';
34
import type { RectDimensions } from './rect';
@@ -9,7 +10,7 @@ export interface MeasurementState {
910

1011
export interface ClickRectState extends RectDimensions {
1112
isComponent?: boolean;
12-
styles?: Record<string, string>;
13+
styles: DomElementStyles | null;
1314
id: string;
1415
}
1516

@@ -48,7 +49,7 @@ export class OverlayState {
4849

4950
addClickRect = (
5051
rect: RectDimensions,
51-
styles: Record<string, string>,
52+
styles: DomElementStyles | null,
5253
isComponent?: boolean,
5354
) => {
5455
this.clickRects = [

apps/studio/src/routes/editor/Canvas/Overlay/ClickRect.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RectDimensions } from '@/lib/editor/engine/overlay/rect';
22
import { adaptValueToCanvas } from '@/lib/editor/engine/overlay/utils';
3+
import type { DomElementStyles } from '@onlook/models/element';
34
import { colors } from '@onlook/ui/tokens';
45
import { nanoid } from 'nanoid';
56
import { BaseRect } from './BaseRect';
@@ -90,7 +91,7 @@ const parseCssBoxValues = (
9091

9192
interface ClickRectProps extends RectDimensions {
9293
isComponent?: boolean;
93-
styles: Record<string, string>;
94+
styles: DomElementStyles | null;
9495
shouldShowResizeHandles: boolean;
9596
}
9697

@@ -104,10 +105,10 @@ export const ClickRect = ({
104105
shouldShowResizeHandles,
105106
}: ClickRectProps) => {
106107
const renderMarginLabels = () => {
107-
if (!styles?.margin) {
108+
if (!styles?.computed.margin) {
108109
return null;
109110
}
110-
const { adjusted, original } = parseCssBoxValues(styles.margin);
111+
const { adjusted, original } = parseCssBoxValues(styles.computed.margin);
111112

112113
const patternId = `margin-pattern-${nanoid()}`;
113114
const maskId = `margin-mask-${nanoid()}`;
@@ -201,10 +202,10 @@ export const ClickRect = ({
201202
};
202203

203204
const renderPaddingLabels = () => {
204-
if (!styles?.padding) {
205+
if (!styles?.computed.padding) {
205206
return null;
206207
}
207-
const { adjusted, original } = parseCssBoxValues(styles.padding);
208+
const { adjusted, original } = parseCssBoxValues(styles.computed.padding);
208209

209210
const patternId = `padding-pattern-${nanoid()}`;
210211
const maskId = `padding-mask-${nanoid()}`;
@@ -301,8 +302,8 @@ export const ClickRect = ({
301302

302303
const renderDimensionLabels = () => {
303304
const rectColor = isComponent ? colors.purple[500] : colors.red[500];
304-
const displayWidth = parseFloat(styles?.width || '0').toFixed(0);
305-
const displayHeight = parseFloat(styles?.height || '0').toFixed(0);
305+
const displayWidth = parseFloat(styles?.defined.width || '0').toFixed(0);
306+
const displayHeight = parseFloat(styles?.defined.height || '0').toFixed(0);
306307
const text = `${displayWidth} × ${displayHeight}`;
307308

308309
// Constants from showDimensions
@@ -355,9 +356,9 @@ export const ClickRect = ({
355356
height={height}
356357
left={left}
357358
top={top}
358-
borderRadius={parseInt(styles?.['borderRadius'] || '0')}
359+
borderRadius={parseInt(styles?.computed['borderRadius'] || '0')}
359360
isComponent={isComponent}
360-
styles={styles}
361+
styles={styles?.computed ?? {}}
361362
/>
362363
)}
363364
</BaseRect>

apps/studio/src/routes/editor/Canvas/Overlay/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const Overlay = observer(({ children }: { children: React.ReactNode }) =>
5151
top={rectState.top}
5252
left={rectState.left}
5353
isComponent={rectState.isComponent}
54-
styles={rectState.styles ?? {}}
54+
styles={rectState.styles}
5555
shouldShowResizeHandles={isSingleSelection}
5656
/>
5757
)),

apps/web/preload/script/api/elements/style.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
import type { DomElementStyles } from '@onlook/models/element';
12
import { jsonClone } from '@onlook/utility';
23
import { elementFromDomId } from '../../helpers';
34

4-
export function getStyles(element: HTMLElement): {
5-
defined: Record<string, string>;
6-
computed: Record<string, string>;
7-
} {
5+
export function getStyles(element: HTMLElement): DomElementStyles {
86
const computed = getElComputedStyle(element);
97
const inline = getInlineStyles(element);
108
const stylesheet = getStylesheetStyles(element);

packages/models/src/element/element.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export interface ParentDomElement extends BaseDomElement {}
1010

1111
export interface DomElement extends BaseDomElement {
1212
tagName: string;
13-
styles: {
14-
defined: Record<string, string>; // Styles from stylesheets or inline
15-
computed: Record<string, string>; // Browser computed styles
16-
} | null;
13+
styles: DomElementStyles | null;
1714
parent: ParentDomElement | null;
1815
}
1916

17+
export interface DomElementStyles {
18+
defined: Record<string, string>; // Styles from stylesheets or inline
19+
computed: Record<string, string>; // Browser computed styles
20+
}
21+
2022
export interface ElementPosition {
2123
x: number;
2224
y: number;

0 commit comments

Comments
 (0)