Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): Add canvas edge toolbar hover show/hide support (no-changelog) #9699

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/editor-ui/src/components/canvas/Canvas.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts" setup>
import type { CanvasConnection, CanvasElement } from '@/types';
import type { NodeDragEvent, Connection } from '@vue-flow/core';
import type { EdgeMouseEvent, NodeDragEvent, Connection } from '@vue-flow/core';
import { useVueFlow, VueFlow, PanelPosition } from '@vue-flow/core';
import { Background } from '@vue-flow/background';
import { Controls } from '@vue-flow/controls';
import { MiniMap } from '@vue-flow/minimap';
import CanvasNode from './elements/nodes/CanvasNode.vue';
import CanvasEdge from './elements/edges/CanvasEdge.vue';
import { onMounted, onUnmounted, useCssModule } from 'vue';
import { onMounted, onUnmounted, ref, useCssModule } from 'vue';

const $style = useCssModule();

Expand Down Expand Up @@ -36,6 +36,8 @@ const props = withDefaults(

const { getSelectedEdges, getSelectedNodes } = useVueFlow({ id: props.id });

const hoveredEdges = ref<Record<string, boolean>>({});

onMounted(() => {
document.addEventListener('keydown', onKeyDown);
});
Expand Down Expand Up @@ -68,6 +70,14 @@ function onKeyDown(e: KeyboardEvent) {
getSelectedNodes.value.forEach(({ id }) => onDeleteNode(id));
}
}

function onMouseEnterEdge(event: EdgeMouseEvent) {
hoveredEdges.value[event.edge.id] = true;
}

function onMouseLeaveEdge(event: EdgeMouseEvent) {
hoveredEdges.value[event.edge.id] = false;
}
</script>

<template>
Expand All @@ -82,14 +92,20 @@ function onKeyDown(e: KeyboardEvent) {
:max-zoom="2"
data-test-id="canvas"
@node-drag-stop="onNodeDragStop"
@edge-mouse-enter="onMouseEnterEdge"
@edge-mouse-leave="onMouseLeaveEdge"
@connect="onConnect"
>
<template #node-canvas-node="canvasNodeProps">
<CanvasNode v-bind="canvasNodeProps" @delete="onDeleteNode" />
</template>

<template #edge-canvas-edge="canvasEdgeProps">
<CanvasEdge v-bind="canvasEdgeProps" @delete="onDeleteConnection" />
<CanvasEdge
v-bind="canvasEdgeProps"
:hovered="hoveredEdges[canvasEdgeProps.id]"
@delete="onDeleteConnection"
/>
</template>

<Background data-test-id="canvas-background" pattern-color="#aaa" :gap="16" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { fireEvent } from '@testing-library/vue';
import CanvasEdge from './CanvasEdge.vue';
import { createComponentRenderer } from '@/__tests__/render';

const renderComponent = createComponentRenderer(CanvasEdge, {
props: {
sourceX: 0,
sourceY: 0,
sourcePosition: 'top',
targetX: 100,
targetY: 100,
targetPosition: 'bottom',
},
});

describe('CanvasEdge', () => {
it('should emit delete event when toolbar delete is clicked', async () => {
const { emitted, getByTestId } = renderComponent();
const deleteButton = getByTestId('delete-connection-button');

await fireEvent.click(deleteButton);

expect(emitted()).toHaveProperty('delete');
});

it('should compute edgeStyle correctly', () => {
const { container } = renderComponent({
props: {
style: {
stroke: 'red',
},
},
});

const edge = container.querySelector('.vue-flow__edge-path');

expect(edge).toHaveStyle({
stroke: 'red',
strokeWidth: 2,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,39 @@
/* eslint-disable vue/no-multiple-template-root */
import type { Connection, EdgeProps } from '@vue-flow/core';
import { BaseEdge, EdgeLabelRenderer, getBezierPath } from '@vue-flow/core';
import CanvasEdgeToolbar from './CanvasEdgeToolbar.vue';
import { computed, useCssModule } from 'vue';
import { useI18n } from '@/composables/useI18n';

const emit = defineEmits<{
delete: [connection: Connection];
}>();

const props = defineProps<EdgeProps>();
const props = defineProps<
EdgeProps & {
hovered?: boolean;
}
>();

const i18n = useI18n();
const $style = useCssModule();

const edgeStyle = computed(() => ({
strokeWidth: 2,
...props.style,
}));

const edgeLabelStyle = computed(() => ({
transform: `translate(-50%, -50%) translate(${path.value[1]}px,${path.value[2]}px)`,
const isEdgeToolbarVisible = computed(() => props.selected || props.hovered);

const edgeToolbarStyle = computed(() => {
return {
transform: `translate(-50%, -50%) translate(${path.value[1]}px,${path.value[2]}px)`,
};
});

const edgeToolbarClasses = computed(() => ({
[$style.edgeToolbar]: true,
[$style.edgeToolbarVisible]: isEdgeToolbarVisible.value,
nodrag: true,
nopan: true,
}));

const path = computed(() =>
Expand Down Expand Up @@ -60,24 +74,24 @@ function onDelete() {
:label-bg-style="{ fill: 'red' }"
:label-bg-padding="[2, 4]"
:label-bg-border-radius="2"
:class="$style.edge"
/>
<EdgeLabelRenderer>
<div :class="[$style.edgeToolbar, 'nodrag', 'nopan']" :style="edgeLabelStyle">
<N8nIconButton
data-test-id="delete-connection-button"
type="tertiary"
size="small"
icon="trash"
:title="i18n.baseText('node.delete')"
@click="onDelete"
/>
</div>
<CanvasEdgeToolbar :class="edgeToolbarClasses" :style="edgeToolbarStyle" @delete="onDelete" />
</EdgeLabelRenderer>
</template>

<style lang="scss" module>
.edgeToolbar {
pointer-events: all;
position: absolute;
opacity: 0;

&.edgeToolbarVisible {
opacity: 1;
}

&:hover {
opacity: 1;
}
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fireEvent } from '@testing-library/vue';
import CanvasEdgeToolbar from './CanvasEdgeToolbar.vue';
import { createComponentRenderer } from '@/__tests__/render';

const renderComponent = createComponentRenderer(CanvasEdgeToolbar);

describe('CanvasEdgeToolbar', () => {
it('should emit delete event when delete button is clicked', async () => {
const { getByTestId, emitted } = renderComponent();
const deleteButton = getByTestId('delete-connection-button');

await fireEvent.click(deleteButton);

expect(emitted()).toHaveProperty('delete');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script lang="ts" setup>
import { useI18n } from '@/composables/useI18n';
import { computed, useCssModule } from 'vue';

const emit = defineEmits<{
delete: [];
}>();

const $style = useCssModule();

const i18n = useI18n();

const classes = computed(() => ({
[$style.canvasEdgeToolbar]: true,
}));

function onDelete() {
emit('delete');
}
</script>

<template>
<div :class="classes" data-test-id="canvas-edge-toolbar">
<N8nIconButton
data-test-id="delete-connection-button"
type="tertiary"
size="small"
icon="trash"
:title="i18n.baseText('node.delete')"
@click="onDelete"
/>
</div>
</template>

<style lang="scss" module>
.canvasEdgeToolbar {
display: flex;
justify-content: center;
align-items: center;
pointer-events: all;
}
</style>
Loading