-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor WDS custom widget (#38038)
/ok-to-test tags="@tag.Widget" Fixes #38028 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Introduced a new `IframeMessenger` class for improved communication between the parent window and iframe. - Added `onConsole`, `onTriggerEvent`, and `onUpdateModel` props to the `Preview` component for enhanced event handling. - Streamlined HTML template generation with the `createHtmlTemplate` function. - **Bug Fixes** - Removed unnecessary UI-related event handling from the widget, simplifying the communication structure. - Updated event names in template files from `"onReset"` to `"onResetClick"` for clarity. - **Refactor** - Renamed `CustomComponent` to `CustomWidgetComponent` for clarity. - Modularized message handling logic in the `CustomWidgetComponent`. - Refactored the `defaultApp.ts` to use dynamic template data instead of hardcoded values. - **Style** - Updated CSS for the `.container` class to enhance layout consistency. - **Tests** - Simplified test assertions in the `customWidgetscript` test suite. <!-- end of auto-generated comment: release notes by coderabbit.ai --> <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12234751176> > Commit: eb55fbd > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12234751176&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Widget` > Spec: > <hr>Mon, 09 Dec 2024 12:47:13 UTC <!-- end of auto-generated comment: Cypress test results -->
- Loading branch information
Showing
19 changed files
with
348 additions
and
442 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
app/client/src/modules/ui-builder/ui/wds/WDSCustomWidget/component/IframeMessenger.ts
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,46 @@ | ||
import type { IframeMessage } from "../types"; | ||
import { EVENTS } from "./customWidgetscript"; | ||
|
||
export class IframeMessenger { | ||
private iframe: HTMLIFrameElement; | ||
|
||
constructor(iframe: HTMLIFrameElement) { | ||
this.iframe = iframe; | ||
} | ||
|
||
handleMessage = ( | ||
event: MessageEvent, | ||
handlers: Record<string, (data: Record<string, unknown>) => void>, | ||
) => { | ||
const iframeWindow = | ||
this.iframe.contentWindow || this.iframe.contentDocument?.defaultView; | ||
|
||
// Without this check, malicious scripts from other windows could inject | ||
// unauthorized messages into our application, potentially leading to data | ||
// breaches or unauthorized state modifications | ||
if (event.source !== iframeWindow) return; | ||
|
||
// We send an acknowledgement message for every event to ensure reliable communication | ||
// between the parent window and iframe. This helps in maintaining message ordering | ||
// and preventing race conditions. | ||
this.acknowledgeMessage(event.data); | ||
|
||
const handler = handlers[event.data.type]; | ||
|
||
if (handler) { | ||
handler(event.data.data); | ||
} | ||
}; | ||
|
||
private acknowledgeMessage(message: IframeMessage) { | ||
this.postMessage({ | ||
type: EVENTS.CUSTOM_WIDGET_MESSAGE_RECEIVED_ACK, | ||
key: message.key, | ||
success: true, | ||
}); | ||
} | ||
|
||
postMessage(message: IframeMessage) { | ||
this.iframe.contentWindow?.postMessage(message, "*"); | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
app/client/src/modules/ui-builder/ui/wds/WDSCustomWidget/component/constants.ts
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
app/client/src/modules/ui-builder/ui/wds/WDSCustomWidget/component/createHtmlTemplate.ts
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,35 @@ | ||
// @ts-expect-error - Type error due to raw loader | ||
import css from "!!raw-loader!./reset.css"; | ||
|
||
// @ts-expect-error - Type error due to raw loader | ||
import script from "!!raw-loader!./customWidgetscript.js"; | ||
|
||
// @ts-expect-error - Type error due to raw loader | ||
import appsmithConsole from "!!raw-loader!./appsmithConsole.js"; | ||
|
||
interface CreateHtmlTemplateProps { | ||
cssTokens: string; | ||
onConsole: boolean; | ||
srcDoc: { html: string; js: string; css: string }; | ||
} | ||
|
||
export const createHtmlTemplate = (props: CreateHtmlTemplateProps) => { | ||
const { cssTokens, onConsole, srcDoc } = props; | ||
|
||
return `<html> | ||
<head> | ||
<style>${css}</style> | ||
<style data-appsmith-theme>${cssTokens}</style> | ||
</head> | ||
<body> | ||
${onConsole ? `<script type="text/javascript">${appsmithConsole}</script>` : ""} | ||
<script type="module"> | ||
${script} | ||
main(); | ||
</script> | ||
${srcDoc.html} | ||
<script type="module">${srcDoc.js}</script> | ||
<style>${srcDoc.css}</style> | ||
</body> | ||
</html>`; | ||
}; |
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
Oops, something went wrong.