| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.keyboardEventsMiddleware = exports.keyDownAction = exports.focusAction = void 0;
|
|---|
| 7 | var _toolkit = require("@reduxjs/toolkit");
|
|---|
| 8 | var _tooltipSlice = require("./tooltipSlice");
|
|---|
| 9 | var _tooltipSelectors = require("./selectors/tooltipSelectors");
|
|---|
| 10 | var _selectors = require("./selectors/selectors");
|
|---|
| 11 | var _axisSelectors = require("./selectors/axisSelectors");
|
|---|
| 12 | var _combineActiveTooltipIndex = require("./selectors/combiners/combineActiveTooltipIndex");
|
|---|
| 13 | var keyDownAction = exports.keyDownAction = (0, _toolkit.createAction)('keyDown');
|
|---|
| 14 | var focusAction = exports.focusAction = (0, _toolkit.createAction)('focus');
|
|---|
| 15 | var keyboardEventsMiddleware = exports.keyboardEventsMiddleware = (0, _toolkit.createListenerMiddleware)();
|
|---|
| 16 | keyboardEventsMiddleware.startListening({
|
|---|
| 17 | actionCreator: keyDownAction,
|
|---|
| 18 | effect: (action, listenerApi) => {
|
|---|
| 19 | var state = listenerApi.getState();
|
|---|
| 20 | var accessibilityLayerIsActive = state.rootProps.accessibilityLayer !== false;
|
|---|
| 21 | if (!accessibilityLayerIsActive) {
|
|---|
| 22 | return;
|
|---|
| 23 | }
|
|---|
| 24 | var {
|
|---|
| 25 | keyboardInteraction
|
|---|
| 26 | } = state.tooltip;
|
|---|
| 27 | var key = action.payload;
|
|---|
| 28 | if (key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Enter') {
|
|---|
| 29 | return;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | // TODO this is lacking index for charts that do not support numeric indexes
|
|---|
| 33 | var resolvedIndex = (0, _combineActiveTooltipIndex.combineActiveTooltipIndex)(keyboardInteraction, (0, _tooltipSelectors.selectTooltipDisplayedData)(state), (0, _axisSelectors.selectTooltipAxisDataKey)(state), (0, _tooltipSelectors.selectTooltipAxisDomain)(state));
|
|---|
| 34 | var currentIndex = resolvedIndex == null ? -1 : Number(resolvedIndex);
|
|---|
| 35 | if (!Number.isFinite(currentIndex) || currentIndex < 0) {
|
|---|
| 36 | return;
|
|---|
| 37 | }
|
|---|
| 38 | var tooltipTicks = (0, _tooltipSelectors.selectTooltipAxisTicks)(state);
|
|---|
| 39 | if (key === 'Enter') {
|
|---|
| 40 | var _coordinate = (0, _selectors.selectCoordinateForDefaultIndex)(state, 'axis', 'hover', String(keyboardInteraction.index));
|
|---|
| 41 | listenerApi.dispatch((0, _tooltipSlice.setKeyboardInteraction)({
|
|---|
| 42 | active: !keyboardInteraction.active,
|
|---|
| 43 | activeIndex: keyboardInteraction.index,
|
|---|
| 44 | activeCoordinate: _coordinate
|
|---|
| 45 | }));
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 | var direction = (0, _axisSelectors.selectChartDirection)(state);
|
|---|
| 49 | var directionMultiplier = direction === 'left-to-right' ? 1 : -1;
|
|---|
| 50 | var movement = key === 'ArrowRight' ? 1 : -1;
|
|---|
| 51 | var nextIndex = currentIndex + movement * directionMultiplier;
|
|---|
| 52 | if (tooltipTicks == null || nextIndex >= tooltipTicks.length || nextIndex < 0) {
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 | var coordinate = (0, _selectors.selectCoordinateForDefaultIndex)(state, 'axis', 'hover', String(nextIndex));
|
|---|
| 56 | listenerApi.dispatch((0, _tooltipSlice.setKeyboardInteraction)({
|
|---|
| 57 | active: true,
|
|---|
| 58 | activeIndex: nextIndex.toString(),
|
|---|
| 59 | activeCoordinate: coordinate
|
|---|
| 60 | }));
|
|---|
| 61 | }
|
|---|
| 62 | });
|
|---|
| 63 | keyboardEventsMiddleware.startListening({
|
|---|
| 64 | actionCreator: focusAction,
|
|---|
| 65 | effect: (_action, listenerApi) => {
|
|---|
| 66 | var state = listenerApi.getState();
|
|---|
| 67 | var accessibilityLayerIsActive = state.rootProps.accessibilityLayer !== false;
|
|---|
| 68 | if (!accessibilityLayerIsActive) {
|
|---|
| 69 | return;
|
|---|
| 70 | }
|
|---|
| 71 | var {
|
|---|
| 72 | keyboardInteraction
|
|---|
| 73 | } = state.tooltip;
|
|---|
| 74 | if (keyboardInteraction.active) {
|
|---|
| 75 | return;
|
|---|
| 76 | }
|
|---|
| 77 | if (keyboardInteraction.index == null) {
|
|---|
| 78 | var nextIndex = '0';
|
|---|
| 79 | var coordinate = (0, _selectors.selectCoordinateForDefaultIndex)(state, 'axis', 'hover', String(nextIndex));
|
|---|
| 80 | listenerApi.dispatch((0, _tooltipSlice.setKeyboardInteraction)({
|
|---|
| 81 | active: true,
|
|---|
| 82 | activeIndex: nextIndex,
|
|---|
| 83 | activeCoordinate: coordinate
|
|---|
| 84 | }));
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | }); |
|---|