NodeHelpers
Methods that helps describe a specified Node
.
#
Usage#
useEditor hookYou can access the NodeHelpers via the node
query method in the useEditor
hook.
import {useEditor} from "@craftjs/core";
const TextComponent = () => { const { id } = useNode(); const { query: {node} } = useEditor(); const isRoot = node(id).Root(), isDraggable = node(id).Draggable(); ...}
#
User Component rulesNodeHelpers can also be accessed via the last parameter of each User Component rules.
const MyComp = () => {
}MyComp.craft = { rules: { canDrag: (node: Node, helper: NodeHelpers) => { const ancestors = helper(node.id).ancestors(); ... }, canMoveIn : (incoming: Node, self: Node, helper: NodeHelpers) => { const isRoot = helper(node.id).isRoot(); ... } canMoveOut: (outgoing: Node, self: Node, helper: NodeHelpers) => { const isDeletable = helper(node.id).isDeletable(); ... } }}
#
Methods#
getGet Node
object from id
#
ReturnsNode
#
descendantsReturns an array of Node ids of all child Nodes of a given Node.
#
Parametersdeep
booleanIf set to true, retrieve all descendants in nested levels. Default is falseincludeOnly?
'childNodes' | 'linkedNodes'Get descendants that are either childNodes or linkedNodes. If unset, get all descendants
#
ReturnsNodeId[]
// The descendants of `div` when deep=false<div> <h2>Yo</h2> <Element is={Container}> <h3>Child</h3> </Element></div>
// The descendants of `div` when deep=true<div> <h2>Yo</h2> <Element is={Container}> <h3>Child</h3> </Element></div>
const Container = () => { return ( <div> <Element id="linked-div"> <h1>Hello</h1> <Element> </div> )}
// The descendants of `div` when deep=true and includeOnly="childNodes" only<div> <h2>Yo</h2> <Element is={Container}> <h3>Child</h3> </Element></div>
const Container = () => { return ( <div> <Element id="linked-div"> <h1>Hello</h1> <Element> </div> )}
// The descendants of `div` when deep=true and includeOnly="linkedNodes" only<div> <h2>Yo</h2> <Element is={Container}> <h3>Child</h3> </Element></div>
const Container = () => { return ( <div> <Element id="linked-div"> <h1>Hello</h1> <Element> </div> )}
#
ancestorsReturns an array of Node ids of all ancestors
#
ReturnsNodeId[]
#
linkedNodesReturns an array of linked Node ids
#
ReturnsNodeId[]
#
childNodesReturns an array of child Node ids
#
ReturnsNodeId[]
#
isRootReturns true
if a given Node is the Root Node
#
Returnsboolean
const App = () => { return ( <Editor> <Frame> <div> // true <div>Yo</div> // false <h2>It's me</h2> // false <Element is={Container}> // false <h3>Child</h3> // false </Element> </div> </Frame> </Editor> )}
#
isCanvasCheck if a given Node is a Canvas
#
Returnsboolean
const App = () => { return ( <Editor> <Frame> <Element canvas> // true <div>Yo</div> // false <Element is={Container}>It's me</Element> // false <Element canvas> // true <h3>Child</h3> // false </Element> </Element> </Frame> </Editor> )}
#
isLinkedNodeCheck if a given Node is linked to the parent Node via an arbitrary id
#
Returnsboolean
const App = () => { return ( <Editor> <Frame> <Element canvas> // false <div>Yo</div> // false <Element is={Hero}>It's me</Element> // false </Element> </Frame> </Editor> )}
const Hero = ({background, title}) => { return ( <div style={{ background }}> <Element id="title" is={Text} text={title} /> // true ... </div> )}
#
isDeletableA Node may be deleted as long as it is not one of the following:
- Root Node
- Top-level Node
#
Parametersnode
NodeThe Node object to check
#
Returnsboolean
const App = () => { return ( <Editor resolves={{Container}}> <Frame> <div> // false <div>Yo</div> // true <h2>It's me</h2> // true <Element canvas> // true <h3>Child</h3> // true <Container /> // true </Element> </div> </Frame> </Editor> )}
const Container = () => { return ( <div> <Element id="main"> // false <h2>Hi</h2> // true </Element> </div> )}
#
isTopLevelNodeA Node is considered top-level if it's one of the following:
- The Root Node
- A linked Node defined inside a User Component
#
Parametersnode
NodeThe Node object to check
#
Returnsboolean
const App = () => { return ( <Editor resolves={{Container}}> <Frame> <div> // true <div>Yo</div> // false <h2>It's me</h2> // false <div> // false <h3>Child</h3> // false <Container /> // false </div> </div> </Frame> </Editor> )}
const Container = () => { return ( <div> <Element id="main"> // true <h2>Hi</h2> // false <Element> // false <h2>Hi</h2> // false </Element> </Element> <Element id="secondary"> // true <h2>Hi</h2> // false <Element> // false <h2>Hi</h2> // false </Element> </Element> </div> )}
#
isParentOfTopLevelNodeThis returns true
if a Node's User Component defines a <Element />
in its render method.
#
Returnsboolean
const App = () => { return ( <Editor resolves={{Container}}> <Frame> <Element> // false <div>Yo</div> // false <h2>It's me</h2> // false <Element> // false <h3>Child</h3> // false <Container /> // true </Element> </Element> </Frame> </Editor> )}
const Container = () => { return ( <div> <Element id="main"> // false <h2>Hi</h2> // false <Element> // false <h2>Hi</h2> // false </Element> </Element> <Element id="seconday"> // false <h2>Hi</h2> // false <Element> // false <h2>Hi</h2> // false </Element> </Element> </div> )}
#
isDraggableA Node may be dragged and moved if it satisfies both of the following conditions:
- The Node is an immediate child of a Canvas Node, hence it's draggable
- The Node's
canDrag
rule allows it to be moved
#
ParametersonError
(err: string) => voidError callback
#
Returnsboolean
#
isDroppableCheck if a Node is Droppable relative to the target Node.
#
ParameterstargetId
NodeIdThe target NodeonError
(err: string) => voidError callback
#
Returnsboolean
#
ExampleIn the following example, we're checking if our MyCanvas
component would be able to accept the current selected Node in the editor.
const MyCanvas = () => { const { id } = useNode(); const { canWeAcceptTheSelectedNode } = useEditor((state, query) => ({ canWeAcceptTheSelectedNode: state.events.selected && query.node(id).Droppable(state.events.selected) }));}
#
toSerializedNodeGets the current Node in it's SerializedNode
form
#
ReturnsSerializedNode
#
toNodeTreeGets the current Node and its descendants in its NodeTree
form
#
ParametersincludeOnly?
'childNodes' | 'linkedNodes'Get descendants that are either childNodes or linkedNodes. If unset, get all descendants
#
ReturnsNodeTree