-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: html_render supports chatUserProfile function #4934
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
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 |
|---|---|---|
|
|
@@ -9,8 +9,8 @@ | |
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed, ref, onMounted, onBeforeUnmount } from 'vue' | ||
|
|
||
| import { computed, ref, onMounted, onBeforeUnmount, inject } from 'vue' | ||
| const chatUserProfile = inject('chatUserProfile') as any | ||
| const htmlRef = ref<HTMLIFrameElement>() | ||
| const props = withDefaults( | ||
| defineProps<{ | ||
|
|
@@ -37,6 +37,40 @@ function createIframeHtml(sourceHtml: string) { | |
| * { margin: 0; padding: 0; box-sizing: border-box; } | ||
| html, body { margin: 0 !important; padding: 0 !important; overflow: hidden; } | ||
| </style> | ||
| <script> | ||
| const _INSTANCE_ID = '${instanceId}'; | ||
| window.chatUserProfile=function chatUserProfile() { | ||
| return new Promise((resolve, reject) => { | ||
| const requestId = Date.now() + '_' + Math.random() | ||
|
|
||
| function handler(e) { | ||
| const data = e.data | ||
|
|
||
| if ( | ||
| data?.type === 'chatUserProfile:response' && | ||
| data.requestId === requestId | ||
| ) { | ||
| window.removeEventListener('message', handler) | ||
| resolve(data.data) | ||
| } | ||
| } | ||
| window.addEventListener('message', handler) | ||
| parent.postMessage( | ||
| { | ||
| type: 'chatUserProfile', | ||
| requestId, | ||
| instanceId: _INSTANCE_ID | ||
| }, | ||
| '*' | ||
| ) | ||
|
|
||
| setTimeout(() => { | ||
| window.removeEventListener('message', handler) | ||
| reject(new Error('timeout')) | ||
| }, 10000) | ||
| }) | ||
| } | ||
| <\/script> | ||
| </head> | ||
| <body> | ||
| ${sourceHtml} | ||
|
|
@@ -46,7 +80,6 @@ const INSTANCE_ID = '${instanceId}'; | |
| function sendMessage(message) { | ||
| parent.postMessage({ type: 'chatMessage', instanceId: INSTANCE_ID, message }, '*'); | ||
| } | ||
|
|
||
| let lastSentHeight = 0; | ||
| let timer = null; | ||
|
|
||
|
|
@@ -88,6 +121,20 @@ function onMessage(e: MessageEvent) { | |
| if (e.data.type === 'chatMessage') { | ||
| props.sendMessage?.(e.data.message, 'new') | ||
| } | ||
| if (e.data?.type === 'chatUserProfile') { | ||
| const iframe = htmlRef.value | ||
| if (!iframe) return | ||
| chatUserProfile().then((ok: any) => { | ||
| iframe.contentWindow?.postMessage( | ||
| { | ||
| type: 'chatUserProfile:response', | ||
| requestId: e.data.requestId, | ||
| data: ok, | ||
| }, | ||
| '*', | ||
| ) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| onMounted(() => { | ||
|
Contributor
Author
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. There is an issue with the use of Additionally, the Here are some corrections:
These changes will help improve robustness and maintainability of the Vue component. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code has several improvements and optimizations that can be applied:
Remove Redundant Declarations: Remove commented-out lines like
window.chatUserProfile.Use
providewith Proper Function Reference for Reusability: Theprovideusage is correct, but ensure it provides a function reference rather than its evaluation.Simplify Conditional Logic: Inline some conditional logic where possible to reduce nesting and make the code more readable.
Optimize Data Types and Variable Assignments: Ensure that variable assignments are clear and minimize reassignment without necessary changes.
Consistent Naming Conventions: Use consistent naming conventions across similar variables or functions.
Here's an optimized version of the code:
Key Changes:
window.chatUserProfile.chatUserProfile.