@ndla/ui 19.1.1 → 20.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/es/Masthead/Masthead.js +1 -0
- package/es/Messages/MessageBanner.js +3 -3
- package/es/MyNdla/Resource/FolderInput.js +29 -36
- package/es/NDLAFilm/FilmSlideshow.js +8 -8
- package/es/Resource/ListResource.js +6 -6
- package/es/Search/LoadingWrapper.js +2 -2
- package/es/Search/SearchResult.js +1 -1
- package/es/SearchTypeResult/SearchTypeResult.js +3 -3
- package/es/TopicMenu/TopicMenu.js +14 -1
- package/es/TreeStructure/FolderItem.js +54 -38
- package/es/TreeStructure/FolderItems.js +29 -35
- package/es/TreeStructure/FolderNameInput.js +12 -16
- package/es/TreeStructure/TreeStructure.js +64 -91
- package/es/TreeStructure/arrowNavigation.js +44 -0
- package/es/TreeStructure/helperFunctions.js +41 -35
- package/es/all.css +1 -1
- package/es/index.js +0 -1
- package/es/locale/messages-en.js +4 -1
- package/es/locale/messages-nb.js +4 -1
- package/es/locale/messages-nn.js +4 -1
- package/es/locale/messages-se.js +4 -1
- package/es/locale/messages-sma.js +4 -1
- package/lib/Masthead/Masthead.js +1 -0
- package/lib/Messages/MessageBanner.js +3 -3
- package/lib/MyNdla/Resource/FolderInput.d.ts +2 -2
- package/lib/MyNdla/Resource/FolderInput.js +26 -33
- package/lib/NDLAFilm/FilmSlideshow.js +10 -10
- package/lib/Resource/ListResource.js +6 -6
- package/lib/Search/LoadingWrapper.js +3 -3
- package/lib/Search/SearchResult.js +2 -2
- package/lib/SearchTypeResult/SearchTypeResult.js +4 -4
- package/lib/TopicMenu/TopicMenu.js +14 -1
- package/lib/TreeStructure/FolderItem.d.ts +6 -3
- package/lib/TreeStructure/FolderItem.js +55 -38
- package/lib/TreeStructure/FolderItems.d.ts +1 -1
- package/lib/TreeStructure/FolderItems.js +29 -35
- package/lib/TreeStructure/FolderNameInput.d.ts +3 -2
- package/lib/TreeStructure/FolderNameInput.js +13 -17
- package/lib/TreeStructure/TreeStructure.d.ts +1 -6
- package/lib/TreeStructure/TreeStructure.js +63 -92
- package/lib/TreeStructure/TreeStructure.types.d.ts +13 -20
- package/lib/TreeStructure/arrowNavigation.d.ts +9 -0
- package/lib/TreeStructure/arrowNavigation.js +54 -0
- package/lib/TreeStructure/helperFunctions.d.ts +3 -4
- package/lib/TreeStructure/helperFunctions.js +45 -35
- package/lib/all.css +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -9
- package/lib/locale/messages-en.d.ts +3 -0
- package/lib/locale/messages-en.js +4 -1
- package/lib/locale/messages-nb.d.ts +3 -0
- package/lib/locale/messages-nb.js +4 -1
- package/lib/locale/messages-nn.d.ts +3 -0
- package/lib/locale/messages-nn.js +4 -1
- package/lib/locale/messages-se.d.ts +3 -0
- package/lib/locale/messages-se.js +4 -1
- package/lib/locale/messages-sma.d.ts +3 -0
- package/lib/locale/messages-sma.js +4 -1
- package/package.json +14 -13
- package/src/Masthead/Masthead.tsx +4 -1
- package/src/Messages/MessageBanner.tsx +1 -1
- package/src/MyNdla/Resource/FolderInput.tsx +41 -44
- package/src/NDLAFilm/FilmSlideshow.tsx +1 -1
- package/src/Resource/ListResource.tsx +1 -0
- package/src/Search/LoadingWrapper.tsx +1 -1
- package/src/Search/SearchResult.jsx +1 -1
- package/src/SearchTypeResult/SearchTypeResult.tsx +1 -1
- package/src/TopicMenu/TopicMenu.jsx +15 -2
- package/src/TreeStructure/FolderItem.tsx +63 -40
- package/src/TreeStructure/FolderItems.tsx +26 -19
- package/src/TreeStructure/FolderNameInput.tsx +10 -12
- package/src/TreeStructure/TreeStructure.tsx +56 -71
- package/src/TreeStructure/TreeStructure.types.ts +13 -17
- package/src/TreeStructure/arrowNavigation.ts +53 -0
- package/src/TreeStructure/helperFunctions.ts +17 -25
- package/src/index.ts +0 -2
- package/src/locale/messages-en.ts +3 -0
- package/src/locale/messages-nb.ts +3 -0
- package/src/locale/messages-nn.ts +3 -0
- package/src/locale/messages-se.ts +3 -0
- package/src/locale/messages-sma.ts +3 -0
- package/es/Spinner/Spinner.js +0 -42
- package/es/Spinner/index.js +0 -2
- package/es/TreeStructure/keyboardNavigation/keyboardNavigation.js +0 -194
- package/es/TreeStructure/keyboardNavigation/keyboardNavigation.types.js +0 -0
- package/lib/Spinner/Spinner.d.ts +0 -16
- package/lib/Spinner/Spinner.js +0 -54
- package/lib/Spinner/index.d.ts +0 -2
- package/lib/Spinner/index.js +0 -13
- package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.d.ts +0 -11
- package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.js +0 -198
- package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.types.d.ts +0 -26
- package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.types.js +0 -1
- package/src/Spinner/Spinner.tsx +0 -46
- package/src/Spinner/index.ts +0 -3
- package/src/TreeStructure/keyboardNavigation/keyboardNavigation.ts +0 -161
- package/src/TreeStructure/keyboardNavigation/keyboardNavigation.types.ts +0 -28
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = exports.KEYBOARD_KEYS_OF_INTEREST = void 0;
|
|
7
|
-
|
|
8
|
-
var _TreeStructure = require("../TreeStructure");
|
|
9
|
-
|
|
10
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
11
|
-
|
|
12
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
13
|
-
|
|
14
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
15
|
-
|
|
16
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
17
|
-
|
|
18
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
19
|
-
|
|
20
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
|
-
|
|
22
|
-
var KEYBOARD_KEYS_OF_INTEREST = ['ArrowDown', 'ArrowUp', 'ArrowRight', 'ArrowLeft', ' ']; // Traverse upwards, incase parent is last element of its parent..
|
|
23
|
-
|
|
24
|
-
exports.KEYBOARD_KEYS_OF_INTEREST = KEYBOARD_KEYS_OF_INTEREST;
|
|
25
|
-
|
|
26
|
-
var traverseUpwards = function traverseUpwards(inital, setFocusedFolderId, paths, index) {
|
|
27
|
-
var findParent = inital;
|
|
28
|
-
var parentNextIds = [];
|
|
29
|
-
paths.forEach(function (pathIndex) {
|
|
30
|
-
var nextParent = findParent ? findParent[pathIndex + 1] : undefined;
|
|
31
|
-
parentNextIds.push((nextParent === null || nextParent === void 0 ? void 0 : nextParent.id) || false);
|
|
32
|
-
findParent = findParent[pathIndex].data || [];
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (!parentNextIds.length) {
|
|
36
|
-
var _findParent;
|
|
37
|
-
|
|
38
|
-
parentNextIds.push(((_findParent = findParent[index + 1]) === null || _findParent === void 0 ? void 0 : _findParent.id) || false);
|
|
39
|
-
} // We use a reversed version of parentNextIds, filtered out falses, to find the next element
|
|
40
|
-
// No newId? We are at the end of the tree so we wont update.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var newId = parentNextIds.reverse().filter(function (id) {
|
|
44
|
-
return id;
|
|
45
|
-
})[0];
|
|
46
|
-
|
|
47
|
-
if (newId) {
|
|
48
|
-
setFocusedFolderId(newId);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
var keyboardNavigation = function keyboardNavigation(_ref) {
|
|
53
|
-
var _document$activeEleme, _elementWithKeyFocus$5;
|
|
54
|
-
|
|
55
|
-
var e = _ref.e,
|
|
56
|
-
data = _ref.data,
|
|
57
|
-
onToggleOpen = _ref.onToggleOpen,
|
|
58
|
-
setFocusedFolderId = _ref.setFocusedFolderId,
|
|
59
|
-
id = _ref.focusedFolderId,
|
|
60
|
-
openFolders = _ref.openFolders;
|
|
61
|
-
|
|
62
|
-
if (e.key === ' ' && ((_document$activeEleme = document.activeElement) === null || _document$activeEleme === void 0 ? void 0 : _document$activeEleme.nodeName) === 'INPUT') {
|
|
63
|
-
return;
|
|
64
|
-
} // We are navigating in the tree.
|
|
65
|
-
// We need to find the next folder in the tree
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var elementWithKeyFocus = {
|
|
69
|
-
paths: [],
|
|
70
|
-
index: 0
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
var updatePathToElementWithKeyFocus = function updatePathToElementWithKeyFocus(data, paths, parent, parentId) {
|
|
74
|
-
return data.some(function (_ref2, _index) {
|
|
75
|
-
var childData = _ref2.data,
|
|
76
|
-
dataId = _ref2.id,
|
|
77
|
-
url = _ref2.url;
|
|
78
|
-
|
|
79
|
-
if (dataId === id) {
|
|
80
|
-
elementWithKeyFocus.paths = paths;
|
|
81
|
-
elementWithKeyFocus.index = _index;
|
|
82
|
-
elementWithKeyFocus.isOpen = openFolders.includes(dataId) && childData && childData.length > 0;
|
|
83
|
-
elementWithKeyFocus.data = childData;
|
|
84
|
-
elementWithKeyFocus.parent = parent;
|
|
85
|
-
elementWithKeyFocus.parentId = parentId;
|
|
86
|
-
elementWithKeyFocus.url = url;
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return childData ? updatePathToElementWithKeyFocus(childData, [].concat(_toConsumableArray(paths), [_index]), _toConsumableArray(childData), dataId) : false;
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
if (!updatePathToElementWithKeyFocus(data, [], data)) {
|
|
95
|
-
var _data$;
|
|
96
|
-
|
|
97
|
-
// Couldn't find its location in the tree.
|
|
98
|
-
// This should not happen, reset its value to root.
|
|
99
|
-
setFocusedFolderId(e.key === 'ArrowDown' ? (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.id : undefined);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
e.preventDefault();
|
|
104
|
-
e.stopPropagation();
|
|
105
|
-
|
|
106
|
-
if (e.key === ' ') {
|
|
107
|
-
var simulatedEvent = new MouseEvent('click', {
|
|
108
|
-
bubbles: true,
|
|
109
|
-
cancelable: true,
|
|
110
|
-
view: window
|
|
111
|
-
});
|
|
112
|
-
document.activeElement && document.activeElement.dispatchEvent(simulatedEvent);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (e.key === 'ArrowRight') {
|
|
117
|
-
var _elementWithKeyFocus$;
|
|
118
|
-
|
|
119
|
-
if (!elementWithKeyFocus.isOpen && ((_elementWithKeyFocus$ = elementWithKeyFocus.data) === null || _elementWithKeyFocus$ === void 0 ? void 0 : _elementWithKeyFocus$.length) && id && elementWithKeyFocus.paths.length < _TreeStructure.MAX_LEVEL_FOR_FOLDERS - 1) {
|
|
120
|
-
onToggleOpen(id);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (e.key === 'ArrowLeft') {
|
|
127
|
-
if (id && elementWithKeyFocus.isOpen) {
|
|
128
|
-
onToggleOpen(id);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (!id && e.key === 'ArrowDown') {
|
|
135
|
-
var _data$2;
|
|
136
|
-
|
|
137
|
-
setFocusedFolderId((_data$2 = data[0]) === null || _data$2 === void 0 ? void 0 : _data$2.id);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!id) {
|
|
142
|
-
return;
|
|
143
|
-
} // Move up
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (e.key === 'ArrowUp') {
|
|
147
|
-
if (elementWithKeyFocus.index > 0) {
|
|
148
|
-
var _elementWithKeyFocus$2;
|
|
149
|
-
|
|
150
|
-
// Move upwards to the parent folder
|
|
151
|
-
setFocusedFolderId(elementWithKeyFocus.parent ? (_elementWithKeyFocus$2 = elementWithKeyFocus.parent[elementWithKeyFocus.index - 1]) === null || _elementWithKeyFocus$2 === void 0 ? void 0 : _elementWithKeyFocus$2.id : undefined);
|
|
152
|
-
} else if (elementWithKeyFocus.paths.length > 0) {
|
|
153
|
-
var _findParent$parentsCu;
|
|
154
|
-
|
|
155
|
-
elementWithKeyFocus.paths.pop();
|
|
156
|
-
var findParent = data;
|
|
157
|
-
elementWithKeyFocus.paths.forEach(function (index) {
|
|
158
|
-
findParent = findParent[index].data;
|
|
159
|
-
});
|
|
160
|
-
var parentsCurrentIndex = findParent.findIndex(function (_ref3) {
|
|
161
|
-
var id = _ref3.id;
|
|
162
|
-
return id === elementWithKeyFocus.parentId;
|
|
163
|
-
});
|
|
164
|
-
setFocusedFolderId((_findParent$parentsCu = findParent[parentsCurrentIndex]) === null || _findParent$parentsCu === void 0 ? void 0 : _findParent$parentsCu.id);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (elementWithKeyFocus.isOpen) {
|
|
171
|
-
var _elementWithKeyFocus$3;
|
|
172
|
-
|
|
173
|
-
if ((_elementWithKeyFocus$3 = elementWithKeyFocus.data) === null || _elementWithKeyFocus$3 === void 0 ? void 0 : _elementWithKeyFocus$3.length) {
|
|
174
|
-
var _elementWithKeyFocus$4;
|
|
175
|
-
|
|
176
|
-
setFocusedFolderId((_elementWithKeyFocus$4 = elementWithKeyFocus.data[0]) === null || _elementWithKeyFocus$4 === void 0 ? void 0 : _elementWithKeyFocus$4.id);
|
|
177
|
-
} else {
|
|
178
|
-
// move to next child of parent if any... need new traverse :-/
|
|
179
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (elementWithKeyFocus.parent && elementWithKeyFocus.index < ((_elementWithKeyFocus$5 = elementWithKeyFocus.parent) === null || _elementWithKeyFocus$5 === void 0 ? void 0 : _elementWithKeyFocus$5.length) - 1) {
|
|
186
|
-
var _elementWithKeyFocus$6;
|
|
187
|
-
|
|
188
|
-
// Move downwards to the next child
|
|
189
|
-
setFocusedFolderId((_elementWithKeyFocus$6 = elementWithKeyFocus.parent[elementWithKeyFocus.index + 1]) === null || _elementWithKeyFocus$6 === void 0 ? void 0 : _elementWithKeyFocus$6.id);
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
194
|
-
return;
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
var _default = keyboardNavigation;
|
|
198
|
-
exports["default"] = _default;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2022-present, NDLA.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
/// <reference types="react" />
|
|
9
|
-
import { FolderStructureProps, SetFocusedFolderId } from '../TreeStructure.types';
|
|
10
|
-
export interface KeyboardNavigationProps {
|
|
11
|
-
e: React.KeyboardEvent<HTMLElement>;
|
|
12
|
-
data: FolderStructureProps[];
|
|
13
|
-
setFocusedFolderId: SetFocusedFolderId;
|
|
14
|
-
openFolders: string[];
|
|
15
|
-
onToggleOpen: (id: string) => void;
|
|
16
|
-
focusedFolderId: string | undefined;
|
|
17
|
-
}
|
|
18
|
-
export interface ElementWithKeyFocusProps {
|
|
19
|
-
paths: number[];
|
|
20
|
-
index: number;
|
|
21
|
-
data?: FolderStructureProps[];
|
|
22
|
-
parent?: FolderStructureProps[];
|
|
23
|
-
parentId?: string;
|
|
24
|
-
isOpen?: boolean;
|
|
25
|
-
url?: string;
|
|
26
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/src/Spinner/Spinner.tsx
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2018-present, NDLA.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import React from 'react';
|
|
10
|
-
import styled from '@emotion/styled';
|
|
11
|
-
import { colors, spacing, SpacingNames } from '@ndla/core';
|
|
12
|
-
|
|
13
|
-
interface Props {
|
|
14
|
-
size?: SpacingNames;
|
|
15
|
-
margin?: string;
|
|
16
|
-
inverted?: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface StyledProps extends Props {
|
|
20
|
-
size: SpacingNames;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const SpinnerDiv = styled('div')<StyledProps>`
|
|
24
|
-
border: calc(${(props) => spacing[props.size]} / 6.5) solid rgba(0, 0, 0, 0.1);
|
|
25
|
-
border-bottom-color: ${(props) => (props.inverted ? '#fff' : colors.brand.primary)};
|
|
26
|
-
border-radius: 50%;
|
|
27
|
-
animation: spinnerAnimation 0.7s linear infinite;
|
|
28
|
-
height: ${(props) => spacing[props.size]};
|
|
29
|
-
width: ${(props) => spacing[props.size]};
|
|
30
|
-
margin: ${(props) => props.margin};
|
|
31
|
-
display: block;
|
|
32
|
-
@keyframes spinnerAnimation {
|
|
33
|
-
0% {
|
|
34
|
-
transform: rotate(0deg);
|
|
35
|
-
}
|
|
36
|
-
100% {
|
|
37
|
-
transform: rotate(360deg);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
`;
|
|
41
|
-
|
|
42
|
-
const Spinner = ({ size = 'large', margin = `${spacing.normal} auto`, inverted }: Partial<Props>) => (
|
|
43
|
-
<SpinnerDiv size={size} margin={margin} inverted={!!inverted} />
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
export default Spinner;
|
package/src/Spinner/index.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2022-present, NDLA.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { FolderStructureProps, SetFocusedFolderId } from '../TreeStructure.types';
|
|
10
|
-
import { KeyboardNavigationProps, ElementWithKeyFocusProps } from './keyboardNavigation.types';
|
|
11
|
-
import { MAX_LEVEL_FOR_FOLDERS } from '../TreeStructure';
|
|
12
|
-
|
|
13
|
-
export const KEYBOARD_KEYS_OF_INTEREST = ['ArrowDown', 'ArrowUp', 'ArrowRight', 'ArrowLeft', ' '];
|
|
14
|
-
|
|
15
|
-
// Traverse upwards, incase parent is last element of its parent..
|
|
16
|
-
const traverseUpwards = (
|
|
17
|
-
inital: FolderStructureProps[],
|
|
18
|
-
setFocusedFolderId: SetFocusedFolderId,
|
|
19
|
-
paths: number[],
|
|
20
|
-
index: number,
|
|
21
|
-
) => {
|
|
22
|
-
let findParent: FolderStructureProps[] = inital;
|
|
23
|
-
const parentNextIds: (string | false)[] = [];
|
|
24
|
-
paths.forEach((pathIndex) => {
|
|
25
|
-
const nextParent = findParent ? findParent[pathIndex + 1] : undefined;
|
|
26
|
-
parentNextIds.push(nextParent?.id || false);
|
|
27
|
-
findParent = findParent[pathIndex].data || [];
|
|
28
|
-
});
|
|
29
|
-
if (!parentNextIds.length) {
|
|
30
|
-
parentNextIds.push(findParent[index + 1]?.id || false);
|
|
31
|
-
}
|
|
32
|
-
// We use a reversed version of parentNextIds, filtered out falses, to find the next element
|
|
33
|
-
// No newId? We are at the end of the tree so we wont update.
|
|
34
|
-
const newId = parentNextIds.reverse().filter((id) => id)[0];
|
|
35
|
-
if (newId) {
|
|
36
|
-
setFocusedFolderId(newId);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const keyboardNavigation = ({
|
|
41
|
-
e,
|
|
42
|
-
data,
|
|
43
|
-
onToggleOpen,
|
|
44
|
-
setFocusedFolderId,
|
|
45
|
-
focusedFolderId: id,
|
|
46
|
-
openFolders,
|
|
47
|
-
}: KeyboardNavigationProps): string | undefined => {
|
|
48
|
-
if (e.key === ' ' && document.activeElement?.nodeName === 'INPUT') {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// We are navigating in the tree.
|
|
53
|
-
// We need to find the next folder in the tree
|
|
54
|
-
const elementWithKeyFocus: ElementWithKeyFocusProps = {
|
|
55
|
-
paths: [],
|
|
56
|
-
index: 0,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const updatePathToElementWithKeyFocus = (
|
|
60
|
-
data: FolderStructureProps[],
|
|
61
|
-
paths: number[],
|
|
62
|
-
parent: FolderStructureProps[],
|
|
63
|
-
parentId?: string,
|
|
64
|
-
): boolean =>
|
|
65
|
-
data.some(({ data: childData, id: dataId, url }, _index) => {
|
|
66
|
-
if (dataId === id) {
|
|
67
|
-
elementWithKeyFocus.paths = paths;
|
|
68
|
-
elementWithKeyFocus.index = _index;
|
|
69
|
-
elementWithKeyFocus.isOpen = openFolders.includes(dataId) && childData && childData.length > 0;
|
|
70
|
-
elementWithKeyFocus.data = childData;
|
|
71
|
-
elementWithKeyFocus.parent = parent;
|
|
72
|
-
elementWithKeyFocus.parentId = parentId;
|
|
73
|
-
elementWithKeyFocus.url = url;
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
return childData ? updatePathToElementWithKeyFocus(childData, [...paths, _index], [...childData], dataId) : false;
|
|
77
|
-
});
|
|
78
|
-
if (!updatePathToElementWithKeyFocus(data, [], data)) {
|
|
79
|
-
// Couldn't find its location in the tree.
|
|
80
|
-
// This should not happen, reset its value to root.
|
|
81
|
-
setFocusedFolderId(e.key === 'ArrowDown' ? data[0]?.id : undefined);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
e.preventDefault();
|
|
85
|
-
e.stopPropagation();
|
|
86
|
-
|
|
87
|
-
if (e.key === ' ') {
|
|
88
|
-
const simulatedEvent = new MouseEvent('click', {
|
|
89
|
-
bubbles: true,
|
|
90
|
-
cancelable: true,
|
|
91
|
-
view: window,
|
|
92
|
-
});
|
|
93
|
-
document.activeElement && document.activeElement.dispatchEvent(simulatedEvent);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (e.key === 'ArrowRight') {
|
|
98
|
-
if (
|
|
99
|
-
!elementWithKeyFocus.isOpen &&
|
|
100
|
-
elementWithKeyFocus.data?.length &&
|
|
101
|
-
id &&
|
|
102
|
-
elementWithKeyFocus.paths.length < MAX_LEVEL_FOR_FOLDERS - 1
|
|
103
|
-
) {
|
|
104
|
-
onToggleOpen(id);
|
|
105
|
-
}
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
if (e.key === 'ArrowLeft') {
|
|
109
|
-
if (id && elementWithKeyFocus.isOpen) {
|
|
110
|
-
onToggleOpen(id);
|
|
111
|
-
}
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (!id && e.key === 'ArrowDown') {
|
|
116
|
-
setFocusedFolderId(data[0]?.id);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (!id) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
// Move up
|
|
123
|
-
if (e.key === 'ArrowUp') {
|
|
124
|
-
if (elementWithKeyFocus.index > 0) {
|
|
125
|
-
// Move upwards to the parent folder
|
|
126
|
-
setFocusedFolderId(
|
|
127
|
-
elementWithKeyFocus.parent ? elementWithKeyFocus.parent[elementWithKeyFocus.index - 1]?.id : undefined,
|
|
128
|
-
);
|
|
129
|
-
} else if (elementWithKeyFocus.paths.length > 0) {
|
|
130
|
-
elementWithKeyFocus.paths.pop();
|
|
131
|
-
let findParent = data;
|
|
132
|
-
elementWithKeyFocus.paths.forEach((index) => {
|
|
133
|
-
findParent = findParent[index].data as FolderStructureProps[];
|
|
134
|
-
});
|
|
135
|
-
const parentsCurrentIndex = findParent.findIndex(({ id }) => id === elementWithKeyFocus.parentId);
|
|
136
|
-
setFocusedFolderId(findParent[parentsCurrentIndex]?.id);
|
|
137
|
-
}
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (elementWithKeyFocus.isOpen) {
|
|
142
|
-
if (elementWithKeyFocus.data?.length) {
|
|
143
|
-
setFocusedFolderId(elementWithKeyFocus.data[0]?.id);
|
|
144
|
-
} else {
|
|
145
|
-
// move to next child of parent if any... need new traverse :-/
|
|
146
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
147
|
-
}
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (elementWithKeyFocus.parent && elementWithKeyFocus.index < elementWithKeyFocus.parent?.length - 1) {
|
|
152
|
-
// Move downwards to the next child
|
|
153
|
-
setFocusedFolderId(elementWithKeyFocus.parent[elementWithKeyFocus.index + 1]?.id);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
158
|
-
return;
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
export default keyboardNavigation;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2022-present, NDLA.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { FolderStructureProps, SetFocusedFolderId } from '../TreeStructure.types';
|
|
10
|
-
|
|
11
|
-
export interface KeyboardNavigationProps {
|
|
12
|
-
e: React.KeyboardEvent<HTMLElement>;
|
|
13
|
-
data: FolderStructureProps[];
|
|
14
|
-
setFocusedFolderId: SetFocusedFolderId;
|
|
15
|
-
openFolders: string[];
|
|
16
|
-
onToggleOpen: (id: string) => void;
|
|
17
|
-
focusedFolderId: string | undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface ElementWithKeyFocusProps {
|
|
21
|
-
paths: number[];
|
|
22
|
-
index: number;
|
|
23
|
-
data?: FolderStructureProps[];
|
|
24
|
-
parent?: FolderStructureProps[];
|
|
25
|
-
parentId?: string;
|
|
26
|
-
isOpen?: boolean;
|
|
27
|
-
url?: string;
|
|
28
|
-
}
|