1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { TreeWidget } from './tree-widget';
|
18 | import { SelectableTreeNode } from './tree-selection';
|
19 |
|
20 | export type TreeWidgetSelection = ReadonlyArray<Readonly<SelectableTreeNode>> & {
|
21 | source: TreeWidget
|
22 | };
|
23 | export namespace TreeWidgetSelection {
|
24 | export function isSource(selection: unknown, source: TreeWidget): selection is TreeWidgetSelection {
|
25 | return getSource(selection) === source;
|
26 | }
|
27 | export function getSource(selection: unknown): TreeWidget | undefined {
|
28 | return is(selection) ? selection.source : undefined;
|
29 | }
|
30 | export function is(selection: unknown): selection is TreeWidgetSelection {
|
31 | return Array.isArray(selection) && ('source' in selection) && (selection as TreeWidgetSelection).source instanceof TreeWidget;
|
32 | }
|
33 |
|
34 | export function create(source: TreeWidget): TreeWidgetSelection {
|
35 | const focusedNode = source.model.getFocusedNode();
|
36 | const selectedNodes = source.model.selectedNodes;
|
37 | const focusedIndex = selectedNodes.indexOf(focusedNode as SelectableTreeNode);
|
38 |
|
39 | if (focusedNode && focusedIndex > 0) {
|
40 | const selection = [focusedNode, ...selectedNodes.slice(0, focusedIndex), ...selectedNodes.slice(focusedIndex + 1)];
|
41 | return Object.assign(selection, { source });
|
42 | }
|
43 | return Object.assign(selectedNodes, { source });
|
44 | }
|
45 | }
|