-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement script component (viewer)
Related to #1102
- Loading branch information
Showing
10 changed files
with
203 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/form-js-viewer/src/render/components/form-fields/JSFunctionField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import Sandbox from 'websandbox'; | ||
import { useCallback, useEffect, useState } from 'preact/hooks'; | ||
import { useExpressionEvaluation, useDeepCompareMemoize, usePrevious } from '../../hooks'; | ||
import { isObject } from 'min-dash'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
export function JSFunctionField(props) { | ||
const { field, onChange } = props; | ||
const { | ||
jsFunction: functionDefinition, | ||
functionParameters: paramsDefinition, | ||
computeOn, | ||
interval | ||
} = field; | ||
|
||
const [ sandbox, setSandbox ] = useState(null); | ||
const [ hasRunLoad, setHasRunLoad ] = useState(false); | ||
const [ iframeContainerId ] = useState(`fjs-sandbox-iframe-container_${uuidv4()}`); | ||
|
||
const paramsEval = useExpressionEvaluation(paramsDefinition); | ||
const params = useDeepCompareMemoize(isObject(paramsEval) ? paramsEval : {}); | ||
|
||
const clearValue = useCallback(() => onChange({ field, value: undefined }), [ field, onChange ]); | ||
|
||
const safeSetValue = useCallback((value) => { | ||
|
||
if (value !== undefined) { | ||
|
||
// strip out functions and handle unserializeable objects | ||
try { | ||
value = JSON.parse(JSON.stringify(value)); | ||
onChange({ field, value }); | ||
} catch (e) { | ||
clearValue(); | ||
} | ||
} | ||
|
||
}, [ field, onChange, clearValue ]); | ||
|
||
useEffect(() => { | ||
const hostAPI = { | ||
setValue: safeSetValue, | ||
error: (e) => { | ||
clearValue(); | ||
} | ||
}; | ||
|
||
// @ts-ignore | ||
const _sandbox = Sandbox.create(hostAPI, { | ||
frameContainer: `#${iframeContainerId}`, | ||
frameClassName: 'fjs-sandbox-iframe' | ||
}); | ||
|
||
const wrappedUserCode = ` | ||
const computeCallThisFunctionIfYouWantToCrashYourBrowser = (data) => { | ||
try { | ||
const setValue = Websandbox.connection.remote.setValue; | ||
${functionDefinition} | ||
} | ||
catch (e) { | ||
Websandbox.connection.remote.error(e); | ||
} | ||
} | ||
Websandbox.connection.setLocalApi({ compute: computeCallThisFunctionIfYouWantToCrashYourBrowser }); | ||
`; | ||
|
||
_sandbox.promise.then((sandboxInstance) => { | ||
sandboxInstance | ||
.run(wrappedUserCode) | ||
.catch(() => { onChange({ field, value: null }); }) | ||
.then(() => { setSandbox(sandboxInstance); setHasRunLoad(false); }); | ||
}); | ||
|
||
return () => { | ||
_sandbox.destroy(); | ||
}; | ||
}, [ iframeContainerId, functionDefinition, onChange, field, paramsDefinition, computeOn, interval, safeSetValue, clearValue ]); | ||
|
||
const prevParams = usePrevious(params); | ||
const prevSandbox = usePrevious(sandbox); | ||
|
||
useEffect(() => { | ||
|
||
if (!sandbox || !sandbox.connection.remote.compute) { | ||
return; | ||
} | ||
|
||
const runCompute = () => { | ||
sandbox.connection.remote.compute(params) | ||
.catch(clearValue) | ||
.then(safeSetValue); | ||
}; | ||
|
||
if (computeOn === 'load' && !hasRunLoad) { | ||
runCompute(); | ||
setHasRunLoad(true); | ||
} | ||
else if (computeOn === 'change' && (params !== prevParams || sandbox !== prevSandbox)) { | ||
runCompute(); | ||
} | ||
else if (computeOn === 'interval') { | ||
const intervalId = setInterval(runCompute, interval); | ||
return () => clearInterval(intervalId); | ||
} | ||
|
||
}, [ params, prevParams, sandbox, prevSandbox, onChange, field, computeOn, hasRunLoad, interval, clearValue, safeSetValue ]); | ||
|
||
return ( | ||
<div id={ iframeContainerId } className="fjs-sandbox-iframe-container"></div> | ||
); | ||
} | ||
|
||
JSFunctionField.config = { | ||
type: 'script', | ||
label: 'JS Function', | ||
group: 'advanced', | ||
keyed: true, | ||
allowDoNotSubmit: true, | ||
escapeGridRender: true, | ||
create: (options = {}) => ({ | ||
jsFunction: 'setValue(data.value)', | ||
functionParameters: '={\n value: 42\n}', | ||
computeOn: 'load', | ||
interval: 1000, | ||
...options, | ||
}) | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/form-js-viewer/src/render/components/icons/JSFunction.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.