craft.js
craft.js copied to clipboard
Access External State in Rules Check
Is your feature request related to a problem? Please describe.
I have a use case where a user should be restricted from dragging an item if they're not a specific access-level. Currently, the canDrop rule is only passed the node and the helpers util and has no way of accessing any external state or variables since the function is statically attached to the component.
It doesn't need to be persisted as it'll be on a user-by-user basis and only applicable here.
Describe the solution you'd like
Potentially something like:
// Top Level Component
// CraftJS code
const { actions } = useEditor();
// My Hook
const session = useSession();
useEffect(() => {
actions.setGlobalState(state => state.session = session);
}, [session, actions]);
// Selector
Component.craft = {
rules: {
canDrag: ({ node, helper, state }: { node: Node, helper: NodeHelpers, state: GlobalEditorState }) => {
return state.session?.role === 'Admin'; etc
}
}
}
Additional context I've tried setting custom values on the Node in order to access them but then the values are persisted to the database. Would also be useful to have in the other checks - canDrop etc
Sorry to chase - @prevwong do you have any thoughts on the above?