@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 CHANGED
@@ -1,113 +1,91 @@
1
1
  # react-drag-any
2
2
 
3
- React 声明式 Canvas 拖放画布。导入组件时会自动加载默认样式。根入口在结构 JSX、`document` 和 `store` 中选择一种。
3
+ 使用 `Drag.Surface` 挂载一张画布。用 `UI.List` 排列卡片,用 `UI.Grid` 声明行列。页面根据可用宽度决定每行容量。导入组件时自动加载默认样式。
4
4
 
5
- ## 通用 Canvas UI 结构
5
+ ## 受控队列
6
6
 
7
- 队列和泳道可直接由 Canvas UI JSX 声明。通过 `getDragRole(key)` 将稳定的 JSX key 映射为拖拽角色、业务 ID 和数据。卡片内容进入视口时才编译,节点可独立响应事件。
7
+ 下面的组件将工单 ID 作为排序键。释放时按目标锚点修改业务数组,锚点失效时返回 `false` 取消移动。
8
8
 
9
- 卡片角色支持 `{ kind: "card", id, data, disabled: true }`,对应文档卡片的 `disabled`。如需锁定拖拽但允许点击详情等卡片操作,可传 `canDrag={({ card }) => !lockedIds.has(card.id)}`。
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
- 使用 `canDrop` 按目标容器和插入锚点限制位置。例如,`canDrop={({ target }) => target.beforeCardId === "locked" ? { allowed: false, reason: "不能插到锁定工单前" } : { allowed: true }}`。锚点为 `null` 时表示追加到容器末尾,也涵盖空容器。可在泳道角色上设置 `dropDisabled: true` 拒绝整条泳道的落点。
60
+ 指针进入滚动容器的边缘并停留时,容器会持续滚动。沿同一边停留时,速度在 1.5 秒内从每秒 480 像素逐渐提高到 1440 像素。指针越过视口或画布边界后,原容器会沿该方向继续加速滚动,直到滚到尽头或松手。回到视口中部会停止滚动,换向或切换滚动容器后重新起速。在画布外松手会取消投放。角落的横纵两轴独立加速。固定表头、行头和空白处也可以触发滚动。使用 `autoScrollEdge={32}` 可将四边的触发距离设为 32 CSS 像素,也可按边设置。省略的边默认 16 像素,设为 0 可关闭该边。
12
61
 
13
62
  ```tsx
14
- import { UI } from "@canvas-components/react-canvas-ui";
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
- dragLayout="lanes"
30
- getDragRole={(key) => roles.get(key)}
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
- <UI.Col>
69
- <UI.Row key="device" style={{ height: 96 }}>
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
- 队列使用纵向布局根和 `card` 子节点,卡片高度及间距映射到画布布局。泳道使用纵向根、横向 `region`、可选 `header` 和横向 `items`。泳道高度、标题宽度、卡片间距和卡片组内边距在画布内共享,卡片可分别设置宽度。结构尺寸使用数字或 `px`,内容内部仍可使用 Canvas UI 支持的布局样式。稀疏网格继续使用下文的 `SparseGrid` 或文档入口。
84
-
85
- `annotations` 的回调以同一视口读取固定顶部、角头和正文。返回项的坐标相对所处分区,指针坐标相对宿主。正文覆盖项只参与绘制。可用 `refreshIntervalMs` 刷新当前时间线等依赖时钟的内容,覆盖帧复用上一帧正文及命中数据。
72
+ ## 撤销和重做
86
73
 
87
- ## 专用结构、文档与 store
74
+ 将同一个 `useDragHistory` 实例传给 `Drag.Surface` 的 `history` 属性。业务校验通过后调用 `history.commit(next, request.itemId)` 保存新的不可变状态。跨画布移动时,将两个画布的顺序保存在同一份状态中,一次提交即可一起撤销或重做。
88
75
 
89
- 支持 `Lanes > Lane > Card`、`SparseGrid > GridSlot > Card`、`Queue > Card` 和 `Flow > Card`。每层也可用 `lanes`、`slots`、`cards` 提供批量记录。记录属性与同层结构 children 互斥。卡片可声明 `size`、`data`、`disabled` 和内容。内容写在 Card 子树或 `content` 属性中。字段 `header` 和 `corner` 可传 `UI.*` JSX 或 `canvas-ui` JSON。Canvas UI 支持布局、文本和 21 类叶节点,排除嵌套表格。独立编译可调用 `compileJSX`,结果统一为 `canvas-ui` 文档。
76
+ ```tsx
77
+ <button disabled={!history.canUndo} onClick={history.undo}>撤销</button>
78
+ <button disabled={!history.canRedo} onClick={history.redo}>重做</button>
79
+ ```
90
80
 
91
- 既有的内置 JSX 子集和旧原生内容 JSON 会在输入处转换成 `canvas-ui` 文档。
81
+ 画布获得焦点后,Ctrl / ⌘ + Z 撤销,Ctrl / ⌘ + Shift + Z 或 Ctrl + Y 重做。回放以完整浮层移动该 ID 对应的卡片,其余可见卡片同步让位,并遵循系统减少动态效果的偏好。新移动会清除重做记录。默认保留最近 50 次提交,可通过第二个参数调整;重新加载或发布数据时可用 `history.reset(next)` 设置新的起点。
92
82
 
93
- 在 `onMoveRequest` 中按提案更新业务数组或调用原生包的 `applyMove`。同一次 React 更新传入新数据及 `acknowledgedTransactionId`。跨画布时,`ReactDragAnyGroup` 直接包含各画布声明。用组的 `onMoveRequest` 和 `acknowledgedTransactionId` 在同一次提交更新源和目标。未配置移动回调的画布可用于只读展示。
83
+ ## 破坏性变更
94
84
 
95
- 用 `ref` 调用 `focusCard`、`scrollTo`、`getSnapshot`、`cancelDrag` 或 `rejectMove`。用 `contextMenu` 定制右键菜单。`storageKey` 可保存滚动条设置。`UI.*` 的 `onClick` 接收节点级点击事件,同区域多个节点可独立处理。画布级 `onRegionContentAction` 接收区域归属和局部坐标。大数据可使用结构化文档,Canvas UI JSX 在进入视口时编译。
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
- 在 `ReactDragAny` 上传入 `contentScroll`,再为需要滚动的 Canvas UI 容器指定 `overflowX: "auto"` 或 `overflowY: "auto"` 及合适的宽度或高度。该属性默认关闭。容器样式设为 `scrollbarVisible: false` 时只隐藏滚动条,卡内滚动仍可用。
89
+ 对于包含大量虚拟列表的跨板页面,可在每个画板传入 `hasItem={id => cardsById.has(id)}`(原生选项为 `hasItem`)。引擎会用这个索引检查目标画板内的重复 ID,包括尚未进入窗口的列表。
100
90
 
101
- ```tsx
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` 保持一致。