diff --git a/packages/form-js-editor/src/render/components/editor-form-fields/EditorJSFunctionField.js b/packages/form-js-editor/src/render/components/editor-form-fields/EditorJSFunctionField.js index ee45e0834..faa2065fc 100644 --- a/packages/form-js-editor/src/render/components/editor-form-fields/EditorJSFunctionField.js +++ b/packages/form-js-editor/src/render/components/editor-form-fields/EditorJSFunctionField.js @@ -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 ( diff --git a/packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js b/packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js index 3810620ef..b8e6e5547 100644 --- a/packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js +++ b/packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js @@ -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(); } @@ -96,11 +96,9 @@ 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); }); }); @@ -108,7 +106,7 @@ export function JSFunctionField(props) { 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); @@ -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 (
diff --git a/packages/form-js-viewer/src/render/components/icons/JSFunction.svg b/packages/form-js-viewer/src/render/components/icons/JSFunction.svg index 65659169d..b46dbfe8d 100644 --- a/packages/form-js-viewer/src/render/components/icons/JSFunction.svg +++ b/packages/form-js-viewer/src/render/components/icons/JSFunction.svg @@ -1,3 +1 @@ - + \ No newline at end of file diff --git a/packages/form-js-viewer/test/spec/render/components/form-fields/JSFunctionField.spec.js b/packages/form-js-viewer/test/spec/render/components/form-fields/JSFunctionField.spec.js index e15ca6398..95c925a7d 100644 --- a/packages/form-js-viewer/test/spec/render/components/form-fields/JSFunctionField.spec.js +++ b/packages/form-js-viewer/test/spec/render/components/form-fields/JSFunctionField.spec.js @@ -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 }); });