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

Support widget setters as they are in flutter framework #620

Open
wants to merge 3 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: 6 additions & 0 deletions .changeset/eleven-chicken-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ensembleui/react-framework": patch
"@ensembleui/react-kitchen-sink": patch
---

Support widget setters for flutter compatibility
10 changes: 9 additions & 1 deletion apps/kitchen-sink/src/ensemble/screens/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ View:
children:
- Icon:
- Text:
id: formHeading
styles:
names: heading-3
text: Forms
- Markdown:
text: More to come! In the meantime, checkout the Ensemble [documentation](https://docs.ensembleui.com/).
- Button:
label: set me
onTap:
executeCode: |
formHeading.text = 'i am set'
iconText.text = 'i am set too'
- Card:
styles:
maxWidth: unset
Expand Down Expand Up @@ -144,7 +151,7 @@ View:
- Text:
styles:
names: heading-3
text: Forms with initial values
text: ${formHeading.text + ' with initial values'}
- Markdown:
text: More to come! In the meantime, checkout the Ensemble [documentation](https://docs.ensembleui.com/).
- Card:
Expand Down Expand Up @@ -261,4 +268,5 @@ API:
Icon:
body:
Text:
id: iconText
text: Hello123
35 changes: 34 additions & 1 deletion packages/framework/src/evaluate/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,49 @@ export const buildEvaluateFn = (
const globalBlock = screen.model?.global;
const importedScriptBlock = screen.model?.importedScripts;

const modifiedJs = modifyJs(Object.fromEntries([...widgets]), js);

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

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return () => jsFunc(...Object.values(invokableObj));
};

const modifyJs = (
invokables: { [key: string]: InvokableMethods | undefined },
js?: string,
): string => {
if (!js || isEmpty(js)) {
return js || "";
}

// check if the code is assigning a value to a property
// eslint-disable-next-line prefer-named-capture-group
const assignmentRegex = /(\w+)\.(\w+)\s*=\s*(.*)$/gm; // matches widgetId.setter = value
let modifiedCode = js;

let match;
while ((match = assignmentRegex.exec(js))) {
const [fullMatch, widgetId, setter, value] = match;
const setterName = `set${setter.charAt(0).toUpperCase() + setter.slice(1)}`;
const originalSetter = invokables[widgetId]?.[setterName];

if (originalSetter) {
// if a setter function exists, replace the assignment with a function call
modifiedCode = modifiedCode.replace(
fullMatch,
`${widgetId}.${setterName}(${value});`,
);
}
}

return modifiedCode;
};
Comment on lines +41 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try passing in the state object into the exec context directly? I doubt it would work but might be worth a shot. I think we will need to create some kind of wrapper object that mimics a setter...


const formatJs = (js?: string): string => {
if (!js || isEmpty(js)) {
if (process.env.NODE_ENV === "debug") {
Expand Down
Loading