Skip to content

Commit

Permalink
Add editor scroll handler (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitenite committed Jun 28, 2024
1 parent 9ea2f3e commit 4db0897
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 6 deletions.
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

0 comments on commit 4db0897

Please sign in to comment.