Skip to content

Commit 9ccae20

Browse files
committed
feat(interaction): the click does not use the 'click' event, but mousedown/mouseup
1 parent aa81928 commit 9ccae20

File tree

12 files changed

+211
-165
lines changed

12 files changed

+211
-165
lines changed

package-lock.json

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"eslint-plugin-unicorn": "56.0.1",
6161
"globals": "15.11.0",
6262
"husky": "9.0.11",
63-
"interacto-nono": "0.5.0",
63+
"interacto-nono": "0.6.0",
6464
"jest": "29.7.0",
6565
"jest-environment-jsdom": "29.7.0",
6666
"jest-mock-extended": "3.0.7",

src/impl/interaction/library/Click.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* along with Interacto. If not, see <https://www.gnu.org/licenses/>.
1313
*/
1414

15-
import {ClickTransition} from "../../fsm/ClickTransition";
1615
import {FSMImpl} from "../../fsm/FSMImpl";
16+
import {MouseTransition} from "../../fsm/MouseTransition";
1717
import {InteractionBase} from "../InteractionBase";
1818
import {PointDataImpl} from "../PointDataImpl";
1919
import type {PointData} from "../../../api/interaction/PointData";
@@ -34,12 +34,19 @@ export class ClickFSM extends FSMImpl {
3434
public constructor(logger: Logger, action?: (evt: MouseEvent) => void) {
3535
super(logger);
3636

37-
new ClickTransition(this.initState, this.addTerminalState("clicked"),
37+
const down = this.addStdState("down");
38+
const clicked = this.addTerminalState("clicked");
39+
40+
this.startingState = clicked;
41+
42+
new MouseTransition(this.initState, down, "mousedown",
3843
(evt: MouseEvent): void => {
3944
this.setCheckButton(evt.button);
40-
action?.(evt);
4145
},
4246
(evt: MouseEvent): boolean => this.checkButton === undefined || evt.button === this.checkButton);
47+
48+
new MouseTransition(down, clicked, "mouseup", action,
49+
(evt: MouseEvent): boolean => this.checkButton === undefined || evt.button === this.checkButton);
4350
}
4451

4552
public getCheckButton(): number {

test/binder/Accumulator.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ describe("testing an accumulator", () => {
466466
.on(elt)
467467
.bind();
468468

469-
robot().click(elt);
469+
robot().click(elt, 1, false);
470470

471471
expect(val).toBe(1);
472472
});
@@ -484,7 +484,7 @@ describe("testing an accumulator", () => {
484484
.on(elt)
485485
.bind();
486486

487-
robot().click(elt, 2);
487+
robot().click(elt, 2, false);
488488

489489
expect(val).toBe(1);
490490
});

test/binding/Bindings.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe("using bindings", () => {
266266
.on(elt)
267267
.toProduce(() => new StubCmd(true))
268268
.bind();
269-
robot().click(elt);
269+
robot().click(elt, 1, false);
270270
expect(ctx.commands).toHaveLength(1);
271271
});
272272

@@ -281,7 +281,7 @@ describe("using bindings", () => {
281281
.catch(fn)
282282
.bind();
283283

284-
robot(elt).click();
284+
robot().click(elt, 1, false);
285285

286286
expect(ctx.commands).toHaveLength(0);
287287
expect(fn).toHaveBeenCalledTimes(1);
@@ -293,7 +293,7 @@ describe("using bindings", () => {
293293
.on(elt)
294294
.toProduce(() => new StubCmd(true))
295295
.bind();
296-
robot(elt).click({}, 2);
296+
robot().click(elt, 2, false);
297297
expect(ctx.commands).toHaveLength(1);
298298
});
299299

@@ -308,7 +308,7 @@ describe("using bindings", () => {
308308
.catch(fn)
309309
.bind();
310310

311-
robot(elt).click({}, 2);
311+
robot().click(elt, 2, false);
312312

313313
expect(ctx.commands).toHaveLength(0);
314314
expect(fn).toHaveBeenCalledTimes(1);
@@ -356,10 +356,10 @@ describe("using bindings", () => {
356356
.on(elt)
357357
.toProduce(() => new StubCmd(true))
358358
.bind();
359-
robot(elt)
360-
.click({}, 2)
359+
robot()
360+
.click(elt, 2, false)
361361
.mousemove()
362-
.click({}, 2);
362+
.click(elt, 2, false);
363363
expect(ctx.commands).toHaveLength(1);
364364
});
365365

@@ -375,9 +375,9 @@ describe("using bindings", () => {
375375
.bind();
376376

377377
robot(elt)
378-
.click({}, 2)
378+
.click({}, 2, false)
379379
.mousemove()
380-
.click({}, 2);
380+
.click({}, 2, false);
381381

382382
expect(ctx.commands).toHaveLength(0);
383383
expect(fn).toHaveBeenCalledTimes(1);
@@ -616,12 +616,12 @@ describe("using bindings", () => {
616616
.bind();
617617

618618
robot(elt)
619-
.click({"button": 1})
619+
.click({"button": 1}, 1, false)
620620
.runOnlyPendingTimers()
621-
.auxclick({"button": 2}, 2)
621+
.click({"button": 2}, 2, false)
622622
.mousemove()
623623
.mousemove()
624-
.auxclick({"button": 2}, 2);
624+
.click({"button": 2}, 2, false);
625625

626626
expect(ctx.commands).toHaveLength(1);
627627
});
@@ -635,8 +635,8 @@ describe("using bindings", () => {
635635

636636
robot(elt)
637637
.keepData()
638-
.click({"button": 0})
639-
.click();
638+
.click({"button": 0}, 1, false)
639+
.click(undefined, 1, false);
640640

641641
expect(binding.interaction.isRunning()).toBeTruthy();
642642
});
@@ -656,12 +656,12 @@ describe("using bindings", () => {
656656
.bind();
657657

658658
robot(elt)
659-
.click({"button": 1})
659+
.click({"button": 1}, 1, false)
660660
.runOnlyPendingTimers()
661-
.auxclick({"button": 2}, 2)
661+
.click({"button": 2}, 2, false)
662662
.mousemove()
663663
.mousemove()
664-
.auxclick({"button": 2}, 2);
664+
.click({"button": 2}, 2, false);
665665

666666
expect(first).toHaveBeenCalledTimes(1);
667667
expect(end).toHaveBeenCalledTimes(1);
@@ -676,7 +676,7 @@ describe("using bindings", () => {
676676
.log("interaction")
677677
.bind();
678678

679-
robot(elt).click();
679+
robot().click(elt, 1, false);
680680

681681
expect(ctx.commands).toHaveLength(1);
682682
});
@@ -1845,8 +1845,8 @@ describe("using bindings", () => {
18451845
.toProduce((_i: ThenData<Array<KeyData | PointData>>) => new StubCmd(true))
18461846
.bind();
18471847

1848-
robot(elt)
1849-
.click()
1848+
robot()
1849+
.click(elt, 1, false)
18501850
.keydown()
18511851
.keyup();
18521852

test/fsm/VisitorFSMDepthFirst.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe("using the std FSM visitor implementation", () => {
8484
test("visiting the click FSM works", () => {
8585
visitedFSM = new ClickFSM(mock<Logger>());
8686
visitedFSM.acceptVisitor(visitor);
87-
expect(visitor.res).toBe(">i[init]-click,auxclick-t[clicked]");
87+
expect(visitor.res).toBe(">i[init]-mousedown-s[down]-mouseup-t[clicked]");
8888
});
8989

9090
test("visiting the key typed FSM works", () => {
@@ -96,7 +96,7 @@ describe("using the std FSM visitor implementation", () => {
9696
test("visiting the double click FSM works", () => {
9797
visitedFSM = new DoubleClickFSM(mock<Logger>());
9898
visitedFSM.acceptVisitor(visitor);
99-
expect(visitor.res).toBe(">i[init]-click,auxclick-s[clicked]-mousemove-c[cancelled]-timeout-c[cancelled]-click,auxclick-t[dbleclicked]");
99+
expect(visitor.res).toBe(">i[init]-mousedown-s[clicked]-mousemove-c[cancelled]-timeout-c[cancelled]-mousedown-t[dbleclicked]");
100100
});
101101

102102
test("visiting the non-cancellable DnD FSM works", () => {

test/interaction/library/Click.test.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*/
1414

1515
import {Click, PointDataImpl} from "../../../src/interacto";
16-
import {createMouseEvent2} from "../StubEvents";
1716
import {beforeEach, describe, expect, jest, test} from "@jest/globals";
1817
import {robot} from "interacto-nono";
1918
import {mock} from "jest-mock-extended";
2019
import type {FSMHandler, Logger} from "../../../src/interacto";
2120
import type {MockProxy} from "jest-mock-extended";
21+
import {createMouseEvent2} from "../StubEvents";
2222

2323
describe("using a click interaction", () => {
2424
let interaction: Click;
@@ -36,22 +36,31 @@ describe("using a click interaction", () => {
3636

3737
test("click on a element starts and stops the interaction Click", () => {
3838
interaction.registerToNodes([canvas]);
39-
canvas.click();
39+
robot(canvas).click({"button": 2}, 1, false);
4040
expect(handler.fsmStarts).toHaveBeenCalledTimes(1);
4141
expect(handler.fsmStops).toHaveBeenCalledTimes(1);
4242
});
4343

44+
test("not same button does not trigger the click", () => {
45+
interaction.registerToNodes([canvas]);
46+
robot(canvas)
47+
.mousedown({"button": 2})
48+
.mouseup({"button": 1});
49+
expect(handler.fsmStarts).not.toHaveBeenCalled();
50+
expect(handler.fsmStops).not.toHaveBeenCalled();
51+
});
52+
4453
test("log interaction is ok", () => {
4554
interaction.log(true);
4655
interaction.registerToNodes([canvas]);
47-
canvas.click();
56+
robot(canvas).click(undefined, 1, false);
4857

49-
expect(logger.logInteractionMsg).toHaveBeenCalledTimes(4);
58+
expect(logger.logInteractionMsg).toHaveBeenCalledTimes(5);
5059
});
5160

5261
test("no log interaction is ok", () => {
5362
interaction.registerToNodes([canvas]);
54-
canvas.click();
63+
robot(canvas).click(undefined, 1, false);
5564

5665
expect(logger.logInteractionMsg).not.toHaveBeenCalled();
5766
});
@@ -65,18 +74,11 @@ describe("using a click interaction", () => {
6574
test("press on a canvas then move don't starts the interaction", () => {
6675
interaction.registerToNodes([canvas]);
6776
robot(canvas)
68-
.mousedown()
69-
.mousemove();
77+
.mousedown({"button": 3})
78+
.mousemove({"button": 3});
7079
expect(handler.fsmStarts).not.toHaveBeenCalled();
7180
});
7281

73-
test("specific mouse button checking OK", () => {
74-
interaction.registerToNodes([canvas]);
75-
robot().auxclick({"target": canvas, "button": 2});
76-
expect(handler.fsmStarts).toHaveBeenCalledTimes(1);
77-
expect(handler.fsmStops).toHaveBeenCalledTimes(1);
78-
});
79-
8082
test("click Data", () => {
8183
const data = new PointDataImpl();
8284
const expected = new PointDataImpl();
@@ -106,7 +108,8 @@ describe("using a click interaction", () => {
106108
handler.fsmStops = jest.fn(() => {
107109
data.copy(interaction.data);
108110
});
109-
interaction.processEvent(createMouseEvent2("click", expected));
111+
interaction.processEvent(createMouseEvent2("mousedown", expected));
112+
interaction.processEvent(createMouseEvent2("mouseup", expected));
110113
expect(data).toStrictEqual(expected);
111114
});
112115

@@ -117,7 +120,8 @@ describe("using a click interaction", () => {
117120
data.copy(interaction.data);
118121
});
119122
interaction.registerToNodes([canvas]);
120-
robot().click({"target": canvas, "button": 1, "screenX": 111, "screenY": 222, "clientX": 11, "clientY": 22});
123+
robot().click({"target": canvas, "button": 1, "screenX": 111, "screenY": 222, "clientX": 11, "clientY": 22}, 1, false);
124+
121125
expect(data.clientX).toBe(11);
122126
expect(data.clientY).toBe(22);
123127
expect(data.screenX).toBe(111);
@@ -129,7 +133,8 @@ describe("using a click interaction", () => {
129133
const newHandler = mock<FSMHandler>();
130134
interaction.fsm.addHandler(newHandler);
131135
interaction.registerToNodes([canvas]);
132-
robot().click({"target": canvas, "button": 1, "screenX": 111, "screenY": 222, "clientX": 11, "clientY": 22});
136+
robot().click({"target": canvas, "button": 1, "screenX": 111, "screenY": 222, "clientX": 11, "clientY": 22}, 1, false);
137+
133138
expect(newHandler.fsmReinit).toHaveBeenCalledTimes(1);
134139
expect(interaction.data.clientX).toBe(0);
135140
expect(interaction.data.clientY).toBe(0);

test/interaction/library/ClickCurrentTarget.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("using a click interaction with a currenttarget", () => {
4343
});
4444

4545
interaction.registerToNodes([groupe]);
46-
robot().click(circle);
46+
robot(circle).click(undefined, 1, false);
4747
expect(data.target).toBe(circle);
4848
});
4949
});

0 commit comments

Comments
 (0)