Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions apps/studio/electron/preload/webview/elements/style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { DomElementStyles } from '@onlook/models/element';
import { jsonClone } from '@onlook/utility';
import { elementFromDomId } from '/common/helpers';

export function getStyles(element: HTMLElement): {
defined: Record<string, string>;
computed: Record<string, string>;
} {
export function getStyles(element: HTMLElement): DomElementStyles {
const computed = getComputedStyle(element);
const inline = getInlineStyles(element);
const stylesheet = getStylesheetStyles(element);
Expand Down
6 changes: 1 addition & 5 deletions apps/studio/src/lib/editor/engine/element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ export class ElementManager {
for (const domEl of domEls) {
const adjustedRect = adaptRectToCanvas(domEl.rect, webview);
const isComponent = !!domEl.instanceId;
this.editorEngine.overlay.state.addClickRect(
adjustedRect,
{ ...domEl.styles?.computed, ...domEl.styles?.defined },
isComponent,
);
this.editorEngine.overlay.state.addClickRect(adjustedRect, domEl.styles, isComponent);
this.addSelectedElement(domEl);
}
}
Expand Down
6 changes: 3 additions & 3 deletions apps/studio/src/lib/editor/engine/overlay/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DomElement } from '@onlook/models/element';
import type { DomElement, DomElementStyles } from '@onlook/models/element';
import { reaction } from 'mobx';
import type { EditorEngine } from '..';
import type { RectDimensions } from './rect';
Expand Down Expand Up @@ -27,7 +27,7 @@ export class OverlayManager {

refreshOverlay = async () => {
this.state.updateHoverRect(null);
const newClickRects: { rect: RectDimensions; styles: Record<string, string> }[] = [];
const newClickRects: { rect: RectDimensions; styles: DomElementStyles | null }[] = [];
for (const selectedElement of this.editorEngine.elements.selected) {
const webview = this.editorEngine.webviews.getWebview(selectedElement.webviewId);
if (!webview) {
Expand All @@ -40,7 +40,7 @@ export class OverlayManager {
continue;
}
const adaptedRect = adaptRectToCanvas(el.rect, webview);
newClickRects.push({ rect: adaptedRect, styles: el.styles?.computed || {} });
newClickRects.push({ rect: adaptedRect, styles: el.styles });
}
this.state.removeClickRects();
for (const clickRect of newClickRects) {
Expand Down
5 changes: 3 additions & 2 deletions apps/studio/src/lib/editor/engine/overlay/state.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DomElementStyles } from '@onlook/models/element';
import { makeAutoObservable } from 'mobx';
import { nanoid } from 'nanoid/non-secure';
import type { RectDimensions } from './rect';
Expand All @@ -9,7 +10,7 @@ export interface MeasurementState {

export interface ClickRectState extends RectDimensions {
isComponent?: boolean;
styles?: Record<string, string>;
styles: DomElementStyles | null;
id: string;
}

Expand Down Expand Up @@ -48,7 +49,7 @@ export class OverlayState {

addClickRect = (
rect: RectDimensions,
styles: Record<string, string>,
styles: DomElementStyles | null,
isComponent?: boolean,
) => {
this.clickRects = [
Expand Down
19 changes: 10 additions & 9 deletions apps/studio/src/routes/editor/Canvas/Overlay/ClickRect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RectDimensions } from '@/lib/editor/engine/overlay/rect';
import { adaptValueToCanvas } from '@/lib/editor/engine/overlay/utils';
import type { DomElementStyles } from '@onlook/models/element';
import { colors } from '@onlook/ui/tokens';
import { nanoid } from 'nanoid';
import { BaseRect } from './BaseRect';
Expand Down Expand Up @@ -90,7 +91,7 @@ const parseCssBoxValues = (

interface ClickRectProps extends RectDimensions {
isComponent?: boolean;
styles: Record<string, string>;
styles: DomElementStyles | null;
shouldShowResizeHandles: boolean;
}

Expand All @@ -104,10 +105,10 @@ export const ClickRect = ({
shouldShowResizeHandles,
}: ClickRectProps) => {
const renderMarginLabels = () => {
if (!styles?.margin) {
if (!styles?.computed.margin) {
return null;
}
const { adjusted, original } = parseCssBoxValues(styles.margin);
const { adjusted, original } = parseCssBoxValues(styles.computed.margin);

const patternId = `margin-pattern-${nanoid()}`;
const maskId = `margin-mask-${nanoid()}`;
Expand Down Expand Up @@ -201,10 +202,10 @@ export const ClickRect = ({
};

const renderPaddingLabels = () => {
if (!styles?.padding) {
if (!styles?.computed.padding) {
return null;
}
const { adjusted, original } = parseCssBoxValues(styles.padding);
const { adjusted, original } = parseCssBoxValues(styles.computed.padding);

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

const renderDimensionLabels = () => {
const rectColor = isComponent ? colors.purple[500] : colors.red[500];
const displayWidth = parseFloat(styles?.width || '0').toFixed(0);
const displayHeight = parseFloat(styles?.height || '0').toFixed(0);
const displayWidth = parseFloat(styles?.defined.width || '0').toFixed(0);
const displayHeight = parseFloat(styles?.defined.height || '0').toFixed(0);
const text = `${displayWidth} × ${displayHeight}`;

// Constants from showDimensions
Expand Down Expand Up @@ -355,9 +356,9 @@ export const ClickRect = ({
height={height}
left={left}
top={top}
borderRadius={parseInt(styles?.['borderRadius'] || '0')}
borderRadius={parseInt(styles?.computed['borderRadius'] || '0')}
isComponent={isComponent}
styles={styles}
styles={styles?.computed ?? {}}
/>
)}
</BaseRect>
Expand Down
2 changes: 1 addition & 1 deletion apps/studio/src/routes/editor/Canvas/Overlay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const Overlay = observer(({ children }: { children: React.ReactNode }) =>
top={rectState.top}
left={rectState.left}
isComponent={rectState.isComponent}
styles={rectState.styles ?? {}}
styles={rectState.styles}
shouldShowResizeHandles={isSingleSelection}
/>
)),
Expand Down
6 changes: 2 additions & 4 deletions apps/web/preload/script/api/elements/style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { DomElementStyles } from '@onlook/models/element';
import { jsonClone } from '@onlook/utility';
import { elementFromDomId } from '../../helpers';

export function getStyles(element: HTMLElement): {
defined: Record<string, string>;
computed: Record<string, string>;
} {
export function getStyles(element: HTMLElement): DomElementStyles {
const computed = getElComputedStyle(element);
const inline = getInlineStyles(element);
const stylesheet = getStylesheetStyles(element);
Expand Down
10 changes: 6 additions & 4 deletions packages/models/src/element/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export interface ParentDomElement extends BaseDomElement {}

export interface DomElement extends BaseDomElement {
tagName: string;
styles: {
defined: Record<string, string>; // Styles from stylesheets or inline
computed: Record<string, string>; // Browser computed styles
} | null;
styles: DomElementStyles | null;
parent: ParentDomElement | null;
}

export interface DomElementStyles {
defined: Record<string, string>; // Styles from stylesheets or inline
computed: Record<string, string>; // Browser computed styles
}

export interface ElementPosition {
x: number;
y: number;
Expand Down