-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): Add initial code for NodeView and Canvas rewrite (no-ch…
…angelog) (#9135) Co-authored-by: Csaba Tuncsik <csaba.tuncsik@gmail.com>
- Loading branch information
1 parent
8566301
commit 70948ec
Showing
49 changed files
with
4,208 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { CanvasNodeKey } from '@/constants'; | ||
import { ref } from 'vue'; | ||
import type { CanvasElement, CanvasElementData } from '@/types'; | ||
|
||
export function createCanvasNodeData({ | ||
id = 'node', | ||
type = 'test', | ||
typeVersion = 1, | ||
inputs = [], | ||
outputs = [], | ||
renderType = 'default', | ||
}: Partial<CanvasElementData> = {}): CanvasElementData { | ||
return { | ||
id, | ||
type, | ||
typeVersion, | ||
inputs, | ||
outputs, | ||
renderType, | ||
}; | ||
} | ||
|
||
export function createCanvasNodeElement({ | ||
id = '1', | ||
type = 'node', | ||
label = 'Node', | ||
position = { x: 100, y: 100 }, | ||
data, | ||
}: Partial< | ||
Omit<CanvasElement, 'data'> & { data: Partial<CanvasElementData> } | ||
> = {}): CanvasElement { | ||
return { | ||
id, | ||
type, | ||
label, | ||
position, | ||
data: createCanvasNodeData({ id, type, ...data }), | ||
}; | ||
} | ||
|
||
export function createCanvasNodeProps({ | ||
id = 'node', | ||
label = 'Test Node', | ||
selected = false, | ||
data = {}, | ||
} = {}) { | ||
return { | ||
id, | ||
label, | ||
selected, | ||
data: createCanvasNodeData(data), | ||
}; | ||
} | ||
|
||
export function createCanvasNodeProvide({ | ||
id = 'node', | ||
label = 'Test Node', | ||
selected = false, | ||
data = {}, | ||
} = {}) { | ||
const props = createCanvasNodeProps({ id, label, selected, data }); | ||
return { | ||
[`${CanvasNodeKey}`]: { | ||
id: ref(props.id), | ||
label: ref(props.label), | ||
selected: ref(props.selected), | ||
data: ref(props.data), | ||
}, | ||
}; | ||
} | ||
|
||
export function createCanvasConnection( | ||
nodeA: CanvasElement, | ||
nodeB: CanvasElement, | ||
{ sourceIndex = 0, targetIndex = 0 } = {}, | ||
) { | ||
const nodeAOutput = nodeA.data?.outputs[sourceIndex]; | ||
const nodeBInput = nodeA.data?.inputs[targetIndex]; | ||
|
||
return { | ||
id: `${nodeA.id}-${nodeB.id}`, | ||
source: nodeA.id, | ||
target: nodeB.id, | ||
...(nodeAOutput ? { sourceHandle: `outputs/${nodeAOutput.type}/${nodeAOutput.index}` } : {}), | ||
...(nodeBInput ? { targetHandle: `inputs/${nodeBInput.type}/${nodeBInput.index}` } : {}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './canvas'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/editor-ui/src/components/canvas/Canvas.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// @vitest-environment jsdom | ||
|
||
import { fireEvent, waitFor } from '@testing-library/vue'; | ||
import { createComponentRenderer } from '@/__tests__/render'; | ||
import Canvas from '@/components/canvas/Canvas.vue'; | ||
import { createPinia, setActivePinia } from 'pinia'; | ||
import type { CanvasConnection, CanvasElement } from '@/types'; | ||
import { createCanvasConnection, createCanvasNodeElement } from '@/__tests__/data'; | ||
import { NodeConnectionType } from 'n8n-workflow'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
global.window = jsdom.window as unknown as Window & typeof globalThis; | ||
|
||
vi.mock('@/stores/nodeTypes.store', () => ({ | ||
useNodeTypesStore: vi.fn(() => ({ | ||
getNodeType: vi.fn(() => ({ | ||
name: 'test', | ||
description: 'Test Node Description', | ||
})), | ||
})), | ||
})); | ||
|
||
let renderComponent: ReturnType<typeof createComponentRenderer>; | ||
beforeEach(() => { | ||
const pinia = createPinia(); | ||
setActivePinia(pinia); | ||
|
||
renderComponent = createComponentRenderer(Canvas, { pinia }); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('Canvas', () => { | ||
it('should initialize with default props', () => { | ||
const { getByTestId } = renderComponent(); | ||
|
||
expect(getByTestId('canvas')).toBeVisible(); | ||
expect(getByTestId('canvas-background')).toBeVisible(); | ||
expect(getByTestId('canvas-minimap')).toBeVisible(); | ||
expect(getByTestId('canvas-controls')).toBeVisible(); | ||
}); | ||
|
||
it('should render nodes and edges', async () => { | ||
const elements: CanvasElement[] = [ | ||
createCanvasNodeElement({ | ||
id: '1', | ||
label: 'Node 1', | ||
data: { | ||
outputs: [ | ||
{ | ||
type: NodeConnectionType.Main, | ||
index: 0, | ||
}, | ||
], | ||
}, | ||
}), | ||
createCanvasNodeElement({ | ||
id: '2', | ||
label: 'Node 2', | ||
position: { x: 200, y: 200 }, | ||
data: { | ||
inputs: [ | ||
{ | ||
type: NodeConnectionType.Main, | ||
index: 0, | ||
}, | ||
], | ||
}, | ||
}), | ||
]; | ||
|
||
const connections: CanvasConnection[] = [createCanvasConnection(elements[0], elements[1])]; | ||
|
||
const { container } = renderComponent({ | ||
props: { | ||
elements, | ||
connections, | ||
}, | ||
}); | ||
|
||
await waitFor(() => expect(container.querySelectorAll('.vue-flow__node')).toHaveLength(2)); | ||
|
||
expect(container.querySelector(`[data-id="${elements[0].id}"]`)).toBeInTheDocument(); | ||
expect(container.querySelector(`[data-id="${elements[1].id}"]`)).toBeInTheDocument(); | ||
expect(container.querySelector(`[data-id="${connections[0].id}"]`)).toBeInTheDocument(); | ||
}); | ||
|
||
it('should handle node drag stop event', async () => { | ||
const elements = [createCanvasNodeElement()]; | ||
const { container, emitted } = renderComponent({ | ||
props: { | ||
elements, | ||
}, | ||
}); | ||
|
||
await waitFor(() => expect(container.querySelectorAll('.vue-flow__node')).toHaveLength(1)); | ||
|
||
const node = container.querySelector(`[data-id="${elements[0].id}"]`) as Element; | ||
await fireEvent.mouseDown(node, { view: window }); | ||
await fireEvent.mouseMove(node, { | ||
view: window, | ||
clientX: 100, | ||
clientY: 100, | ||
}); | ||
await fireEvent.mouseUp(node, { view: window }); | ||
|
||
expect(emitted()['update:node:position']).toEqual([['1', { x: 100, y: 100 }]]); | ||
}); | ||
}); |
Oops, something went wrong.