@dxos/react-ui-mosaic 0.3.3-main.88f4df1 → 0.3.3-main.971c74a
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 +90 -46
- 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 +9 -1
- package/dist/types/src/mosaic/Container.d.ts.map +1 -1
- package/dist/types/src/mosaic/Root.d.ts.map +1 -1
- package/dist/types/src/mosaic/hooks/index.d.ts +0 -1
- package/dist/types/src/mosaic/hooks/index.d.ts.map +1 -1
- package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts +2 -1
- package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts.map +1 -1
- package/dist/types/src/testing/generator.d.ts +4 -4
- 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/Grid/Grid.d.ts +1 -1
- package/dist/types/src/views/Grid/Grid.d.ts.map +1 -1
- package/dist/types/src/views/Grid/Grid.stories.d.ts +1 -1
- package/dist/types/src/views/Stack/Columns.stories.d.ts +1 -1
- package/dist/types/src/views/Stack/Columns.stories.d.ts.map +1 -1
- package/dist/types/src/views/Tree/testing.d.ts +1 -1
- package/dist/types/src/views/Tree/testing.d.ts.map +1 -1
- package/package.json +14 -15
- package/src/mosaic/Container.tsx +10 -2
- package/src/mosaic/Root.tsx +1 -2
- package/src/mosaic/hooks/index.ts +0 -1
- package/src/mosaic/hooks/useItemsWithPreview.ts +56 -20
- package/src/testing/generator.ts +9 -9
- package/src/testing/index.ts +0 -1
- package/src/views/Grid/Grid.tsx +2 -0
- package/src/views/Kanban/Kanban.tsx +1 -1
- package/src/views/Stack/Columns.stories.tsx +1 -1
- package/src/views/Stack/testing.tsx +1 -1
- package/src/views/Tree/Tree.tsx +17 -11
- package/src/views/Tree/testing.tsx +13 -12
- package/dist/types/src/mosaic/hooks/useSortedItems.d.ts +0 -6
- package/dist/types/src/mosaic/hooks/useSortedItems.d.ts.map +0 -1
- 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/mosaic/hooks/useSortedItems.ts +0 -14
- 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 } from 'react';
|
|
5
|
+
import React, { forwardRef, useMemo } 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,7 +15,7 @@ import {
|
|
|
15
15
|
useContainer,
|
|
16
16
|
useMosaic,
|
|
17
17
|
Path,
|
|
18
|
-
|
|
18
|
+
useItemsWithPreview,
|
|
19
19
|
type MosaicCompareDataItem,
|
|
20
20
|
} from '../../mosaic';
|
|
21
21
|
|
|
@@ -55,13 +55,16 @@ export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], deb
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
const TreeRoot = ({ items }: { items: TreeData[] }) => {
|
|
58
|
-
const { id, Component } = useContainer();
|
|
59
|
-
const sortedItems =
|
|
58
|
+
const { id, Component, compare } = useContainer();
|
|
59
|
+
const sortedItems = useMemo(() => {
|
|
60
|
+
return compare ? [...items].sort(compare) : items;
|
|
61
|
+
}, [items, compare]);
|
|
62
|
+
const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path: id, strategy: 'layout-stable' });
|
|
60
63
|
|
|
61
64
|
return (
|
|
62
65
|
<TreeComponent.Root classNames='flex flex-col'>
|
|
63
|
-
<Mosaic.SortableContext id={id} items={
|
|
64
|
-
{
|
|
66
|
+
<Mosaic.SortableContext id={id} items={itemsWithPreview} direction='vertical'>
|
|
67
|
+
{itemsWithPreview.map((item, index) => (
|
|
65
68
|
<TreeItemComponent.Root key={item.id} collapsible defaultOpen>
|
|
66
69
|
<Mosaic.SortableTile item={item} path={id} position={index} Component={Component!} />
|
|
67
70
|
</TreeItemComponent.Root>
|
|
@@ -101,13 +104,16 @@ const TreeItem: MosaicTileComponent<TreeData> = forwardRef(
|
|
|
101
104
|
|
|
102
105
|
const TreeBranch = ({ path, items }: { path: string; items: TreeData[] }) => {
|
|
103
106
|
const { operation, overItem } = useMosaic();
|
|
104
|
-
const { Component } = useContainer();
|
|
105
|
-
const sortedItems =
|
|
107
|
+
const { Component, compare } = useContainer();
|
|
108
|
+
const sortedItems = useMemo(() => {
|
|
109
|
+
return compare ? [...items].sort(compare) : items;
|
|
110
|
+
}, [items, compare]);
|
|
111
|
+
const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path, strategy: 'layout-stable' });
|
|
106
112
|
|
|
107
113
|
return (
|
|
108
114
|
<TreeItemComponent.Body className='pis-4'>
|
|
109
|
-
<Mosaic.SortableContext id={path} items={
|
|
110
|
-
{
|
|
115
|
+
<Mosaic.SortableContext id={path} items={itemsWithPreview} direction='vertical'>
|
|
116
|
+
{itemsWithPreview.map((child, index) => (
|
|
111
117
|
<TreeComponent.Branch key={child.id}>
|
|
112
118
|
<TreeItemComponent.Root collapsible defaultOpen>
|
|
113
119
|
<Mosaic.SortableTile
|
|
@@ -116,7 +122,7 @@ const TreeBranch = ({ path, items }: { path: string; items: TreeData[] }) => {
|
|
|
116
122
|
position={index}
|
|
117
123
|
Component={Component!}
|
|
118
124
|
isOver={
|
|
119
|
-
overItem?.path === Path.create(path, child.id) && (operation === '
|
|
125
|
+
overItem?.path === Path.create(path, child.id) && (operation === 'transfer' || operation === 'copy')
|
|
120
126
|
}
|
|
121
127
|
/>
|
|
122
128
|
</TreeItemComponent.Root>
|
|
@@ -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, type Node } from '@
|
|
9
|
-
import { buildGraph } from '@
|
|
8
|
+
import { type Graph, GraphBuilder, type Node } from '@dxos/app-graph';
|
|
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
|
-
import { type MosaicDropEvent, type MosaicMoveEvent, Path } from '../../mosaic';
|
|
14
|
-
import { TestObjectGenerator
|
|
13
|
+
import { type MosaicDropEvent, type MosaicMoveEvent, Path, type MosaicOperation } from '../../mosaic';
|
|
14
|
+
import { TestObjectGenerator } from '../../testing';
|
|
15
15
|
|
|
16
16
|
const fake = faker.helpers.fake;
|
|
17
17
|
|
|
@@ -47,8 +47,8 @@ export const DemoTree = ({ id = 'tree', initialItems, types, debug }: DemoTreePr
|
|
|
47
47
|
}),
|
|
48
48
|
);
|
|
49
49
|
|
|
50
|
-
const handleOver = useCallback(({
|
|
51
|
-
return
|
|
50
|
+
const handleOver = useCallback(({ over }: MosaicMoveEvent<number>) => {
|
|
51
|
+
return over && Path.hasChild(id, over.path) ? 'transfer' : 'reject';
|
|
52
52
|
}, []);
|
|
53
53
|
|
|
54
54
|
// NOTE: Does not handle deep operations.
|
|
@@ -67,8 +67,8 @@ export const DemoTree = ({ id = 'tree', initialItems, types, debug }: DemoTreePr
|
|
|
67
67
|
if (Path.last(Path.parent(active.path)) === item.id) {
|
|
68
68
|
children.splice(active.position!, 1);
|
|
69
69
|
}
|
|
70
|
-
if (Path.last(
|
|
71
|
-
children.
|
|
70
|
+
if (Path.last(over.path) === item.id) {
|
|
71
|
+
children.push(active.item as TreeData);
|
|
72
72
|
}
|
|
73
73
|
return { ...item, children };
|
|
74
74
|
}),
|
|
@@ -117,7 +117,6 @@ const graphNodeCompare = (a: Node, b: Node) => {
|
|
|
117
117
|
};
|
|
118
118
|
|
|
119
119
|
export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; graph?: Graph; debug: boolean }) => {
|
|
120
|
-
// TODO(wittjosiah): This graph does not handle order currently.
|
|
121
120
|
const handleDrop = ({ operation, active, over }: MosaicDropEvent<number>) => {
|
|
122
121
|
// Moving within the tree.
|
|
123
122
|
if (Path.hasDescendent(id, active.path) && Path.hasDescendent(id, over.path)) {
|
|
@@ -135,20 +134,22 @@ export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; gr
|
|
|
135
134
|
operation === 'rearrange'
|
|
136
135
|
) {
|
|
137
136
|
// This is a rearrange operation
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
} else if (activeNode && activeParent && overParent && operation === 'adopt') {
|
|
137
|
+
console.warn('[react-ui-mosaic]', 'Tree', 'rearrange', 'needs implementation');
|
|
138
|
+
} else if (activeNode && activeParent && overParent && operation === 'transfer') {
|
|
141
139
|
activeParent.removeNode(active.item.id);
|
|
142
140
|
overNode.addNode('tree', { ...activeNode });
|
|
143
141
|
}
|
|
144
142
|
}
|
|
145
143
|
};
|
|
146
144
|
|
|
145
|
+
const handleOver = (): MosaicOperation => 'transfer';
|
|
146
|
+
|
|
147
147
|
return (
|
|
148
148
|
<Tree
|
|
149
149
|
id={id}
|
|
150
150
|
items={graph.root.children as TreeData[]}
|
|
151
151
|
onDrop={handleDrop}
|
|
152
|
+
onOver={handleOver}
|
|
152
153
|
debug={debug}
|
|
153
154
|
compare={graphNodeCompare}
|
|
154
155
|
/>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSortedItems.d.ts","sourceRoot":"","sources":["../../../../../src/mosaic/hooks/useSortedItems.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,cAAc,+CAG1B,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { type UniqueIdentifier } from '@dnd-kit/core';
|
|
2
|
-
import { type Node } from '@braneframe/plugin-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,0BAA0B,CAAC;AAErD,eAAO,MAAM,kBAAkB,UAAW,IAAI,EAAE,YAAY,gBAAgB,WAAW,gBAAgB,kBAYtG,CAAC;AAEF,eAAO,MAAM,aAAa,UAAW,IAAI,EAAE,WAAW,gBAAgB,WAYrE,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { useContainer } from './useContainer';
|
|
6
|
-
import { type MosaicDataItem } from '../types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Returns a sorted collection of items if a compare function is provided.
|
|
10
|
-
*/
|
|
11
|
-
export const useSortedItems = <T extends MosaicDataItem>(items: T[]): T[] => {
|
|
12
|
-
const { compare } = useContainer();
|
|
13
|
-
return compare ? [...items].sort(compare) : items;
|
|
14
|
-
};
|
|
@@ -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 '@braneframe/plugin-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
|
-
};
|