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

Add editor scroll handler #13

Merged
merged 10 commits into from
Jun 28, 2024
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
4 changes: 2 additions & 2 deletions electron/preload/webview/elements/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EditorAttributes } from "../../../../common/constants";
import { ElementMetadata } from "../../../../common/models";
import { finder } from "./finder";
import { EditorAttributes } from "/common/constants";
import { ElementMetadata } from "/common/models";

export const handleMouseEvent = (e: MouseEvent): Object => {
if (!e.metaKey) {
Expand Down
4 changes: 2 additions & 2 deletions electron/preload/webview/eventBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class EventBridge {
'click': handleMouseEvent,
'dblclick': handleMouseEvent,
'wheel': (e: WheelEvent) => {
return {}
return { x: window.scrollX, y: window.scrollY }
},
'scroll': (e: Event) => {
return {}
return { x: window.scrollX, y: window.scrollY }
},
'dom-ready': () => {
const { body } = document;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/editor/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class WebviewEventHandler {
console.error('No args found for mouseover event');
return;
}
const scrollPosition: { x: number, y: number } = JSON.parse(e.args[0]);
overlayManager.updateScroll(scrollPosition);
},
};

Expand Down
9 changes: 9 additions & 0 deletions src/lib/editor/overlay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class OverlayManager {
clickedRects: ClickRect[]
parentRect: ParentRect
editRect: EditRect
scrollPosition: { x: number, y: number } = { x: 0, y: 0 }

constructor() {
this.hoverRect = new HoverRect();
Expand Down Expand Up @@ -80,6 +81,14 @@ export class OverlayManager {
this.removeEditRect()
}

updateScroll = ({ x, y }: { x: number, y: number }) => {
this.scrollPosition = { x, y }
this.hoverRect.applyScroll(x, y)
this.clickedRects.forEach(clickRect => {
clickRect.applyScroll(x, y)
})
}

addClickRect = (rect: DOMRect, computerStyle: CSSStyleDeclaration) => {
const clickRect = new ClickRect()
this.appendRectToPopover(clickRect.element)
Expand Down
5 changes: 5 additions & 0 deletions src/lib/editor/overlay/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export class RectImpl implements Rect {
this.rectElement.setAttribute('height', height.toString())
this.element.style.top = `${top}px`
this.element.style.left = `${left}px`
this.element.style.transform = 'translate(0, 0)'
}

applyScroll(scrollX: number, scrollY: number) {
this.element.style.transform = `translate(${-scrollX}px, ${-scrollY}px)`
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/routes/project/webview/WebviewArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function WebviewArea() {
{
id: nanoid(),
title: 'Desktop',
src: 'https://www.framer.com/',
src: 'https://www.github.com/',
},
];

Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"paths": {
"@/*": [
"src/*"
],
"/*": [
"./*"
]
},
},
Expand Down
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default defineConfig(({ command }) => {
return {
resolve: {
alias: {
'@': path.join(__dirname, 'src')
'@': path.join(__dirname, 'src'),
'common': path.join(__dirname, 'common'),
},
},
plugins: [
Expand Down