@keboola/flow-builder
Advanced tools
Comparing version 0.3.0 to 0.4.1
@@ -1,6 +0,3 @@ | ||
import { RefObject } from "react"; | ||
export declare const GraphContext: import("react").Context<{ | ||
parent: string | null; | ||
}>; | ||
export declare const ContainerContext: import("react").Context<RefObject<HTMLDivElement>>; | ||
export declare const useContainerContext: () => RefObject<HTMLDivElement>; |
@@ -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 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`}}}const r=(e=0,t=0)=>new l(e,t);r.from=e=>new l(e[0],e[1]),r.pmid=(e,t)=>new l((e.x+t.x)/2,(e.y+t.y)/2),r.rectMid=e=>new l(e.x+e.width/2,e.y+e.height/2);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=r.pmid(r(e.x,e.y),r(t.x,t.y)),l=t.x-e.x,a=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-l/32} ${e.y+a/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},i=(e,t)=>{let n=[];for(let l=0,r=e.length;l<r;++l){const r=e[l];t(r,l,e)?n.push(r):e[l-n.length]=r}return e.length-=n.length,n},u=/(.+\..+)->(.+\..+)/,s=t=>{const n=e.Children.toArray(t);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))}},d=e=>null,c=e=>null;d._$isInput=!0,c._$isOutput=!0;const p=e.createContext({parent:null}),m=e.createContext(e.createRef());exports.Graph=({edges:t,children:l,style:a,calculatePath:i})=>{if(!l)return null;const[s,d]=e.useState([]),c=e.useRef(null);return e.useEffect((()=>{const e=c.current.getBoundingClientRect(),n=r(e.x,e.y);d(t.map((e=>((e,t,n,l=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[i,s]=a.slice(1),d=e.querySelector(`div[data-name='${i}']`),c=e.querySelector(`div[data-name='${s}']`);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:l(r.rectMid(d.getBoundingClientRect()).subtract(n),r.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,i))))}),[t,i]),n.default.createElement(m.Provider,{value:c},n.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},a)},n.default.createElement("svg",null,s.map((e=>n.default.createElement("path",{key:e.edge,d:e.d})))),l))},exports.Group=t=>{if(!t.children)return null;const{inputs:l,outputs:a,remainder:o}=s(t.children),i=e.useRef(null),[u]=e.useState({value:!1}),d=e.useCallback((e=>{var n,l,r;const a=null===(n=i.current)||void 0===n?void 0:n.getBoundingClientRect();if(!a)return!1;const o=e.clientX,s=e.clientY,d=a.x<o&&a.y<s&&a.x+a.width>o&&a.y+a.height>s;d!==u.value&&(u.value=d,u.value?null===(l=t.onMouseEnter)||void 0===l||l.call(t):null===(r=t.onMouseLeave)||void 0===r||r.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:i,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),r.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"},l.map((({name:e,children:l,style:r})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:r},l)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},a.map((({name:e,children:l,style:r})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:r},l))))))},exports.Input=d,exports.Node=t=>{if(!t.children)return null;const l=e.useContext(p),{inputs:o,outputs:i,remainder:u}=s(t.children);"Group"===l.parent&&(o.length>0||i.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored"),e.useContext(m);const[d]=e.useState({start:null,current:null}),c=e.useCallback((e=>{d.start=r(e.clientX,e.clientY)}),[]),f=e.useCallback((e=>{var n,l;d.start&&(d.current?(d.current=r(e.clientX,e.clientY),null===(n=t.onDragMove)||void 0===n||n.call(t,[e.clientX,e.clientY])):d.start.dist(r(e.clientX,e.clientY))>10&&(d.current=r(e.clientX,e.clientY),null===(l=t.onDragStart)||void 0===l||l.call(t,[e.clientX,e.clientY])))}),[]),v=e.useCallback((e=>{var n;d.start=null,d.current&&(d.current=null,null===(n=t.onDragEnd)||void 0===n||n.call(t,[e.clientX,e.clientY]))}),[]);return e.useEffect((()=>(window.addEventListener("mousemove",f),window.addEventListener("mouseup",v),()=>{window.removeEventListener("mousemove",f),window.removeEventListener("mouseup",v)})),[]),n.default.createElement("div",{"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?r.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"!==l.parent&&o.map((({name:e,children:l,style:r})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:r},l)))),n.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==l.parent&&i.map((({name:e,children:l,style:r})=>n.default.createElement("div",{className:"flow-builder--io-port",key:e,"data-name":`${t.name}.${e}`,style:r},l)))))},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 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; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,2 +0,2 @@ | ||
import e,{Children as t,createContext as n,createRef as l,useContext as r,useState as i,useCallback as o,useEffect as a,useRef as s}from"react";class u{constructor(e,t){this.x=e,this.y=t}subtract(e){return new u(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`}}}const d=(e=0,t=0)=>new u(e,t);d.from=e=>new u(e[0],e[1]),d.pmid=(e,t)=>new u((e.x+t.x)/2,(e.y+t.y)/2),d.rectMid=e=>new u(e.x+e.width/2,e.y+e.height/2);const c=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),m=(e,t)=>{const n=d.pmid(d(e.x,e.y),d(t.x,t.y)),l=t.x-e.x,r=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-l/32} ${e.y+r/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},p=(e,t)=>{let n=[];for(let l=0,r=e.length;l<r;++l){const r=e[l];t(r,l,e)?n.push(r):e[l-n.length]=r}return e.length-=n.length,n},v=/(.+\..+)->(.+\..+)/,y=e=>{const n=t.toArray(e);return{remainder:n,inputs:p(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:p(n,(e=>{var t;return null===(t=null==e?void 0:e.type)||void 0===t?void 0:t._$isOutput})).map((e=>e.props))}},w=e=>null,h=e=>null;w._$isInput=!0,h._$isOutput=!0;const g=n({parent:null}),f=n(l()),$=t=>{if(!t.children)return null;const n=r(g),{inputs:l,outputs:s,remainder:u}=y(t.children);"Group"===n.parent&&(l.length>0||s.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored"),r(f);const[m]=i({start:null,current:null}),p=o((e=>{m.start=d(e.clientX,e.clientY)}),[]),v=o((e=>{var n,l;m.start&&(m.current?(m.current=d(e.clientX,e.clientY),null===(n=t.onDragMove)||void 0===n||n.call(t,[e.clientX,e.clientY])):m.start.dist(d(e.clientX,e.clientY))>10&&(m.current=d(e.clientX,e.clientY),null===(l=t.onDragStart)||void 0===l||l.call(t,[e.clientX,e.clientY])))}),[]),w=o((e=>{var n;m.start=null,m.current&&(m.current=null,null===(n=t.onDragEnd)||void 0===n||n.call(t,[e.clientX,e.clientY]))}),[]);return a((()=>(window.addEventListener("mousemove",v),window.addEventListener("mouseup",w),()=>{window.removeEventListener("mousemove",v),window.removeEventListener("mouseup",w)})),[]),e.createElement("div",{"data-type":"node","data-name":t.name,className:c("flow-builder--node",[t.className,!!t.className]),style:Object.assign(Object.assign({},t.style),t.position?d.from(t.position).css():{}),onMouseDown:p},e.createElement("div",{className:"flow-builder--content"},u),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&l.map((({name:n,children:l,style:r})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:r},l)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&s.map((({name:n,children:l,style:r})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:r},l)))))},b=t=>{if(!t.children)return null;const{inputs:n,outputs:l,remainder:r}=y(t.children),u=s(null),[c]=i({value:!1}),m=o((e=>{var n,l,r;const i=null===(n=u.current)||void 0===n?void 0:n.getBoundingClientRect();if(!i)return!1;const o=e.clientX,a=e.clientY,s=i.x<o&&i.y<a&&i.x+i.width>o&&i.y+i.height>a;s!==c.value&&(c.value=s,c.value?null===(l=t.onMouseEnter)||void 0===l||l.call(t):null===(r=t.onMouseLeave)||void 0===r||r.call(t))}),[]);return a((()=>(window.addEventListener("mousemove",m),()=>window.removeEventListener("mousemove",m))),[]),e.createElement(g.Provider,{value:{parent:"Group"}},e.createElement("div",{ref:u,"data-type":"group","data-name":t.name,className:"flow-builder--group",style:Object.assign(Object.assign({},t.style),d.from(t.position).css())},e.createElement("div",{className:"flow-builder--content"},r),e.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:n,children:l,style:r})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:r},l)))),e.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},l.map((({name:n,children:l,style:r})=>e.createElement("div",{className:"flow-builder--io-port",key:n,"data-name":`${t.name}.${n}`,style:r},l))))))},E=({edges:t,children:n,style:l,calculatePath:r})=>{if(!n)return null;const[o,u]=i([]),c=s(null);return a((()=>{const e=c.current.getBoundingClientRect(),n=d(e.x,e.y);u(t.map((e=>((e,t,n,l=m)=>{const r=t.match(v);if(!r)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[i,o]=r.slice(1),a=e.querySelector(`div[data-name='${i}']`),s=e.querySelector(`div[data-name='${o}']`);if(!a||!s){const[e,n]=a?["destination","input"]:["source","output"];throw new Error(`flow-builder: Invalid edge '${t}', ${e} node does not exist or has no such ${n}`)}if("group"===a.parentElement.dataset.type||"group"===s.parentElement.dataset.type){const e="group"===a.parentElement.dataset.type?"source":"destination";throw new Error(`flow-builder: Invalid edge '${t}', ${e} node is inside a group`)}return{edge:t,d:l(d.rectMid(a.getBoundingClientRect()).subtract(n),d.rectMid(s.getBoundingClientRect()).subtract(n))}})(c.current,e,n,r))))}),[t,r]),e.createElement(f.Provider,{value:c},e.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},l)},e.createElement("svg",null,o.map((t=>e.createElement("path",{key:t.edge,d:t.d})))),n))};export{E as Graph,b as Group,w as Input,$ as Node,h 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,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}; | ||
//# 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 l=n(t);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`}}}const a=(e=0,t=0)=>new r(e,t);a.from=e=>new r(e[0],e[1]),a.pmid=(e,t)=>new r((e.x+t.x)/2,(e.y+t.y)/2),a.rectMid=e=>new r(e.x+e.width/2,e.y+e.height/2);const o=(...e)=>e.map((e=>"string"==typeof e?e:e[1]?e[0]:null)).filter((e=>null!==e)).join(" "),i=(e,t)=>{const n=a.pmid(a(e.x,e.y),a(t.x,t.y)),l=t.x-e.x,r=t.y-e.y;return`M ${e.x} ${e.y} Q ${e.x-l/32} ${e.y+r/2}, ${n.x} ${n.y} T ${t.x} ${t.y}`},u=(e,t)=>{let n=[];for(let l=0,r=e.length;l<r;++l){const r=e[l];t(r,l,e)?n.push(r):e[l-n.length]=r}return e.length-=n.length,n},s=/(.+\..+)->(.+\..+)/,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,p=e=>null;c._$isInput=!0,p._$isOutput=!0;const m=t.createContext({parent:null}),f=t.createContext(t.createRef());e.Graph=({edges:e,children:n,style:r,calculatePath:o})=>{if(!n)return null;const[u,d]=t.useState([]),c=t.useRef(null);return t.useEffect((()=>{const t=c.current.getBoundingClientRect(),n=a(t.x,t.y);d(e.map((e=>((e,t,n,l=i)=>{const r=t.match(s);if(!r)throw new Error(`flow-builder: Invalid edge '${t}', the format should be \`\${source}.\${output}->\${destination}.\${input}`);const[o,u]=r.slice(1),d=e.querySelector(`div[data-name='${o}']`),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:l(a.rectMid(d.getBoundingClientRect()).subtract(n),a.rectMid(c.getBoundingClientRect()).subtract(n))}})(c.current,e,n,o))))}),[e,o]),l.default.createElement(f.Provider,{value:c},l.default.createElement("div",{ref:c,"data-type":"graph",className:"flow-builder",style:Object.assign({},r)},l.default.createElement("svg",null,u.map((e=>l.default.createElement("path",{key:e.edge,d:e.d})))),n))},e.Group=e=>{if(!e.children)return null;const{inputs:n,outputs:r,remainder:o}=d(e.children),i=t.useRef(null),[u]=t.useState({value:!1}),s=t.useCallback((t=>{var n,l,r;const a=null===(n=i.current)||void 0===n?void 0:n.getBoundingClientRect();if(!a)return!1;const o=t.clientX,s=t.clientY,d=a.x<o&&a.y<s&&a.x+a.width>o&&a.y+a.height>s;d!==u.value&&(u.value=d,u.value?null===(l=e.onMouseEnter)||void 0===l||l.call(e):null===(r=e.onMouseLeave)||void 0===r||r.call(e))}),[]);return t.useEffect((()=>(window.addEventListener("mousemove",s),()=>window.removeEventListener("mousemove",s))),[]),l.default.createElement(m.Provider,{value:{parent:"Group"}},l.default.createElement("div",{ref:i,"data-type":"group","data-name":e.name,className:"flow-builder--group",style:Object.assign(Object.assign({},e.style),a.from(e.position).css())},l.default.createElement("div",{className:"flow-builder--content"},o),l.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},n.map((({name:t,children:n,style:r})=>l.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:r},n)))),l.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},r.map((({name:t,children:n,style:r})=>l.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:r},n))))))},e.Input=c,e.Node=e=>{if(!e.children)return null;const n=t.useContext(m),{inputs:r,outputs:i,remainder:u}=d(e.children);"Group"===n.parent&&(r.length>0||i.length>0)&&console.warn("flow-builder: Inputs/Outputs on Nodes within Groups are ignored"),t.useContext(f);const[s]=t.useState({start:null,current:null}),c=t.useCallback((e=>{s.start=a(e.clientX,e.clientY)}),[]),p=t.useCallback((t=>{var n,l;s.start&&(s.current?(s.current=a(t.clientX,t.clientY),null===(n=e.onDragMove)||void 0===n||n.call(e,[t.clientX,t.clientY])):s.start.dist(a(t.clientX,t.clientY))>10&&(s.current=a(t.clientX,t.clientY),null===(l=e.onDragStart)||void 0===l||l.call(e,[t.clientX,t.clientY])))}),[]),v=t.useCallback((t=>{var n;s.start=null,s.current&&(s.current=null,null===(n=e.onDragEnd)||void 0===n||n.call(e,[t.clientX,t.clientY]))}),[]);return t.useEffect((()=>(window.addEventListener("mousemove",p),window.addEventListener("mouseup",v),()=>{window.removeEventListener("mousemove",p),window.removeEventListener("mouseup",v)})),[]),l.default.createElement("div",{"data-type":"node","data-name":e.name,className:o("flow-builder--node",[e.className,!!e.className]),style:Object.assign(Object.assign({},e.style),e.position?a.from(e.position).css():{}),onMouseDown:c},l.default.createElement("div",{className:"flow-builder--content"},u),l.default.createElement("div",{className:"flow-builder--io flow-builder--io-top"},"Group"!==n.parent&&r.map((({name:t,children:n,style:r})=>l.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:r},n)))),l.default.createElement("div",{className:"flow-builder--io flow-builder--io-bottom"},"Group"!==n.parent&&i.map((({name:t,children:n,style:r})=>l.default.createElement("div",{className:"flow-builder--io-port",key:t,"data-name":`${e.name}.${t}`,style:r},n)))))},e.Output=p,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(" "),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})})); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -10,6 +10,8 @@ import React from "react"; | ||
children?: React.ReactNode; | ||
draggable?: boolean; | ||
onDragStart?: (position: [number, number]) => void; | ||
onDragMove?: (position: [number, number]) => void; | ||
onDragEnd?: (position: [number, number]) => void; | ||
onClick?: () => void; | ||
}; | ||
} |
@@ -12,2 +12,3 @@ export declare class Vector2 { | ||
}; | ||
array(): [number, number]; | ||
} | ||
@@ -19,3 +20,3 @@ /** | ||
(x?: number, y?: number): Vector2; | ||
from(v: [number, number]): Vector2; | ||
from(v: [number, number] | MouseEvent): Vector2; | ||
/** | ||
@@ -29,2 +30,3 @@ * Calculates the midpoint of `a` and `b` | ||
rectMid(rect: DOMRect): Vector2; | ||
offsetOf(rect: DOMRect): Vector2; | ||
}; | ||
@@ -37,5 +39,3 @@ /** | ||
* <element | ||
* style={classes({ | ||
* "disabled": isDisabled | ||
* })} | ||
* style={classes(["disabled", isDisabled])} | ||
* /> | ||
@@ -104,1 +104,4 @@ * ``` | ||
}) => string) => ProcessedEdge; | ||
export declare function walkParents(start: HTMLElement): Generator<HTMLElement, void, unknown>; | ||
export declare function findParent(start: HTMLElement, predicate: (node: HTMLElement) => boolean): HTMLElement | null; | ||
export declare const NOOP: () => void; |
{ | ||
"name": "@keboola/flow-builder", | ||
"description": "Flow graph rendering", | ||
"version": "0.3.0", | ||
"version": "0.4.1", | ||
"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
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
97789
19
325