3434</template >
3535
3636<script setup>
37- import { onMounted , ref , computed , watch , nextTick } from ' vue' ;
37+ import { onBeforeUnmount , onMounted , ref , computed , watch , nextTick } from ' vue' ;
3838import { newInstance , ready , EVENT_DRAG_STOP } from " @jsplumb/browser-ui" ;
3939import { downloadTextFile } from " @/services/common/fileDownload" ;
4040import {
@@ -46,6 +46,7 @@ import {
4646import { buildGeneralWorkspaceHierarchy } from " @/services/workspace/mapping/generalWorkspaceHierarchy" ;
4747import { normalizeConnection } from " @/services/workspace/core/connectionUtils" ;
4848import { createDefaultDotEndpointDefinition } from " @/services/workspace/core/jsPlumbEndpointUtils" ;
49+ import { createJsPlumbElementLayoutObserver } from " @/services/workspace/core/jsPlumbLayoutObserverUtils" ;
4950import { reconcileMaterialEndpoints } from " @/services/workspace/core/generalMaterialEndpointUtils" ;
5051import {
5152 ensureParallelIndicatorDefaults ,
@@ -78,6 +79,9 @@ const workspaceContentRef = ref(null);
7879const jsplumbInstance = ref (null );
7980const jsplumbElements = ref ([]);
8081const managedElements = ref ({});
82+ const layoutObserver = createJsPlumbElementLayoutObserver ({
83+ getInstance : () => jsplumbInstance .value ,
84+ });
8185const zoomLevel = ref (1 );
8286const initialWorkspaceWidth = 10000 ;
8387const initialWorkspaceHeight = 10000 ;
@@ -117,7 +121,7 @@ onMounted(async () => {
117121 emit (' update:workspace_items' , updated);
118122 });
119123
120- watch (computedWorkspaceItems, createUpdateItemListHandler (jsplumbInstance, jsplumbElements, managedElements), { deep: true });
124+ watch (computedWorkspaceItems, createUpdateItemListHandler (jsplumbInstance, jsplumbElements, managedElements, layoutObserver ), { deep: true });
121125 watch (
122126 () => getMaterialTypeSignatures (computedWorkspaceItems .value ),
123127 syncMaterialTypeChanges,
@@ -132,6 +136,11 @@ onMounted(async () => {
132136 });
133137});
134138
139+ onBeforeUnmount (() => {
140+ stopPanning ();
141+ layoutObserver .disconnect ();
142+ });
143+
135144const computedWorkspaceItems = computed ({
136145 get : () => props .workspace_items ,
137146 set : (newValue ) => {
@@ -381,6 +390,7 @@ function syncMaterialEndpoints(instance, elementRef, item) {
381390 if (changed) {
382391 instance .revalidate ? .(elementRef);
383392 instance .repaintEverything ? .();
393+ layoutObserver .schedule (elementRef);
384394 }
385395}
386396
@@ -600,6 +610,7 @@ function rebuildParallelIndicatorEndpoints(item) {
600610 jsplumbInstance .value .deleteConnectionsForElement (elementRef);
601611 jsplumbInstance .value .removeAllEndpoints (elementRef);
602612 addJsPlumbEndpoints (jsplumbInstance .value , elementRef, item);
613+ layoutObserver .observe (elementRef);
603614 jsplumbInstance .value .revalidate ? .(elementRef);
604615
605616 connectionsToRestore .forEach ((connection ) => {
@@ -609,7 +620,7 @@ function rebuildParallelIndicatorEndpoints(item) {
609620 jsplumbInstance .value .repaintEverything ? .();
610621}
611622
612- function createUpdateItemListHandler (instance , jsplumbElementsRef , managedElementsRef ) {
623+ function createUpdateItemListHandler (instance , jsplumbElementsRef , managedElementsRef , layoutObserverRef ) {
613624 return async () => {
614625 await nextTick ();
615626 await nextTick ();
@@ -623,12 +634,17 @@ function createUpdateItemListHandler(instance, jsplumbElementsRef, managedElemen
623634 (element ) => element .id === item .id
624635 );
625636 if (! elementRef || managedElementsRef .value [item .id ] === true ) {
637+ if (elementRef) {
638+ layoutObserverRef .observe (elementRef);
639+ }
626640 return ;
627641 }
628642
643+ layoutObserverRef .observe (elementRef);
629644 elementRef .style .left = ` ${ item .x } px` ;
630645 elementRef .style .top = ` ${ item .y } px` ;
631646 addJsPlumbEndpoints (instance .value , elementRef, item);
647+ layoutObserverRef .schedule (elementRef);
632648 managedElementsRef .value [item .id ] = true ;
633649 });
634650 };
@@ -752,6 +768,7 @@ async function clearWorkspace() {
752768 (element ) => element .id === item .id
753769 );
754770 if (elementRef !== undefined ) {
771+ layoutObserver .unobserve (elementRef);
755772 jsplumbInstance .value ? .removeAllEndpoints (elementRef);
756773 }
757774 }
@@ -817,6 +834,8 @@ async function addElements(list) {
817834 elementRef .style .top = ` ${ element .y } px` ;
818835 await nextTick ();
819836 await addJsPlumbEndpoints (jsplumbInstance .value , elementRef, element);
837+ layoutObserver .observe (elementRef);
838+ layoutObserver .schedule (elementRef);
820839 managedElements .value [element .id ] = true ;
821840 }
822841 }
@@ -828,6 +847,7 @@ function deleteElement(item) {
828847 (element ) => element .id === item .id
829848 );
830849 if (elementRef !== undefined ) {
850+ layoutObserver .unobserve (elementRef);
831851 jsplumbInstance .value .removeAllEndpoints (elementRef);
832852 jsplumbInstance .value .deleteConnectionsForElement (elementRef);
833853 elementRef .remove ();
0 commit comments