Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate overlay rectangles to React components #908

Merged
merged 6 commits into from
Dec 18, 2024
20 changes: 4 additions & 16 deletions apps/studio/src/lib/editor/engine/element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export class ElementManager {
...domEl,
webviewId: webview.id,
};
const adjustedRect = this.editorEngine.overlay.adaptRectFromSourceElement(
webviewEl.rect,
webview,
);
const adjustedRect = this.editorEngine.overlay.adaptRect(webviewEl.rect, webview);
const isComponent = !!domEl.instanceId;
this.editorEngine.overlay.updateHoverRect(adjustedRect, isComponent);
this.setHoveredElement(webviewEl);
Expand All @@ -63,14 +60,8 @@ export class ElementManager {
return;
}

const selectedRect = this.editorEngine.overlay.adaptRectFromSourceElement(
selectedEl.rect,
webview,
);
const hoverRect = this.editorEngine.overlay.adaptRectFromSourceElement(
hoverEl.rect,
webview,
);
const selectedRect = this.editorEngine.overlay.adaptRect(selectedEl.rect, webview);
const hoverRect = this.editorEngine.overlay.adaptRect(hoverEl.rect, webview);

this.editorEngine.overlay.updateMeasurement(selectedRect, hoverRect);
}
Expand All @@ -92,10 +83,7 @@ export class ElementManager {
this.clearSelectedElements();

for (const domEl of domEls) {
const adjustedRect = this.editorEngine.overlay.adaptRectFromSourceElement(
domEl.rect,
webview,
);
const adjustedRect = this.editorEngine.overlay.adaptRect(domEl.rect, webview);
const isComponent = !!domEl.instanceId;
this.editorEngine.overlay.addClickRect(adjustedRect, domEl.styles, isComponent);
this.addSelectedElement(domEl);
Expand Down
61 changes: 61 additions & 0 deletions apps/studio/src/lib/editor/engine/overlay/components/BaseRect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { EditorAttributes } from '@onlook/models/constants';
import { colors } from '@onlook/ui/tokens';
import React from 'react';

export interface RectDimensions {
width: number;
height: number;
top: number;
left: number;
}

export interface RectProps extends RectDimensions {
isComponent?: boolean;
className?: string;
children?: React.ReactNode;
strokeWidth?: number;
}

export const BaseRect: React.FC<RectProps> = ({
width,
height,
top,
left,
isComponent,
className,
children,
strokeWidth = 2,
}) => {
return (
<div
style={{
position: 'absolute',
top: `${top}px`,
left: `${left}px`,
pointerEvents: 'none',
zIndex: 999,
}}
className={className}
data-onlook-ignore="true"
id={EditorAttributes.ONLOOK_RECT_ID}
>
<svg
overflow="visible"
width={width}
height={height}
viewBox={`0 0 ${width} ${height}`}
>
<rect
width={width}
height={height}
fill="none"
stroke={isComponent ? colors.purple[500] : colors.red[500]}
strokeWidth={strokeWidth}
strokeLinecap="round"
strokeLinejoin="round"
/>
{children}
</svg>
</div>
);
};
Loading