Skip to content

Commit

Permalink
refactor(editor): Fix duplicate NodeView keys when navigating between…
Browse files Browse the repository at this point in the history
… routes (no-changelog) (#5325)

* refactor(editor): Fix duplicate NodeView keys when navigating between routes (no-changelog)

* Prettier fixes

* Use computed to export jsPlumb instance from canvas

* Force jsPlumb computed instance type
  • Loading branch information
OlegIvaniv authored Feb 2, 2023
1 parent 8f5f1c3 commit 96ec5bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/editor-ui/src/mixins/mouseSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ export const mouseSelect = mixins(deviceSupportHelpers).extend({
nodeDeselected(node: INodeUi) {
this.uiStore.removeNodeFromSelection(node);
// @ts-ignore
this.instance.removeFromDragSelection(this.$refs[`node-${node.id}`][0].$el);
this.instance.removeFromDragSelection(this.instance.getManagedElement(node?.id));
},
nodeSelected(node: INodeUi) {
this.uiStore.addSelectedNode(node);
// @ts-ignore
this.instance.addToDragSelection(this.$refs[`node-${node.id}`][0].$el);
this.instance.addToDragSelection(this.instance.getManagedElement(node?.id));
},
deselectAllNodes() {
// @ts-ignore
Expand Down
22 changes: 12 additions & 10 deletions packages/editor-ui/src/stores/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useUIStore } from '@/stores/ui';
import { useHistoryStore } from '@/stores/history';
import { INodeUi, XYPosition } from '@/Interface';
import { scaleBigger, scaleReset, scaleSmaller } from '@/utils';
import { START_NODE_TYPE, STICKY_NODE_TYPE } from '@/constants';
import { START_NODE_TYPE } from '@/constants';
import type {
BeforeStartEventParams,
BrowserJsPlumbInstance,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const useCanvasStore = defineStore('canvas', () => {
const uiStore = useUIStore();
const historyStore = useHistoryStore();

const jsPlumbInstance = ref<BrowserJsPlumbInstance>();
const jsPlumbInstanceRef = ref<BrowserJsPlumbInstance>();
const isDragging = ref<boolean>(false);

const nodes = computed<INodeUi[]>(() => workflowStore.allNodes);
Expand Down Expand Up @@ -78,7 +78,7 @@ export const useCanvasStore = defineStore('canvas', () => {

const setZoomLevel = (zoomLevel: number, offset: XYPosition) => {
nodeViewScale.value = zoomLevel;
jsPlumbInstance.value?.setZoom(zoomLevel);
jsPlumbInstanceRef.value?.setZoom(zoomLevel);
uiStore.nodeViewOffsetPosition = offset;
};

Expand Down Expand Up @@ -143,13 +143,13 @@ export const useCanvasStore = defineStore('canvas', () => {

function initInstance(container: Element) {
// Make sure to clean-up previous instance if it exists
if (jsPlumbInstance.value) {
jsPlumbInstance.value.destroy();
jsPlumbInstance.value.reset();
jsPlumbInstance.value = undefined;
if (jsPlumbInstanceRef.value) {
jsPlumbInstanceRef.value.destroy();
jsPlumbInstanceRef.value.reset();
jsPlumbInstanceRef.value = undefined;
}

jsPlumbInstance.value = newInstance({
jsPlumbInstanceRef.value = newInstance({
container,
connector: CONNECTOR_FLOWCHART_TYPE,
resizeObserver: false,
Expand All @@ -168,7 +168,7 @@ export const useCanvasStore = defineStore('canvas', () => {
// Only the node which gets dragged directly gets an event, for all others it is
// undefined. So check if the currently dragged node is selected and if not clear
// the drag-selection.
jsPlumbInstance.value?.clearDragSelection();
jsPlumbInstanceRef.value?.clearDragSelection();
uiStore.resetSelectedNodes();
}

Expand Down Expand Up @@ -231,7 +231,7 @@ export const useCanvasStore = defineStore('canvas', () => {
filter: '.node-description, .node-description .node-name, .node-description .node-subtitle',
},
});
jsPlumbInstance.value?.setDragConstrainFunction((pos: PointXY) => {
jsPlumbInstanceRef.value?.setDragConstrainFunction((pos: PointXY) => {
const isReadOnly = uiStore.isReadOnlyView;
if (isReadOnly) {
// Do not allow to move nodes in readOnly mode
Expand All @@ -240,6 +240,8 @@ export const useCanvasStore = defineStore('canvas', () => {
return pos;
});
}

const jsPlumbInstance = computed(() => jsPlumbInstanceRef.value as BrowserJsPlumbInstance);
return {
isDemo,
nodeViewScale,
Expand Down
15 changes: 6 additions & 9 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
@runWorkflow="onRunNode"
@moved="onNodeMoved"
@run="onNodeRun"
:ref="`node-${nodeData.id}`"
:key="`${nodeData.id}_node`"
:name="nodeData.name"
:isReadOnly="isReadOnly"
Expand All @@ -76,7 +75,6 @@
@nodeSelected="nodeSelectedByName"
@removeNode="(name) => removeNode(name, true)"
:key="`${nodeData.id}_sticky`"
:ref="`node-${nodeData.id}`"
:name="nodeData.name"
:isReadOnly="isReadOnly"
:instance="instance"
Expand Down Expand Up @@ -406,8 +404,6 @@ export default mixins(
next();
return;
}
// Make sure workflow id is empty when leaving the editor
this.workflowsStore.setWorkflowId(PLACEHOLDER_EMPTY_WORKFLOW_ID);
if (this.uiStore.stateIsDirty) {
const confirmModal = await this.confirmModal(
this.$locale.baseText('generic.unsavedWork.confirmMessage.message'),
Expand All @@ -418,6 +414,8 @@ export default mixins(
true,
);
if (confirmModal === MODAL_CONFIRMED) {
// Make sure workflow id is empty when leaving the editor
this.workflowsStore.setWorkflowId(PLACEHOLDER_EMPTY_WORKFLOW_ID);
const saved = await this.saveCurrentWorkflow({}, false);
if (saved) {
await this.settingsStore.fetchPromptsData();
Expand All @@ -439,6 +437,7 @@ export default mixins(
next();
}
} else if (confirmModal === MODAL_CANCEL) {
this.workflowsStore.setWorkflowId(PLACEHOLDER_EMPTY_WORKFLOW_ID);
await this.resetWorkspace();
this.uiStore.stateIsDirty = false;
next();
Expand Down Expand Up @@ -2755,12 +2754,10 @@ export default mixins(
},
getJSPlumbEndpoints(nodeName: string): Endpoint[] {
const node = this.workflowsStore.getNodeByName(nodeName);
const nodeEls: Element = (this.$refs[`node-${node?.id}`] as ComponentInstance[])[0]
.$el as Element;
const endpoints = this.instance?.getEndpoints(nodeEls);
const nodeEl = this.instance.getManagedElement(node?.id);
return endpoints as Endpoint[];
const endpoints = this.instance?.getEndpoints(nodeEl);
return endpoints;
},
getPlusEndpoint(nodeName: string, outputIndex: number): Endpoint | undefined {
const endpoints = this.getJSPlumbEndpoints(nodeName);
Expand Down

0 comments on commit 96ec5bc

Please sign in to comment.