Move selecting action logic inside the board component

This commit is contained in:
thepaperpilot 2023-04-22 22:59:28 -05:00
parent 693c34f585
commit 21f49756dc
4 changed files with 20 additions and 7 deletions

View file

@ -44,6 +44,7 @@
:selectedAction="unref(selectedAction)" :selectedAction="unref(selectedAction)"
@mouseDown="mouseDown" @mouseDown="mouseDown"
@endDragging="endDragging" @endDragging="endDragging"
@clickAction="actionId => clickAction(node, actionId)"
/> />
</g> </g>
</transition-group> </transition-group>
@ -240,6 +241,15 @@ function endDragging(nodeID: number | null) {
props.state.value.selectedAction = null; props.state.value.selectedAction = null;
} }
} }
function clickAction(node: BoardNode, actionId: string) {
if (props.state.value.selectedAction === actionId) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
unref(props.selectedAction)!.onClick(unref(props.selectedNode)!);
} else {
props.state.value = { ...props.state.value, selectedAction: actionId };
}
}
</script> </script>
<style> <style>

View file

@ -12,6 +12,7 @@
:node="node" :node="node"
:node-type="nodeType" :node-type="nodeType"
:selected-action="selectedAction" :selected-action="selectedAction"
@click-action="actionId => emit('clickAction', actionId)"
/> />
<g <g
@ -165,6 +166,7 @@ const props = toRefs(_props);
const emit = defineEmits<{ const emit = defineEmits<{
(e: "mouseDown", event: MouseEvent | TouchEvent, node: number, isDraggable: boolean): void; (e: "mouseDown", event: MouseEvent | TouchEvent, node: number, isDraggable: boolean): void;
(e: "endDragging", node: number): void; (e: "endDragging", node: number): void;
(e: "clickAction", actionId: string): void;
}>(); }>();
const isSelected = computed(() => unref(props.selectedNode) === unref(props.node)); const isSelected = computed(() => unref(props.selectedNode) === unref(props.node));

View file

@ -52,6 +52,10 @@ const _props = defineProps<{
}>(); }>();
const props = toRefs(_props); const props = toRefs(_props);
const emit = defineEmits<{
(e: "clickAction", actionId: string): void;
}>();
const size = computed(() => getNodeProperty(props.nodeType.value.size, unref(props.node))); const size = computed(() => getNodeProperty(props.nodeType.value.size, unref(props.node)));
const outlineColor = computed( const outlineColor = computed(
() => () =>
@ -68,12 +72,9 @@ const actionDistance = computed(() =>
); );
function performAction(e: MouseEvent | TouchEvent, action: GenericBoardNodeAction) { function performAction(e: MouseEvent | TouchEvent, action: GenericBoardNodeAction) {
// If the onClick function made this action selected, emit("clickAction", action.id);
// don't propagate the event (which will deselect everything) e.preventDefault();
if (action.onClick(unref(props.node)) || unref(props.selectedAction)?.id === action.id) { e.stopPropagation();
e.preventDefault();
e.stopPropagation();
}
} }
function actionMouseUp(e: MouseEvent | TouchEvent, action: GenericBoardNodeAction) { function actionMouseUp(e: MouseEvent | TouchEvent, action: GenericBoardNodeAction) {

View file

@ -188,7 +188,7 @@ export interface BoardNodeActionOptions {
/** An array of board node links associated with the action. They appear when the action is focused. */ /** An array of board node links associated with the action. They appear when the action is focused. */
links?: NodeComputable<BoardNodeLink[]>; links?: NodeComputable<BoardNodeLink[]>;
/** A function that is called when the action is clicked. */ /** A function that is called when the action is clicked. */
onClick: (node: BoardNode) => boolean | undefined; onClick: (node: BoardNode) => void;
} }
/** /**