Skip to content

Commit 87da46d

Browse files
committed
feat: add void methods with serializable params to ref
- https://visjs.github.io/vis-network/docs/network/#methods - https://github.com/visjs/vis-network/blob/2260f9e7c1b9472a3f55309b68e05f6698dbbb59/types/network/Network.d.ts - Did *not* add the following void methods with serializable params due to various issues: - enableEditMode - Couldn't verify that it did anything - disableEditMode - Couldn't verify that it did anything - editNode - Web view JavaScript error: "No function has been configured to handle the editing of nodes" - storePositions - Couldn't verify that this works in the web view - setSelection - Web view JavaScript error about undefined `this.selectionHandler.unselectAll`
1 parent 709e4e1 commit 87da46d

File tree

2 files changed

+187
-1
lines changed

2 files changed

+187
-1
lines changed

src/VisNetworkRef.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { ForwardedRef, useImperativeHandle } from 'react';
22
import type {
33
CallbackCache,
4+
Data,
45
EventCallback,
56
FitOptions,
67
FocusOptions,
78
IdType,
9+
MoveToOptions,
810
NetworkEvents,
11+
Options,
912
Position,
1013
VisNetworkRef,
1114
} from './types';
@@ -102,6 +105,21 @@ export default function useVisNetworkRef(
102105

103106
return { remove: () => removeEventListener(eventName, id) };
104107
},
108+
addEdgeMode(): void {
109+
send('addEdgeMode');
110+
},
111+
addNodeMode(): void {
112+
send('addNodeMode');
113+
},
114+
deleteSelected(): void {
115+
send('deleteSelected');
116+
},
117+
destroy(): void {
118+
send('destroy');
119+
},
120+
editEdgeMode(): void {
121+
send('editEdgeMode');
122+
},
105123
async getPositions(
106124
nodeIds?: IdType[] | IdType
107125
): Promise<{ [nodeId: string]: Position }> {
@@ -113,6 +131,45 @@ export default function useVisNetworkRef(
113131
focus(nodeId: IdType, options?: FocusOptions): void {
114132
send('focus', nodeId, options);
115133
},
134+
moveNode(nodeId: IdType, x: number, y: number): void {
135+
send('moveNode', nodeId, x, y);
136+
},
137+
moveTo(options: MoveToOptions): void {
138+
send('moveTo', options);
139+
},
140+
redraw(): void {
141+
send('redraw');
142+
},
143+
releaseNode(): void {
144+
send('releaseNode');
145+
},
146+
selectEdges(edgeIds: IdType[]): void {
147+
send('selectEdges', edgeIds);
148+
},
149+
selectNodes(nodeIds: IdType[], highlightEdges?: boolean): void {
150+
send('selectNodes', nodeIds, highlightEdges);
151+
},
152+
setData(data: Data): void {
153+
send('setData', data);
154+
},
155+
setOptions(options: Options): void {
156+
send('setOptions', options);
157+
},
158+
setSize(width: string, height: string): void {
159+
send('setSize', width, height);
160+
},
161+
stabilize(iterations?: number): void {
162+
send('stabilize', iterations);
163+
},
164+
startSimulation(): void {
165+
send('startSimulation');
166+
},
167+
stopSimulation(): void {
168+
send('stopSimulation');
169+
},
170+
unselectAll(): void {
171+
send('unselectAll');
172+
},
116173
};
117174
},
118175
[callbackCache, webview]

src/types.ts

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ export interface LocaleMessages {
296296
back: string;
297297
addNode: string;
298298
addEdge: string;
299-
editNode: string;
300299
editEdge: string;
301300
addDescription: string;
302301
edgeDescription: string;
@@ -685,6 +684,37 @@ export type VisNetworkRef = {
685684
callback: EventCallback
686685
) => NativeEventSubscription;
687686

687+
/**
688+
* Go into addEdge mode.
689+
* The explaination from addNodeMode applies here as well.
690+
*/
691+
addEdgeMode(): void;
692+
693+
/**
694+
* Go into addNode mode. Having edit mode or manipulation enabled is not required.
695+
* To get out of this mode, call disableEditMode().
696+
* The callback functions defined in handlerFunctions still apply.
697+
* To use these methods without having the manipulation GUI, make sure you set enabled to false.
698+
*/
699+
addNodeMode(): void;
700+
701+
/**
702+
* Delete selected.
703+
* Having edit mode or manipulation enabled is not required.
704+
*/
705+
deleteSelected(): void;
706+
707+
/**
708+
* Remove the network from the DOM and remove all Hammer bindings and references.
709+
*/
710+
destroy(): void;
711+
712+
/**
713+
* Go into editEdge mode.
714+
* The explaination from addNodeMode applies here as well.
715+
*/
716+
editEdgeMode(): void;
717+
688718
/**
689719
* Returns the x y positions in canvas space of a requested node or array of nodes.
690720
*
@@ -715,4 +745,103 @@ export type VisNetworkRef = {
715745
*
716746
*/
717747
focus(nodeId: IdType, options?: FocusOptions): void;
748+
749+
/**
750+
* You can use this to programatically move a node.
751+
* The supplied x and y positions have to be in canvas space!
752+
*
753+
* @param nodeId the node that will be moved
754+
* @param x new canvas space x position
755+
* @param y new canvas space y position
756+
*/
757+
moveNode(nodeId: IdType, x: number, y: number): void;
758+
759+
/**
760+
* You can animate or move the camera using the moveTo method.
761+
*
762+
*/
763+
moveTo(options: MoveToOptions): void;
764+
765+
/**
766+
* Redraw the network.
767+
*/
768+
redraw(): void;
769+
770+
/**
771+
* Programatically release the focussed node.
772+
*/
773+
releaseNode(): void;
774+
775+
/**
776+
* Selects the edges corresponding to the id's in the input array.
777+
* This method unselects all other objects before selecting its own objects.
778+
* Does not fire events.
779+
*
780+
*/
781+
selectEdges(edgeIds: IdType[]): void;
782+
783+
/**
784+
* Selects the nodes corresponding to the id's in the input array.
785+
* If highlightEdges is true or undefined, the neighbouring edges will also be selected.
786+
* This method unselects all other objects before selecting its own objects. Does not fire events.
787+
*
788+
*/
789+
selectNodes(nodeIds: IdType[], highlightEdges?: boolean): void;
790+
791+
/**
792+
* Override all the data in the network.
793+
* If stabilization is enabled in the physics module,
794+
* the network will stabilize again.
795+
* This method is also performed when first initializing the network.
796+
*
797+
* @param data network data
798+
*/
799+
setData(data: Data): void;
800+
801+
/**
802+
* Set the options.
803+
* All available options can be found in the modules above.
804+
* Each module requires it's own container with the module name to contain its options.
805+
*
806+
* @param options network options
807+
*/
808+
setOptions(options: Options): void;
809+
810+
/**
811+
* Set the size of the canvas.
812+
* This is automatically done on a window resize.
813+
*
814+
* @param width width in a common format, f.e. '100px'
815+
* @param height height in a common format, f.e. '100px'
816+
*/
817+
setSize(width: string, height: string): void;
818+
819+
/**
820+
* You can manually call stabilize at any time.
821+
* All the stabilization options above are used.
822+
* You can optionally supply the number of iterations it should do.
823+
*
824+
* @param [iterations] the number of iterations it should do
825+
*/
826+
stabilize(iterations?: number): void;
827+
828+
/**
829+
* Start the physics simulation.
830+
* This is normally done whenever needed and is only really useful
831+
* if you stop the simulation yourself and wish to continue it afterwards.
832+
*/
833+
startSimulation(): void;
834+
835+
/**
836+
* This stops the physics simulation and triggers a stabilized event.
837+
* Tt can be restarted by dragging a node,
838+
* altering the dataset or calling startSimulation().
839+
*/
840+
stopSimulation(): void;
841+
842+
/**
843+
* Unselect all objects.
844+
* Does not fire events.
845+
*/
846+
unselectAll(): void;
718847
};

0 commit comments

Comments
 (0)