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): Overhaul node insert position computation in new canvas (no-changelog) #10637

Merged
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
1 change: 1 addition & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,7 @@ export type ToggleNodeCreatorOptions = {
createNodeActive: boolean;
source?: NodeCreatorOpenSource;
nodeCreatorView?: NodeFilterType;
hasAddedNodes?: boolean;
};

export type AppliedThemeOption = 'light' | 'dark';
Expand Down
6 changes: 3 additions & 3 deletions packages/editor-ui/src/__tests__/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ export const mockNode = ({
}) => mock<INodeUi>({ id, name, type, position, disabled, issues, typeVersion, parameters });

export const mockNodeTypeDescription = ({
name,
name = SET_NODE_TYPE,
version = 1,
credentials = [],
inputs = [NodeConnectionType.Main],
outputs = [NodeConnectionType.Main],
}: {
name: INodeTypeDescription['name'];
name?: INodeTypeDescription['name'];
version?: INodeTypeDescription['version'];
credentials?: INodeTypeDescription['credentials'];
inputs?: INodeTypeDescription['inputs'];
outputs?: INodeTypeDescription['outputs'];
}) =>
} = {}) =>
mock<INodeTypeDescription>({
name,
displayName: name,
Expand Down
10 changes: 6 additions & 4 deletions packages/editor-ui/src/components/Node/NodeCreation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const LazyNodeCreator = defineAsyncComponent(
);
const props = withDefaults(defineProps<Props>(), {
createNodeActive: false,
createNodeActive: false, // Determines if the node creator is open
});
const emit = defineEmits<{
Expand Down Expand Up @@ -88,13 +88,15 @@ function addStickyNote() {
emit('addNodes', getAddedNodesAndConnections([{ type: STICKY_NODE_TYPE, position }]));
}
function closeNodeCreator() {
emit('toggleNodeCreator', { createNodeActive: false });
function closeNodeCreator(hasAddedNodes = false) {
if (props.createNodeActive) {
emit('toggleNodeCreator', { createNodeActive: false, hasAddedNodes });
}
}
function nodeTypeSelected(nodeTypes: string[]) {
emit('addNodes', getAddedNodesAndConnections(nodeTypes.map((type) => ({ type }))));
closeNodeCreator();
closeNodeCreator(true);
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ watch(
);
// Close node creator when the last view stacks is closed
watch(viewStacksLength, (viewStacksLength) => {
if (viewStacksLength === 0) {
watch(viewStacksLength, (value) => {
if (value === 0) {
emit('closeNodeCreator');
setShowScrim(false);
}
Expand Down
23 changes: 16 additions & 7 deletions packages/editor-ui/src/components/canvas/Canvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ const emit = defineEmits<{
'create:connection:start': [handle: ConnectStartEvent];
'create:connection': [connection: Connection];
'create:connection:end': [connection: Connection, event?: MouseEvent];
'create:connection:cancelled': [handle: ConnectStartEvent, event?: MouseEvent];
'create:connection:cancelled': [
handle: ConnectStartEvent,
position: XYPosition,
event?: MouseEvent,
];
'click:connection:add': [connection: Connection];
'click:pane': [position: XYPosition];
'run:workflow': [];
Expand Down Expand Up @@ -227,7 +231,7 @@ function onConnectEnd(event?: MouseEvent) {
if (connectedHandle.value) {
emit('create:connection:end', connectedHandle.value, event);
} else if (connectingHandle.value) {
emit('create:connection:cancelled', connectingHandle.value, event);
emit('create:connection:cancelled', connectingHandle.value, getProjectedPosition(event), event);
}
connectedHandle.value = undefined;
Expand Down Expand Up @@ -291,14 +295,19 @@ function emitWithLastSelectedNode(emitFn: (id: string) => void) {
const defaultZoom = 1;
const zoom = ref(defaultZoom);
function onClickPane(event: MouseEvent) {
function getProjectedPosition(event?: MouseEvent) {
const bounds = viewportRef.value?.getBoundingClientRect() ?? { left: 0, top: 0 };
const position = project({
x: event.offsetX - bounds.left,
y: event.offsetY - bounds.top,
const offsetX = event?.clientX ?? 0;
const offsetY = event?.clientY ?? 0;
return project({
x: offsetX - bounds.left,
y: offsetY - bounds.top,
});
}
emit('click:pane', position);
function onClickPane(event: MouseEvent) {
emit('click:pane', getProjectedPosition(event));
}
async function onFitView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ function openContextMenu(event: MouseEvent) {
*/
&.configuration {
--canvas-node--width: 76px;
--canvas-node--height: 76px;
--canvas-node--width: 80px;
--canvas-node--height: 80px;
background: var(--canvas-node--background, var(--node-type-supplemental-background));
border: var(--canvas-node-border-width) solid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`useCanvasOperations > copyNodes > should copy nodes 1`] = `
"parameters": {},
"id": "1",
"name": "Node 1",
"type": "type",
"type": "n8n-nodes-base.set",
"position": [
40,
40
Expand All @@ -20,7 +20,7 @@ exports[`useCanvasOperations > copyNodes > should copy nodes 1`] = `
"parameters": {},
"id": "2",
"name": "Node 2",
"type": "type",
"type": "n8n-nodes-base.set",
"position": [
40,
40
Expand All @@ -44,7 +44,7 @@ exports[`useCanvasOperations > cutNodes > should copy and delete nodes 1`] = `
"parameters": {},
"id": "1",
"name": "Node 1",
"type": "type",
"type": "n8n-nodes-base.set",
"position": [
40,
40
Expand All @@ -55,7 +55,7 @@ exports[`useCanvasOperations > cutNodes > should copy and delete nodes 1`] = `
"parameters": {},
"id": "2",
"name": "Node 2",
"type": "type",
"type": "n8n-nodes-base.set",
"position": [
40,
40
Expand Down
Loading
Loading