UNPKG

@chief-editor/runtime

Version:

React Runtime for Flow Editor

1 lines 1.97 kB
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var a in e=arguments[n])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t}).apply(this,arguments)},__rest=this&&this.__rest||function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(r=Object.getOwnPropertySymbols(t);a<r.length;a++)e.indexOf(r[a])<0&&Object.prototype.propertyIsEnumerable.call(t,r[a])&&(n[r[a]]=t[r[a]])}return n};import{useRuntime}from"@chief-editor/runtime";import React,{useCallback}from"react";import{useSafeInit,useUpdate}from"@rc-hooks/use";import{getBrickLayout}from"../util/layout";import{RuntimeDispatcher}from"../components/RuntimeDispatcher";export function useBrickInstance(t){var e=useRuntime(),n=useUpdate();useSafeInit((function(){return t.addListener("repaint",n),function(){return t.removeListener("repaint",n)}}));var r=useCallback((function(e,n){"string"!=typeof e?t.dispatchEvent(e.type,e.nativeEvent):t.dispatchEvent(e,n)}),[]),a=useCallback((function(e){t.setValue(e,!0),t.dispatchEvent("onChange",e)}),[]),s=useCallback((function(e,n,r){var a=t.getPart(e);return null==a?null:React.createElement(RuntimeDispatcher,{node:a,className:r,style:n})}),[]),o=useCallback((function(e){return t.getDatasource(e)}),[]),i=useCallback((function(e){return t.getHook(e)}),[]),u=useCallback((function(e){return t.setState(e)}),[]),c=useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return t.getByExpression.apply(t,e)}),[]),l=t.getInstance(),p=l.layout,f=l.styles,y=void 0===f?{}:f,g=l.env,m=__rest(l,["layout","styles","env"]),v=t.getBoardType(),h=t.isFloat();return __assign(__assign({},m),{layout:getBrickLayout(p,v,h),env:e.env||g,styles:y,setValue:a,setState:u,dispatchEvent:r,renderPart:s,getDatasource:o,getHook:i,getByExpression:c})}