@dxos/react-ui-mosaic 0.3.3-main.ff5734a → 0.3.3
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/dist/lib/browser/index.mjs +65 -88
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/mosaic/Container.d.ts +1 -6
- package/dist/types/src/mosaic/Container.d.ts.map +1 -1
- package/dist/types/src/mosaic/Mosaic.d.ts +1 -2
- package/dist/types/src/mosaic/Mosaic.d.ts.map +1 -1
- package/dist/types/src/mosaic/hooks/index.d.ts +1 -0
- package/dist/types/src/mosaic/hooks/index.d.ts.map +1 -1
- package/dist/types/src/mosaic/hooks/useItemsWithOrigin.d.ts +3 -0
- package/dist/types/src/mosaic/hooks/useItemsWithOrigin.d.ts.map +1 -0
- package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts +1 -2
- package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts.map +1 -1
- package/dist/types/src/testing/generator.d.ts +0 -7
- package/dist/types/src/testing/generator.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +0 -1
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/views/Tree/Graph.stories.d.ts +1 -1
- package/dist/types/src/views/Tree/Tree.d.ts +2 -3
- package/dist/types/src/views/Tree/Tree.d.ts.map +1 -1
- package/dist/types/src/views/Tree/Tree.stories.d.ts +1 -1
- package/dist/types/src/views/Tree/testing.d.ts.map +1 -1
- package/package.json +13 -14
- package/src/mosaic/Container.tsx +0 -9
- package/src/mosaic/Mosaic.ts +0 -3
- package/src/mosaic/hooks/index.ts +1 -0
- package/src/mosaic/hooks/useItemsWithOrigin.ts +33 -0
- package/src/mosaic/hooks/useItemsWithPreview.ts +24 -55
- package/src/testing/generator.ts +2 -5
- package/src/testing/index.ts +0 -1
- package/src/views/Tree/Tree.tsx +11 -20
- package/src/views/Tree/testing.tsx +4 -24
- package/dist/types/src/testing/next-fractional-index.d.ts +0 -5
- package/dist/types/src/testing/next-fractional-index.d.ts.map +0 -1
- package/src/testing/next-fractional-index.ts +0 -36
package/src/views/Tree/Tree.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, { forwardRef
|
|
5
|
+
import React, { forwardRef } from 'react';
|
|
6
6
|
|
|
7
7
|
import { Card, Tree as TreeComponent, TreeItem as TreeItemComponent } from '@dxos/react-ui';
|
|
8
8
|
import { dropRing, mx } from '@dxos/react-ui-theme';
|
|
@@ -15,8 +15,7 @@ import {
|
|
|
15
15
|
useContainer,
|
|
16
16
|
useMosaic,
|
|
17
17
|
Path,
|
|
18
|
-
|
|
19
|
-
type MosaicCompareDataItem,
|
|
18
|
+
useItemsWithOrigin,
|
|
20
19
|
} from '../../mosaic';
|
|
21
20
|
|
|
22
21
|
// TODO(burdon): Tree data model that provides a pure abstraction of the plugin Graph.
|
|
@@ -27,7 +26,6 @@ import {
|
|
|
27
26
|
export type TreeProps<TData extends MosaicDataItem = TreeData> = MosaicContainerProps<TData, number> & {
|
|
28
27
|
items?: TData[];
|
|
29
28
|
debug?: boolean;
|
|
30
|
-
compare?: MosaicCompareDataItem;
|
|
31
29
|
};
|
|
32
30
|
|
|
33
31
|
export type TreeData = {
|
|
@@ -37,7 +35,7 @@ export type TreeData = {
|
|
|
37
35
|
};
|
|
38
36
|
|
|
39
37
|
// TODO(burdon): Make generic (and forwardRef).
|
|
40
|
-
export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], debug
|
|
38
|
+
export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], debug }: TreeProps) => {
|
|
41
39
|
return (
|
|
42
40
|
<Mosaic.Container
|
|
43
41
|
{...{
|
|
@@ -46,7 +44,6 @@ export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], deb
|
|
|
46
44
|
Component,
|
|
47
45
|
onOver,
|
|
48
46
|
onDrop,
|
|
49
|
-
compare,
|
|
50
47
|
}}
|
|
51
48
|
>
|
|
52
49
|
<TreeRoot items={items} />
|
|
@@ -55,16 +52,13 @@ export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], deb
|
|
|
55
52
|
};
|
|
56
53
|
|
|
57
54
|
const TreeRoot = ({ items }: { items: TreeData[] }) => {
|
|
58
|
-
const { id, Component
|
|
59
|
-
const
|
|
60
|
-
return compare ? [...items].sort(compare) : items;
|
|
61
|
-
}, [items, compare]);
|
|
62
|
-
const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path: id, strategy: 'layout-stable' });
|
|
55
|
+
const { id, Component } = useContainer();
|
|
56
|
+
const itemsWithOrigin = useItemsWithOrigin(id, items);
|
|
63
57
|
|
|
64
58
|
return (
|
|
65
59
|
<TreeComponent.Root classNames='flex flex-col'>
|
|
66
|
-
<Mosaic.SortableContext id={id} items={
|
|
67
|
-
{
|
|
60
|
+
<Mosaic.SortableContext id={id} items={itemsWithOrigin} direction='vertical'>
|
|
61
|
+
{itemsWithOrigin.map((item, index) => (
|
|
68
62
|
<TreeItemComponent.Root key={item.id} collapsible defaultOpen>
|
|
69
63
|
<Mosaic.SortableTile item={item} path={id} position={index} Component={Component!} />
|
|
70
64
|
</TreeItemComponent.Root>
|
|
@@ -104,16 +98,13 @@ const TreeItem: MosaicTileComponent<TreeData> = forwardRef(
|
|
|
104
98
|
|
|
105
99
|
const TreeBranch = ({ path, items }: { path: string; items: TreeData[] }) => {
|
|
106
100
|
const { operation, overItem } = useMosaic();
|
|
107
|
-
const { Component
|
|
108
|
-
const
|
|
109
|
-
return compare ? [...items].sort(compare) : items;
|
|
110
|
-
}, [items, compare]);
|
|
111
|
-
const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path, strategy: 'layout-stable' });
|
|
101
|
+
const { Component } = useContainer();
|
|
102
|
+
const itemsWithOrigin = useItemsWithOrigin(path, items);
|
|
112
103
|
|
|
113
104
|
return (
|
|
114
105
|
<TreeItemComponent.Body className='pis-4'>
|
|
115
|
-
<Mosaic.SortableContext id={path} items={
|
|
116
|
-
{
|
|
106
|
+
<Mosaic.SortableContext id={path} items={itemsWithOrigin} direction='vertical'>
|
|
107
|
+
{itemsWithOrigin.map((child, index) => (
|
|
117
108
|
<TreeComponent.Branch key={child.id}>
|
|
118
109
|
<TreeItemComponent.Root collapsible defaultOpen>
|
|
119
110
|
<Mosaic.SortableTile
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
import { faker } from '@faker-js/faker';
|
|
6
6
|
import React, { useCallback, useState } from 'react';
|
|
7
7
|
|
|
8
|
-
import { type Graph, GraphBuilder
|
|
8
|
+
import { type Graph, GraphBuilder } from '@dxos/app-graph';
|
|
9
9
|
import { buildGraph } from '@dxos/app-graph/testing';
|
|
10
10
|
import { arrayMove } from '@dxos/util';
|
|
11
11
|
|
|
12
12
|
import { Tree, type TreeData, type TreeProps } from './Tree';
|
|
13
13
|
import { type MosaicDropEvent, type MosaicMoveEvent, Path, type MosaicOperation } from '../../mosaic';
|
|
14
|
-
import { TestObjectGenerator
|
|
14
|
+
import { TestObjectGenerator } from '../../testing';
|
|
15
15
|
|
|
16
16
|
const fake = faker.helpers.fake;
|
|
17
17
|
|
|
@@ -104,18 +104,6 @@ export const createGraph = () => {
|
|
|
104
104
|
return buildGraph(new GraphBuilder().build(), 'tree', content);
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
-
const graphNodeCompare = (a: Node, b: Node) => {
|
|
108
|
-
if (a.properties.index && b.properties.index) {
|
|
109
|
-
if (a.properties.index < b.properties.index) {
|
|
110
|
-
return -1;
|
|
111
|
-
} else if (a.properties.index > b.properties.index) {
|
|
112
|
-
return 1;
|
|
113
|
-
}
|
|
114
|
-
return 0;
|
|
115
|
-
}
|
|
116
|
-
return 0;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
107
|
export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; graph?: Graph; debug: boolean }) => {
|
|
120
108
|
const handleDrop = ({ operation, active, over }: MosaicDropEvent<number>) => {
|
|
121
109
|
// Moving within the tree.
|
|
@@ -134,8 +122,7 @@ export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; gr
|
|
|
134
122
|
operation === 'rearrange'
|
|
135
123
|
) {
|
|
136
124
|
// This is a rearrange operation
|
|
137
|
-
|
|
138
|
-
activeNode.properties.index = nextIndex ?? 'a0';
|
|
125
|
+
console.warn('[react-ui-mosaic]', 'Tree', 'rearrange', 'needs implementation');
|
|
139
126
|
} else if (activeNode && activeParent && overParent && operation === 'transfer') {
|
|
140
127
|
activeParent.removeNode(active.item.id);
|
|
141
128
|
overNode.addNode('tree', { ...activeNode });
|
|
@@ -146,13 +133,6 @@ export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; gr
|
|
|
146
133
|
const handleOver = (): MosaicOperation => 'transfer';
|
|
147
134
|
|
|
148
135
|
return (
|
|
149
|
-
<Tree
|
|
150
|
-
id={id}
|
|
151
|
-
items={graph.root.children as TreeData[]}
|
|
152
|
-
onDrop={handleDrop}
|
|
153
|
-
onOver={handleOver}
|
|
154
|
-
debug={debug}
|
|
155
|
-
compare={graphNodeCompare}
|
|
156
|
-
/>
|
|
136
|
+
<Tree id={id} items={graph.root.children as TreeData[]} onDrop={handleDrop} onOver={handleOver} debug={debug} />
|
|
157
137
|
);
|
|
158
138
|
};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { type UniqueIdentifier } from '@dnd-kit/core';
|
|
2
|
-
import { type Node } from '@dxos/app-graph';
|
|
3
|
-
export declare const nextRearrangeIndex: (nodes: Node[], activeId: UniqueIdentifier, overId?: UniqueIdentifier) => string | null;
|
|
4
|
-
export declare const nextCopyIndex: (nodes: Node[], overId?: UniqueIdentifier) => string;
|
|
5
|
-
//# sourceMappingURL=next-fractional-index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"next-fractional-index.d.ts","sourceRoot":"","sources":["../../../../src/testing/next-fractional-index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAGtD,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,eAAO,MAAM,kBAAkB,UAAW,IAAI,EAAE,YAAY,gBAAgB,WAAW,gBAAgB,kBAYtG,CAAC;AAEF,eAAO,MAAM,aAAa,UAAW,IAAI,EAAE,WAAW,gBAAgB,WAYrE,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type UniqueIdentifier } from '@dnd-kit/core';
|
|
6
|
-
import { getIndexAbove, getIndexBelow, getIndexBetween } from '@tldraw/indices';
|
|
7
|
-
|
|
8
|
-
import { type Node } from '@dxos/app-graph';
|
|
9
|
-
|
|
10
|
-
export const nextRearrangeIndex = (nodes: Node[], activeId: UniqueIdentifier, overId?: UniqueIdentifier) => {
|
|
11
|
-
const overOrderIndex = nodes.length > 0 ? nodes.findIndex(({ id }) => id === overId) : -1;
|
|
12
|
-
if (overOrderIndex >= 0) {
|
|
13
|
-
const activeOrderIndex = nodes.findIndex(({ id }) => id === activeId);
|
|
14
|
-
return overOrderIndex < 1
|
|
15
|
-
? getIndexBelow(nodes[overOrderIndex].properties.index)
|
|
16
|
-
: activeOrderIndex < overOrderIndex
|
|
17
|
-
? getIndexBetween(nodes[overOrderIndex].properties.index, nodes[overOrderIndex + 1]?.properties.index)
|
|
18
|
-
: getIndexBetween(nodes[overOrderIndex - 1].properties.index, nodes[overOrderIndex].properties.index);
|
|
19
|
-
} else {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const nextCopyIndex = (nodes: Node[], overId?: UniqueIdentifier) => {
|
|
25
|
-
const overOrderIndex = nodes.length > 0 ? nodes.findIndex(({ id }) => id === overId) : -1;
|
|
26
|
-
const previewOrderIndex = nodes.findIndex(({ id }) => id.startsWith('preview--'));
|
|
27
|
-
if (overOrderIndex >= 0) {
|
|
28
|
-
return overOrderIndex < 1
|
|
29
|
-
? getIndexBelow(nodes[overOrderIndex].properties.index)
|
|
30
|
-
: previewOrderIndex < overOrderIndex && previewOrderIndex >= 0
|
|
31
|
-
? getIndexBetween(nodes[overOrderIndex].properties.index, nodes[overOrderIndex + 1]?.properties.index)
|
|
32
|
-
: getIndexBetween(nodes[overOrderIndex - 1].properties.index, nodes[overOrderIndex]?.properties.index);
|
|
33
|
-
} else {
|
|
34
|
-
return nodes.length ? getIndexAbove(nodes[nodes.length - 1].properties.index) : 'a0';
|
|
35
|
-
}
|
|
36
|
-
};
|