Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/kitchen-sink/src/ensemble/scripts/common.js
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}`
}
getDateLabel = (val) => {
return `i am a date label ${val}`;
};
33 changes: 9 additions & 24 deletions packages/framework/src/evaluate/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ export const buildEvaluateFn = (
// Need to filter out invalid JS identifiers
].filter(([key, _]) => !key.includes(".")),
);
const globalBlock = screen.model?.global;
const importedScriptBlock = screen.model?.importedScripts;

const args = Object.keys(invokableObj).join(",");

const combinedJs = `
return myScreenScope(() => {
${formatJs(js)};
}, {${args}});
`;

// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
const jsFunc = new Function(
...Object.keys(invokableObj),
addScriptBlock(formatJs(js), globalBlock, importedScriptBlock),
);
const jsFunc = new Function(...Object.keys(invokableObj), combinedJs);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return () => jsFunc(...Object.values(invokableObj));
Expand Down Expand Up @@ -80,24 +83,6 @@ const formatJs = (js?: string): string => {
return `return ${sanitizedJs}`;
};

const addScriptBlock = (
js: string,
globalBlock?: string,
importedScriptBlock?: string,
): string => {
let jsString = ``;

if (importedScriptBlock) {
jsString += `${importedScriptBlock}\n\n`;
}

if (globalBlock) {
jsString += `${globalBlock}\n\n`;
}

return (jsString += `${js}`);
};

/**
* @deprecated Consider using useEvaluate or createBinding which will
* optimize creating the evaluation context
Expand Down
29 changes: 29 additions & 0 deletions packages/runtime/src/runtime/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ export const EnsembleScreen: React.FC<EnsembleScreenProps> = ({
};
}, [screen.customWidgets]);

useEffect(() => {
const globalBlock = screen.global;
const importedScripts = screen.importedScripts;

const isScriptExist = document.getElementById("custom-scope-script");

const jsString = `
const myScreenScope = function(scriptToExecute, context) {
with (context) {
${importedScripts || ""}
${globalBlock || ""}

return eval('(' + scriptToExecute.toString() + ')()');
}
}
`;

if (isScriptExist) {
isScriptExist.textContent = jsString;
} 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;
}
Expand Down
Loading