@dxos/plugin-navtree 0.7.1-staging.8709ba7 → 0.7.2-staging.6d26b2a

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.
Files changed (34) hide show
  1. package/dist/lib/browser/index.mjs +190 -187
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/NavTreePlugin.d.ts.map +1 -1
  5. package/dist/types/src/components/NavTree.d.ts +7 -5
  6. package/dist/types/src/components/NavTree.d.ts.map +1 -1
  7. package/dist/types/src/components/NavTree.stories.d.ts.map +1 -1
  8. package/dist/types/src/components/NavTreeContainer.d.ts +2 -6
  9. package/dist/types/src/components/NavTreeContainer.d.ts.map +1 -1
  10. package/dist/types/src/components/NavTreeItemAction.stories.d.ts +1 -0
  11. package/dist/types/src/components/NavTreeItemAction.stories.d.ts.map +1 -1
  12. package/dist/types/src/stories/Graph.stories.d.ts +2 -1
  13. package/dist/types/src/stories/Graph.stories.d.ts.map +1 -1
  14. package/dist/types/src/testing/index.d.ts +1 -1
  15. package/dist/types/src/testing/index.d.ts.map +1 -1
  16. package/dist/types/src/testing/update-state.d.ts +10 -0
  17. package/dist/types/src/testing/update-state.d.ts.map +1 -0
  18. package/dist/types/src/types.d.ts +12 -3
  19. package/dist/types/src/types.d.ts.map +1 -1
  20. package/dist/types/src/util.d.ts +1 -20
  21. package/dist/types/src/util.d.ts.map +1 -1
  22. package/package.json +28 -28
  23. package/src/NavTreePlugin.tsx +103 -90
  24. package/src/components/NavTree.stories.tsx +73 -69
  25. package/src/components/NavTree.tsx +30 -20
  26. package/src/components/NavTreeContainer.tsx +71 -47
  27. package/src/stories/Graph.stories.tsx +68 -96
  28. package/src/testing/index.ts +1 -1
  29. package/src/testing/update-state.ts +70 -0
  30. package/src/types.ts +18 -3
  31. package/src/util.ts +2 -94
  32. package/dist/types/src/testing/visitor.d.ts +0 -18
  33. package/dist/types/src/testing/visitor.d.ts.map +0 -1
  34. package/src/testing/visitor.ts +0 -152
@@ -1,18 +0,0 @@
1
- import { type Instruction } from '@atlaskit/pragmatic-drag-and-drop-hitbox/tree-item';
2
- import { type NodeArg } from '@dxos/app-graph';
3
- import { type NavTreeItem } from '../types';
4
- export declare const flattenTree: (tree: NodeArg<any>, open: string[]) => NavTreeItem[];
5
- export declare function visitor({ node, getItem, isOpen, }: {
6
- node: NodeArg<any>;
7
- getItem: (node: NodeArg<any>, parent?: readonly string[]) => NavTreeItem;
8
- isOpen?: (node: NavTreeItem) => boolean;
9
- }): Generator<NavTreeItem>;
10
- export declare const getItem: (arg: NodeArg<any>, parent?: readonly string[]) => NavTreeItem;
11
- export declare const invalidateCache: (id: string) => void;
12
- export declare const updateState: ({ state, instruction, source, target, }: {
13
- state: NodeArg<any>;
14
- instruction: Instruction;
15
- source: NavTreeItem;
16
- target: NavTreeItem;
17
- }) => void;
18
- //# sourceMappingURL=visitor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../../../../src/testing/visitor.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,oDAAoD,CAAC;AAEtF,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAIxE,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,eAAO,MAAM,WAAW,SAAU,OAAO,CAAC,GAAG,CAAC,QAAQ,MAAM,EAAE,KAAG,WAAW,EAQ3E,CAAC;AAEF,wBAAiB,OAAO,CAAC,EACvB,IAAI,EACJ,OAAO,EACP,MAAM,GACP,EAAE;IACD,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,KAAK,WAAW,CAAC;IACzE,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC;CACzC,GAAG,SAAS,CAAC,WAAW,CAAC,CAgBzB;AAKD,eAAO,MAAM,OAAO,QAAS,OAAO,CAAC,GAAG,CAAC,WAAW,SAAS,MAAM,EAAE,KAAG,WA6BvE,CAAC;AAEF,eAAO,MAAM,eAAe,OAAQ,MAAM,SAEzC,CAAC;AAmBF,eAAO,MAAM,WAAW,4CAKrB;IACD,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IACpB,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC;CACrB,SAsCA,CAAC"}
@@ -1,152 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type Instruction } from '@atlaskit/pragmatic-drag-and-drop-hitbox/tree-item';
6
-
7
- import { isActionLike, type Node, type NodeArg } from '@dxos/app-graph';
8
- import { log } from '@dxos/log';
9
- import { Path } from '@dxos/react-ui-mosaic';
10
-
11
- import { type NavTreeItem } from '../types';
12
-
13
- export const flattenTree = (tree: NodeArg<any>, open: string[]): NavTreeItem[] => {
14
- return Array.from(
15
- visitor({
16
- node: tree,
17
- getItem,
18
- isOpen: ({ path }) => open.includes(Path.create(...path)),
19
- }),
20
- );
21
- };
22
-
23
- export function* visitor({
24
- node,
25
- getItem,
26
- isOpen,
27
- }: {
28
- node: NodeArg<any>;
29
- getItem: (node: NodeArg<any>, parent?: readonly string[]) => NavTreeItem;
30
- isOpen?: (node: NavTreeItem) => boolean;
31
- }): Generator<NavTreeItem> {
32
- const stack: [NodeArg<any>, NavTreeItem][] = [[node, getItem(node)]];
33
- while (stack.length > 0) {
34
- const [node, item] = stack.pop()!;
35
- if (item.path.length > 1) {
36
- yield item;
37
- }
38
-
39
- const children = Array.from(node.nodes ?? []);
40
- if (item.path.length === 1 || isOpen?.(item)) {
41
- for (let i = children.length - 1; i >= 0; i--) {
42
- const child = children[i];
43
- stack.push([child, getItem(child, item.path)]);
44
- }
45
- }
46
- }
47
- }
48
-
49
- // Ensures that the same item is not created multiple times, causing the tree to be re-rendered.
50
- const itemsCache: Record<string, NavTreeItem> = {};
51
-
52
- export const getItem = (arg: NodeArg<any>, parent?: readonly string[]): NavTreeItem => {
53
- const cachedItem = itemsCache[arg.id];
54
- if (cachedItem) {
55
- return cachedItem;
56
- }
57
-
58
- const children = arg.nodes?.filter((node) => !isActionLike(node)) ?? [];
59
- const actions = arg.nodes?.filter((node) => isActionLike(node)) ?? [];
60
- const node: Node = {
61
- id: arg.id,
62
- type: arg.type,
63
- properties: arg.properties ?? {},
64
- data: arg.data ?? null,
65
- };
66
-
67
- const item = {
68
- id: arg.id,
69
- label: arg.properties!.label,
70
- icon: arg.properties!.icon,
71
- path: parent ? [...parent, arg.id] : [arg.id],
72
- ...(children.length > 0 && {
73
- parentOf: children.map(({ id }) => id),
74
- }),
75
- node,
76
- actions,
77
- groupedActions: {},
78
- };
79
- itemsCache[node.id] = item;
80
- return item;
81
- };
82
-
83
- export const invalidateCache = (id: string) => {
84
- delete itemsCache[id];
85
- };
86
-
87
- const removeItem = (tree: NodeArg<any>, source: NavTreeItem) => {
88
- const parent = getNode(tree, source.path.slice(1, -1));
89
- const index = parent.nodes!.findIndex(({ id }) => id === source.id);
90
- const item = parent.nodes![index];
91
- parent.nodes!.splice(index, 1);
92
- return item;
93
- };
94
-
95
- const getNode = (tree: NodeArg<any>, path: string[]) => {
96
- let node = tree;
97
- for (const part of path) {
98
- node = node.nodes!.find(({ id }) => id === part)!;
99
- }
100
- return node;
101
- };
102
-
103
- // TODO(wittjosiah): Reconcile with react-ui-list/Tree/testing.
104
- export const updateState = ({
105
- state,
106
- instruction,
107
- source,
108
- target,
109
- }: {
110
- state: NodeArg<any>;
111
- instruction: Instruction;
112
- source: NavTreeItem;
113
- target: NavTreeItem;
114
- }) => {
115
- switch (instruction.type) {
116
- case 'reorder-above': {
117
- invalidateCache(source.id);
118
- const item = removeItem(state, source);
119
- const parent = getNode(state, target.path.slice(1, -1));
120
- const index = parent.nodes!.findIndex(({ id }) => id === target.id);
121
- invalidateCache(target.id);
122
- parent.nodes!.splice(index, 0, item);
123
- break;
124
- }
125
-
126
- case 'reorder-below': {
127
- invalidateCache(source.id);
128
- const item = removeItem(state, source);
129
- const parent = getNode(state, target.path.slice(1, -1));
130
- const index = parent.nodes!.findIndex(({ id }) => id === target.id);
131
- invalidateCache(target.id);
132
- parent.nodes!.splice(index + 1, 0, item);
133
- break;
134
- }
135
-
136
- case 'make-child': {
137
- invalidateCache(source.id);
138
- const item = removeItem(state, source);
139
- const parent = getNode(state, target.path.slice(1));
140
- invalidateCache(target.id);
141
- parent.nodes!.push(item);
142
- break;
143
- }
144
-
145
- case 'instruction-blocked':
146
- break;
147
-
148
- default:
149
- log.warn('Unsupported instruction', instruction);
150
- break;
151
- }
152
- };