Skip to content

armgit / ui

Public Imported from GitHub
0
TypeScript

Refine schema builder cross-container drag handling

Andrew Luoc4f6f262f33a
diff --git a/apps/v4/registry/new-york-v4/ui/schema-builder.tsx b/apps/v4/registry/new-york-v4/ui/schema-builder.tsx
index 2cb31f1..f7e95be 100644
--- a/apps/v4/registry/new-york-v4/ui/schema-builder.tsx
+++ b/apps/v4/registry/new-york-v4/ui/schema-builder.tsx
@@ -155,15 +155,18 @@ const ARRAY_SCALAR_TYPES: SchemaBuilderArrayScalarType[] = [
const ROOT_SCHEMA_CONTAINER_ID = "schema-root"
const SCHEMA_PROPERTY_DRAG_TYPE = "schema-property"
const ENUM_VALUE_DRAG_TYPE = "enum-value"
+const SCHEMA_PROPERTY_BLOCK_ATTRIBUTE = "data-schema-builder-property-id"
+const SCHEMA_PROPERTY_ROW_ATTRIBUTE = "data-schema-builder-property-row-id"
const PROPERTY_REORDER_EDGE_THRESHOLD_PX = 18
const SCHEMA_DND_MEASURING = {
droppable: {
strategy: MeasuringStrategy.WhileDragging,
},
}
+type SchemaCollisionArgs = Parameters<CollisionDetection>[0]
function getDroppableRectArea(
- droppableRects: Parameters<CollisionDetection>[0]["droppableRects"],
+ droppableRects: SchemaCollisionArgs["droppableRects"],
id: UniqueIdentifier
) {
const rect = droppableRects.get(id)
@@ -173,7 +176,7 @@ function getDroppableRectArea(
function getProjectedPropertyChildContainerId(
properties: SchemaBuilderProperty[],
propertyId: UniqueIdentifier,
- args: Parameters<CollisionDetection>[0]
+ args: SchemaCollisionArgs
) {
const overLocation = findPropertyLocation(properties, String(propertyId))
const childContainerId = overLocation
@@ -193,6 +196,32 @@ function getProjectedPropertyChildContainerId(
return null
}
+function getSchemaPropertyElementRect(attribute: string, id: UniqueIdentifier) {
+ if (typeof document === "undefined") return null
+
+ for (const element of document.querySelectorAll<HTMLElement>(
+ `[${attribute}]`
+ )) {
+ if (element.getAttribute(attribute) === String(id)) {
+ return element.getBoundingClientRect()
+ }
+ }
+
+ return null
+}
+
+function getPropertyBlockRect(id: UniqueIdentifier, args: SchemaCollisionArgs) {
+ return (
+ getSchemaPropertyElementRect(SCHEMA_PROPERTY_BLOCK_ATTRIBUTE, id) ??
+ args.droppableRects.get(id) ??
+ null
+ )
+}
+
+function getPropertyRowRect(id: UniqueIdentifier) {
+ return getSchemaPropertyElementRect(SCHEMA_PROPERTY_ROW_ATTRIBUTE, id)
+}
+
const TYPE_LABELS: Record<
SchemaBuilderFieldType | `array-${SchemaBuilderArrayItemType}`,
string
@@ -908,14 +937,14 @@ function isSameContainerPropertyReorderReady(
properties: SchemaBuilderProperty[],
activeId: UniqueIdentifier,
overId: UniqueIdentifier,
- args: Parameters<CollisionDetection>[0]
+ args: SchemaCollisionArgs
) {
const activeLocation = findPropertyLocation(properties, String(activeId))
const overLocation = findPropertyLocation(properties, String(overId))
if (!activeLocation || !overLocation) return true
if (activeLocation.containerId !== overLocation.containerId) return true
- const overRect = args.droppableRects.get(overId)
+ const overRect = getPropertyBlockRect(overId, args)
if (!overRect || !args.pointerCoordinates) return true
const threshold = Math.min(
@@ -938,7 +967,7 @@ function isSchemaCollisionCandidate(
properties: SchemaBuilderProperty[],
id: UniqueIdentifier,
activeId: UniqueIdentifier,
- args: Parameters<CollisionDetection>[0]
+ args: SchemaCollisionArgs
) {
if (id === activeId) return false
@@ -955,17 +984,64 @@ function isSchemaCollisionCandidate(
function isPointerBelowLastContainerProperty(
containerProperties: SchemaBuilderProperty[],
- args: Parameters<CollisionDetection>[0]
+ args: SchemaCollisionArgs
) {
const lastProperty = containerProperties.at(-1)
if (!lastProperty || !args.pointerCoordinates) return true
- const lastPropertyRect = args.droppableRects.get(lastProperty.id)
+ const lastPropertyRect = getPropertyBlockRect(lastProperty.id, args)
if (!lastPropertyRect) return false
return args.pointerCoordinates.y >= lastPropertyRect.bottom
}
+function getNextPropertyInsertionId(
+ properties: SchemaBuilderProperty[],
+ location: PropertyLocation
+) {
+ const targetProperties = getContainerProperties(
+ properties,
+ location.containerId
+ )
+
+ return targetProperties[location.index + 1]?.id ?? location.containerId
+}
+
+function resolveCrossContainerPropertyInsertionId(
+ properties: SchemaBuilderProperty[],
+ activeId: UniqueIdentifier,
+ overId: UniqueIdentifier,
+ args: SchemaCollisionArgs
+) {
+ const activeLocation = findPropertyLocation(properties, String(activeId))
+ const overLocation = findPropertyLocation(properties, String(overId))
+ if (!activeLocation || !overLocation || !args.pointerCoordinates) {
+ return overId
+ }
+
+ if (activeLocation.containerId === overLocation.containerId) {
+ return overId
+ }
+
+ const overRowRect = getPropertyRowRect(overId)
+ if (overRowRect) {
+ const overRowMiddle = overRowRect.top + overRowRect.height / 2
+
+ return args.pointerCoordinates.y < overRowMiddle
+ ? overId
+ : getNextPropertyInsertionId(properties, overLocation)
+ }
+
+ const overBlockRect = getPropertyBlockRect(overId, args)
+ if (!overBlockRect) return overId
+
+ const overBlockMiddle = overBlockRect.top + overBlockRect.height / 2
+
+ return args.pointerCoordinates.y < overBlockMiddle
+ ? overId
+ : getNextPropertyInsertionId(properties, overLocation)
+}
+
function useSchemaBuilderSensors() {
return useSensors(
useSensor(PointerSensor, {
@@ -1652,6 +1728,7 @@ function SortablePropertyRows({
return (
<tbody
ref={setNodeRef}
+ data-schema-builder-property-id={property.id}
className={cn(isDragging && "relative z-10 opacity-70")}
style={{
transform: CSS.Translate.toString(transform),
@@ -1659,6 +1736,7 @@ function SortablePropertyRows({
}}
>
<tr
+ data-schema-builder-property-row-id={property.id}
className={cn(
"group/property-row border-b",
hasNestedEditor && "bg-muted/20"
@@ -2210,6 +2288,12 @@ export function SchemaBuilderPanel({
}
}
+ overId = resolveCrossContainerPropertyInsertionId(
+ schema.properties,
+ args.active.id,
+ overId,
+ args
+ )
lastSchemaOverIdRef.current = overId
return [{ id: overId }]
diff --git a/document-splits-drag-overlay.png b/document-splits-drag-overlay.png
deleted file mode 100644
index 86e479d..0000000
Binary files a/document-splits-drag-overlay.png and /dev/null differ
diff --git a/docx-viewer-page.png b/docx-viewer-page.png
deleted file mode 100644
index d6bfa3d..0000000
Binary files a/docx-viewer-page.png and /dev/null differ
diff --git a/excel-document-splits-block.png b/excel-document-splits-block.png
deleted file mode 100644
index a6516b8..0000000
Binary files a/excel-document-splits-block.png and /dev/null differ
diff --git a/file-thumbnail-docs.png b/file-thumbnail-docs.png
deleted file mode 100644
index e984b7d..0000000
Binary files a/file-thumbnail-docs.png and /dev/null differ
diff --git a/file-thumbnail-docx-fixed-later.png b/file-thumbnail-docx-fixed-later.png
deleted file mode 100644
index aca15cb..0000000
Binary files a/file-thumbnail-docx-fixed-later.png and /dev/null differ
diff --git a/file-thumbnail-docx-fixed.png b/file-thumbnail-docx-fixed.png
deleted file mode 100644
index aca15cb..0000000
Binary files a/file-thumbnail-docx-fixed.png and /dev/null differ
diff --git a/file-thumbnail-docx-manual-render.png b/file-thumbnail-docx-manual-render.png
deleted file mode 100644
index e5f415d..0000000
Binary files a/file-thumbnail-docx-manual-render.png and /dev/null differ
diff --git a/file-thumbnail-page.png b/file-thumbnail-page.png
deleted file mode 100644
index 35a19b3..0000000
Binary files a/file-thumbnail-page.png and /dev/null differ
diff --git a/gem-smoke-root-page.png b/gem-smoke-root-page.png
deleted file mode 100644
index b3d69e6..0000000
Binary files a/gem-smoke-root-page.png and /dev/null differ
diff --git a/gem-smoke-updated-settings.png b/gem-smoke-updated-settings.png
deleted file mode 100644
index f58205b..0000000
Binary files a/gem-smoke-updated-settings.png and /dev/null differ
diff --git a/heatmap-knicks-homepage.png b/heatmap-knicks-homepage.png
deleted file mode 100644
index b1a8799..0000000
Binary files a/heatmap-knicks-homepage.png and /dev/null differ
diff --git a/knicks-shader-homepage.png b/knicks-shader-homepage.png
deleted file mode 100644
index 891123d..0000000
Binary files a/knicks-shader-homepage.png and /dev/null differ
diff --git a/root-knicks-pdf.png b/root-knicks-pdf.png
deleted file mode 100644
index b341b70..0000000
Binary files a/root-knicks-pdf.png and /dev/null differ