-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: add global or import scripts into DOM #1030
base: main
Are you sure you want to change the base?
Changes from all commits
0b54ba0
903d022
1fd32ef
3482779
4a7d9d0
cc73007
387d335
68c03ad
39974db
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,5 +1,5 @@ | ||
const productTitleName = "SgrDvr"; | ||
|
||
const getDateLabel = (val) => { | ||
return `i am a date label ${val}` | ||
} | ||
return `i am a date label ${val}`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,52 @@ export const EnsembleScreen: React.FC<EnsembleScreenProps> = ({ | |
}; | ||
}, [screen.customWidgets]); | ||
|
||
useEffect(() => { | ||
const globalBlock = screen.global; | ||
const importedScripts = screen.importedScripts; | ||
|
||
const isScriptExist = document.getElementById("custom-scope-script"); | ||
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. This needs to be unique per screen. |
||
|
||
const jsString = ` | ||
// Create a base object and pin its reference | ||
const ensembleObj = {}; | ||
Object.defineProperty(window, 'ensemble', { | ||
get: () => ensembleObj, | ||
set: (value) => { | ||
// Copy properties instead of replacing reference | ||
Object.assign(ensembleObj, value); | ||
return true; | ||
}, | ||
configurable: true, | ||
enumerable: true | ||
}); | ||
|
||
const createEvalClosure = () => { | ||
${importedScripts || ""} | ||
${globalBlock || ""} | ||
|
||
return (scriptToExecute, context) => { | ||
with (context) { | ||
return eval('(' + scriptToExecute.toString() + ')()'); | ||
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. This can just be |
||
} | ||
} | ||
} | ||
|
||
const evalInClosure = createEvalClosure() | ||
`; | ||
|
||
if (isScriptExist) { | ||
isScriptExist.textContent = jsString; | ||
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. This does not rerun the script, you need to remove the node and reinsert it. |
||
} else { | ||
const script = document.createElement("script"); | ||
script.id = "custom-scope-script"; | ||
script.type = "text/javascript"; | ||
script.textContent = jsString; | ||
|
||
document.body.appendChild(script); | ||
} | ||
}, [screen.global, screen.importedScripts]); | ||
|
||
if (!isInitialized) { | ||
return null; | ||
} | ||
|
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.
This can be set once - most of the functions in ensemble API do not require any screen params. The only thing would be the modal context, which can be passed in as an argument.