@dxos/plugin-explorer 0.8.4-main.72ec0f3 → 0.8.4-main.765dc60934

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 (234) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/neutral/ExplorerArticle-EW2MBCRK.mjs +141 -0
  3. package/dist/lib/neutral/ExplorerArticle-EW2MBCRK.mjs.map +7 -0
  4. package/dist/lib/neutral/ExplorerPlugin.mjs +10 -0
  5. package/dist/lib/neutral/capabilities/index.mjs +11 -0
  6. package/dist/lib/neutral/capabilities/index.mjs.map +7 -0
  7. package/dist/lib/neutral/chunk-7SPMPHRS.mjs +72 -0
  8. package/dist/lib/neutral/chunk-7SPMPHRS.mjs.map +7 -0
  9. package/dist/lib/neutral/chunk-GRJXLL4Z.mjs +25 -0
  10. package/dist/lib/neutral/chunk-GRJXLL4Z.mjs.map +7 -0
  11. package/dist/lib/{browser/chunk-UBHZGWZQ.mjs → neutral/chunk-HPIS2WXY.mjs} +2 -2
  12. package/dist/lib/neutral/chunk-HPIS2WXY.mjs.map +7 -0
  13. package/dist/lib/{browser/chunk-ARBGXQFH.mjs → neutral/components/index.mjs} +817 -288
  14. package/dist/lib/{node-esm/chunk-NPIP4VEH.mjs.map → neutral/components/index.mjs.map} +4 -4
  15. package/dist/lib/neutral/containers/index.mjs +9 -0
  16. package/dist/lib/neutral/containers/index.mjs.map +7 -0
  17. package/dist/lib/neutral/create-object-F6TKVAGV.mjs +39 -0
  18. package/dist/lib/neutral/create-object-F6TKVAGV.mjs.map +7 -0
  19. package/dist/lib/neutral/hooks/index.mjs +45 -0
  20. package/dist/lib/neutral/hooks/index.mjs.map +7 -0
  21. package/dist/lib/neutral/index.mjs +14 -0
  22. package/dist/lib/neutral/meta.json +1 -0
  23. package/dist/lib/{browser → neutral}/meta.mjs +1 -1
  24. package/dist/lib/neutral/plugin.mjs +12 -0
  25. package/dist/lib/neutral/plugin.mjs.map +7 -0
  26. package/dist/lib/neutral/react-surface-APBW2VQG.mjs +26 -0
  27. package/dist/lib/neutral/react-surface-APBW2VQG.mjs.map +7 -0
  28. package/dist/lib/neutral/testing.mjs +8 -0
  29. package/dist/lib/neutral/translations.mjs +33 -0
  30. package/dist/lib/neutral/translations.mjs.map +7 -0
  31. package/dist/lib/{browser → neutral}/types/index.mjs +1 -2
  32. package/dist/types/data/cities.d.ts +4 -4
  33. package/dist/types/data/cities.d.ts.map +1 -1
  34. package/dist/types/data/countries-110m.d.ts +19 -22
  35. package/dist/types/data/countries-110m.d.ts.map +1 -1
  36. package/dist/types/src/ExplorerPlugin.d.ts +3 -1
  37. package/dist/types/src/ExplorerPlugin.d.ts.map +1 -1
  38. package/dist/types/src/ExplorerPlugin.test.d.ts +2 -0
  39. package/dist/types/src/ExplorerPlugin.test.d.ts.map +1 -0
  40. package/dist/types/src/capabilities/create-object.d.ts +11 -0
  41. package/dist/types/src/capabilities/create-object.d.ts.map +1 -0
  42. package/dist/types/src/capabilities/index.d.ts +8 -2
  43. package/dist/types/src/capabilities/index.d.ts.map +1 -1
  44. package/dist/types/src/capabilities/react-surface.d.ts +3 -2
  45. package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
  46. package/dist/types/src/components/Chart/Chart.d.ts.map +1 -1
  47. package/dist/types/src/components/Chart/Chart.stories.d.ts +4 -1
  48. package/dist/types/src/components/Chart/Chart.stories.d.ts.map +1 -1
  49. package/dist/types/src/components/Globe/Globe.d.ts.map +1 -1
  50. package/dist/types/src/components/Globe/Globe.stories.d.ts +5 -2
  51. package/dist/types/src/components/Globe/Globe.stories.d.ts.map +1 -1
  52. package/dist/types/src/components/Graph/CanvasForceGraph.d.ts +13 -0
  53. package/dist/types/src/components/Graph/CanvasForceGraph.d.ts.map +1 -0
  54. package/dist/types/src/components/Graph/CanvasForceGraph.stories.d.ts +17 -0
  55. package/dist/types/src/components/Graph/CanvasForceGraph.stories.d.ts.map +1 -0
  56. package/dist/types/src/components/Graph/ForceGraph.d.ts +12 -5
  57. package/dist/types/src/components/Graph/ForceGraph.d.ts.map +1 -1
  58. package/dist/types/src/components/Graph/ForceGraph.stories.d.ts +4 -2
  59. package/dist/types/src/components/Graph/ForceGraph.stories.d.ts.map +1 -1
  60. package/dist/types/src/components/Graph/{adapter.d.ts → graph-adapter.d.ts} +2 -2
  61. package/dist/types/src/components/Graph/graph-adapter.d.ts.map +1 -0
  62. package/dist/types/src/components/Graph/index.d.ts +1 -1
  63. package/dist/types/src/components/Graph/index.d.ts.map +1 -1
  64. package/dist/types/src/components/Tree/EdgeBundling.stories.d.ts +21 -0
  65. package/dist/types/src/components/Tree/EdgeBundling.stories.d.ts.map +1 -0
  66. package/dist/types/src/components/Tree/Tree.d.ts +20 -23
  67. package/dist/types/src/components/Tree/Tree.d.ts.map +1 -1
  68. package/dist/types/src/components/Tree/Tree.stories.d.ts +5 -12
  69. package/dist/types/src/components/Tree/Tree.stories.d.ts.map +1 -1
  70. package/dist/types/src/components/Tree/index.d.ts +3 -0
  71. package/dist/types/src/components/Tree/index.d.ts.map +1 -1
  72. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts +35 -2
  73. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts.map +1 -1
  74. package/dist/types/src/components/Tree/layout/RadialTree.d.ts +35 -2
  75. package/dist/types/src/components/Tree/layout/RadialTree.d.ts.map +1 -1
  76. package/dist/types/src/components/Tree/layout/TidyTree.d.ts +24 -2
  77. package/dist/types/src/components/Tree/layout/TidyTree.d.ts.map +1 -1
  78. package/dist/types/src/components/Tree/layout/hierarchy.d.ts +17 -0
  79. package/dist/types/src/components/Tree/layout/hierarchy.d.ts.map +1 -0
  80. package/dist/types/src/components/Tree/layout/index.d.ts +5 -4
  81. package/dist/types/src/components/Tree/layout/index.d.ts.map +1 -1
  82. package/dist/types/src/components/Tree/layout/slots.d.ts +7 -0
  83. package/dist/types/src/components/Tree/layout/slots.d.ts.map +1 -0
  84. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts +15 -0
  85. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts.map +1 -0
  86. package/dist/types/src/components/Tree/space-graph-adapter.d.ts +32 -0
  87. package/dist/types/src/components/Tree/space-graph-adapter.d.ts.map +1 -0
  88. package/dist/types/src/components/Tree/testing/generator.d.ts.map +1 -1
  89. package/dist/types/src/components/Tree/testing/index.d.ts +1 -0
  90. package/dist/types/src/components/Tree/testing/index.d.ts.map +1 -1
  91. package/dist/types/src/components/Tree/testing/relations.d.ts +47 -0
  92. package/dist/types/src/components/Tree/testing/relations.d.ts.map +1 -0
  93. package/dist/types/src/components/Tree/types/tree.d.ts +18 -16
  94. package/dist/types/src/components/Tree/types/tree.d.ts.map +1 -1
  95. package/dist/types/src/components/Tree/types/types.d.ts +14 -4
  96. package/dist/types/src/components/Tree/types/types.d.ts.map +1 -1
  97. package/dist/types/src/components/index.d.ts +0 -2
  98. package/dist/types/src/components/index.d.ts.map +1 -1
  99. package/dist/types/src/components/plot.d.ts.map +1 -1
  100. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.d.ts +8 -0
  101. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.d.ts.map +1 -0
  102. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.stories.d.ts +24 -0
  103. package/dist/types/src/containers/ExplorerArticle/ExplorerArticle.stories.d.ts.map +1 -0
  104. package/dist/types/src/containers/ExplorerArticle/index.d.ts +2 -0
  105. package/dist/types/src/containers/ExplorerArticle/index.d.ts.map +1 -0
  106. package/dist/types/src/containers/index.d.ts +3 -0
  107. package/dist/types/src/containers/index.d.ts.map +1 -0
  108. package/dist/types/src/hooks/useGraphModel.d.ts +2 -2
  109. package/dist/types/src/hooks/useGraphModel.d.ts.map +1 -1
  110. package/dist/types/src/index.d.ts +1 -3
  111. package/dist/types/src/index.d.ts.map +1 -1
  112. package/dist/types/src/meta.d.ts +2 -2
  113. package/dist/types/src/meta.d.ts.map +1 -1
  114. package/dist/types/src/plugin.d.ts +3 -0
  115. package/dist/types/src/plugin.d.ts.map +1 -0
  116. package/dist/types/src/testing.d.ts +2 -0
  117. package/dist/types/src/testing.d.ts.map +1 -0
  118. package/dist/types/src/translations.d.ts +31 -22
  119. package/dist/types/src/translations.d.ts.map +1 -1
  120. package/dist/types/src/types/ExplorerAction.d.ts +1 -18
  121. package/dist/types/src/types/ExplorerAction.d.ts.map +1 -1
  122. package/dist/types/src/types/Graph.d.ts +14 -25
  123. package/dist/types/src/types/Graph.d.ts.map +1 -1
  124. package/dist/types/tsconfig.tsbuildinfo +1 -1
  125. package/package.json +113 -62
  126. package/src/ExplorerPlugin.test.ts +26 -0
  127. package/src/ExplorerPlugin.tsx +15 -56
  128. package/src/capabilities/create-object.ts +36 -0
  129. package/src/capabilities/index.ts +3 -3
  130. package/src/capabilities/react-surface.tsx +24 -19
  131. package/src/components/Chart/Chart.stories.tsx +16 -23
  132. package/src/components/Globe/Globe.stories.tsx +19 -22
  133. package/src/components/Graph/CanvasForceGraph.stories.tsx +83 -0
  134. package/src/components/Graph/CanvasForceGraph.tsx +124 -0
  135. package/src/components/Graph/ForceGraph.stories.tsx +79 -42
  136. package/src/components/Graph/ForceGraph.tsx +104 -85
  137. package/src/components/Graph/{adapter.ts → graph-adapter.ts} +14 -8
  138. package/src/components/Graph/index.ts +1 -1
  139. package/src/components/Tree/EdgeBundling.stories.tsx +144 -0
  140. package/src/components/Tree/Tree.stories.tsx +20 -38
  141. package/src/components/Tree/Tree.tsx +69 -95
  142. package/src/components/Tree/index.ts +3 -0
  143. package/src/components/Tree/layout/HierarchicalEdgeBundling.tsx +277 -0
  144. package/src/components/Tree/layout/RadialTree.tsx +237 -0
  145. package/src/components/Tree/layout/TidyTree.tsx +246 -0
  146. package/src/components/Tree/layout/hierarchy.ts +32 -0
  147. package/src/components/Tree/layout/index.ts +5 -5
  148. package/src/components/Tree/layout/slots.ts +19 -0
  149. package/src/components/Tree/layout/useContainerSize.ts +43 -0
  150. package/src/components/Tree/space-graph-adapter.ts +96 -0
  151. package/src/components/Tree/testing/generator.ts +4 -2
  152. package/src/components/Tree/testing/index.ts +1 -0
  153. package/src/components/Tree/testing/relations.ts +182 -0
  154. package/src/components/Tree/types/tree.test.ts +5 -4
  155. package/src/components/Tree/types/tree.ts +41 -20
  156. package/src/components/Tree/types/types.ts +38 -29
  157. package/src/components/index.ts +0 -4
  158. package/src/containers/ExplorerArticle/ExplorerArticle.stories.tsx +119 -0
  159. package/src/containers/ExplorerArticle/ExplorerArticle.tsx +153 -0
  160. package/src/containers/ExplorerArticle/index.ts +5 -0
  161. package/src/containers/index.ts +7 -0
  162. package/src/hooks/useGraphModel.ts +25 -14
  163. package/src/index.ts +1 -4
  164. package/src/meta.ts +3 -3
  165. package/src/plugin.ts +9 -0
  166. package/src/testing.ts +7 -0
  167. package/src/translations.ts +16 -13
  168. package/src/types/ExplorerAction.ts +9 -19
  169. package/src/types/Graph.ts +25 -23
  170. package/src/typings.d.ts +8 -0
  171. package/dist/lib/browser/ExplorerContainer-NOLLVUTE.mjs +0 -50
  172. package/dist/lib/browser/ExplorerContainer-NOLLVUTE.mjs.map +0 -7
  173. package/dist/lib/browser/chunk-2MKBRIUT.mjs +0 -31
  174. package/dist/lib/browser/chunk-2MKBRIUT.mjs.map +0 -7
  175. package/dist/lib/browser/chunk-6BVXZQPP.mjs +0 -188
  176. package/dist/lib/browser/chunk-6BVXZQPP.mjs.map +0 -7
  177. package/dist/lib/browser/chunk-ARBGXQFH.mjs.map +0 -7
  178. package/dist/lib/browser/chunk-JDSUIUNR.mjs +0 -80
  179. package/dist/lib/browser/chunk-JDSUIUNR.mjs.map +0 -7
  180. package/dist/lib/browser/chunk-UBHZGWZQ.mjs.map +0 -7
  181. package/dist/lib/browser/index.mjs +0 -119
  182. package/dist/lib/browser/index.mjs.map +0 -7
  183. package/dist/lib/browser/intent-resolver-YS5LZC3A.mjs +0 -31
  184. package/dist/lib/browser/intent-resolver-YS5LZC3A.mjs.map +0 -7
  185. package/dist/lib/browser/meta.json +0 -1
  186. package/dist/lib/browser/react-surface-BVTCOVLK.mjs +0 -35
  187. package/dist/lib/browser/react-surface-BVTCOVLK.mjs.map +0 -7
  188. package/dist/lib/node-esm/ExplorerContainer-N3S5KSUX.mjs +0 -51
  189. package/dist/lib/node-esm/ExplorerContainer-N3S5KSUX.mjs.map +0 -7
  190. package/dist/lib/node-esm/chunk-3ODK27PU.mjs +0 -33
  191. package/dist/lib/node-esm/chunk-3ODK27PU.mjs.map +0 -7
  192. package/dist/lib/node-esm/chunk-CRSVAZNA.mjs +0 -190
  193. package/dist/lib/node-esm/chunk-CRSVAZNA.mjs.map +0 -7
  194. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  195. package/dist/lib/node-esm/chunk-MS72BATS.mjs +0 -81
  196. package/dist/lib/node-esm/chunk-MS72BATS.mjs.map +0 -7
  197. package/dist/lib/node-esm/chunk-NPIP4VEH.mjs +0 -11091
  198. package/dist/lib/node-esm/chunk-UXZM5VJB.mjs +0 -26
  199. package/dist/lib/node-esm/chunk-UXZM5VJB.mjs.map +0 -7
  200. package/dist/lib/node-esm/index.mjs +0 -120
  201. package/dist/lib/node-esm/index.mjs.map +0 -7
  202. package/dist/lib/node-esm/intent-resolver-VCEC67WX.mjs +0 -32
  203. package/dist/lib/node-esm/intent-resolver-VCEC67WX.mjs.map +0 -7
  204. package/dist/lib/node-esm/meta.json +0 -1
  205. package/dist/lib/node-esm/meta.mjs +0 -9
  206. package/dist/lib/node-esm/react-surface-4HFEX52O.mjs +0 -36
  207. package/dist/lib/node-esm/react-surface-4HFEX52O.mjs.map +0 -7
  208. package/dist/lib/node-esm/types/index.mjs +0 -12
  209. package/dist/types/src/capabilities/intent-resolver.d.ts +0 -4
  210. package/dist/types/src/capabilities/intent-resolver.d.ts.map +0 -1
  211. package/dist/types/src/components/ExplorerContainer.d.ts +0 -9
  212. package/dist/types/src/components/ExplorerContainer.d.ts.map +0 -1
  213. package/dist/types/src/components/Graph/D3ForceGraph.d.ts +0 -14
  214. package/dist/types/src/components/Graph/D3ForceGraph.d.ts.map +0 -1
  215. package/dist/types/src/components/Graph/D3ForceGraph.stories.d.ts +0 -15
  216. package/dist/types/src/components/Graph/D3ForceGraph.stories.d.ts.map +0 -1
  217. package/dist/types/src/components/Graph/adapter.d.ts.map +0 -1
  218. package/dist/types/src/components/Graph/testing.d.ts +0 -14
  219. package/dist/types/src/components/Graph/testing.d.ts.map +0 -1
  220. package/src/capabilities/intent-resolver.ts +0 -21
  221. package/src/components/ExplorerContainer.tsx +0 -54
  222. package/src/components/Graph/D3ForceGraph.stories.tsx +0 -78
  223. package/src/components/Graph/D3ForceGraph.tsx +0 -101
  224. package/src/components/Graph/testing.ts +0 -55
  225. package/src/components/Tree/layout/HierarchicalEdgeBundling.ts +0 -162
  226. package/src/components/Tree/layout/RadialTree.ts +0 -94
  227. package/src/components/Tree/layout/TidyTree.ts +0 -101
  228. /package/dist/lib/{browser/chunk-J5LGTIGS.mjs.map → neutral/ExplorerPlugin.mjs.map} +0 -0
  229. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
  230. /package/dist/lib/{browser/meta.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
  231. /package/dist/lib/{browser/types → neutral}/index.mjs.map +0 -0
  232. /package/dist/lib/{node-esm → neutral}/meta.mjs.map +0 -0
  233. /package/dist/lib/{node-esm/chunk-HSLMI22Q.mjs.map → neutral/testing.mjs.map} +0 -0
  234. /package/dist/lib/{node-esm → neutral}/types/index.mjs.map +0 -0
@@ -3,73 +3,55 @@
3
3
  //
4
4
 
5
5
  import { type Meta, type StoryObj } from '@storybook/react-vite';
6
- import React, { useEffect, useState } from 'react';
6
+ import React, { useMemo } from 'react';
7
7
 
8
- import { faker } from '@dxos/random';
9
- import { useClient } from '@dxos/react-client';
10
- import { type ClientRepeatedComponentProps, ClientRepeater } from '@dxos/react-client/testing';
11
- import { withTheme } from '@dxos/react-ui/testing';
8
+ import { random } from '@dxos/random';
9
+ import { Loading, withLayout, withTheme } from '@dxos/react-ui/testing';
12
10
 
11
+ import { createTree } from './testing';
13
12
  import { Tree, type TreeComponentProps } from './Tree';
14
- import { Tree as TreeModel, TreeType } from './types';
13
+ import { treeTypeToTreeNode } from './types';
15
14
 
16
- // TODO(burdon): Storybook for Graph/Tree/Plot (generics); incl. GraphModel.
17
- // TODO(burdon): Type for all Explorer components (Space, Object, Query, etc.) incl.
15
+ random.seed(1);
18
16
 
19
- faker.seed(1);
17
+ type StoryArgs = Pick<TreeComponentProps, 'variant'>;
20
18
 
21
- type ComponentProps = ClientRepeatedComponentProps & { type?: TreeComponentProps<any>['variant'] };
22
-
23
- const Component = ({ type }: ComponentProps) => {
24
- const client = useClient();
25
- const space = client.spaces.default;
26
- const [object, setObject] = useState<TreeType>();
27
- useEffect(() => {
28
- setTimeout(() => {
29
- const tree = space.db.add(TreeModel.create());
30
- setObject(tree);
31
- });
32
- }, []);
33
-
34
- if (!object) {
35
- return null;
19
+ const DefaultStory = ({ variant }: StoryArgs) => {
20
+ const data = useMemo(() => treeTypeToTreeNode(createTree([3, [2, 4], [1, 3]]).tree), []);
21
+ if (!data) {
22
+ return <Loading />;
36
23
  }
37
24
 
38
- return <Tree space={space} selected={object?.id} variant={type} />;
25
+ return <Tree data={data} variant={variant} />;
39
26
  };
40
27
 
41
- const DefaultStory = () => {
42
- return <ClientRepeater component={Component} types={[TreeType]} createSpace />;
43
- };
44
-
45
- const meta = {
46
- title: 'plugins/plugin-explorer/Tree',
47
- component: Tree as any,
28
+ const meta: Meta<StoryArgs> = {
29
+ title: 'plugins/plugin-explorer/components/Tree',
48
30
  render: DefaultStory,
49
- decorators: [withTheme],
31
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
50
32
  parameters: {
51
33
  layout: 'fullscreen',
52
34
  },
53
- } satisfies Meta<typeof DefaultStory>;
35
+ };
54
36
 
55
37
  export default meta;
56
38
 
57
- type Story = StoryObj<typeof meta>;
39
+ type Story = StoryObj<StoryArgs>;
58
40
 
59
41
  export const Tidy: Story = {
60
42
  args: {
61
- type: 'tidy',
43
+ variant: 'tidy',
62
44
  },
63
45
  };
64
46
 
65
47
  export const Radial: Story = {
66
48
  args: {
67
- type: 'radial',
49
+ variant: 'radial',
68
50
  },
69
51
  };
70
52
 
71
53
  export const Edge: Story = {
72
54
  args: {
73
- type: 'edge',
55
+ variant: 'edge',
74
56
  },
75
57
  };
@@ -2,106 +2,80 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import React, { useEffect, useRef, useState } from 'react';
5
+ import React, { useMemo } from 'react';
6
6
 
7
- import { type Space } from '@dxos/client/echo';
8
- import { useAsyncState } from '@dxos/react-ui';
9
- import { SVG, type SVGContext } from '@dxos/react-ui-graph';
10
- import { SpaceGraphModel } from '@dxos/schema';
7
+ import { type ThemedClassName } from '@dxos/react-ui';
11
8
 
12
- import { HierarchicalEdgeBundling, RadialTree, TidyTree } from './layout';
13
- import { type TreeNode, mapGraphToTreeData } from './types';
14
-
15
- // TODO(burdon): Create dge bundling graph using d3.hierarchy.
16
- // https://observablehq.com/@d3/hierarchical-edge-bundling?intent=fork
17
-
18
- type Renderer = (svg: SVGSVGElement, data: any, options: any) => void;
9
+ import { type BundleEdge, HierarchicalEdgeBundling, RadialTree, TidyTree, type TreeLayoutSlots } from './layout';
10
+ import { type TreeNode } from './types';
19
11
 
20
12
  export type LayoutVariant = 'tidy' | 'radial' | 'edge';
21
13
 
22
- // TODO(burdon): Remove slots?
23
- export type TreeLayoutSlots = {
24
- node?: string;
25
- path?: string;
26
- text?: string;
27
- };
28
-
29
- export type TreeOptions = {
30
- label: (d: any) => string;
31
-
32
- slots?: TreeLayoutSlots;
33
- radius?: number;
34
-
35
- width: number;
36
- height: number;
37
- margin?: number;
38
-
39
- padding?: number;
40
- // Radius of nodes.
41
- r?: number;
42
- };
43
-
44
- export const defaultTreeLayoutSlots: TreeLayoutSlots = {
45
- node: 'fill-blue-600',
46
- path: 'fill-none stroke-blue-400 stroke-[0.5px]',
47
- text: 'stroke-[0.5px] stroke-neutral-700 text-xs', // TODO(burdon): Create box instead of halo.
48
- };
49
-
50
- const renderers = new Map<LayoutVariant, Renderer>([
51
- ['tidy', TidyTree],
52
- ['radial', RadialTree],
53
- ['edge', HierarchicalEdgeBundling],
54
- ]);
55
-
56
- export type TreeComponentProps<N = unknown> = {
57
- space: Space;
58
- selected?: string;
14
+ export type TreeComponentProps = ThemedClassName<{
15
+ data: TreeNode;
16
+ /** Optional edges for the `edge` variant (hierarchical edge bundling). */
17
+ edges?: BundleEdge[];
59
18
  variant?: LayoutVariant;
60
- onNodeClick?: (node?: N) => void;
61
- };
62
-
63
- // TODO(burdon): Label accessor.
64
- export const Tree = <N,>({ space, selected, variant = 'tidy', onNodeClick }: TreeComponentProps<N>) => {
65
- const [model] = useAsyncState(async () => (space ? new SpaceGraphModel().open(space) : undefined), [space, selected]);
66
-
67
- const [tree, setTree] = useState<TreeNode>();
68
- useEffect(() => {
69
- return model?.subscribe(() => {
70
- const tree = mapGraphToTreeData(model);
71
- setTree(tree);
72
- }, true);
73
- }, [model]);
74
-
75
- const context = useRef<SVGContext>(null);
76
-
77
- useEffect(() => {
78
- if (context.current?.size) {
79
- const { width, height } = context.current.size;
80
- const size = Math.min(width, height);
81
- const radius = size * 0.4;
82
- const options = {
83
- // TODO(burdon): Type.
84
- label: (d: any) => d.label ?? d.id,
85
- width,
86
- height,
87
- radius,
88
- marginLeft: (width - radius * 2) / 2,
89
- marginRight: (width - radius * 2) / 2,
90
- marginTop: (height - radius * 2) / 2,
91
- marginBottom: (height - radius * 2) / 2,
92
- slots: defaultTreeLayoutSlots,
93
- };
94
-
95
- if (tree) {
96
- const renderer = renderers.get(variant);
97
- renderer?.(context.current!.svg, tree, options);
98
- }
19
+ label?: (node: TreeNode) => string;
20
+ slots?: TreeLayoutSlots;
21
+ initialCollapsed?: Iterable<string>;
22
+ onNodeClick?: (node: TreeNode) => void;
23
+ onNodeHover?: (node: TreeNode | null, event?: MouseEvent) => void;
24
+ }>;
25
+
26
+ /**
27
+ * Tree visualization wrapping the three layout variants.
28
+ * - `tidy` — horizontal tidy tree (collapsible)
29
+ * - `radial` — radial tree (collapsible)
30
+ * - `edge` — hierarchical edge bundling (`edges` connect leaves)
31
+ */
32
+ export const Tree = ({
33
+ classNames,
34
+ data,
35
+ edges,
36
+ variant = 'tidy',
37
+ label,
38
+ slots,
39
+ initialCollapsed,
40
+ onNodeClick,
41
+ onNodeHover,
42
+ }: TreeComponentProps) => {
43
+ return useMemo(() => {
44
+ switch (variant) {
45
+ case 'tidy':
46
+ return (
47
+ <TidyTree
48
+ classNames={classNames}
49
+ data={data}
50
+ label={label}
51
+ slots={slots}
52
+ initialCollapsed={initialCollapsed}
53
+ onNodeClick={onNodeClick}
54
+ />
55
+ );
56
+ case 'radial':
57
+ return (
58
+ <RadialTree
59
+ classNames={classNames}
60
+ data={data}
61
+ label={label}
62
+ slots={slots}
63
+ initialCollapsed={initialCollapsed}
64
+ onNodeClick={onNodeClick}
65
+ onNodeHover={onNodeHover}
66
+ />
67
+ );
68
+ case 'edge':
69
+ return (
70
+ <HierarchicalEdgeBundling
71
+ classNames={classNames}
72
+ data={data}
73
+ edges={edges ?? []}
74
+ label={label}
75
+ slots={slots}
76
+ onNodeHover={onNodeHover}
77
+ />
78
+ );
99
79
  }
100
- }, [context.current, tree]);
101
-
102
- return (
103
- <div onClick={() => onNodeClick?.()}>
104
- <SVG.Root ref={context} />
105
- </div>
106
- );
80
+ }, [variant, classNames, data, edges, label, slots, initialCollapsed, onNodeClick, onNodeHover]);
107
81
  };
@@ -3,3 +3,6 @@
3
3
  //
4
4
 
5
5
  export * from './Tree';
6
+ export * from './layout';
7
+ export * from './space-graph-adapter';
8
+ export { type TreeNode, treeTypeToTreeNode } from './types';
@@ -0,0 +1,277 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Observable, Inc.
4
+ //
5
+
6
+ import { cluster, curveBundle, hierarchy, lineRadial, select } from 'd3';
7
+ import type { HierarchyNode } from 'd3-hierarchy';
8
+ import React, { useEffect, useMemo, useRef } from 'react';
9
+
10
+ import { type ThemedClassName } from '@dxos/react-ui';
11
+ import { mx } from '@dxos/ui-theme';
12
+
13
+ import { type TreeNode } from '../types';
14
+ import { type TreeLayoutSlots, defaultTreeLayoutSlots } from './slots';
15
+ import { useContainerSize } from './useContainerSize';
16
+
17
+ const TRANSITION_MS = 350;
18
+
19
+ /** A directed edge between two leaves of the hierarchy, identified by node id. */
20
+ export type BundleEdge = {
21
+ source: string;
22
+ target: string;
23
+ kind?: string;
24
+ };
25
+
26
+ export type HierarchicalEdgeBundlingProps = ThemedClassName<{
27
+ /** Hierarchical data; leaves are the connectable entities. */
28
+ data: TreeNode;
29
+ /** Edges between leaves (by id). Bundled through the hierarchy. */
30
+ edges?: BundleEdge[];
31
+ /** Label accessor for leaf nodes. */
32
+ label?: (d: TreeNode) => string;
33
+ /** Padding (in screen pixels) reserved around the radial layout. */
34
+ padding?: number;
35
+ /** Bundling tension; 0 = straight, 1 = maximally bundled. */
36
+ tension?: number;
37
+ slots?: TreeLayoutSlots;
38
+ /**
39
+ * Called when the user hovers a leaf node (with the event so callers can dispatch
40
+ * `DxAnchorActivate` for previews). Receives `null` on leave.
41
+ */
42
+ onNodeHover?: (node: TreeNode | null, event?: MouseEvent) => void;
43
+ }>;
44
+
45
+ /**
46
+ * Hierarchical edge bundling.
47
+ * https://observablehq.com/@d3/hierarchical-edge-bundling?intent=fork
48
+ *
49
+ * Leaves are placed on a circle, grouped by their parent in the hierarchy.
50
+ * Edges between leaves are drawn as bundled curves that route through their lowest common ancestor.
51
+ */
52
+ export const HierarchicalEdgeBundling = ({
53
+ classNames,
54
+ data,
55
+ edges = [],
56
+ label = (d) => d.label ?? d.id,
57
+ padding = 120,
58
+ tension = 0.85,
59
+ slots = defaultTreeLayoutSlots,
60
+ onNodeHover,
61
+ }: HierarchicalEdgeBundlingProps) => {
62
+ const svgRef = useRef<SVGSVGElement | null>(null);
63
+ const { setRef, width, height } = useContainerSize();
64
+
65
+ const root = useMemo(() => buildBundleHierarchy(data, edges), [data, edges]);
66
+
67
+ // Stable hover ref so the effect doesn't rebind handlers on every render.
68
+ const handleHoverRef = useRef<(node: TreeNode | null, event?: MouseEvent) => void>(() => {});
69
+ handleHoverRef.current = (node, event) => onNodeHover?.(node, event);
70
+
71
+ useEffect(() => {
72
+ if (!svgRef.current || !width || !height) {
73
+ return;
74
+ }
75
+
76
+ const radius = Math.max(0, Math.min(width, height) / 2 - padding);
77
+ renderBundling(svgRef.current, root, {
78
+ radius,
79
+ label,
80
+ slots,
81
+ tension,
82
+ onNodeHover: (n, e) => handleHoverRef.current(n, e),
83
+ });
84
+ }, [root, width, height, padding, tension, label, slots]);
85
+
86
+ return (
87
+ <div ref={setRef} className={mx('dx-expander relative', classNames)}>
88
+ {width > 0 && height > 0 && (
89
+ <svg
90
+ ref={svgRef}
91
+ xmlns='http://www.w3.org/2000/svg'
92
+ width={width}
93
+ height={height}
94
+ viewBox={`${-width / 2} ${-height / 2} ${width} ${height}`}
95
+ />
96
+ )}
97
+ </div>
98
+ );
99
+ };
100
+
101
+ type BundleHierarchy = HierarchyNode<TreeNode> & {
102
+ outgoing?: Array<[BundleHierarchy, BundleHierarchy, BundleEdge]>;
103
+ incoming?: Array<[BundleHierarchy, BundleHierarchy, BundleEdge]>;
104
+ pathEl?: SVGPathElement | null;
105
+ text?: SVGTextElement | null;
106
+ };
107
+
108
+ /**
109
+ * Build the bundling hierarchy from data + edges.
110
+ * Edges connect leaves (by id); the cluster layout places leaves on the circle.
111
+ */
112
+ const buildBundleHierarchy = (data: TreeNode, edges: BundleEdge[]): BundleHierarchy => {
113
+ const root = hierarchy<TreeNode>(data) as BundleHierarchy;
114
+ const byId = new Map<string, BundleHierarchy>();
115
+ for (const node of root.descendants() as BundleHierarchy[]) {
116
+ byId.set(node.data.id, node);
117
+ node.outgoing = [];
118
+ node.incoming = [];
119
+ }
120
+
121
+ for (const edge of edges) {
122
+ const source = byId.get(edge.source);
123
+ const target = byId.get(edge.target);
124
+ if (!source || !target || source === target) {
125
+ continue;
126
+ }
127
+ source.outgoing!.push([source, target, edge]);
128
+ target.incoming!.push([source, target, edge]);
129
+ }
130
+
131
+ return root;
132
+ };
133
+
134
+ type RenderOptions = {
135
+ radius: number;
136
+ tension: number;
137
+ label: (d: TreeNode) => string;
138
+ slots: TreeLayoutSlots;
139
+ onNodeHover: (node: TreeNode | null, event?: MouseEvent) => void;
140
+ };
141
+
142
+ const renderBundling = (svgElement: SVGSVGElement, root: BundleHierarchy, options: RenderOptions) => {
143
+ const { radius, tension, label, slots, onNodeHover } = options;
144
+ const svg = select(svgElement);
145
+
146
+ // Degenerate root (no descendants yet): clear any previously rendered bundle so a stale
147
+ // graph doesn't linger after the hierarchy empties out, then no-op.
148
+ if (!root.children?.length) {
149
+ svg.selectAll('g.dx-bundle-root').remove();
150
+ return;
151
+ }
152
+
153
+ cluster<TreeNode>().size([2 * Math.PI, radius])(root);
154
+
155
+ const g = svg.selectAll<SVGGElement, null>('g.dx-bundle-root').data([null]).join('g').classed('dx-bundle-root', true);
156
+
157
+ const linksLayer = g
158
+ .selectAll<SVGGElement, null>('g.dx-bundle-links')
159
+ .data([null])
160
+ .join('g')
161
+ .classed('dx-bundle-links', true);
162
+
163
+ const nodesLayer = g
164
+ .selectAll<SVGGElement, null>('g.dx-bundle-nodes')
165
+ .data([null])
166
+ .join('g')
167
+ .classed('dx-bundle-nodes', true);
168
+
169
+ const line = lineRadial<any>()
170
+ .curve(curveBundle.beta(tension))
171
+ .radius((d: any) => d.y)
172
+ .angle((d: any) => d.x);
173
+
174
+ // Each edge: route through the lowest common ancestor via hierarchy.path().
175
+ const leaves = root.leaves() as BundleHierarchy[];
176
+ const flatEdges = leaves.flatMap((leaf) => leaf.outgoing ?? []);
177
+
178
+ const paths = linksLayer
179
+ .selectAll<SVGPathElement, any>('path')
180
+ .data(flatEdges, (d: any) => `${d[0].data.id}->${d[1].data.id}`)
181
+ .join(
182
+ (enter) =>
183
+ enter
184
+ .append('path')
185
+ .attr('class', slots.path ?? '')
186
+ .attr('fill', 'none')
187
+ .attr('opacity', 0),
188
+ (update) => update,
189
+ (exit) =>
190
+ exit
191
+ .each(function () {
192
+ select(this).interrupt();
193
+ })
194
+ .transition()
195
+ .duration(TRANSITION_MS)
196
+ .attr('opacity', 0)
197
+ .remove(),
198
+ );
199
+
200
+ paths
201
+ .each(function (d: any) {
202
+ d[0].pathEl = this;
203
+ })
204
+ .transition()
205
+ .duration(TRANSITION_MS)
206
+ .attr('opacity', 1)
207
+ .attr('d', ([s, t]) => line(s.path(t)));
208
+
209
+ // Render leaf labels along the perimeter.
210
+ const labels = nodesLayer
211
+ .selectAll<SVGGElement, any>('g.dx-bundle-leaf')
212
+ .data(leaves, (d: any) => d.data.id)
213
+ .join(
214
+ (enter) => {
215
+ const ge = enter.append('g').classed('dx-bundle-leaf', true).attr('opacity', 0);
216
+ ge.append('text').attr('dy', '0.32em').attr('paint-order', 'stroke').style('cursor', 'pointer');
217
+ return ge;
218
+ },
219
+ (update) => update,
220
+ (exit) =>
221
+ exit
222
+ .each(function () {
223
+ select(this).interrupt();
224
+ })
225
+ .transition()
226
+ .duration(TRANSITION_MS)
227
+ .attr('opacity', 0)
228
+ .remove(),
229
+ );
230
+
231
+ labels
232
+ .transition()
233
+ .duration(TRANSITION_MS)
234
+ .attr('opacity', 1)
235
+ .attr('transform', (d: any) => `rotate(${(d.x * 180) / Math.PI - 90}) translate(${d.y},0)`);
236
+
237
+ labels
238
+ .select<SVGTextElement>('text')
239
+ .attr('class', slots.text ?? '')
240
+ .attr('x', (d: any) => (d.x < Math.PI ? 6 : -6))
241
+ .attr('text-anchor', (d: any) => (d.x < Math.PI ? 'start' : 'end'))
242
+ .attr('transform', (d: any) => (d.x >= Math.PI ? 'rotate(180)' : null))
243
+ .each(function (d: BundleHierarchy) {
244
+ d.text = this;
245
+ })
246
+ .text((d: any) => label(d.data))
247
+ .on('pointerenter', function (event: MouseEvent, d: BundleHierarchy) {
248
+ onNodeHover(d.data, event);
249
+ hover(linksLayer, leaves, d, true);
250
+ })
251
+ .on('pointerleave', function (event: MouseEvent, d: BundleHierarchy) {
252
+ onNodeHover(null);
253
+ hover(linksLayer, leaves, d, false);
254
+ });
255
+ };
256
+
257
+ const hover = (linksLayer: any, leaves: BundleHierarchy[], focused: BundleHierarchy, on: boolean) => {
258
+ const outgoing = new Set((focused.outgoing ?? []).map(([, t]) => t));
259
+ const incoming = new Set((focused.incoming ?? []).map(([s]) => s));
260
+
261
+ (linksLayer.selectAll('path') as any)
262
+ .classed('dx-bundle-out', (d: any) => on && d[0] === focused)
263
+ .classed('dx-bundle-in', (d: any) => on && d[1] === focused)
264
+ .classed('dx-bundle-dim', (d: any) => on && d[0] !== focused && d[1] !== focused);
265
+
266
+ for (const leaf of leaves) {
267
+ if (!leaf.text) {
268
+ continue;
269
+ }
270
+ select(leaf.text)
271
+ .classed('dx-bundle-focused', on && leaf === focused)
272
+ .classed('dx-bundle-out-text', on && outgoing.has(leaf))
273
+ .classed('dx-bundle-in-text', on && incoming.has(leaf));
274
+ }
275
+ };
276
+
277
+ export default HierarchicalEdgeBundling;