@keboola/flow-builder
Advanced tools
Comparing version 0.4.1 to 0.5.0
export declare type DragEventHandler = (position: [number, number]) => void; | ||
export declare function useDrag({ onDragStart, onDragMove, onDragEnd, onClick }: { | ||
export declare function useDrag({ onDragStart, onDragMove, onDragEnd, onSelect }: { | ||
onDragStart?: DragEventHandler; | ||
onDragMove?: DragEventHandler; | ||
onDragEnd?: DragEventHandler; | ||
onClick?: () => void; | ||
onSelect?: () => void; | ||
}): readonly [(ref: HTMLElement | null) => void, (evt: React.MouseEvent) => void]; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e);class r{constructor(e,t){this.x=e,this.y=t}subtract(e){return new r(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const l=(e=0,t=0)=>new r(e,t);l.from=e=>e instanceof MouseEvent?new r(e.clientX,e.clientY):new r(e[0],e[1]),l.pmid=(e,t)=>new r((e.x+t.x)/2,(e.y+t.y)/2),l.rectMid=e=>new r(e.x+e.width/2,e.y+e.height/2),l.offsetOf=e=>new r(e.left,e.top);const a=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),o=(e,t)=>{const n=l.pmid(l(e.x,e.y),l(t.x,t.y)),r=t.x-e.x,a=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+a/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},s=(e,t)=>{let n=[];for(let r=0,l=e.length;r<l;++r){const l=e[r];t(l,r,e)?n.push(l):e[r-n.length]=l}return e.length-=n.length,n},u=/(.+\..+)->(.+\..+)/;const i=t=>{const n=e.Children.toArray(t);return{remainder:n,inputs:s(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:s(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},d=e=>null,c=e=>null;d._$isInput=!0,c._$isOutput=!0;const p=e.createContext({parent:null});exports.Graph=({edges:t,children:r,style:a,calculatePath:s})=>{if(!r)return null;const[i,d]=e.useState([]),c=e.useRef(null);return e.useEffect((()=>{const e=c.current.getBoundingClientRect(),n=l(e.x,e.y);d(t.map((e=>((e,t,n,r=o)=>{const a=t.match(u);if(!a)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[s,i]=a.slice(1),d=e.querySelector(`div[data-name='${s}']`),c=e.querySelector(`div[data-name='${i}']`);if(!d||!c){const[e,n]=d?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===d.parentElement.dataset.type||"group"===c.parentElement.dataset.type){const e="group"===d.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(l.rectMid(d.getBoundingClientRect()).subtract(n),l.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,s))))}),[t,s]),n.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},a)},n.default.createElement("svg",null,i.map((e=>n.default.createElement("path",{key:e.edge,d:e.d})))),r)},exports.Group=t=>{if(!t.children)return null;const{inputs:r,outputs:a,remainder:o}=i(t.children),s=e.useRef(null),[u]=e.useState({value:!1}),d=e.useCallback((e=>{var n,r,l;const a=null===(n=s.current)||void 0===n?void 0:n.getBoundingClientRect();if(!a)return!1;const o=e.clientX,i=e.clientY,d=a.x<o&&a.y<i&&a.x+a.width>o&&a.y+a.height>i;d!==u.value&&(u.value=d,u.value?null===(r=t.onMouseEnter)||void 0===r||r.call(t):null===(l=t.onMouseLeave)||void 0===l||l.call(t))}),[]);return e.useEffect((()=>(window.addEventListener("mousemove",d),()=>window.removeEventListener("mousemove",d))),[]),n.default.createElement(p.Provider,{value:{parent:"Group"}},n.default.createElement("div",{ref:s,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),l.from(t.position).css())},n.default.createElement("div",{className:"flow-builder--content"},o),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},r.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},a.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r))))))},exports.Input=d,exports.Node=t=>{if(!t.children)return null;const r=e.useContext(p),{inputs:o,outputs:s,remainder:u}=i(t.children);"Group"===r.parent&&(o.length>0||s.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[d,c]=function({onDragStart:t,onDragMove:n,onDragEnd:r,onClick:a}){const o=[t,n,r,a],[s]=e.useState({start:null,current:null,node:null,container:null}),u=()=>s.container?l.offsetOf(s.container.getBoundingClientRect()):l();return e.useEffect((()=>{const e=e=>{if(!s.start)return;const r=l.from(e),a=r.subtract(u());s.current?(s.current=a,null==n||n(a.array())):s.start.dist(r)>20&&(s.current=a,null==t||t(a.array()))},o=e=>{const t=l.from(e).subtract(u());s.current?null==r||r(t.array()):s.start&&(null==a||a()),s.start=null,s.current=null};return window.addEventListener("mousemove",e),window.addEventListener("mouseup",o),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("mouseup",o)}}),o),[e.useCallback((e=>{e&&s.node!==e&&(s.node=e,s.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),o),e.useCallback((e=>{s.start=l(e.clientX,e.clientY)}),o)]}(t.draggable?t:{onClick:t.onClick});return n.default.createElement("div",{ref:d,"data-type":"node","data-name":t.name,className:a("flow-builder--node",[t.className,!!t.className]),style:Object.assign(Object.assign({},t.style),t.position?l.from(t.position).css():{}),onMouseDown:c},n.default.createElement("div",{className:"flow-builder--content"},u),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==r.parent&&o.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==r.parent&&s.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))))},exports.Output=c; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e);class r{constructor(e,t){this.x=e,this.y=t}subtract(e){return new r(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const l=(e=0,t=0)=>new r(e,t);l.from=e=>e instanceof MouseEvent?new r(e.clientX,e.clientY):new r(e[0],e[1]),l.pmid=(e,t)=>new r((e.x+t.x)/2,(e.y+t.y)/2),l.rectMid=e=>new r(e.x+e.width/2,e.y+e.height/2),l.offsetOf=e=>new r(e.left,e.top);const o=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),a=(e,t)=>{const n=l.pmid(l(e.x,e.y),l(t.x,t.y)),r=t.x-e.x,o=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+o/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},u=(e,t)=>{let n=[];for(let r=0,l=e.length;r<l;++r){const l=e[r];t(l,r,e)?n.push(l):e[r-n.length]=l}return e.length-=n.length,n},s=/(.+\..+)->(.+\..+)/;const i=t=>{const n=e.Children.toArray(t);return{remainder:n,inputs:u(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:u(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},d=e=>null,c=e=>null;d._$isInput=!0,c._$isOutput=!0;const p=e.createContext({parent:null});exports.Graph=({edges:t,children:r,style:o,calculatePath:u})=>{if(!r)return null;const[i,d]=e.useState([]),c=e.useRef(null);return e.useEffect((()=>{const e=c.current.getBoundingClientRect(),n=l(e.x,e.y);d(t.map((e=>((e,t,n,r=a)=>{const o=t.match(s);if(!o)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[u,i]=o.slice(1),d=e.querySelector(`div[data-name='${u}']`),c=e.querySelector(`div[data-name='${i}']`);if(!d||!c){const[e,n]=d?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===d.parentElement.dataset.type||"group"===c.parentElement.dataset.type){const e="group"===d.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(l.rectMid(d.getBoundingClientRect()).subtract(n),l.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,u))))}),[t,u]),n.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},o)},n.default.createElement("svg",null,i.map((e=>n.default.createElement("path",{key:e.edge,d:e.d})))),r)},exports.Group=t=>{if(!t.children)return null;const{inputs:r,outputs:o,remainder:a}=i(t.children),u=e.useRef(null),[s]=e.useState({value:!1}),d=e.useCallback((e=>{var n,r,l;const o=null===(n=u.current)||void 0===n?void 0:n.getBoundingClientRect();if(!o)return!1;const a=e.clientX,i=e.clientY,d=o.x<a&&o.y<i&&o.x+o.width>a&&o.y+o.height>i;d!==s.value&&(s.value=d,s.value?null===(r=t.onMouseEnter)||void 0===r||r.call(t):null===(l=t.onMouseLeave)||void 0===l||l.call(t))}),[]);return e.useEffect((()=>(window.addEventListener("mousemove",d),()=>window.removeEventListener("mousemove",d))),[]),n.default.createElement(p.Provider,{value:{parent:"Group"}},n.default.createElement("div",{ref:u,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),l.from(t.position).css())},n.default.createElement("div",{className:"flow-builder--content"},a),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},r.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},o.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r))))))},exports.Input=d,exports.Node=t=>{if(!t.children)return null;const r=e.useContext(p),{inputs:a,outputs:u,remainder:s}=i(t.children);"Group"===r.parent&&(a.length>0||u.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[d,c]=function({onDragStart:t,onDragMove:n,onDragEnd:r,onSelect:o}){const a=[t,n,r,o],[u]=e.useState({start:null,current:null,node:null,container:null}),s=()=>u.container?l.offsetOf(u.container.getBoundingClientRect()):l();return e.useEffect((()=>{const e=e=>{if(!u.start)return;const r=l.from(e),o=r.subtract(s());u.current?(u.current=o,null==n||n(o.array())):u.start.dist(r)>20&&(u.current=o,null==t||t(o.array()))},a=e=>{const t=l.from(e).subtract(s());u.current?null==r||r(t.array()):u.start&&(null==o||o()),u.start=null,u.current=null};return window.addEventListener("mousemove",e),window.addEventListener("mouseup",a),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("mouseup",a)}}),a),[e.useCallback((e=>{e&&u.node!==e&&(u.node=e,u.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),a),e.useCallback((e=>{u.start=l(e.clientX,e.clientY)}),a)]}(t.draggable?t:{onSelect:t.onSelect});return n.default.createElement("div",{ref:d,"data-type":"node","data-name":t.name,className:o("flow-builder--node",[t.className,!!t.className]),style:Object.assign(Object.assign({},t.style),t.position?l.from(t.position).css():{}),onMouseDown:c,tabIndex:t.onSelect&&0,onKeyDown:e=>{var n;return"Enter"===e.key&&(null===(n=t.onSelect)||void 0===n?void 0:n.call(t))},role:t.onSelect&&"button"},n.default.createElement("div",{className:"flow-builder--content"},s),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==r.parent&&a.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==r.parent&&u.map((({name:e,children:r,style:l})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:l},r)))))},exports.Output=c; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,2 +0,2 @@ | ||
import e,{Children as t,createContext as n,useCallback as r,useState as o,useEffect as l,useContext as a,useRef as i}from"react";class s{constructor(e,t){this.x=e,this.y=t}subtract(e){return new s(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const u=(e=0,t=0)=>new s(e,t);u.from=e=>e instanceof MouseEvent?new s(e.clientX,e.clientY):new s(e[0],e[1]),u.pmid=(e,t)=>new s((e.x+t.x)/2,(e.y+t.y)/2),u.rectMid=e=>new s(e.x+e.width/2,e.y+e.height/2),u.offsetOf=e=>new s(e.left,e.top);const d=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),c=(e,t)=>{const n=u.pmid(u(e.x,e.y),u(t.x,t.y)),r=t.x-e.x,o=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+o/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},m=(e,t)=>{let n=[];for(let r=0,o=e.length;r<o;++r){const o=e[r];t(o,r,e)?n.push(o):e[r-n.length]=o}return e.length-=n.length,n},p=/(.+\..+)->(.+\..+)/;const y=e=>{const n=t.toArray(e);return{remainder:n,inputs:m(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:m(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},f=e=>null,v=e=>null;f._$isInput=!0,v._$isOutput=!0;const w=n({parent:null});const h=t=>{if(!t.children)return null;const n=a(w),{inputs:i,outputs:s,remainder:c}=y(t.children);"Group"===n.parent&&(i.length>0||s.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[m,p]=function({onDragStart:e,onDragMove:t,onDragEnd:n,onClick:a}){const i=[e,t,n,a],[s]=o({start:null,current:null,node:null,container:null}),d=()=>s.container?u.offsetOf(s.container.getBoundingClientRect()):u();return l((()=>{const r=n=>{if(!s.start)return;const r=u.from(n),o=r.subtract(d());s.current?(s.current=o,null==t||t(o.array())):s.start.dist(r)>20&&(s.current=o,null==e||e(o.array()))},o=e=>{const t=u.from(e).subtract(d());s.current?null==n||n(t.array()):s.start&&(null==a||a()),s.start=null,s.current=null};return window.addEventListener("mousemove",r),window.addEventListener("mouseup",o),()=>{window.removeEventListener("mousemove",r),window.removeEventListener("mouseup",o)}}),i),[r((e=>{e&&s.node!==e&&(s.node=e,s.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),i),r((e=>{s.start=u(e.clientX,e.clientY)}),i)]}(t.draggable?t:{onClick:t.onClick});return e.createElement("div",{ref:m,"data-type":"node","data-name":t.name,className:d("flow-builder--node",[t.className,!!t.className]),style:Object.assign(Object.assign({},t.style),t.position?u.from(t.position).css():{}),onMouseDown:p},e.createElement("div",{className:"flow-builder--content"},c),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&i.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&s.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))))},g=t=>{if(!t.children)return null;const{inputs:n,outputs:a,remainder:s}=y(t.children),d=i(null),[c]=o({value:!1}),m=r((e=>{var n,r,o;const l=null===(n=d.current)||void 0===n?void 0:n.getBoundingClientRect();if(!l)return!1;const a=e.clientX,i=e.clientY,s=l.x<a&&l.y<i&&l.x+l.width>a&&l.y+l.height>i;s!==c.value&&(c.value=s,c.value?null===(r=t.onMouseEnter)||void 0===r||r.call(t):null===(o=t.onMouseLeave)||void 0===o||o.call(t))}),[]);return l((()=>(window.addEventListener("mousemove",m),()=>window.removeEventListener("mousemove",m))),[]),e.createElement(w.Provider,{value:{parent:"Group"}},e.createElement("div",{ref:d,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),u.from(t.position).css())},e.createElement("div",{className:"flow-builder--content"},s),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},a.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r))))))},b=({edges:t,children:n,style:r,calculatePath:a})=>{if(!n)return null;const[s,d]=o([]),m=i(null);return l((()=>{const e=m.current.getBoundingClientRect(),n=u(e.x,e.y);d(t.map((e=>((e,t,n,r=c)=>{const o=t.match(p);if(!o)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[l,a]=o.slice(1),i=e.querySelector(`div[data-name='${l}']`),s=e.querySelector(`div[data-name='${a}']`);if(!i||!s){const[e,n]=i?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===i.parentElement.dataset.type||"group"===s.parentElement.dataset.type){const e="group"===i.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(u.rectMid(i.getBoundingClientRect()).subtract(n),u.rectMid(s.getBoundingClientRect()).subtract(n))}})(m.current,e,n,a))))}),[t,a]),e.createElement("div",{ref:m,"data-type":"graph",className:"flow-builder",style:Object.assign({},r)},e.createElement("svg",null,s.map((t=>e.createElement("path",{key:t.edge,d:t.d})))),n)};export{b as Graph,g as Group,f as Input,h as Node,v as Output}; | ||
import e,{Children as t,createContext as n,useCallback as r,useState as o,useEffect as l,useContext as a,useRef as i}from"react";class s{constructor(e,t){this.x=e,this.y=t}subtract(e){return new s(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const u=(e=0,t=0)=>new s(e,t);u.from=e=>e instanceof MouseEvent?new s(e.clientX,e.clientY):new s(e[0],e[1]),u.pmid=(e,t)=>new s((e.x+t.x)/2,(e.y+t.y)/2),u.rectMid=e=>new s(e.x+e.width/2,e.y+e.height/2),u.offsetOf=e=>new s(e.left,e.top);const d=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),c=(e,t)=>{const n=u.pmid(u(e.x,e.y),u(t.x,t.y)),r=t.x-e.x,o=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+o/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},m=(e,t)=>{let n=[];for(let r=0,o=e.length;r<o;++r){const o=e[r];t(o,r,e)?n.push(o):e[r-n.length]=o}return e.length-=n.length,n},p=/(.+\..+)->(.+\..+)/;const y=e=>{const n=t.toArray(e);return{remainder:n,inputs:m(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:m(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},f=e=>null,v=e=>null;f._$isInput=!0,v._$isOutput=!0;const w=n({parent:null});const h=t=>{if(!t.children)return null;const n=a(w),{inputs:i,outputs:s,remainder:c}=y(t.children);"Group"===n.parent&&(i.length>0||s.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[m,p]=function({onDragStart:e,onDragMove:t,onDragEnd:n,onSelect:a}){const i=[e,t,n,a],[s]=o({start:null,current:null,node:null,container:null}),d=()=>s.container?u.offsetOf(s.container.getBoundingClientRect()):u();return l((()=>{const r=n=>{if(!s.start)return;const r=u.from(n),o=r.subtract(d());s.current?(s.current=o,null==t||t(o.array())):s.start.dist(r)>20&&(s.current=o,null==e||e(o.array()))},o=e=>{const t=u.from(e).subtract(d());s.current?null==n||n(t.array()):s.start&&(null==a||a()),s.start=null,s.current=null};return window.addEventListener("mousemove",r),window.addEventListener("mouseup",o),()=>{window.removeEventListener("mousemove",r),window.removeEventListener("mouseup",o)}}),i),[r((e=>{e&&s.node!==e&&(s.node=e,s.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),i),r((e=>{s.start=u(e.clientX,e.clientY)}),i)]}(t.draggable?t:{onSelect:t.onSelect});return e.createElement("div",{ref:m,"data-type":"node","data-name":t.name,className:d("flow-builder--node",[t.className,!!t.className]),style:Object.assign(Object.assign({},t.style),t.position?u.from(t.position).css():{}),onMouseDown:p,tabIndex:t.onSelect&&0,onKeyDown:e=>{var n;return"Enter"===e.key&&(null===(n=t.onSelect)||void 0===n?void 0:n.call(t))},role:t.onSelect&&"button"},e.createElement("div",{className:"flow-builder--content"},c),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&i.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&s.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))))},g=t=>{if(!t.children)return null;const{inputs:n,outputs:a,remainder:s}=y(t.children),d=i(null),[c]=o({value:!1}),m=r((e=>{var n,r,o;const l=null===(n=d.current)||void 0===n?void 0:n.getBoundingClientRect();if(!l)return!1;const a=e.clientX,i=e.clientY,s=l.x<a&&l.y<i&&l.x+l.width>a&&l.y+l.height>i;s!==c.value&&(c.value=s,c.value?null===(r=t.onMouseEnter)||void 0===r||r.call(t):null===(o=t.onMouseLeave)||void 0===o||o.call(t))}),[]);return l((()=>(window.addEventListener("mousemove",m),()=>window.removeEventListener("mousemove",m))),[]),e.createElement(w.Provider,{value:{parent:"Group"}},e.createElement("div",{ref:d,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),u.from(t.position).css())},e.createElement("div",{className:"flow-builder--content"},s),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},a.map((({name:n,children:r,style:o})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:o},r))))))},b=({edges:t,children:n,style:r,calculatePath:a})=>{if(!n)return null;const[s,d]=o([]),m=i(null);return l((()=>{const e=m.current.getBoundingClientRect(),n=u(e.x,e.y);d(t.map((e=>((e,t,n,r=c)=>{const o=t.match(p);if(!o)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[l,a]=o.slice(1),i=e.querySelector(`div[data-name='${l}']`),s=e.querySelector(`div[data-name='${a}']`);if(!i||!s){const[e,n]=i?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===i.parentElement.dataset.type||"group"===s.parentElement.dataset.type){const e="group"===i.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(u.rectMid(i.getBoundingClientRect()).subtract(n),u.rectMid(s.getBoundingClientRect()).subtract(n))}})(m.current,e,n,a))))}),[t,a]),e.createElement("div",{ref:m,"data-type":"graph",className:"flow-builder",style:Object.assign({},r)},e.createElement("svg",null,s.map((t=>e.createElement("path",{key:t.edge,d:t.d})))),n)};export{b as Graph,g as Group,f as Input,h as Node,v as Output}; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Flow={},e.React)}(this,(function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t);class l{constructor(e,t){this.x=e,this.y=t}subtract(e){return new l(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const o=(e=0,t=0)=>new l(e,t);o.from=e=>e instanceof MouseEvent?new l(e.clientX,e.clientY):new l(e[0],e[1]),o.pmid=(e,t)=>new l((e.x+t.x)/2,(e.y+t.y)/2),o.rectMid=e=>new l(e.x+e.width/2,e.y+e.height/2),o.offsetOf=e=>new l(e.left,e.top);const a=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),i=(e,t)=>{const n=o.pmid(o(e.x,e.y),o(t.x,t.y)),r=t.x-e.x,l=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+l/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},u=(e,t)=>{let n=[];for(let r=0,l=e.length;r<l;++r){const l=e[r];t(l,r,e)?n.push(l):e[r-n.length]=l}return e.length-=n.length,n},s=/(.+\..+)->(.+\..+)/;const d=e=>{const n=t.Children.toArray(e);return{remainder:n,inputs:u(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:u(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},c=e=>null,f=e=>null;c._$isInput=!0,f._$isOutput=!0;const p=t.createContext({parent:null});e.Graph=({edges:e,children:n,style:l,calculatePath:a})=>{if(!n)return null;const[u,d]=t.useState([]),c=t.useRef(null);return t.useEffect((()=>{const t=c.current.getBoundingClientRect(),n=o(t.x,t.y);d(e.map((e=>((e,t,n,r=i)=>{const l=t.match(s);if(!l)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[a,u]=l.slice(1),d=e.querySelector(`div[data-name='${a}']`),c=e.querySelector(`div[data-name='${u}']`);if(!d||!c){const[e,n]=d?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===d.parentElement.dataset.type||"group"===c.parentElement.dataset.type){const e="group"===d.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(o.rectMid(d.getBoundingClientRect()).subtract(n),o.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,a))))}),[e,a]),r.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},l)},r.default.createElement("svg",null,u.map((e=>r.default.createElement("path",{key:e.edge,d:e.d})))),n)},e.Group=e=>{if(!e.children)return null;const{inputs:n,outputs:l,remainder:a}=d(e.children),i=t.useRef(null),[u]=t.useState({value:!1}),s=t.useCallback((t=>{var n,r,l;const o=null===(n=i.current)||void 0===n?void 0:n.getBoundingClientRect();if(!o)return!1;const a=t.clientX,s=t.clientY,d=o.x<a&&o.y<s&&o.x+o.width>a&&o.y+o.height>s;d!==u.value&&(u.value=d,u.value?null===(r=e.onMouseEnter)||void 0===r||r.call(e):null===(l=e.onMouseLeave)||void 0===l||l.call(e))}),[]);return t.useEffect((()=>(window.addEventListener("mousemove",s),()=>window.removeEventListener("mousemove",s))),[]),r.default.createElement(p.Provider,{value:{parent:"Group"}},r.default.createElement("div",{ref:i,"data-type":"group","data-name":e.name,className:"flow-builder--group",style:Object.assign(Object.assign({},e.style),o.from(e.position).css())},r.default.createElement("div",{className:"flow-builder--content"},a),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},l.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n))))))},e.Input=c,e.Node=e=>{if(!e.children)return null;const n=t.useContext(p),{inputs:l,outputs:i,remainder:u}=d(e.children);"Group"===n.parent&&(l.length>0||i.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[s,c]=function({onDragStart:e,onDragMove:n,onDragEnd:r,onClick:l}){const a=[e,n,r,l],[i]=t.useState({start:null,current:null,node:null,container:null}),u=()=>i.container?o.offsetOf(i.container.getBoundingClientRect()):o();return t.useEffect((()=>{const t=t=>{if(!i.start)return;const r=o.from(t),l=r.subtract(u());i.current?(i.current=l,null==n||n(l.array())):i.start.dist(r)>20&&(i.current=l,null==e||e(l.array()))},a=e=>{const t=o.from(e).subtract(u());i.current?null==r||r(t.array()):i.start&&(null==l||l()),i.start=null,i.current=null};return window.addEventListener("mousemove",t),window.addEventListener("mouseup",a),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",a)}}),a),[t.useCallback((e=>{e&&i.node!==e&&(i.node=e,i.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),a),t.useCallback((e=>{i.start=o(e.clientX,e.clientY)}),a)]}(e.draggable?e:{onClick:e.onClick});return r.default.createElement("div",{ref:s,"data-type":"node","data-name":e.name,className:a("flow-builder--node",[e.className,!!e.className]),style:Object.assign(Object.assign({},e.style),e.position?o.from(e.position).css():{}),onMouseDown:c},r.default.createElement("div",{className:"flow-builder--content"},u),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&l.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&i.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))))},e.Output=f,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Flow={},e.React)}(this,(function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t);class l{constructor(e,t){this.x=e,this.y=t}subtract(e){return new l(this.x-e.x,this.y-e.y)}dist(e){return Math.hypot(e.x-this.x,e.y-this.y)}toString(){return`[${this.x.toFixed(2)}, ${this.y.toFixed(2)}]`}css(){return{left:`${this.x}px`,top:`${this.y}px`}}array(){return[this.x,this.y]}}const o=(e=0,t=0)=>new l(e,t);o.from=e=>e instanceof MouseEvent?new l(e.clientX,e.clientY):new l(e[0],e[1]),o.pmid=(e,t)=>new l((e.x+t.x)/2,(e.y+t.y)/2),o.rectMid=e=>new l(e.x+e.width/2,e.y+e.height/2),o.offsetOf=e=>new l(e.left,e.top);const a=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),u=(e,t)=>{const n=o.pmid(o(e.x,e.y),o(t.x,t.y)),r=t.x-e.x,l=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-r/32} ${e.y+l/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},i=(e,t)=>{let n=[];for(let r=0,l=e.length;r<l;++r){const l=e[r];t(l,r,e)?n.push(l):e[r-n.length]=l}return e.length-=n.length,n},s=/(.+\..+)->(.+\..+)/;const d=e=>{const n=t.Children.toArray(e);return{remainder:n,inputs:i(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isInput})).map((e=>e.props)),outputs:i(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},c=e=>null,f=e=>null;c._$isInput=!0,f._$isOutput=!0;const p=t.createContext({parent:null});e.Graph=({edges:e,children:n,style:l,calculatePath:a})=>{if(!n)return null;const[i,d]=t.useState([]),c=t.useRef(null);return t.useEffect((()=>{const t=c.current.getBoundingClientRect(),n=o(t.x,t.y);d(e.map((e=>((e,t,n,r=u)=>{const l=t.match(s);if(!l)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[a,i]=l.slice(1),d=e.querySelector(`div[data-name='${a}']`),c=e.querySelector(`div[data-name='${i}']`);if(!d||!c){const[e,n]=d?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===d.parentElement.dataset.type||"group"===c.parentElement.dataset.type){const e="group"===d.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:r(o.rectMid(d.getBoundingClientRect()).subtract(n),o.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,a))))}),[e,a]),r.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},l)},r.default.createElement("svg",null,i.map((e=>r.default.createElement("path",{key:e.edge,d:e.d})))),n)},e.Group=e=>{if(!e.children)return null;const{inputs:n,outputs:l,remainder:a}=d(e.children),u=t.useRef(null),[i]=t.useState({value:!1}),s=t.useCallback((t=>{var n,r,l;const o=null===(n=u.current)||void 0===n?void 0:n.getBoundingClientRect();if(!o)return!1;const a=t.clientX,s=t.clientY,d=o.x<a&&o.y<s&&o.x+o.width>a&&o.y+o.height>s;d!==i.value&&(i.value=d,i.value?null===(r=e.onMouseEnter)||void 0===r||r.call(e):null===(l=e.onMouseLeave)||void 0===l||l.call(e))}),[]);return t.useEffect((()=>(window.addEventListener("mousemove",s),()=>window.removeEventListener("mousemove",s))),[]),r.default.createElement(p.Provider,{value:{parent:"Group"}},r.default.createElement("div",{ref:u,"data-type":"group","data-name":e.name,className:"flow-builder--group",style:Object.assign(Object.assign({},e.style),o.from(e.position).css())},r.default.createElement("div",{className:"flow-builder--content"},a),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},l.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n))))))},e.Input=c,e.Node=e=>{if(!e.children)return null;const n=t.useContext(p),{inputs:l,outputs:u,remainder:i}=d(e.children);"Group"===n.parent&&(l.length>0||u.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored");const[s,c]=function({onDragStart:e,onDragMove:n,onDragEnd:r,onSelect:l}){const a=[e,n,r,l],[u]=t.useState({start:null,current:null,node:null,container:null}),i=()=>u.container?o.offsetOf(u.container.getBoundingClientRect()):o();return t.useEffect((()=>{const t=t=>{if(!u.start)return;const r=o.from(t),l=r.subtract(i());u.current?(u.current=l,null==n||n(l.array())):u.start.dist(r)>20&&(u.current=l,null==e||e(l.array()))},a=e=>{const t=o.from(e).subtract(i());u.current?null==r||r(t.array()):u.start&&(null==l||l()),u.start=null,u.current=null};return window.addEventListener("mousemove",t),window.addEventListener("mouseup",a),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",a)}}),a),[t.useCallback((e=>{e&&u.node!==e&&(u.node=e,u.container=function(e,t){for(const n of function*(e){let t=e.parentElement;for(;t;)yield t,t=t.parentElement}(e))if(t(n))return n;return null}(e,(e=>"div"===e.tagName.toLowerCase()&&"graph"===e.dataset.type&&e.classList.contains("flow-builder"))))}),a),t.useCallback((e=>{u.start=o(e.clientX,e.clientY)}),a)]}(e.draggable?e:{onSelect:e.onSelect});return r.default.createElement("div",{ref:s,"data-type":"node","data-name":e.name,className:a("flow-builder--node",[e.className,!!e.className]),style:Object.assign(Object.assign({},e.style),e.position?o.from(e.position).css():{}),onMouseDown:c,tabIndex:e.onSelect&&0,onKeyDown:t=>{var n;return"Enter"===t.key&&(null===(n=e.onSelect)||void 0===n?void 0:n.call(e))},role:e.onSelect&&"button"},r.default.createElement("div",{className:"flow-builder--content"},i),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&l.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))),r.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&u.map((({name:t,children:n,style:l})=>r.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:l},n)))))},e.Output=f,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -14,4 +14,4 @@ import React from "react"; | ||
onDragEnd?: (position: [number, number]) => void; | ||
onClick?: () => void; | ||
onSelect?: () => void; | ||
}; | ||
} |
{ | ||
"name": "@keboola/flow-builder", | ||
"description": "Flow graph rendering", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99193
327