1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var React = require("react");
|
4 | var getWindow_1 = require("./dom/getWindow");
|
5 | var keyboard_1 = require("./keyboard");
|
6 | var setFocusVisibility_1 = require("./setFocusVisibility");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var mountCounters = new WeakMap();
|
12 | function setMountCounters(key, delta) {
|
13 | var newValue;
|
14 | var currValue = mountCounters.get(key);
|
15 | if (currValue) {
|
16 | newValue = currValue + delta;
|
17 | }
|
18 | else {
|
19 | newValue = 1;
|
20 | }
|
21 | mountCounters.set(key, newValue);
|
22 | return newValue;
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function useFocusRects(rootRef) {
|
40 | React.useEffect(function () {
|
41 | var _a, _b;
|
42 | var win = getWindow_1.getWindow((_a = rootRef) === null || _a === void 0 ? void 0 : _a.current);
|
43 | if (!win || ((_b = win.FabricConfig) === null || _b === void 0 ? void 0 : _b.disableFocusRects) === true) {
|
44 | return undefined;
|
45 | }
|
46 | var count = setMountCounters(win, 1);
|
47 | if (count <= 1) {
|
48 | win.addEventListener('mousedown', _onMouseDown, true);
|
49 | win.addEventListener('pointerdown', _onPointerDown, true);
|
50 | win.addEventListener('keydown', _onKeyDown, true);
|
51 | }
|
52 | return function () {
|
53 | var _a;
|
54 | if (!win || ((_a = win.FabricConfig) === null || _a === void 0 ? void 0 : _a.disableFocusRects) === true) {
|
55 | return;
|
56 | }
|
57 | count = setMountCounters(win, -1);
|
58 | if (count === 0) {
|
59 | win.removeEventListener('mousedown', _onMouseDown, true);
|
60 | win.removeEventListener('pointerdown', _onPointerDown, true);
|
61 | win.removeEventListener('keydown', _onKeyDown, true);
|
62 | }
|
63 | };
|
64 | }, [rootRef]);
|
65 | }
|
66 | exports.useFocusRects = useFocusRects;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | exports.FocusRects = function (props) {
|
72 | useFocusRects(props.rootRef);
|
73 | return null;
|
74 | };
|
75 | function _onMouseDown(ev) {
|
76 | setFocusVisibility_1.setFocusVisibility(false, ev.target);
|
77 | }
|
78 | function _onPointerDown(ev) {
|
79 | if (ev.pointerType !== 'mouse') {
|
80 | setFocusVisibility_1.setFocusVisibility(false, ev.target);
|
81 | }
|
82 | }
|
83 | function _onKeyDown(ev) {
|
84 |
|
85 | if (keyboard_1.isDirectionalKeyCode(ev.which)) {
|
86 | setFocusVisibility_1.setFocusVisibility(true, ev.target);
|
87 | }
|
88 | }
|
89 |
|
\ | No newline at end of file |