-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up webview preload script to start editing (#9)
- Loading branch information
Showing
8 changed files
with
155 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ipcRenderer } from 'electron'; | ||
|
||
const documentBodyInit = () => { | ||
// Context Menu | ||
window.addEventListener('contextmenu', (e) => { | ||
e.preventDefault(); | ||
ipcRenderer.send('show-context-menu', { | ||
contextMenuMeta: { x: e.x, y: e.y }, | ||
}); | ||
}); | ||
|
||
window.addEventListener('wheel', (e) => { | ||
ipcRenderer.sendToHost('pass-scroll-data', { | ||
coordinates: { x: e.deltaX, y: e.deltaY }, | ||
innerHeight: document.body.scrollHeight, | ||
innerWidth: window.innerWidth, | ||
}); | ||
}); | ||
|
||
window.addEventListener('dom-ready', () => { | ||
const { body } = document; | ||
const html = document.documentElement; | ||
|
||
const height = Math.max( | ||
body.scrollHeight, | ||
body.offsetHeight, | ||
html.clientHeight, | ||
html.scrollHeight, | ||
html.offsetHeight | ||
); | ||
|
||
ipcRenderer.sendToHost('pass-scroll-data', { | ||
coordinates: { x: 0, y: 0 }, | ||
innerHeight: height, | ||
innerWidth: window.innerWidth, | ||
}); | ||
}); | ||
}; | ||
|
||
ipcRenderer.on('context-menu-command', (_, command) => { | ||
ipcRenderer.sendToHost('context-menu-command', command); | ||
}); | ||
|
||
const documentBodyWaitHandle = setInterval(() => { | ||
window.onerror = function logError(errorMsg, url, lineNumber) { | ||
// eslint-disable-next-line no-console | ||
console.log(`Unhandled error: ${errorMsg} ${url} ${lineNumber}`); | ||
// Code to run when an error has occurred on the page | ||
}; | ||
|
||
if (window?.document?.body) { | ||
clearInterval(documentBodyWaitHandle); | ||
try { | ||
documentBodyInit(); | ||
} catch (err) { | ||
console.log('Error in documentBodyInit:', err); | ||
} | ||
return; | ||
} | ||
console.log('document.body not ready'); | ||
}, 300); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export enum MainChannel { | ||
NOTIFICATION = 'notification', | ||
WEBVIEW_PRELOAD_PATH = 'webview-preload-path', | ||
} | ||
|
||
export const APP_NAME = 'Onlook'; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,51 @@ | ||
import React, { useRef } from 'react'; | ||
import { MainChannel } from '@/lib/constants'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
function FrameList() { | ||
const webviewRef = useRef(null); | ||
return ( | ||
<webview ref={webviewRef} className='w-[96rem] h-[54rem]' src="https://www.framer.com/" ></webview> | ||
); | ||
const ref = useRef(null); | ||
const [webviewPreloadPath, setWebviewPreloadPath] = useState<string>(''); | ||
|
||
useEffect(() => { | ||
window.Main.invoke(MainChannel.WEBVIEW_PRELOAD_PATH).then((preloadPath: any) => { | ||
setWebviewPreloadPath(preloadPath); | ||
}); | ||
|
||
if (!ref.current) { | ||
return; | ||
} | ||
const webview = ref.current as Electron.WebviewTag; | ||
const handlerRemovers: (() => void)[] = []; | ||
|
||
webview.addEventListener('dom-ready', () => { | ||
console.log('dom-ready'); | ||
}); | ||
|
||
const ipcMessageHandler = (e: Electron.IpcMessageEvent) => { | ||
console.log("🚀 ~ ipcMessageHandler ~ e.channel:", e.channel) | ||
}; | ||
|
||
webview.addEventListener('ipc-message', ipcMessageHandler); | ||
handlerRemovers.push(() => { | ||
webview.removeEventListener('ipc-message', ipcMessageHandler); | ||
}); | ||
|
||
return () => { | ||
handlerRemovers.forEach((handlerRemover) => { | ||
handlerRemover(); | ||
}); | ||
}; | ||
}, [ref, webviewPreloadPath]); | ||
|
||
if (webviewPreloadPath) | ||
return ( | ||
<webview | ||
ref={ref} | ||
className='w-[96rem] h-[54rem]' | ||
src="https://www.framer.com/" | ||
preload={`file://${webviewPreloadPath}`} | ||
allowpopups={"true" as any} | ||
></webview> | ||
); | ||
} | ||
|
||
export default FrameList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters