Fix canAccept not passing in otherNode

This commit is contained in:
thepaperpilot 2023-04-23 00:37:46 -05:00
parent a8815f9eb5
commit 97f1bb9f9a
3 changed files with 20 additions and 10 deletions

View file

@ -122,7 +122,7 @@ watchEffect(() => {
return smallest; return smallest;
} }
const nodeType = props.types.value[curr.type]; const nodeType = props.types.value[curr.type];
const canAccept = getNodeProperty(nodeType.canAccept, curr); const canAccept = getNodeProperty(nodeType.canAccept, curr, node);
if (!canAccept) { if (!canAccept) {
return smallest; return smallest;
} }
@ -225,8 +225,10 @@ function drag(e: MouseEvent | TouchEvent) {
function endDragging(node: BoardNode | null) { function endDragging(node: BoardNode | null) {
if (props.draggingNode.value != null && props.draggingNode.value === node) { if (props.draggingNode.value != null && props.draggingNode.value === node) {
props.draggingNode.value.position.x += Math.round(dragged.value.x / 25) * 25; if (props.receivingNode.value == null) {
props.draggingNode.value.position.y += Math.round(dragged.value.y / 25) * 25; props.draggingNode.value.position.x += Math.round(dragged.value.x / 25) * 25;
props.draggingNode.value.position.y += Math.round(dragged.value.y / 25) * 25;
}
const nodes = props.nodes.value; const nodes = props.nodes.value;
nodes.push(nodes.splice(nodes.indexOf(props.draggingNode.value), 1)[0]); nodes.push(nodes.splice(nodes.indexOf(props.draggingNode.value), 1)[0]);

View file

@ -257,9 +257,9 @@ const progressDisplay = computed(() =>
); );
const canAccept = computed( const canAccept = computed(
() => () =>
unref(props.dragging) != null && props.dragging.value != null &&
unref(props.hasDragged) && unref(props.hasDragged) &&
getNodeProperty(props.nodeType.value.canAccept, unref(props.node)) getNodeProperty(props.nodeType.value.canAccept, unref(props.node), props.dragging.value)
); );
const style = computed(() => getNodeProperty(props.nodeType.value.style, unref(props.node))); const style = computed(() => getNodeProperty(props.nodeType.value.style, unref(props.node)));
const classes = computed(() => getNodeProperty(props.nodeType.value.classes, unref(props.node))); const classes = computed(() => getNodeProperty(props.nodeType.value.classes, unref(props.node)));

View file

@ -33,7 +33,9 @@ export const BoardType = Symbol("Board");
/** /**
* A type representing a computable value for a node on the board. Used for node types to return different values based on the given node and the state of the board. * A type representing a computable value for a node on the board. Used for node types to return different values based on the given node and the state of the board.
*/ */
export type NodeComputable<T> = Computable<T> | ((node: BoardNode) => T); export type NodeComputable<T, S extends unknown[] = []> =
| Computable<T>
| ((node: BoardNode, ...args: S) => T);
/** Ways to display progress of an action with a duration. */ /** Ways to display progress of an action with a duration. */
export enum ProgressDisplay { export enum ProgressDisplay {
@ -103,7 +105,7 @@ export interface NodeTypeOptions {
/** The shape of the node. */ /** The shape of the node. */
shape: NodeComputable<Shape>; shape: NodeComputable<Shape>;
/** Whether the node can accept another node being dropped upon it. */ /** Whether the node can accept another node being dropped upon it. */
canAccept?: boolean | Ref<boolean> | ((node: BoardNode, otherNode: BoardNode) => boolean); canAccept?: NodeComputable<boolean, [BoardNode]>;
/** The progress value of the node. */ /** The progress value of the node. */
progress?: NodeComputable<number>; progress?: NodeComputable<number>;
/** How the progress should be displayed on the node. */ /** How the progress should be displayed on the node. */
@ -167,7 +169,7 @@ export type GenericNodeType = Replace<
size: NodeComputable<number>; size: NodeComputable<number>;
draggable: NodeComputable<boolean>; draggable: NodeComputable<boolean>;
shape: NodeComputable<Shape>; shape: NodeComputable<Shape>;
canAccept: NodeComputable<boolean>; canAccept: NodeComputable<boolean, [BoardNode]>;
progressDisplay: NodeComputable<ProgressDisplay>; progressDisplay: NodeComputable<ProgressDisplay>;
progressColor: NodeComputable<string>; progressColor: NodeComputable<string>;
actionDistance: NodeComputable<number>; actionDistance: NodeComputable<number>;
@ -494,8 +496,14 @@ export function createBoard<T extends BoardOptions>(
* @param property The property to find the value of * @param property The property to find the value of
* @param node The node to get the property of * @param node The node to get the property of
*/ */
export function getNodeProperty<T>(property: NodeComputable<T>, node: BoardNode): T { export function getNodeProperty<T, S extends unknown[]>(
return isFunction<T, [BoardNode], Computable<T>>(property) ? property(node) : unref(property); property: NodeComputable<T, S>,
node: BoardNode,
...args: S
): T {
return isFunction<T, [BoardNode, ...S], Computable<T>>(property)
? property(node, ...args)
: unref(property);
} }
/** /**