Skip to content

Commit

Permalink
wip review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Skaiir committed Apr 8, 2024
1 parent dcbe010 commit b661452
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const type = 'script';

export function EditorJSFunctionField(props) {
const { field } = props;
const { jsFunction = '' } = field;
const { jsFunction = '', key } = field;

const Icon = iconsByType(type);

let placeholderContent = 'JS function is empty';

if (jsFunction.trim()) {
placeholderContent = 'JS function';
placeholderContent = `JS function for '${key}'`;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function JSFunctionField(props) {
try {
value = JSON.parse(JSON.stringify(value));
onChange({ field, value });
} catch (e) {
} catch {
sandboxError('Unparsable return value');
clearValue();
}
Expand Down Expand Up @@ -96,19 +96,17 @@ export function JSFunctionField(props) {
const iframe = iframeContainerRef.current.querySelector('iframe');
iframe.removeAttribute('allow');

// (3) run user code in sandbox
// (3) load user code in sandbox
_sandbox.promise.then((sandboxInstance) => {
sandboxInstance

// @ts-ignore
.run(wrappedUserCode)
.then(() => { setSandbox(sandboxInstance); setHasRunLoad(false); });
});

return () => {
_sandbox.destroy();
};
}, [ iframeContainerId, functionDefinition, onChange, field, paramsDefinition, computeOn, interval, safeSetValue, clearValue, sandboxError ]);
}, [ clearValue, functionDefinition, iframeContainerId, safeSetValue, sandboxError ]);

const prevParams = usePrevious(params);
const prevSandbox = usePrevious(sandbox);
Expand Down Expand Up @@ -137,7 +135,7 @@ export function JSFunctionField(props) {
return () => clearInterval(intervalId);
}

}, [ params, prevParams, sandbox, prevSandbox, onChange, field, computeOn, hasRunLoad, interval, clearValue, safeSetValue ]);
}, [ params, prevParams, sandbox, prevSandbox, field, computeOn, hasRunLoad, interval, clearValue, safeSetValue ]);

return (
<div ref={ iframeContainerRef } id={ iframeContainerId } className="fjs-sandbox-iframe-container"></div>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ describe('JSFunctionField', function() {
};

// when
act(() => {
await act(() => {
createJSFunctionField({ field, onChange: onChangeSpy, services });
});

// wait for the iframe to compute the expression and pass it back
await new Promise(r => setTimeout(r, 100)).then(() => {

// then
expect(onChangeSpy).to.be.calledOnce;
expect(onChangeSpy).to.be.calledWith({ field, value: 42 });
});
// then
expect(onChangeSpy).to.be.calledOnce;
expect(onChangeSpy).to.be.calledWith({ field, value: 42 });

});

Expand Down

0 comments on commit b661452

Please sign in to comment.