Skip to content

Commit

Permalink
feat(elementTools.Control): add pointer event to setPosition() and re…
Browse files Browse the repository at this point in the history
…setPosition() signature
  • Loading branch information
kumilingus committed Aug 21, 2024
1 parent 58df31d commit 6eea89e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/joint-core/src/elementTools/Control.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const Control = ToolView.extend({
const { clientX, clientY } = util.normalizeEvent(evt);
const coords = paper.clientToLocalPoint(clientX, clientY);
const relativeCoords = model.getRelativePointFromAbsolute(coords);
this.setPosition(relatedView, relativeCoords, this);
this.setPosition(relatedView, relativeCoords, evt);
this.update();
},
onPointerUp: function(_evt) {
Expand All @@ -144,9 +144,9 @@ export const Control = ToolView.extend({
this.toggleExtras(false);
relatedView.model.stopBatch('control-move', { ui: true, tool: this.cid });
},
onPointerDblClick: function() {
onPointerDblClick: function(evt) {
const { relatedView } = this;
this.resetPosition(relatedView, this);
this.resetPosition(relatedView, evt);
this.update();
}

Expand Down
36 changes: 35 additions & 1 deletion packages/joint-core/test/jointjs/dia/elementTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ QUnit.module('elementTools', function(hooks) {
QUnit.test('position (angle ' + testCase.angle + ')', function(assert) {
var angle = testCase.angle;
var position = { x: 10, y: 10 };
const setPositionSpy = sinon.spy();
const resetPositionSpy = sinon.spy();
var CustomControl = joint.elementTools.Control.extend({
getPosition: function() { return position; }
getPosition: function() { return position; },
setPosition: setPositionSpy,
resetPosition: resetPositionSpy
});
var control = new CustomControl;
element.rotate(angle);
Expand All @@ -172,6 +176,36 @@ QUnit.module('elementTools', function(hooks) {
resultingPosition = control.vel.getBBox({ target: paper.svg }).center();
expectedPosition = element.position().offset(position).rotate(bbox.center(), -angle);
assert.ok(resultingPosition.round().equals(expectedPosition.round()));
// 3. `setPosition()`
const clientX = 11;
const clientY = 13;
const relativePosition = element.getRelativePointFromAbsolute(
paper.clientToLocalPoint(clientX, clientY)
);
simulate.mousedown({ clientX: 0, clientY: 0, el: control.el });
simulate.mousemove({ clientX, clientY, el: control.el });
simulate.mouseup({ clientX, clientY, el: control.el });
assert.ok(setPositionSpy.calledOnce);
assert.ok(
setPositionSpy.calledWithExactly(
elementView,
sinon.match(relativePosition.toJSON()),
sinon.match.instanceOf(joint.mvc.Event)
)
);
assert.notOk(resetPositionSpy.called);
// 4. `resetPosition()`
setPositionSpy.resetHistory();
resetPositionSpy.resetHistory();
simulate.mouseevent({ type: 'dblclick', el: control.el });
assert.ok(resetPositionSpy.calledOnce);
assert.ok(
resetPositionSpy.calledWithExactly(
elementView,
sinon.match.instanceOf(joint.mvc.Event)
)
);
assert.notOk(setPositionSpy.called);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/joint-core/types/joint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4223,8 +4223,8 @@ export namespace elementTools {
constructor(opt?: T);

protected getPosition(view: dia.ElementView): dia.Point;
protected setPosition(view: dia.ElementView, coordinates: g.Point): void;
protected resetPosition(view: dia.ElementView): void;
protected setPosition(view: dia.ElementView, coordinates: g.Point, evt: dia.Event): void;
protected resetPosition(view: dia.ElementView, evt: dia.Event): void;

protected updateHandle(handleNode: SVGElement): void;
protected updateExtras(extrasNode: SVGElement): void;
Expand Down

0 comments on commit 6eea89e

Please sign in to comment.