@ndla/ui 19.1.1 → 19.2.0
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/es/TreeStructure/FolderItem.js +54 -38
- package/es/TreeStructure/FolderItems.js +29 -35
- package/es/TreeStructure/FolderNameInput.js +11 -15
- package/es/TreeStructure/TreeStructure.js +64 -91
- package/es/TreeStructure/arrowNavigation.js +44 -0
- package/es/TreeStructure/helperFunctions.js +41 -35
- 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 +11 -15
- 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/package.json +5 -5
- package/src/TreeStructure/FolderItem.tsx +63 -40
- package/src/TreeStructure/FolderItems.tsx +26 -19
- package/src/TreeStructure/FolderNameInput.tsx +9 -11
- 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/es/TreeStructure/keyboardNavigation/keyboardNavigation.js +0 -194
- package/es/TreeStructure/keyboardNavigation/keyboardNavigation.types.js +0 -0
- 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/TreeStructure/keyboardNavigation/keyboardNavigation.ts +0 -161
- package/src/TreeStructure/keyboardNavigation/keyboardNavigation.types.ts +0 -28
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FolderStructureProps } from './TreeStructure.types';
|
|
2
2
|
|
|
3
|
-
const getPathOfFolder = (data: FolderStructureProps[], findId: string): string[] => {
|
|
4
|
-
const paths = (
|
|
5
|
-
for (const { id,
|
|
3
|
+
export const getPathOfFolder = (data: FolderStructureProps[], findId: string): string[] => {
|
|
4
|
+
const paths = (folders: FolderStructureProps[], path: string[]): string[] => {
|
|
5
|
+
for (const { id, subfolders } of folders) {
|
|
6
6
|
if (id === findId) {
|
|
7
7
|
return [...path, id];
|
|
8
|
-
} else if (
|
|
9
|
-
return paths(
|
|
8
|
+
} else if (subfolders?.length) {
|
|
9
|
+
return paths(subfolders, [...path, id]);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
return [];
|
|
@@ -14,33 +14,18 @@ const getPathOfFolder = (data: FolderStructureProps[], findId: string): string[]
|
|
|
14
14
|
return paths(data, []);
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
const
|
|
18
|
-
let currentPath: number[] = [];
|
|
19
|
-
const paths = (dataChildren: FolderStructureProps[], path: number[]) => {
|
|
20
|
-
dataChildren.forEach(({ id, data: dataChildrenSub }, _index) => {
|
|
21
|
-
if (id === findId) {
|
|
22
|
-
currentPath = [...path, _index];
|
|
23
|
-
} else if (dataChildrenSub?.length) {
|
|
24
|
-
paths(dataChildrenSub, [...path, _index]);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
paths(data, []);
|
|
29
|
-
return currentPath;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getFolderName = (data: FolderStructureProps[], findId: string | undefined): string | undefined => {
|
|
17
|
+
export const getFolderName = (data: FolderStructureProps[], findId: string | undefined): string | undefined => {
|
|
33
18
|
if (!findId) {
|
|
34
19
|
return undefined;
|
|
35
20
|
}
|
|
36
21
|
let folderName: string | undefined;
|
|
37
22
|
const paths = (dataChildren: FolderStructureProps[]) => {
|
|
38
|
-
dataChildren.some(({ id, name,
|
|
23
|
+
dataChildren.some(({ id, name, subfolders }, _index) => {
|
|
39
24
|
if (id === findId) {
|
|
40
25
|
folderName = name;
|
|
41
26
|
return true;
|
|
42
|
-
} else if (
|
|
43
|
-
return paths(
|
|
27
|
+
} else if (subfolders?.length) {
|
|
28
|
+
return paths(subfolders);
|
|
44
29
|
}
|
|
45
30
|
return false;
|
|
46
31
|
});
|
|
@@ -49,4 +34,11 @@ const getFolderName = (data: FolderStructureProps[], findId: string | undefined)
|
|
|
49
34
|
return folderName;
|
|
50
35
|
};
|
|
51
36
|
|
|
52
|
-
export
|
|
37
|
+
export const flattenFolders = (folders: FolderStructureProps[], openFolders?: string[]): FolderStructureProps[] => {
|
|
38
|
+
return folders.reduce((acc, { subfolders, id, ...rest }) => {
|
|
39
|
+
if (!subfolders || (openFolders && !openFolders.includes(id))) {
|
|
40
|
+
return acc.concat({ subfolders, id, ...rest });
|
|
41
|
+
}
|
|
42
|
+
return acc.concat({ subfolders, id, ...rest }, flattenFolders(subfolders, openFolders));
|
|
43
|
+
}, [] as FolderStructureProps[]);
|
|
44
|
+
};
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
-
|
|
3
|
-
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."); }
|
|
4
|
-
|
|
5
|
-
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); }
|
|
6
|
-
|
|
7
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
8
|
-
|
|
9
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
10
|
-
|
|
11
|
-
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; }
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Copyright (c) 2022-present, NDLA.
|
|
15
|
-
*
|
|
16
|
-
* This source code is licensed under the GPLv3 license found in the
|
|
17
|
-
* LICENSE file in the root directory of this source tree.
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
import { MAX_LEVEL_FOR_FOLDERS } from '../TreeStructure';
|
|
21
|
-
export var KEYBOARD_KEYS_OF_INTEREST = ['ArrowDown', 'ArrowUp', 'ArrowRight', 'ArrowLeft', ' ']; // Traverse upwards, incase parent is last element of its parent..
|
|
22
|
-
|
|
23
|
-
var traverseUpwards = function traverseUpwards(inital, setFocusedFolderId, paths, index) {
|
|
24
|
-
var findParent = inital;
|
|
25
|
-
var parentNextIds = [];
|
|
26
|
-
paths.forEach(function (pathIndex) {
|
|
27
|
-
var nextParent = findParent ? findParent[pathIndex + 1] : undefined;
|
|
28
|
-
parentNextIds.push((nextParent === null || nextParent === void 0 ? void 0 : nextParent.id) || false);
|
|
29
|
-
findParent = findParent[pathIndex].data || [];
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
if (!parentNextIds.length) {
|
|
33
|
-
var _findParent;
|
|
34
|
-
|
|
35
|
-
parentNextIds.push(((_findParent = findParent[index + 1]) === null || _findParent === void 0 ? void 0 : _findParent.id) || false);
|
|
36
|
-
} // We use a reversed version of parentNextIds, filtered out falses, to find the next element
|
|
37
|
-
// No newId? We are at the end of the tree so we wont update.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
var newId = parentNextIds.reverse().filter(function (id) {
|
|
41
|
-
return id;
|
|
42
|
-
})[0];
|
|
43
|
-
|
|
44
|
-
if (newId) {
|
|
45
|
-
setFocusedFolderId(newId);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
var keyboardNavigation = function keyboardNavigation(_ref) {
|
|
50
|
-
var _document$activeEleme, _elementWithKeyFocus$5;
|
|
51
|
-
|
|
52
|
-
var e = _ref.e,
|
|
53
|
-
data = _ref.data,
|
|
54
|
-
onToggleOpen = _ref.onToggleOpen,
|
|
55
|
-
setFocusedFolderId = _ref.setFocusedFolderId,
|
|
56
|
-
id = _ref.focusedFolderId,
|
|
57
|
-
openFolders = _ref.openFolders;
|
|
58
|
-
|
|
59
|
-
if (e.key === ' ' && ((_document$activeEleme = document.activeElement) === null || _document$activeEleme === void 0 ? void 0 : _document$activeEleme.nodeName) === 'INPUT') {
|
|
60
|
-
return;
|
|
61
|
-
} // We are navigating in the tree.
|
|
62
|
-
// We need to find the next folder in the tree
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
var elementWithKeyFocus = {
|
|
66
|
-
paths: [],
|
|
67
|
-
index: 0
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
var updatePathToElementWithKeyFocus = function updatePathToElementWithKeyFocus(data, paths, parent, parentId) {
|
|
71
|
-
return data.some(function (_ref2, _index) {
|
|
72
|
-
var childData = _ref2.data,
|
|
73
|
-
dataId = _ref2.id,
|
|
74
|
-
url = _ref2.url;
|
|
75
|
-
|
|
76
|
-
if (dataId === id) {
|
|
77
|
-
elementWithKeyFocus.paths = paths;
|
|
78
|
-
elementWithKeyFocus.index = _index;
|
|
79
|
-
elementWithKeyFocus.isOpen = openFolders.includes(dataId) && childData && childData.length > 0;
|
|
80
|
-
elementWithKeyFocus.data = childData;
|
|
81
|
-
elementWithKeyFocus.parent = parent;
|
|
82
|
-
elementWithKeyFocus.parentId = parentId;
|
|
83
|
-
elementWithKeyFocus.url = url;
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return childData ? updatePathToElementWithKeyFocus(childData, [].concat(_toConsumableArray(paths), [_index]), _toConsumableArray(childData), dataId) : false;
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
if (!updatePathToElementWithKeyFocus(data, [], data)) {
|
|
92
|
-
var _data$;
|
|
93
|
-
|
|
94
|
-
// Couldn't find its location in the tree.
|
|
95
|
-
// This should not happen, reset its value to root.
|
|
96
|
-
setFocusedFolderId(e.key === 'ArrowDown' ? (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.id : undefined);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
e.preventDefault();
|
|
101
|
-
e.stopPropagation();
|
|
102
|
-
|
|
103
|
-
if (e.key === ' ') {
|
|
104
|
-
var simulatedEvent = new MouseEvent('click', {
|
|
105
|
-
bubbles: true,
|
|
106
|
-
cancelable: true,
|
|
107
|
-
view: window
|
|
108
|
-
});
|
|
109
|
-
document.activeElement && document.activeElement.dispatchEvent(simulatedEvent);
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (e.key === 'ArrowRight') {
|
|
114
|
-
var _elementWithKeyFocus$;
|
|
115
|
-
|
|
116
|
-
if (!elementWithKeyFocus.isOpen && ((_elementWithKeyFocus$ = elementWithKeyFocus.data) === null || _elementWithKeyFocus$ === void 0 ? void 0 : _elementWithKeyFocus$.length) && id && elementWithKeyFocus.paths.length < MAX_LEVEL_FOR_FOLDERS - 1) {
|
|
117
|
-
onToggleOpen(id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (e.key === 'ArrowLeft') {
|
|
124
|
-
if (id && elementWithKeyFocus.isOpen) {
|
|
125
|
-
onToggleOpen(id);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (!id && e.key === 'ArrowDown') {
|
|
132
|
-
var _data$2;
|
|
133
|
-
|
|
134
|
-
setFocusedFolderId((_data$2 = data[0]) === null || _data$2 === void 0 ? void 0 : _data$2.id);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (!id) {
|
|
139
|
-
return;
|
|
140
|
-
} // Move up
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (e.key === 'ArrowUp') {
|
|
144
|
-
if (elementWithKeyFocus.index > 0) {
|
|
145
|
-
var _elementWithKeyFocus$2;
|
|
146
|
-
|
|
147
|
-
// Move upwards to the parent folder
|
|
148
|
-
setFocusedFolderId(elementWithKeyFocus.parent ? (_elementWithKeyFocus$2 = elementWithKeyFocus.parent[elementWithKeyFocus.index - 1]) === null || _elementWithKeyFocus$2 === void 0 ? void 0 : _elementWithKeyFocus$2.id : undefined);
|
|
149
|
-
} else if (elementWithKeyFocus.paths.length > 0) {
|
|
150
|
-
var _findParent$parentsCu;
|
|
151
|
-
|
|
152
|
-
elementWithKeyFocus.paths.pop();
|
|
153
|
-
var findParent = data;
|
|
154
|
-
elementWithKeyFocus.paths.forEach(function (index) {
|
|
155
|
-
findParent = findParent[index].data;
|
|
156
|
-
});
|
|
157
|
-
var parentsCurrentIndex = findParent.findIndex(function (_ref3) {
|
|
158
|
-
var id = _ref3.id;
|
|
159
|
-
return id === elementWithKeyFocus.parentId;
|
|
160
|
-
});
|
|
161
|
-
setFocusedFolderId((_findParent$parentsCu = findParent[parentsCurrentIndex]) === null || _findParent$parentsCu === void 0 ? void 0 : _findParent$parentsCu.id);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (elementWithKeyFocus.isOpen) {
|
|
168
|
-
var _elementWithKeyFocus$3;
|
|
169
|
-
|
|
170
|
-
if ((_elementWithKeyFocus$3 = elementWithKeyFocus.data) === null || _elementWithKeyFocus$3 === void 0 ? void 0 : _elementWithKeyFocus$3.length) {
|
|
171
|
-
var _elementWithKeyFocus$4;
|
|
172
|
-
|
|
173
|
-
setFocusedFolderId((_elementWithKeyFocus$4 = elementWithKeyFocus.data[0]) === null || _elementWithKeyFocus$4 === void 0 ? void 0 : _elementWithKeyFocus$4.id);
|
|
174
|
-
} else {
|
|
175
|
-
// move to next child of parent if any... need new traverse :-/
|
|
176
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (elementWithKeyFocus.parent && elementWithKeyFocus.index < ((_elementWithKeyFocus$5 = elementWithKeyFocus.parent) === null || _elementWithKeyFocus$5 === void 0 ? void 0 : _elementWithKeyFocus$5.length) - 1) {
|
|
183
|
-
var _elementWithKeyFocus$6;
|
|
184
|
-
|
|
185
|
-
// Move downwards to the next child
|
|
186
|
-
setFocusedFolderId((_elementWithKeyFocus$6 = elementWithKeyFocus.parent[elementWithKeyFocus.index + 1]) === null || _elementWithKeyFocus$6 === void 0 ? void 0 : _elementWithKeyFocus$6.id);
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
|
|
191
|
-
return;
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
export default keyboardNavigation;
|
|
File without changes
|
|
@@ -1,11 +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
|
-
import { KeyboardNavigationProps } from './keyboardNavigation.types';
|
|
9
|
-
export declare const KEYBOARD_KEYS_OF_INTEREST: string[];
|
|
10
|
-
declare const keyboardNavigation: ({ e, data, onToggleOpen, setFocusedFolderId, focusedFolderId: id, openFolders, }: KeyboardNavigationProps) => string | undefined;
|
|
11
|
-
export default keyboardNavigation;
|
|
@@ -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";
|
|
@@ -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;
|