-
Notifications
You must be signed in to change notification settings - Fork 1k
refactor(draft): replace <webview> with <iframe> #1191
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
Changes from all commits
c40a245
d8c01f9
5305361
166d8ac
1ee5611
b5301df
2f151e3
48fea5a
bcdb85f
9a5a297
5b0861c
78d9484
30f0b36
818bb3c
ee7420c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { contextBridge } from 'electron'; | ||
import { processDom } from './dom'; | ||
import { | ||
getChildrenCount, | ||
|
@@ -24,44 +23,69 @@ import { editText, startEditingText, stopEditingText } from './elements/text'; | |
import { setWebviewId } from './state'; | ||
import { getTheme, setTheme } from './theme'; | ||
|
||
export function setApi() { | ||
contextBridge.exposeInMainWorld('api', { | ||
// Misc | ||
processDom, | ||
getComputedStyleByDomId, | ||
updateElementInstance, | ||
setWebviewId, | ||
getFirstOnlookElement, | ||
const onlookApi = { | ||
// Misc | ||
processDom, | ||
getComputedStyleByDomId, | ||
updateElementInstance, | ||
setWebviewId, | ||
getFirstOnlookElement, | ||
|
||
// Elements | ||
getElementAtLoc, | ||
getDomElementByDomId, | ||
setElementType, | ||
getElementType, | ||
getParentElement, | ||
getChildrenCount, | ||
getOffsetParent, | ||
// Elements | ||
getElementAtLoc, | ||
getDomElementByDomId, | ||
setElementType, | ||
getElementType, | ||
getParentElement, | ||
getChildrenCount, | ||
getOffsetParent, | ||
|
||
// Actions | ||
getActionLocation, | ||
getActionElementByDomId, | ||
getInsertLocation, | ||
getRemoveActionFromDomId, | ||
// Actions | ||
getActionLocation, | ||
getActionElementByDomId, | ||
getInsertLocation, | ||
getRemoveActionFromDomId, | ||
|
||
// Theme | ||
getTheme, | ||
setTheme, | ||
// Theme | ||
getTheme, | ||
setTheme, | ||
|
||
// Drag | ||
startDrag, | ||
drag, | ||
endDrag, | ||
getElementIndex, | ||
endAllDrag, | ||
// Drag | ||
startDrag, | ||
drag, | ||
endDrag, | ||
getElementIndex, | ||
endAllDrag, | ||
|
||
// Edit text | ||
startEditingText, | ||
editText, | ||
stopEditingText, | ||
}); | ||
// Edit text | ||
startEditingText, | ||
editText, | ||
stopEditingText, | ||
}; | ||
|
||
export type TOnlookWindow = typeof window & { | ||
_onlookWebviewId: string; | ||
onlook: { | ||
api: typeof onlookApi; | ||
bridge: { | ||
send: (channel: string, data?: any, transfer?: Transferable[]) => void; | ||
receive: (handler: (event: MessageEvent, data?: any) => void) => void; | ||
}; | ||
}; | ||
}; | ||
|
||
export function setApi() { | ||
(window as TOnlookWindow).onlook = { | ||
api: onlookApi, | ||
bridge: { | ||
send: (channel: string, data?: any, transfer?: Transferable[]) => { | ||
window.parent.postMessage({ type: channel, data }, '*', transfer); | ||
Kitenite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
receive: (handler: (event: MessageEvent, data?: any) => void) => { | ||
window.addEventListener('message', (event) => { | ||
handler(event, event.data); | ||
}); | ||
Kitenite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}; | ||
Comment on lines
+78
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Direct window object modification without Object.defineProperty may allow overwriting of onlook API. Consider using Object.defineProperty with configurable: false. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { EditorAttributes, WebviewChannels } from '@onlook/models/constants'; | ||
import type { LayerNode } from '@onlook/models/element'; | ||
import { ipcRenderer } from 'electron'; | ||
import { buildLayerTree } from '../dom'; | ||
import type { TOnlookWindow } from '../api'; | ||
|
||
export function listenForDomMutation() { | ||
const targetNode = document.body; | ||
|
@@ -46,7 +46,7 @@ export function listenForDomMutation() { | |
} | ||
|
||
if (added.size > 0 || removed.size > 0) { | ||
ipcRenderer.sendToHost(WebviewChannels.WINDOW_MUTATED, { | ||
(window as TOnlookWindow).onlook.bridge.send(WebviewChannels.WINDOW_MUTATED, { | ||
added: Object.fromEntries(added), | ||
removed: Object.fromEntries(removed), | ||
}); | ||
Comment on lines
+49
to
52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Type assertion to TOnlookWindow could fail silently if window.onlook or bridge is undefined. Consider adding a null check or error handling. |
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ import type { | |||||||||||||
} from '@onlook/models/actions'; | ||||||||||||||
import { WebviewChannels } from '@onlook/models/constants'; | ||||||||||||||
import type { StyleChange } from '@onlook/models/style'; | ||||||||||||||
import { ipcRenderer } from 'electron'; | ||||||||||||||
import type { TOnlookWindow } from '../api'; | ||||||||||||||
import { processDom } from '../dom'; | ||||||||||||||
import { groupElements, ungroupElements } from '../elements/dom/group'; | ||||||||||||||
import { insertImage, removeImage } from '../elements/dom/image'; | ||||||||||||||
|
@@ -30,17 +30,19 @@ import { | |||||||||||||
export function listenForEvents() { | ||||||||||||||
listenForWindowEvents(); | ||||||||||||||
listenForDomMutation(); | ||||||||||||||
listenForEditEvents(); | ||||||||||||||
|
||||||||||||||
// TODO: Disabled for debugging. This is listening to all events instead of specific ones. | ||||||||||||||
// listenForEditEvents(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
function listenForWindowEvents() { | ||||||||||||||
window.addEventListener('resize', () => { | ||||||||||||||
ipcRenderer.sendToHost(WebviewChannels.WINDOW_RESIZED); | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.send(WebviewChannels.WINDOW_RESIZED); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Type casting window on every bridge access is repetitive and error-prone. Consider creating a helper function or storing the bridge reference. |
||||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
function listenForEditEvents() { | ||||||||||||||
ipcRenderer.on(WebviewChannels.UPDATE_STYLE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, change } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
change: Change<Record<string, StyleChange>>; | ||||||||||||||
|
@@ -50,7 +52,7 @@ function listenForEditEvents() { | |||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.INSERT_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { element, location, editText } = data as { | ||||||||||||||
element: ActionElement; | ||||||||||||||
location: ActionLocation; | ||||||||||||||
|
@@ -62,13 +64,13 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.REMOVE_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { location } = data as { location: ActionLocation }; | ||||||||||||||
removeElement(location); | ||||||||||||||
publishRemoveElement(location); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.MOVE_ELEMENT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, newIndex } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
newIndex: number; | ||||||||||||||
|
@@ -79,7 +81,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.EDIT_ELEMENT_TEXT, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, content } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
content: string; | ||||||||||||||
|
@@ -90,7 +92,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.GROUP_ELEMENTS, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { parent, container, children } = data as { | ||||||||||||||
parent: ActionTarget; | ||||||||||||||
container: GroupContainer; | ||||||||||||||
|
@@ -102,7 +104,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.UNGROUP_ELEMENTS, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { parent, container, children } = data as { | ||||||||||||||
parent: ActionTarget; | ||||||||||||||
container: GroupContainer; | ||||||||||||||
|
@@ -114,7 +116,7 @@ function listenForEditEvents() { | |||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.INSERT_IMAGE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId, image } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
image: ImageContentData; | ||||||||||||||
|
@@ -123,15 +125,15 @@ function listenForEditEvents() { | |||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.REMOVE_IMAGE, (_, data) => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive((_, data) => { | ||||||||||||||
const { domId } = data as { | ||||||||||||||
domId: string; | ||||||||||||||
}; | ||||||||||||||
removeImage(domId); | ||||||||||||||
publishStyleUpdate(domId); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
ipcRenderer.on(WebviewChannels.CLEAN_AFTER_WRITE_TO_CODE, () => { | ||||||||||||||
(window as TOnlookWindow).onlook.bridge.receive(() => { | ||||||||||||||
processDom(); | ||||||||||||||
}); | ||||||||||||||
Comment on lines
+136
to
138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This receive handler has no channel identifier and no data type checking. Consider adding type safety for the message data.
Suggested change
|
||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.