@pisell/materials 6.12.103 → 6.12.105

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 (41) hide show
  1. package/build/lowcode/assets-daily.json +11 -11
  2. package/build/lowcode/assets-dev.json +2 -2
  3. package/build/lowcode/assets-prod.json +11 -11
  4. package/build/lowcode/meta.js +1 -1
  5. package/build/lowcode/render/default/view.css +1 -1
  6. package/build/lowcode/render/default/view.js +2 -2
  7. package/build/lowcode/view.css +1 -1
  8. package/build/lowcode/view.js +2 -2
  9. package/es/components/pisellDraggable/components/TreeItem/SortableTreeItem.js +44 -5
  10. package/es/components/pisellDraggable/components/TreeItem/TreeItem.d.ts +2 -1
  11. package/es/components/pisellDraggable/components/TreeItem/TreeItem.js +5 -2
  12. package/es/components/pisellDraggable/components/TreeItem/TreeItem.less +4 -2
  13. package/es/components/pisellDraggable/index.d.ts +3 -1
  14. package/es/components/pisellDraggable/index.js +287 -207
  15. package/es/components/pisellDraggable/types.d.ts +3 -1
  16. package/es/components/pisellDraggable/utilities.js +90 -79
  17. package/es/components/pisellDropSort/PisellDropSort.js +11 -4
  18. package/es/components/pisellDropSort/components/SortableItem/index.js +8 -5
  19. package/es/components/pisellDropSort/components/SortableItem/index.less +22 -0
  20. package/es/components/pisellDropSort/types.d.ts +1 -0
  21. package/es/components/pisellNavigationMenu/PisellNavigationMenu.js +44 -21
  22. package/es/components/pisellNavigationMenu/PisellNavigationMenu.less +7 -0
  23. package/es/components/pisellNavigationMenu/index.d.ts +1 -1
  24. package/es/components/pisellNavigationMenu/types.d.ts +15 -2
  25. package/lib/components/pisellDraggable/components/TreeItem/SortableTreeItem.js +43 -3
  26. package/lib/components/pisellDraggable/components/TreeItem/TreeItem.d.ts +2 -1
  27. package/lib/components/pisellDraggable/components/TreeItem/TreeItem.js +5 -2
  28. package/lib/components/pisellDraggable/components/TreeItem/TreeItem.less +4 -2
  29. package/lib/components/pisellDraggable/index.d.ts +3 -1
  30. package/lib/components/pisellDraggable/index.js +281 -201
  31. package/lib/components/pisellDraggable/types.d.ts +3 -1
  32. package/lib/components/pisellDraggable/utilities.js +90 -79
  33. package/lib/components/pisellDropSort/PisellDropSort.js +10 -3
  34. package/lib/components/pisellDropSort/components/SortableItem/index.js +8 -5
  35. package/lib/components/pisellDropSort/components/SortableItem/index.less +22 -0
  36. package/lib/components/pisellDropSort/types.d.ts +1 -0
  37. package/lib/components/pisellNavigationMenu/PisellNavigationMenu.js +44 -21
  38. package/lib/components/pisellNavigationMenu/PisellNavigationMenu.less +7 -0
  39. package/lib/components/pisellNavigationMenu/index.d.ts +1 -1
  40. package/lib/components/pisellNavigationMenu/types.d.ts +15 -2
  41. package/package.json +1 -1
@@ -1,13 +1,14 @@
1
1
  import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
2
2
  import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
3
- import { buildTree, flattenTree, getChildCount, getProjection, removeChildrenOf, removeItem, setProperty } from "./utilities.js";
4
- import SortableTreeItem_default from "./components/TreeItem/SortableTreeItem.js";
5
- import React, { useEffect, useMemo, useRef, useState } from "react";
3
+ import { TreeItem } from "./components/TreeItem/TreeItem.js";
4
+ import { buildTree, createNavigationHitTest, flattenTree, getChildCount, getProjection, removeChildrenOf, removeItem, setProperty } from "./utilities.js";
5
+ import SortableTreeItem_default, { DropTreeItem } from "./components/TreeItem/SortableTreeItem.js";
6
+ import React, { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
6
7
  import { useMemoizedFn } from "ahooks";
7
8
  import { createPortal } from "react-dom";
8
- import { DndContext, DragOverlay, MeasuringStrategy, PointerSensor, closestCenter, defaultDropAnimation, useSensor, useSensors } from "@dnd-kit/core";
9
- import { SortableContext, arrayMove, verticalListSortingStrategy } from "@dnd-kit/sortable";
10
- import { CSS } from "@dnd-kit/utilities";
9
+ import { DndContext, DragOverlay, MeasuringStrategy, PointerSensor, closestCenter, defaultDropAnimation, useDroppable, useSensor, useSensors } from "@dnd-kit/core";
10
+ import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable";
11
+ import { CSS, getEventCoordinates } from "@dnd-kit/utilities";
11
12
  //#region src/components/pisellDraggable/index.tsx
12
13
  /**
13
14
  * 可拖拽树形组件
@@ -89,239 +90,318 @@ const dropAnimationConfig = {
89
90
  });
90
91
  }
91
92
  };
92
- function SortableTree({ collapsible = true, value = initialItems, indicator = false, indentationWidth = 50, removable = true, renderItem, onChange, onRemoveBefore, __designMode, maxLevel = 3, canDrop }) {
93
- /** 当前树形数据 */
94
- const [items, setItems] = useState([]);
95
- /** 当前正在拖拽的节点ID */
96
- const [activeId, setActiveId] = useState(null);
97
- /** 当前拖拽悬停的节点ID */
98
- const [overId, setOverId] = useState(null);
99
- /** 水平偏移量,用于确定拖拽时的层级变化 */
100
- const [offsetLeft, setOffsetLeft] = useState(0);
93
+ const overlayStyle = { pointerEvents: "none" };
94
+ const dropPositionAttribute = "data-drop-position";
95
+ const dropLineOffset = "--pisell-drop-sort-line-offset";
96
+ const draggingAttribute = "data-pisell-drop-sort-dragging";
97
+ function clearDropMarker(row) {
98
+ row === null || row === void 0 || row.removeAttribute(dropPositionAttribute);
99
+ row === null || row === void 0 || row.style.removeProperty(dropLineOffset);
100
+ }
101
+ function NavigationDropSurface({ id, boundary, rows, firstRowId }) {
102
+ const { setNodeRef } = useDroppable({ id });
101
103
  useEffect(() => {
102
- console.log("初始化", value);
103
- setItems(value);
104
- }, [value]);
105
- const flattenTreeData = useMemo(() => {
106
- return flattenTree(items);
107
- }, [items]);
108
- /**
109
- * 扁平化处理树形数据
110
- * 1. 将树形结构转换为扁平数组
111
- * 2. 处理折叠状态
112
- * 3. 移除被折叠节点的子节点
113
- */
114
- const flattenedItems = useMemo(() => {
115
- const collapsedItems = flattenTreeData.reduce((acc, { children = [], collapsed, id }) => collapsed && children.length ? [...acc, id] : acc, []);
116
- return removeChildrenOf(flattenTreeData, activeId != null ? [activeId, ...collapsedItems] : collapsedItems);
117
- }, [activeId, flattenTreeData]);
118
- console.log("flattenedItems", flattenedItems);
119
- /**
120
- * 计算拖拽投影
121
- * 根据当前拖拽状态计算目标位置和深度
122
- */
123
- const projected = activeId && overId ? getProjection(flattenedItems, activeId, overId, offsetLeft, indentationWidth) : null;
124
- /**
125
- * 传感器上下文
126
- * 存储扁平化的节点数据和偏移量信息
127
- */
128
- const sensorContext = useRef({
129
- items: flattenedItems,
130
- offset: offsetLeft
131
- });
104
+ var _ref, _boundary$current;
105
+ const firstRow = firstRowId === void 0 ? null : rows.get(firstRowId);
106
+ setNodeRef((_ref = (_boundary$current = boundary === null || boundary === void 0 ? void 0 : boundary.current) !== null && _boundary$current !== void 0 ? _boundary$current : firstRow) !== null && _ref !== void 0 ? _ref : null);
107
+ return () => setNodeRef(null);
108
+ }, [
109
+ boundary,
110
+ firstRowId,
111
+ rows,
112
+ setNodeRef
113
+ ]);
114
+ return null;
115
+ }
116
+ function SortableTree({ collapsible = true, value = initialItems, indicator = false, indentationWidth = 50, removable = true, renderItem, onChange, onRemoveBefore, __designMode, maxLevel = 3, canDrop, dropOnPageAsSibling = false, dropBoundary }) {
117
+ var _activeItem$children, _items$;
118
+ const [items, setItems] = useState(value);
119
+ const [activeId, setActiveId] = useState(null);
120
+ const [preview, setPreview] = useState(null);
121
+ const previewRef = useRef(null);
122
+ const dragItems = useRef(null);
123
+ const navigationRows = useRef(/* @__PURE__ */ new Map());
124
+ const markedRow = useRef();
125
+ const draggingList = useRef();
126
+ const releasedPointer = useRef(null);
132
127
  const sensors = useSensors(useSensor(PointerSensor));
133
- const sortedIds = useMemo(() => flattenedItems.map(({ id }) => id), [flattenedItems]);
134
- const activeItem = useMemo(() => {
135
- return activeId ? flattenedItems.find(({ id }) => id === activeId) : null;
136
- }, [activeId, flattenedItems]);
137
- useEffect(() => {
138
- sensorContext.current = {
139
- items: flattenedItems,
140
- offset: offsetLeft
141
- };
142
- }, [flattenedItems, offsetLeft]);
128
+ const surfaceId = `navigation-drop-${useId()}`;
129
+ const setNavigationRow = useCallback((id, node) => {
130
+ if (node) navigationRows.current.set(id, node);
131
+ else navigationRows.current.delete(id);
132
+ }, []);
133
+ const onPointerRelease = useCallback((event) => {
134
+ if (event.isPrimary) releasedPointer.current = event;
135
+ }, []);
136
+ useEffect(() => setItems(value), [value]);
143
137
  useEffect(() => {
144
138
  document.body.id = "body";
145
- }, []);
146
- /**
147
- * 检查子树是否超过最大深度限制
148
- * @param items 当前项目列表
149
- * @param itemId 要检查的项目ID
150
- * @param baseDepth 基础深度
151
- * @returns 是否超过限制
152
- */
153
- function checkSubtreeDepth(items, itemId, baseDepth) {
154
- if (!items.find((i) => i.id === itemId)) return false;
155
- if (baseDepth >= maxLevel) return true;
156
- const children = items.filter((i) => i.parentId === itemId);
157
- if (baseDepth >= maxLevel - 1 && children.length > 0) return true;
158
- return children.some((child) => {
159
- const childNewDepth = baseDepth + 1;
160
- return childNewDepth >= maxLevel || checkSubtreeDepth(items, child.id, childNewDepth);
161
- });
162
- }
163
- /**
164
- * 处理节点删除
165
- * 从树形结构中移除指定节点
166
- */
167
- const handleRemove = useMemoizedFn(function() {
168
- var _ref = _asyncToGenerator(function* (item) {
169
- if (onRemoveBefore) {
170
- if (!(yield onRemoveBefore === null || onRemoveBefore === void 0 ? void 0 : onRemoveBefore(item))) return;
139
+ return () => {
140
+ var _draggingList$current;
141
+ clearDropMarker(markedRow.current);
142
+ (_draggingList$current = draggingList.current) === null || _draggingList$current === void 0 || _draggingList$current.removeAttribute(draggingAttribute);
143
+ document.removeEventListener("pointerup", onPointerRelease, true);
144
+ if (dragItems.current) document.body.style.removeProperty("cursor");
145
+ };
146
+ }, [onPointerRelease]);
147
+ const { rows, indices, subtreeHeights } = useMemo(() => {
148
+ const rows = flattenTree(items);
149
+ const subtreeHeights = /* @__PURE__ */ new Map();
150
+ for (let index = rows.length - 1; index >= 0; index--) {
151
+ var _subtreeHeights$get, _subtreeHeights$get2;
152
+ const { id, parentId } = rows[index];
153
+ if (parentId === null) continue;
154
+ const height = ((_subtreeHeights$get = subtreeHeights.get(id)) !== null && _subtreeHeights$get !== void 0 ? _subtreeHeights$get : 0) + 1;
155
+ if (height > ((_subtreeHeights$get2 = subtreeHeights.get(parentId)) !== null && _subtreeHeights$get2 !== void 0 ? _subtreeHeights$get2 : 0)) subtreeHeights.set(parentId, height);
156
+ }
157
+ return {
158
+ rows,
159
+ indices: new Map(rows.map(({ id }, index) => [id, index])),
160
+ subtreeHeights
161
+ };
162
+ }, [items]);
163
+ const activeIndex = activeId === null ? void 0 : indices.get(activeId);
164
+ const activeItem = activeIndex === void 0 ? void 0 : rows[activeIndex];
165
+ const hiddenParent = (activeItem === null || activeItem === void 0 || (_activeItem$children = activeItem.children) === null || _activeItem$children === void 0 ? void 0 : _activeItem$children.length) && !activeItem.collapsed ? activeId : null;
166
+ const { visibleRows, visibleIndices, sortedIds, lastDescendants } = useMemo(() => {
167
+ const exclude = rows.filter((row) => {
168
+ var _row$children;
169
+ return row.collapsed && ((_row$children = row.children) === null || _row$children === void 0 ? void 0 : _row$children.length);
170
+ }).map(({ id }) => id);
171
+ if (hiddenParent !== null) exclude.push(hiddenParent);
172
+ const visibleRows = removeChildrenOf(rows, exclude);
173
+ const sortedIds = visibleRows.map(({ id }) => id);
174
+ const lastDescendants = /* @__PURE__ */ new Map();
175
+ if (dropOnPageAsSibling) for (let index = visibleRows.length - 1; index >= 0; index--) {
176
+ const row = visibleRows[index];
177
+ if (row.parentId !== null && !lastDescendants.has(row.parentId)) {
178
+ var _lastDescendants$get;
179
+ lastDescendants.set(row.parentId, (_lastDescendants$get = lastDescendants.get(row.id)) !== null && _lastDescendants$get !== void 0 ? _lastDescendants$get : row);
171
180
  }
172
- setItems((items) => removeItem(items, item.id));
173
- onChange === null || onChange === void 0 || onChange(items);
174
- });
175
- return function(_x) {
176
- return _ref.apply(this, arguments);
181
+ }
182
+ return {
183
+ visibleRows,
184
+ sortedIds,
185
+ visibleIndices: new Map(sortedIds.map((id, index) => [id, index])),
186
+ lastDescendants
187
+ };
188
+ }, [
189
+ dropOnPageAsSibling,
190
+ hiddenParent,
191
+ rows
192
+ ]);
193
+ const hitTest = useMemo(() => createNavigationHitTest(sortedIds, navigationRows.current), [sortedIds]);
194
+ const navigationCollision = useCallback(({ pointerCoordinates, droppableContainers }) => {
195
+ const surface = droppableContainers[0];
196
+ if (!pointerCoordinates || (surface === null || surface === void 0 ? void 0 : surface.id) !== surfaceId) return [];
197
+ const target = hitTest(pointerCoordinates);
198
+ if (!target) return [];
199
+ return [{
200
+ id: surfaceId,
201
+ data: {
202
+ target,
203
+ pointerCoordinates,
204
+ droppableContainer: surface,
205
+ value: 0
206
+ }
207
+ }];
208
+ }, [hitTest, surfaceId]);
209
+ const activeChildCount = useMemo(() => activeId === null ? 0 : getChildCount(items, activeId) + 1, [activeId, items]);
210
+ const sectionDepth = ((_items$ = items[0]) === null || _items$ === void 0 ? void 0 : _items$.isSection) ? 1 : 0;
211
+ const resolveDrop = useMemoizedFn((event, targetHit, pointer) => {
212
+ var _event$over, _subtreeHeights$get3, _canDrop;
213
+ if (dragItems.current !== items || activeId !== event.active.id) return null;
214
+ const targetId = dropOnPageAsSibling ? targetHit === null || targetHit === void 0 ? void 0 : targetHit.rowId : (_event$over = event.over) === null || _event$over === void 0 ? void 0 : _event$over.id;
215
+ if (targetId == null || dropOnPageAsSibling && targetId === activeId) return null;
216
+ const from = visibleIndices.get(event.active.id);
217
+ const to = visibleIndices.get(targetId);
218
+ if (from === void 0 || to === void 0) return null;
219
+ const item = visibleRows[from];
220
+ const target = visibleRows[to];
221
+ if (item.isSection) return null;
222
+ let depth;
223
+ let parentId;
224
+ let position;
225
+ const origin = pointer ? getEventCoordinates(event.activatorEvent) : null;
226
+ const offset = (pointer && origin ? pointer.x - origin.x : event.delta.x) / indentationWidth;
227
+ if (dropOnPageAsSibling) {
228
+ var _targetHit$position;
229
+ position = (_targetHit$position = targetHit === null || targetHit === void 0 ? void 0 : targetHit.position) !== null && _targetHit$position !== void 0 ? _targetHit$position : "before";
230
+ if (target.isSection || !target.disabledDrop && position === "after" && item.depth + Math.trunc(offset) > target.depth && !item.isGroup) {
231
+ position = "inside";
232
+ parentId = target.id;
233
+ depth = target.depth + 1;
234
+ } else {
235
+ parentId = target.parentId;
236
+ depth = target.depth;
237
+ }
238
+ } else {
239
+ ({depth, parentId} = getProjection(visibleRows, visibleIndices, from, to, Math.round(offset)));
240
+ position = from < to ? "after" : "before";
241
+ }
242
+ const parentIndex = parentId === null ? void 0 : indices.get(parentId);
243
+ const parent = parentIndex === void 0 ? void 0 : rows[parentIndex];
244
+ const allowed = !(parent === null || parent === void 0 ? void 0 : parent.disabledDrop) && depth + ((_subtreeHeights$get3 = subtreeHeights.get(item.id)) !== null && _subtreeHeights$get3 !== void 0 ? _subtreeHeights$get3 : 0) < maxLevel && ((_canDrop = canDrop === null || canDrop === void 0 ? void 0 : canDrop(item, target, parentId, parent !== null && parent !== void 0 ? parent : null)) !== null && _canDrop !== void 0 ? _canDrop : true);
245
+ return {
246
+ targetId,
247
+ depth,
248
+ parentId,
249
+ position,
250
+ allowed
177
251
  };
178
- }());
179
- /**
180
- * 处理拖拽开始事件
181
- * 设置当前拖拽项和初始状态
182
- */
183
- const handleDragStart = useMemoizedFn(({ active: { id: activeId } }) => {
184
- setActiveId(activeId);
185
- setOverId(activeId);
186
- document.body.style.setProperty("cursor", "grabbing");
187
- });
188
- /**
189
- * 处理拖拽移动事件
190
- * 更新水平偏移量
191
- */
192
- const handleDragMove = useMemoizedFn(({ delta }) => {
193
- setOffsetLeft(delta.x);
194
252
  });
195
- const handleDragOver = useMemoizedFn(({ over }) => {
196
- if (!projected || !over) {
197
- var _over$id;
198
- setOverId((_over$id = over === null || over === void 0 ? void 0 : over.id) !== null && _over$id !== void 0 ? _over$id : null);
199
- return;
253
+ const handleDragStart = useMemoizedFn(({ active }) => {
254
+ dragItems.current = items;
255
+ if (dropOnPageAsSibling) {
256
+ var _navigationRows$curre, _navigationRows$curre2, _draggingList$current2;
257
+ draggingList.current = (_navigationRows$curre = (_navigationRows$curre2 = navigationRows.current.get(active.id)) === null || _navigationRows$curre2 === void 0 ? void 0 : _navigationRows$curre2.parentElement) !== null && _navigationRows$curre !== void 0 ? _navigationRows$curre : void 0;
258
+ (_draggingList$current2 = draggingList.current) === null || _draggingList$current2 === void 0 || _draggingList$current2.setAttribute(draggingAttribute, "");
259
+ document.addEventListener("pointerup", onPointerRelease, true);
200
260
  }
201
- const targetParent = projected.parentId ? flattenedItems.find((item) => item.id === projected.parentId) : null;
202
- if (targetParent === null || targetParent === void 0 ? void 0 : targetParent.disabledDrop) return;
203
- if (projected.depth >= maxLevel) return;
204
- if (checkSubtreeDepth(flattenTreeData, activeId, projected.depth)) return;
205
- setOverId(over.id);
261
+ setActiveId(active.id);
262
+ document.body.style.setProperty("cursor", "grabbing");
206
263
  });
207
- const parentNode = useMemo(() => {
208
- return (projected === null || projected === void 0 ? void 0 : projected.parentId) ? flattenedItems.find((item) => item.id === projected.parentId) : null;
209
- }, [flattenedItems, projected === null || projected === void 0 ? void 0 : projected.parentId]);
210
- /**
211
- * 处理拖拽结束事件
212
- * 1. 重置状态
213
- * 2. 更新节点位置和层级
214
- * 3. 重建树形结构
215
- */
216
- const handleDragEnd = useMemoizedFn(({ active, over }) => {
217
- resetState();
218
- if (projected && over) {
219
- const { depth, parentId } = projected;
220
- const targetParent = parentId ? flattenedItems.find((item) => item.id === parentId) : null;
221
- if (targetParent === null || targetParent === void 0 ? void 0 : targetParent.disabledDrop) return;
222
- const dragItem = flattenedItems.find((item) => item.id === active.id);
223
- const dropTarget = flattenedItems.find((item) => item.id === over.id);
224
- if (canDrop && dragItem && dropTarget) {
225
- if (!canDrop(dragItem, dropTarget, parentId, targetParent)) return;
264
+ const updatePreview = useMemoizedFn((event) => {
265
+ var _event$collisions;
266
+ const data = (_event$collisions = event.collisions) === null || _event$collisions === void 0 || (_event$collisions = _event$collisions[0]) === null || _event$collisions === void 0 ? void 0 : _event$collisions.data;
267
+ const next = resolveDrop(event, data === null || data === void 0 ? void 0 : data.target, data === null || data === void 0 ? void 0 : data.pointerCoordinates);
268
+ const previous = previewRef.current;
269
+ if ((previous === null || previous === void 0 ? void 0 : previous.targetId) === (next === null || next === void 0 ? void 0 : next.targetId) && (previous === null || previous === void 0 ? void 0 : previous.position) === (next === null || next === void 0 ? void 0 : next.position) && (previous === null || previous === void 0 ? void 0 : previous.parentId) === (next === null || next === void 0 ? void 0 : next.parentId) && (previous === null || previous === void 0 ? void 0 : previous.depth) === (next === null || next === void 0 ? void 0 : next.depth) && (previous === null || previous === void 0 ? void 0 : previous.allowed) === (next === null || next === void 0 ? void 0 : next.allowed)) return;
270
+ previewRef.current = next;
271
+ if (dropOnPageAsSibling) {
272
+ var _descendant$id;
273
+ const descendant = (next === null || next === void 0 ? void 0 : next.allowed) && next.position === "after" ? lastDescendants.get(next.targetId) : void 0;
274
+ const row = (next === null || next === void 0 ? void 0 : next.allowed) ? navigationRows.current.get((_descendant$id = descendant === null || descendant === void 0 ? void 0 : descendant.id) !== null && _descendant$id !== void 0 ? _descendant$id : next.targetId) : void 0;
275
+ if (markedRow.current !== row) clearDropMarker(markedRow.current);
276
+ if (row && next) {
277
+ row.setAttribute(dropPositionAttribute, next.position);
278
+ if (descendant) row.style.setProperty(dropLineOffset, `${(next.depth - descendant.depth) * indentationWidth}px`);
279
+ else row.style.removeProperty(dropLineOffset);
226
280
  }
227
- if (depth >= maxLevel) return;
228
- if (checkSubtreeDepth(flattenTreeData, active.id, depth)) return;
229
- const clonedItems = JSON.parse(JSON.stringify(flattenTree(items)));
230
- const overIndex = clonedItems.findIndex(({ id }) => id === over.id);
231
- const activeIndex = clonedItems.findIndex(({ id }) => id === active.id);
232
- const activeTreeItem = clonedItems[activeIndex];
233
- clonedItems[activeIndex] = _objectSpread2(_objectSpread2({}, activeTreeItem), {}, {
234
- depth,
235
- parentId
236
- });
237
- const newItems = buildTree(arrayMove(clonedItems, activeIndex, overIndex));
238
- setItems(newItems);
239
- onChange === null || onChange === void 0 || onChange(newItems);
281
+ markedRow.current = row;
240
282
  }
283
+ if (!dropOnPageAsSibling || (previous === null || previous === void 0 ? void 0 : previous.allowed) === false !== ((next === null || next === void 0 ? void 0 : next.allowed) === false)) setPreview(next);
241
284
  });
242
- /**
243
- * 处理节点折叠/展开
244
- * 切换指定节点的折叠状态
245
- */
246
- const handleCollapse = useMemoizedFn((id) => {
247
- setItems((items) => {
248
- const newVal = setProperty(items, id, "collapsed", (value) => {
249
- return !value;
250
- });
285
+ function resetDrag() {
286
+ var _draggingList$current3;
287
+ dragItems.current = null;
288
+ releasedPointer.current = null;
289
+ document.removeEventListener("pointerup", onPointerRelease, true);
290
+ previewRef.current = null;
291
+ clearDropMarker(markedRow.current);
292
+ markedRow.current = void 0;
293
+ (_draggingList$current3 = draggingList.current) === null || _draggingList$current3 === void 0 || _draggingList$current3.removeAttribute(draggingAttribute);
294
+ draggingList.current = void 0;
295
+ setPreview(null);
296
+ setActiveId(null);
297
+ document.body.style.removeProperty("cursor");
298
+ }
299
+ const handleDragEnd = useMemoizedFn((event) => {
300
+ var _event$collisions2;
301
+ const release = releasedPointer.current;
302
+ const activator = event.activatorEvent;
303
+ const pointer = release && release.pointerId === activator.pointerId ? {
304
+ x: release.clientX,
305
+ y: release.clientY
306
+ } : (_event$collisions2 = event.collisions) === null || _event$collisions2 === void 0 || (_event$collisions2 = _event$collisions2[0]) === null || _event$collisions2 === void 0 || (_event$collisions2 = _event$collisions2.data) === null || _event$collisions2 === void 0 ? void 0 : _event$collisions2.pointerCoordinates;
307
+ const proposal = resolveDrop(event, dropOnPageAsSibling && pointer ? hitTest(pointer) : void 0, pointer);
308
+ resetDrag();
309
+ if (!(proposal === null || proposal === void 0 ? void 0 : proposal.allowed)) return;
310
+ if (dropOnPageAsSibling && pointer && (dropBoundary === null || dropBoundary === void 0 ? void 0 : dropBoundary.current) && !dropBoundary.current.contains(document.elementFromPoint(pointer.x, pointer.y))) return;
311
+ const from = indices.get(event.active.id);
312
+ const targetIndex = indices.get(proposal.targetId);
313
+ if (from === void 0 || targetIndex === void 0) return;
314
+ const item = rows[from];
315
+ const to = targetIndex + Number(proposal.position !== "before") - Number(from < targetIndex);
316
+ if (from === to && item.parentId === proposal.parentId) return;
317
+ const nextRows = rows.slice();
318
+ nextRows.splice(from, 1);
319
+ nextRows.splice(to, 0, _objectSpread2(_objectSpread2({}, item), {}, {
320
+ depth: proposal.depth,
321
+ parentId: proposal.parentId
322
+ }));
323
+ const next = buildTree(nextRows);
324
+ setItems(next);
325
+ onChange === null || onChange === void 0 || onChange(next);
326
+ });
327
+ const handleRemove = useMemoizedFn(function() {
328
+ var _ref2 = _asyncToGenerator(function* (item) {
329
+ if (onRemoveBefore && !(yield onRemoveBefore(item))) return;
330
+ setItems((items) => removeItem(items, item.id));
251
331
  onChange === null || onChange === void 0 || onChange(items);
252
- return newVal;
253
332
  });
333
+ return function(_x) {
334
+ return _ref2.apply(this, arguments);
335
+ };
336
+ }());
337
+ const handleCollapse = useMemoizedFn((id) => {
338
+ const next = setProperty(items, id, "collapsed", (value) => !value);
339
+ setItems(next);
340
+ onChange === null || onChange === void 0 || onChange(next);
254
341
  });
255
- const isError = useMemo(() => {
256
- return !(canDrop === null || canDrop === void 0 ? void 0 : canDrop(activeItem || {}, {}, projected === null || projected === void 0 ? void 0 : projected.parentId, parentNode));
257
- }, [
258
- projected,
259
- activeItem,
260
- canDrop,
261
- parentNode
262
- ]);
263
- const renderTreeItems = useMemo(() => {
264
- return flattenedItems.map((item) => {
265
- const { id, children, collapsed, depth } = item;
266
- return /* @__PURE__ */ React.createElement(SortableTreeItem_default, {
267
- key: id,
268
- id,
269
- item,
270
- depth: id === activeId && projected ? projected.depth : depth,
271
- indentationWidth,
272
- indicator,
273
- collapsed: Boolean(collapsed && (children === null || children === void 0 ? void 0 : children.length)),
274
- onCollapse: collapsible && (children === null || children === void 0 ? void 0 : children.length) ? () => handleCollapse(id) : void 0,
275
- onRemove: removable ? () => handleRemove(item) : void 0,
276
- renderItem,
277
- __designMode
278
- });
279
- });
280
- }, [
281
- flattenedItems,
282
- activeId,
283
- projected === null || projected === void 0 ? void 0 : projected.depth,
342
+ const projectedDepth = !dropOnPageAsSibling && (preview === null || preview === void 0 ? void 0 : preview.allowed) ? preview.depth : void 0;
343
+ const projectedId = projectedDepth === void 0 ? null : activeId;
344
+ const tree = useMemo(() => visibleRows.map((item) => {
345
+ var _item$children, _item$children2;
346
+ const rowProps = {
347
+ id: item.id,
348
+ item,
349
+ depth: Math.max(0, (item.id === projectedId ? projectedDepth !== null && projectedDepth !== void 0 ? projectedDepth : item.depth : item.depth) - sectionDepth),
350
+ indentationWidth,
351
+ indicator,
352
+ collapsed: Boolean(item.collapsed && ((_item$children = item.children) === null || _item$children === void 0 ? void 0 : _item$children.length)),
353
+ onCollapseItem: collapsible && !item.isSection && ((_item$children2 = item.children) === null || _item$children2 === void 0 ? void 0 : _item$children2.length) ? handleCollapse : void 0,
354
+ onRemoveItem: removable && !item.isSection ? handleRemove : void 0,
355
+ renderItem,
356
+ __designMode
357
+ };
358
+ return dropOnPageAsSibling ? /* @__PURE__ */ React.createElement(DropTreeItem, _objectSpread2(_objectSpread2({ key: item.id }, rowProps), {}, { setRowNode: setNavigationRow })) : /* @__PURE__ */ React.createElement(SortableTreeItem_default, _objectSpread2({ key: item.id }, rowProps));
359
+ }), [
360
+ visibleRows,
361
+ projectedId,
362
+ projectedDepth,
363
+ sectionDepth,
284
364
  indentationWidth,
285
365
  indicator,
286
366
  collapsible,
367
+ removable,
287
368
  handleCollapse,
288
369
  handleRemove,
289
370
  renderItem,
290
- __designMode
371
+ __designMode,
372
+ dropOnPageAsSibling,
373
+ setNavigationRow
291
374
  ]);
292
375
  return /* @__PURE__ */ React.createElement(DndContext, {
293
376
  sensors,
294
- collisionDetection: closestCenter,
295
- measuring,
377
+ collisionDetection: dropOnPageAsSibling ? navigationCollision : closestCenter,
378
+ measuring: dropOnPageAsSibling ? void 0 : measuring,
296
379
  onDragStart: handleDragStart,
297
- onDragMove: handleDragMove,
298
- onDragOver: handleDragOver,
380
+ onDragMove: updatePreview,
381
+ onDragOver: updatePreview,
299
382
  onDragEnd: handleDragEnd,
300
- onDragCancel: handleDragCancel
301
- }, /* @__PURE__ */ React.createElement(SortableContext, {
383
+ onDragCancel: resetDrag
384
+ }, dropOnPageAsSibling && /* @__PURE__ */ React.createElement(NavigationDropSurface, {
385
+ id: surfaceId,
386
+ boundary: dropBoundary,
387
+ rows: navigationRows.current,
388
+ firstRowId: sortedIds[0]
389
+ }), dropOnPageAsSibling ? tree : /* @__PURE__ */ React.createElement(SortableContext, {
302
390
  items: sortedIds,
303
391
  strategy: verticalListSortingStrategy
304
- }, renderTreeItems, createPortal(/* @__PURE__ */ React.createElement(DragOverlay, {
392
+ }, tree), createPortal(/* @__PURE__ */ React.createElement(DragOverlay, {
305
393
  dropAnimation: dropAnimationConfig,
306
- modifiers: indicator ? [adjustTranslate] : void 0
307
- }, activeId && activeItem ? /* @__PURE__ */ React.createElement(SortableTreeItem_default, {
308
- id: activeId,
309
- depth: activeItem.depth,
394
+ modifiers: indicator ? [adjustTranslate] : void 0,
395
+ style: dropOnPageAsSibling ? overlayStyle : void 0
396
+ }, activeItem ? /* @__PURE__ */ React.createElement(TreeItem, {
397
+ depth: Math.max(0, activeItem.depth - sectionDepth),
310
398
  clone: true,
311
- childCount: getChildCount(items, activeId) + 1,
399
+ childCount: activeChildCount,
312
400
  item: activeItem,
313
401
  indentationWidth,
314
- renderItem: (props, ref) => renderItem === null || renderItem === void 0 ? void 0 : renderItem(_objectSpread2(_objectSpread2({}, props), {}, { isError }), ref)
315
- }) : null), document.body)));
316
- function handleDragCancel() {
317
- resetState();
318
- }
319
- function resetState() {
320
- setOverId(null);
321
- setActiveId(null);
322
- setOffsetLeft(0);
323
- document.body.style.setProperty("cursor", "");
324
- }
402
+ isError: (preview === null || preview === void 0 ? void 0 : preview.allowed) === false,
403
+ renderItem
404
+ }) : null), document.body));
325
405
  }
326
406
  /**
327
407
  * 调整拖拽项的位置
@@ -6,6 +6,7 @@ interface TreeItem {
6
6
  children: TreeItem[];
7
7
  collapsed?: boolean;
8
8
  disabledDrop?: boolean;
9
+ isSection?: boolean;
9
10
  [key: string]: any;
10
11
  }
11
12
  type TreeItems = TreeItem[];
@@ -14,5 +15,6 @@ interface FlattenedItem extends TreeItem {
14
15
  depth: number;
15
16
  index: number;
16
17
  }
18
+ type DropPosition = 'before' | 'after' | 'inside';
17
19
  //#endregion
18
- export { FlattenedItem, TreeItem, TreeItems };
20
+ export { DropPosition, FlattenedItem, TreeItem, TreeItems };