@canvas-components/react-drag-any 1.0.4 → 2.0.1
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/README.md +71 -93
- package/dist/index.cjs +461 -1693
- package/dist/index.d.cts +50 -174
- package/dist/index.d.ts +50 -174
- package/dist/index.js +463 -1685
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,113 +1,91 @@
|
|
|
1
1
|
# react-drag-any
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
使用 `Drag.Surface` 挂载一张画布。用 `UI.List` 排列卡片,用 `UI.Grid` 声明行列。页面根据可用宽度决定每行容量。导入组件时自动加载默认样式。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 受控队列
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
下面的组件将工单 ID 作为排序键。释放时按目标锚点修改业务数组,锚点失效时返回 `false` 取消移动。
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
```tsx
|
|
10
|
+
import { Drag, useDragHistory } from "@canvas-components/react-drag-any";
|
|
11
|
+
import { UI } from "@canvas-components/react-canvas-ui";
|
|
12
|
+
|
|
13
|
+
type Task = { id: string; title: string };
|
|
14
|
+
|
|
15
|
+
export function TaskQueue({ initial }: { initial: Task[] }) {
|
|
16
|
+
const history = useDragHistory(initial);
|
|
17
|
+
const tasks = history.value;
|
|
18
|
+
return (
|
|
19
|
+
<Drag.Surface<Task>
|
|
20
|
+
id="tasks"
|
|
21
|
+
history={history}
|
|
22
|
+
onMoveRequest={(request) => {
|
|
23
|
+
const rest = tasks.filter((task) => task.id !== request.itemId);
|
|
24
|
+
const before =
|
|
25
|
+
"beforeId" in request.anchor ? request.anchor.beforeId : undefined;
|
|
26
|
+
const index =
|
|
27
|
+
before === undefined
|
|
28
|
+
? rest.length
|
|
29
|
+
: rest.findIndex((task) => task.id === before);
|
|
30
|
+
if (index < 0 || rest.length === tasks.length) return false;
|
|
31
|
+
rest.splice(index, 0, request.data);
|
|
32
|
+
history.commit(rest, request.itemId);
|
|
33
|
+
return true;
|
|
34
|
+
}}
|
|
35
|
+
>
|
|
36
|
+
<UI.div style={{ height: "100%", overflowY: "auto" }}>
|
|
37
|
+
<Drag.Target id="queue">
|
|
38
|
+
<UI.List items={tasks} itemKey="id" itemHeight={48}>
|
|
39
|
+
{(task) => (
|
|
40
|
+
<Drag.Source id={task.id} data={task}>
|
|
41
|
+
<UI.div>{task.title}</UI.div>
|
|
42
|
+
</Drag.Source>
|
|
43
|
+
)}
|
|
44
|
+
</UI.List>
|
|
45
|
+
</Drag.Target>
|
|
46
|
+
</UI.div>
|
|
47
|
+
</Drag.Surface>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
泳道和稀疏矩阵在每个可投放位置放置 `Drag.Target`,其直接子节点为 `UI.List`。矩阵可用 `{ rowId, columnId }` 标识位置。多张画布通过相同的 `group` 接收跨画布拖放,由源画布的回调同步更新两侧业务数据。
|
|
53
|
+
|
|
54
|
+
自动换行的单卡网格支持整卡占位。给每个位置的 `Drag.Target` 传入 `slot={{ index, layout }}`。同一网格共享一个 `layout` 对象,其中 `length` 是工单总数,`position(index)` 按当前列数、卡片尺寸和间距返回相对网格原点的坐标。拖动时,起点到落点之间的卡片按顺序移动一格;末行空位对应追加到队尾。
|
|
55
|
+
|
|
56
|
+
卡片内容可直接写 JSX,也可通过 `<UI.Content value={content} />` 嵌入 Canvas UI 内容文档。`Drag.Source` 支持 `disabled` 和 `disabledStyle`,目标的 `canDrop` 可以按锚点拒绝插入。
|
|
57
|
+
|
|
58
|
+
## 拖拽边缘滚动
|
|
10
59
|
|
|
11
|
-
|
|
60
|
+
指针进入滚动容器的边缘并停留时,容器会持续滚动。沿同一边停留时,速度在 1.5 秒内从每秒 480 像素逐渐提高到 1440 像素。指针越过视口或画布边界后,原容器会沿该方向继续加速滚动,直到滚到尽头或松手。回到视口中部会停止滚动,换向或切换滚动容器后重新起速。在画布外松手会取消投放。角落的横纵两轴独立加速。固定表头、行头和空白处也可以触发滚动。使用 `autoScrollEdge={32}` 可将四边的触发距离设为 32 CSS 像素,也可按边设置。省略的边默认 16 像素,设为 0 可关闭该边。
|
|
12
61
|
|
|
13
62
|
```tsx
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
ReactDragAny,
|
|
17
|
-
type DragJSXRole,
|
|
18
|
-
} from "@canvas-components/react-drag-any";
|
|
19
|
-
|
|
20
|
-
const roles = new Map<string, DragJSXRole<{ order: string }>>([
|
|
21
|
-
["device", { kind: "region", id: "device-a" }],
|
|
22
|
-
["title", { kind: "header" }],
|
|
23
|
-
["jobs", { kind: "items" }],
|
|
24
|
-
["order", { kind: "card", id: "job-1", data: { order: "WO-001" } }],
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
<ReactDragAny
|
|
63
|
+
<Drag.Surface
|
|
28
64
|
id="schedule"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
annotations={{
|
|
32
|
-
topHeight: 32,
|
|
33
|
-
renderCorner: () => [
|
|
34
|
-
{
|
|
35
|
-
kind: "content",
|
|
36
|
-
rect: { x: 8, y: 4, width: 96, height: 24 },
|
|
37
|
-
content: "设备",
|
|
38
|
-
},
|
|
39
|
-
],
|
|
40
|
-
renderTop: ({ top, viewport }) => [
|
|
41
|
-
{
|
|
42
|
-
kind: "content",
|
|
43
|
-
rect: { x: 8, y: 4, width: top.width - 8, height: 24 },
|
|
44
|
-
content: `时间轴 · 偏移 ${viewport.scrollLeft}px`,
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
renderOverlay: ({ body, pointer }) =>
|
|
48
|
-
pointer &&
|
|
49
|
-
pointer.x >= body.x &&
|
|
50
|
-
pointer.x < body.x + body.width &&
|
|
51
|
-
pointer.y >= body.y &&
|
|
52
|
-
pointer.y < body.y + body.height
|
|
53
|
-
? [
|
|
54
|
-
{
|
|
55
|
-
kind: "line",
|
|
56
|
-
props: {
|
|
57
|
-
x1: pointer.x - body.x,
|
|
58
|
-
x2: pointer.x - body.x,
|
|
59
|
-
y1: 0,
|
|
60
|
-
y2: body.height,
|
|
61
|
-
stroke: "#2963ae",
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
]
|
|
65
|
-
: [],
|
|
66
|
-
}}
|
|
65
|
+
onMoveRequest={move}
|
|
66
|
+
autoScrollEdge={{ left: rowHeaderWidth + 16, top: columnHeaderHeight + 16 }}
|
|
67
67
|
>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
<UI.Col key="title" style={{ width: 112 }}>
|
|
71
|
-
<UI.Text>设备 A</UI.Text>
|
|
72
|
-
</UI.Col>
|
|
73
|
-
<UI.Row key="jobs" gap={8} style={{ flex: 1 }}>
|
|
74
|
-
<UI.Col key="order" style={{ width: 220, padding: 8 }}>
|
|
75
|
-
<UI.Text>工单 A</UI.Text>
|
|
76
|
-
</UI.Col>
|
|
77
|
-
</UI.Row>
|
|
78
|
-
</UI.Row>
|
|
79
|
-
</UI.Col>
|
|
80
|
-
</ReactDragAny>;
|
|
68
|
+
{layout}
|
|
69
|
+
</Drag.Surface>
|
|
81
70
|
```
|
|
82
71
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
`annotations` 的回调以同一视口读取固定顶部、角头和正文。返回项的坐标相对所处分区,指针坐标相对宿主。正文覆盖项只参与绘制。可用 `refreshIntervalMs` 刷新当前时间线等依赖时钟的内容,覆盖帧复用上一帧正文及命中数据。
|
|
72
|
+
## 撤销和重做
|
|
86
73
|
|
|
87
|
-
|
|
74
|
+
将同一个 `useDragHistory` 实例传给 `Drag.Surface` 的 `history` 属性。业务校验通过后调用 `history.commit(next, request.itemId)` 保存新的不可变状态。跨画布移动时,将两个画布的顺序保存在同一份状态中,一次提交即可一起撤销或重做。
|
|
88
75
|
|
|
89
|
-
|
|
76
|
+
```tsx
|
|
77
|
+
<button disabled={!history.canUndo} onClick={history.undo}>撤销</button>
|
|
78
|
+
<button disabled={!history.canRedo} onClick={history.redo}>重做</button>
|
|
79
|
+
```
|
|
90
80
|
|
|
91
|
-
|
|
81
|
+
画布获得焦点后,Ctrl / ⌘ + Z 撤销,Ctrl / ⌘ + Shift + Z 或 Ctrl + Y 重做。回放以完整浮层移动该 ID 对应的卡片,其余可见卡片同步让位,并遵循系统减少动态效果的偏好。新移动会清除重做记录。默认保留最近 50 次提交,可通过第二个参数调整;重新加载或发布数据时可用 `history.reset(next)` 设置新的起点。
|
|
92
82
|
|
|
93
|
-
|
|
83
|
+
## 破坏性变更
|
|
94
84
|
|
|
95
|
-
|
|
85
|
+
从旧版本升级时,将 `ReactDragAny` 替换为 `Drag.Surface`。原先使用 `ReactDragAnyGroup` 的页面为相关画布指定相同的 `group`。原先传入 `DragDocument`、`document`、`store` 或手工布局的页面改为在业务组件维护数组,并通过 `onMoveRequest` 提交排序。
|
|
96
86
|
|
|
97
|
-
|
|
87
|
+
`Queue`、`Lanes`、`Lane`、`Flow`、`SparseGrid`、`GridSlot` 和 `Card` 已移除。用 Canvas UI 容器、`UI.List` 和 `UI.Grid` 表达布局。将 `getDragRole` 和 `dragLayout` 替换为模板中的 `Drag.Source` 和集合外的 `Drag.Target`。原先调用 `compileJSX` 或 `createContentBindings` 的页面可直接把内容节点写入声明树,画布负责注册和释放事件。
|
|
98
88
|
|
|
99
|
-
|
|
89
|
+
对于包含大量虚拟列表的跨板页面,可在每个画板传入 `hasItem={id => cardsById.has(id)}`(原生选项为 `hasItem`)。引擎会用这个索引检查目标画板内的重复 ID,包括尚未进入窗口的列表。
|
|
100
90
|
|
|
101
|
-
|
|
102
|
-
<ReactDragAny
|
|
103
|
-
id="jobs"
|
|
104
|
-
dragLayout="queue"
|
|
105
|
-
getDragRole={(key) => key === "job" ? { kind: "card", id: key } : undefined}
|
|
106
|
-
contentScroll
|
|
107
|
-
>
|
|
108
|
-
<UI.Col key="job" style={{ height: 96, overflowY: "auto" }}>
|
|
109
|
-
<UI.div style={{ height: 72 }}>白班工单</UI.div>
|
|
110
|
-
<UI.div style={{ height: 72 }}>夜班工单</UI.div>
|
|
111
|
-
</UI.Col>
|
|
112
|
-
</ReactDragAny>
|
|
113
|
-
```
|
|
91
|
+
使用过属性绑定写法的页面,将列表和卡片分别包在 `Drag.Target` 和 `Drag.Source` 中,把绑定参数直接作为 JSX 属性传入。条目的 `id` 与列表的 `itemKey` 保持一致。
|