Skip to content

Commit

Permalink
html_int/dnd: dnd_draggable can receive callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-pH committed Feb 12, 2024
1 parent 25ffb38 commit 6939504
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/html_interactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,21 @@ export class Interactive {
* @param name name of the draggable
* @param diagram diagram of the draggable
* @param container_diagram diagram of the container, if not provided, a container will be created automatically
* @param callback callback function when the draggable is moved
*/
public dnd_draggable(name : string, diagram : Diagram, container_diagram? : Diagram) {
public dnd_draggable(name : string, diagram : Diagram, container_diagram? : Diagram, callback? : (name:string, pos:Vector2) => any) {
this.init_drag_and_drop();
if (this.dragAndDropHandler == undefined) throw Error("dragAndDropHandler in Interactive class is undefined");

this.inp_variables[name] = diagram.origin;
this.dragAndDropHandler.add_draggable(name, diagram, container_diagram);

const callback = (pos : Vector2, redraw : boolean = true) => {
const dnd_callback = (pos : Vector2, redraw : boolean = true) => {
this.inp_variables[name] = pos;
if (callback) callback(name, pos);
if (redraw) this.draw();
}
this.dragAndDropHandler.registerCallback(name, callback);
this.dragAndDropHandler.registerCallback(name, dnd_callback);
}

/**
Expand Down

0 comments on commit 6939504

Please sign in to comment.