Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Replace react-beautiful-dnd with @dnd-kit

ZachTRice661aaaa9a937
diff --git a/package-lock.json b/package-lock.json
index f99c12899..d2bf180c8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10,6 +10,9 @@
"hasInstallScript": true,
"license": "NASA-1.3",
"dependencies": {
+ "@dnd-kit/core": "^6.3.1",
+ "@dnd-kit/sortable": "^10.0.0",
+ "@dnd-kit/utilities": "^3.2.2",
"@edsc/earthdata-react-icons": "^0.0.2",
"@elastic/react-search-ui": "^1.24.2",
"@elastic/react-search-ui-views": "^1.24.2",
@@ -47,7 +50,6 @@
"proj4": "^2.20.3",
"prop-types": "^15.8.1",
"react": "^18.3.1",
- "react-beautiful-dnd": "^13.1.1",
"react-device-detect": "^2.2.3",
"react-dom": "^18.3.1",
"react-draggable": "^4.5.0",
@@ -2267,6 +2269,59 @@
"node": ">=10.0.0"
}
},
+ "node_modules/@dnd-kit/accessibility": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
+ "integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
+ "license": "MIT",
+ "dependencies": {
+ "tslib": "^2.0.0"
+ },
+ "peerDependencies": {
+ "react": ">=16.8.0"
+ }
+ },
+ "node_modules/@dnd-kit/core": {
+ "version": "6.3.1",
+ "resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
+ "integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@dnd-kit/accessibility": "^3.1.1",
+ "@dnd-kit/utilities": "^3.2.2",
+ "tslib": "^2.0.0"
+ },
+ "peerDependencies": {
+ "react": ">=16.8.0",
+ "react-dom": ">=16.8.0"
+ }
+ },
+ "node_modules/@dnd-kit/sortable": {
+ "version": "10.0.0",
+ "resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
+ "integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
+ "license": "MIT",
+ "dependencies": {
+ "@dnd-kit/utilities": "^3.2.2",
+ "tslib": "^2.0.0"
+ },
+ "peerDependencies": {
+ "@dnd-kit/core": "^6.3.0",
+ "react": ">=16.8.0"
+ }
+ },
+ "node_modules/@dnd-kit/utilities": {
+ "version": "3.2.2",
+ "resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
+ "integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
+ "license": "MIT",
+ "dependencies": {
+ "tslib": "^2.0.0"
+ },
+ "peerDependencies": {
+ "react": ">=16.8.0"
+ }
+ },
"node_modules/@dual-bundle/import-meta-resolve": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/@dual-bundle/import-meta-resolve/-/import-meta-resolve-4.2.1.tgz",
@@ -4239,9 +4294,6 @@
"arm"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4263,9 +4315,6 @@
"arm"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4287,9 +4336,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4311,9 +4357,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4335,9 +4378,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -4359,9 +4399,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5038,18 +5075,6 @@
"@types/node": "*"
}
},
- "node_modules/@types/hoist-non-react-statics": {
- "version": "3.3.7",
- "resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.7.tgz",
- "integrity": "sha512-PQTyIulDkIDro8P+IHbKCsw7U2xxBYflVzW/FgWdCAePD9xGSidgA76/GeJ6lBKoblyhf9pBY763gbrN+1dI8g==",
- "license": "MIT",
- "dependencies": {
- "hoist-non-react-statics": "^3.3.0"
- },
- "peerDependencies": {
- "@types/react": "*"
- }
- },
"node_modules/@types/http-errors": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.5.tgz",
@@ -5173,31 +5198,11 @@
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.14.tgz",
"integrity": "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"csstype": "^3.2.2"
}
},
- "node_modules/@types/react-redux": {
- "version": "7.1.34",
- "resolved": "https://registry.npmjs.org/@types/react-redux/-/react-redux-7.1.34.tgz",
- "integrity": "sha512-GdFaVjEbYv4Fthm2ZLvj1VSCedV7TqE5y1kNwnjSdBOTXuRSgowux6J8TAct15T3CKBr63UMk+2CO7ilRhyrAQ==",
- "license": "MIT",
- "dependencies": {
- "@types/hoist-non-react-statics": "^3.3.0",
- "@types/react": "*",
- "hoist-non-react-statics": "^3.3.0",
- "redux": "^4.0.0"
- }
- },
- "node_modules/@types/react-redux/node_modules/redux": {
- "version": "4.2.1",
- "resolved": "https://registry.npmjs.org/redux/-/redux-4.2.1.tgz",
- "integrity": "sha512-LAUYz4lc+Do8/g7aeRa8JkyDErK6ekstQaqWQrNRW//MY1TvCEpMtpTWvlQ+FPbWCx+Xixu/6SHt5N0HR+SB4w==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.9.2"
- }
- },
"node_modules/@types/react-transition-group": {
"version": "4.4.12",
"resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.12.tgz",
@@ -5709,9 +5714,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5726,9 +5728,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5743,9 +5742,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5760,9 +5756,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5777,9 +5770,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5794,9 +5784,6 @@
"s390x"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5811,9 +5798,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -5828,9 +5812,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -7860,15 +7841,6 @@
"node": ">= 8"
}
},
- "node_modules/css-box-model": {
- "version": "1.2.1",
- "resolved": "https://registry.npmjs.org/css-box-model/-/css-box-model-1.2.1.tgz",
- "integrity": "sha512-a7Vr4Q/kd/aw96bnJG332W9V9LkJO69JRcaCYDUqjp6/z0w6VcZjgAcTbgFxEPfBgdnAwlh3iwu+hLopa+flJw==",
- "license": "MIT",
- "dependencies": {
- "tiny-invariant": "^1.0.6"
- }
- },
"node_modules/css-declaration-sorter": {
"version": "7.3.1",
"resolved": "https://registry.npmjs.org/css-declaration-sorter/-/css-declaration-sorter-7.3.1.tgz",
@@ -13777,12 +13749,6 @@
"tslib": "2"
}
},
- "node_modules/memoize-one": {
- "version": "5.2.1",
- "resolved": "https://registry.npmjs.org/memoize-one/-/memoize-one-5.2.1.tgz",
- "integrity": "sha512-zYiwtZUcYyXKo/np96AGZAckk+FWWsUdJ3cHGGmld7+AhvcWmQyGCYUh1hc4Q/pkOhb65dQR/pqCyK0cOaHz4Q==",
- "license": "MIT"
- },
"node_modules/memory-fs": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/memory-fs/-/memory-fs-0.2.0.tgz",
@@ -16454,12 +16420,6 @@
"integrity": "sha512-XdjUArbK4Bm5fLLvlm5KpTFOiOThgfWWI4axAZDWg4E/0mKdZyI9tNEfds27qCi1ze/vwTR16kvmmGhRra3c2g==",
"license": "ISC"
},
- "node_modules/raf-schd": {
- "version": "4.0.3",
- "resolved": "https://registry.npmjs.org/raf-schd/-/raf-schd-4.0.3.tgz",
- "integrity": "sha512-tQkJl2GRWh83ui2DiPTJz9wEiMN20syf+5oKfB03yYP7ioZcJwsIK8FjrtLwH1m7C7e+Tt2yYBlrOpdT+dyeIQ==",
- "license": "MIT"
- },
"node_modules/range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@@ -16559,66 +16519,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/react-beautiful-dnd": {
- "version": "13.1.1",
- "resolved": "https://registry.npmjs.org/react-beautiful-dnd/-/react-beautiful-dnd-13.1.1.tgz",
- "integrity": "sha512-0Lvs4tq2VcrEjEgDXHjT98r+63drkKEgqyxdA7qD3mvKwga6a5SscbdLPO2IExotU1jW8L0Ksdl0Cj2AF67nPQ==",
- "deprecated": "react-beautiful-dnd is now deprecated. Context and options: https://github.com/atlassian/react-beautiful-dnd/issues/2672",
- "license": "Apache-2.0",
- "dependencies": {
- "@babel/runtime": "^7.9.2",
- "css-box-model": "^1.2.0",
- "memoize-one": "^5.1.1",
- "raf-schd": "^4.0.2",
- "react-redux": "^7.2.0",
- "redux": "^4.0.4",
- "use-memo-one": "^1.1.1"
- },
- "peerDependencies": {
- "react": "^16.8.5 || ^17.0.0 || ^18.0.0",
- "react-dom": "^16.8.5 || ^17.0.0 || ^18.0.0"
- }
- },
- "node_modules/react-beautiful-dnd/node_modules/react-is": {
- "version": "17.0.2",
- "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz",
- "integrity": "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==",
- "license": "MIT"
- },
- "node_modules/react-beautiful-dnd/node_modules/react-redux": {
- "version": "7.2.9",
- "resolved": "https://registry.npmjs.org/react-redux/-/react-redux-7.2.9.tgz",
- "integrity": "sha512-Gx4L3uM182jEEayZfRbI/G11ZpYdNAnBs70lFVMNdHJI76XYtR+7m0MN+eAs7UHBPhWXcnFPaS+9owSCJQHNpQ==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.15.4",
- "@types/react-redux": "^7.1.20",
- "hoist-non-react-statics": "^3.3.2",
- "loose-envify": "^1.4.0",
- "prop-types": "^15.7.2",
- "react-is": "^17.0.2"
- },
- "peerDependencies": {
- "react": "^16.8.3 || ^17 || ^18"
- },
- "peerDependenciesMeta": {
- "react-dom": {
- "optional": true
- },
- "react-native": {
- "optional": true
- }
- }
- },
- "node_modules/react-beautiful-dnd/node_modules/redux": {
- "version": "4.2.1",
- "resolved": "https://registry.npmjs.org/redux/-/redux-4.2.1.tgz",
- "integrity": "sha512-LAUYz4lc+Do8/g7aeRa8JkyDErK6ekstQaqWQrNRW//MY1TvCEpMtpTWvlQ+FPbWCx+Xixu/6SHt5N0HR+SB4w==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.9.2"
- }
- },
"node_modules/react-device-detect": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/react-device-detect/-/react-device-detect-2.2.3.tgz",
@@ -19674,7 +19574,6 @@
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
- "dev": true,
"license": "0BSD"
},
"node_modules/tsyringe": {
@@ -20133,15 +20032,6 @@
}
}
},
- "node_modules/use-memo-one": {
- "version": "1.1.3",
- "resolved": "https://registry.npmjs.org/use-memo-one/-/use-memo-one-1.1.3.tgz",
- "integrity": "sha512-g66/K7ZQGYrI6dy8GLpVcMsBp4s17xNkYJVSMvTEevGy3nDxHOfE6z8BVE22+5G5x7t3+bhzrlTDB7ObrEE0cQ==",
- "license": "MIT",
- "peerDependencies": {
- "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
- }
- },
"node_modules/use-sync-external-store": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.6.0.tgz",
diff --git a/package.json b/package.json
index 47e0af83b..cdccee9ff 100644
--- a/package.json
+++ b/package.json
@@ -191,7 +191,9 @@
"proj4": "^2.20.3",
"prop-types": "^15.8.1",
"react": "^18.3.1",
- "react-beautiful-dnd": "^13.1.1",
+ "@dnd-kit/core": "^6.3.1",
+ "@dnd-kit/sortable": "^10.0.0",
+ "@dnd-kit/utilities": "^3.2.2",
"react-device-detect": "^2.2.3",
"react-dom": "^18.3.1",
"react-draggable": "^4.5.0",
diff --git a/web/js/components/layer/settings/granule-date-list.js b/web/js/components/layer/settings/granule-date-list.js
index 576dbbfe1..3b6a92c1b 100644
--- a/web/js/components/layer/settings/granule-date-list.js
+++ b/web/js/components/layer/settings/granule-date-list.js
@@ -1,7 +1,14 @@
/* eslint-disable react/jsx-props-no-spreading */
-import { PureComponent } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
-import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
+import { DndContext, closestCenter } from '@dnd-kit/core';
+import {
+ SortableContext,
+ verticalListSortingStrategy,
+ arrayMove,
+ useSortable,
+} from '@dnd-kit/sortable';
+import { CSS } from '@dnd-kit/utilities';
import { Button } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faArrowCircleUp, faArrowCircleDown } from '@fortawesome/free-solid-svg-icons';
@@ -13,20 +20,14 @@ const { events } = util;
const itemHeight = 30;
const itemMargin = 2;
-const reorder = (list, startIndex, endIndex) => {
- const result = Array.from(list);
- const [removed] = result.splice(startIndex, 1);
- result.splice(endIndex, 0, removed);
- return result;
-};
-
-const getItemStyle = (isDragging, isHover, isLastMovedItem, draggableStyle) => ({
+const getItemStyle = (isDragging, isHover, isLastMovedItem, sortableStyle) => ({
userSelect: 'none',
height: itemHeight,
marginBottom: itemMargin,
outline: isLastMovedItem ? '1px solid #007BFF' : 'none',
border: isHover ? '1px solid #eee' : '1px solid #666',
- ...draggableStyle,
+ ...sortableStyle,
+ zIndex: isDragging ? 1 : undefined,
});
const getListStyle = (needsScrollBar, items, screenHeight) => {
@@ -45,257 +46,224 @@ const getListStyle = (needsScrollBar, items, screenHeight) => {
};
};
-class GranuleDateList extends PureComponent {
- constructor(props) {
- super(props);
- this.state = {
- hoveredItem: null,
- lastMovedItem: null,
- sorted: true,
- items: [],
- };
- this.onDragEnd = this.onDragEnd.bind(this);
+const checkGranuleDateSorting = (granuleDates) => {
+ const dates = granuleDates || [];
+ for (let i = 0; i < dates.length - 1; i += 1) {
+ if (dates[i] < dates[i + 1]) {
+ return false;
+ }
}
+ return true;
+};
- componentDidMount() {
- const { granuleDates } = this.props;
- this.setItems(granuleDates);
- this.checkGranuleDateSorting(granuleDates);
- }
+function SortableGranuleItem(props) {
+ const {
+ date,
+ index,
+ itemsLength,
+ hoveredItem,
+ lastMovedItem,
+ onMouseEnter,
+ onMouseLeave,
+ onMoveUp,
+ onMoveDown,
+ } = props;
- componentDidUpdate(prevProps) {
- const { granuleDates } = this.props;
- if (JSON.stringify(granuleDates) !== JSON.stringify(prevProps.granuleDates)) {
- this.setItems(granuleDates);
- this.checkGranuleDateSorting(granuleDates);
- }
- }
+ const {
+ attributes,
+ listeners,
+ setNodeRef,
+ transform,
+ transition,
+ isDragging,
+ } = useSortable({ id: date });
- // handle update on complete granule item drag
- onDragEnd = (result) => {
- const { updateGranuleLayerOptions, granuleCount, def } = this.props;
- // dropped granule outside the list
- if (!result.destination) {
- return;
- }
- const reorderedItems = this.reorderItems(
- result.source.index,
- result.destination.index,
- );
- updateGranuleLayerOptions(reorderedItems, def, granuleCount);
- this.setState({
- lastMovedItem: result.draggableId, // granule date
- });
+ const sortableStyle = {
+ transform: CSS.Transform.toString(transform),
+ transition,
};
- // move granule item to top of list
- moveUp = (e, sourceIndex, granuleDate) => {
- e.preventDefault();
- const { updateGranuleLayerOptions, granuleCount, def } = this.props;
- const reorderedItems = this.reorderItems(
- sourceIndex,
- sourceIndex - 1,
- );
+ const renderDownBtn = () => index < itemsLength - 1 && (
+ <button
+ type="button"
+ className="granule-date-item-down-button"
+ onPointerDown={(e) => e.stopPropagation()}
+ onClick={(e) => onMoveDown(e, index, date)}
+ >
+ <FontAwesomeIcon icon={faArrowCircleDown} fixedWidth widthAuto />
+ </button>
+ );
+ const renderUpBtn = () => index > 0 && (
+ <button
+ type="button"
+ className="granule-date-item-up-button"
+ onPointerDown={(e) => e.stopPropagation()}
+ onClick={(e) => onMoveUp(e, index, date)}
+ >
+ <FontAwesomeIcon icon={faArrowCircleUp} fixedWidth widthAuto />
+ </button>
+ );
+
+ return (
+ <div
+ className="granule-date-item"
+ onMouseEnter={() => onMouseEnter(date)}
+ onMouseLeave={() => onMouseLeave()}
+ ref={setNodeRef}
+ {...attributes}
+ {...listeners}
+ style={getItemStyle(
+ isDragging,
+ hoveredItem === date,
+ lastMovedItem === date,
+ sortableStyle,
+ )}
+ >
+ <div className="granule-date monospace">
+ {date}
+ </div>
+ <div className="granule-date-buttons">
+ {renderDownBtn()}
+ {renderUpBtn()}
+ </div>
+ </div>
+ );
+}
+
+function GranuleDateList(props) {
+ const {
+ def,
+ granuleCount,
+ granuleDates,
+ resetGranuleLayerDates,
+ granulePlatform,
+ screenHeight,
+ updateGranuleLayerOptions,
+ } = props;
+
+ const [hoveredItem, setHoveredItem] = useState(null);
+ const [lastMovedItem, setLastMovedItem] = useState(null);
+ const [sorted, setSorted] = useState(true);
+ const [items, setItems] = useState([]);
+
+ useEffect(() => {
+ const newItems = granuleDates || [];
+ setItems(newItems);
+ setSorted(checkGranuleDateSorting(newItems));
+ }, [granuleDates]);
+
+ const onDragEnd = (event) => {
+ const { active, over } = event;
+ if (!over || active.id === over.id) return;
+
+ const sourceIndex = items.indexOf(active.id);
+ const destinationIndex = items.indexOf(over.id);
+ if (sourceIndex < 0 || destinationIndex < 0 || sourceIndex === destinationIndex) return;
+
+ const reorderedItems = arrayMove(items, sourceIndex, destinationIndex);
+ setItems(reorderedItems);
+ setSorted(checkGranuleDateSorting(reorderedItems));
updateGranuleLayerOptions(reorderedItems, def, granuleCount);
- this.setState({
- lastMovedItem: granuleDate,
- });
+ setLastMovedItem(active.id);
};
- // move granule item to top of list
- moveDown = (e, sourceIndex, granuleDate) => {
+ const moveUp = (e, sourceIndex, granuleDate) => {
e.preventDefault();
- const { updateGranuleLayerOptions, granuleCount, def } = this.props;
- const reorderedItems = this.reorderItems(
- sourceIndex,
- sourceIndex + 1,
- );
+ if (sourceIndex <= 0) return;
+ const reorderedItems = arrayMove(items, sourceIndex, sourceIndex - 1);
+ setItems(reorderedItems);
+ setSorted(checkGranuleDateSorting(reorderedItems));
updateGranuleLayerOptions(reorderedItems, def, granuleCount);
- this.setState({
- lastMovedItem: granuleDate,
- });
+ setLastMovedItem(granuleDate);
};
- // reorder granule items based on source and target index
- reorderItems = (sourceIndex, destinationIndex) => {
- const { items } = this.state;
- const reorderedItems = reorder(
- items,
- sourceIndex,
- destinationIndex,
- );
- return reorderedItems;
+ const moveDown = (e, sourceIndex, granuleDate) => {
+ e.preventDefault();
+ if (sourceIndex >= items.length - 1) return;
+ const reorderedItems = arrayMove(items, sourceIndex, sourceIndex + 1);
+ setItems(reorderedItems);
+ setSorted(checkGranuleDateSorting(reorderedItems));
+ updateGranuleLayerOptions(reorderedItems, def, granuleCount);
+ setLastMovedItem(granuleDate);
};
- // reset granule order
- onClickReset = (e) => {
+ const onClickReset = (e) => {
e.preventDefault();
- const { resetGranuleLayerDates, def } = this.props;
resetGranuleLayerDates(def.id);
- this.setState({
- lastMovedItem: null,
- });
+ setLastMovedItem(null);
};
- // set local granule item state
- setItems = (items) => {
- this.setState({
- items,
- });
- };
-
- // handle mouse over item
- handleMouseOverItem = (granuleDate) => {
- const { granulePlatform } = this.props;
+ const handleMouseOverItem = (granuleDate) => {
events.trigger(GRANULE_HOVERED, granulePlatform, granuleDate);
- this.setState({
- hoveredItem: granuleDate,
- });
+ setHoveredItem(granuleDate);
};
- // handle mouse leave item
- handleMouseLeaveItem = () => {
- const { granulePlatform } = this.props;
+ const handleMouseLeaveItem = () => {
events.trigger(GRANULE_HOVERED, granulePlatform, null);
- this.setState({
- hoveredItem: null,
- });
- };
-
- // determine if grnaule dates are in order - used for RESET button toggle
- checkGranuleDateSorting = (granuleDates) => {
- const { sorted } = this.state;
- let isSorted = true;
- for (let i = 0; i < granuleDates.length - 1; i += 1) {
- if (granuleDates[i] < granuleDates[i + 1]) {
- isSorted = false;
- break;
- }
- }
- if (sorted !== isSorted) {
- this.setState({
- sorted: isSorted,
- });
- }
+ setHoveredItem(null);
};
- renderDraggableGranule = (date, index) => {
- const {
- items,
- hoveredItem,
- lastMovedItem,
- } = this.state;
- const renderDownBtn = () => index < items.length - 1 && (
- <button
- type="button"
- className="granule-date-item-down-button"
- onClick={(e) => this.moveDown(e, index, date)}
- >
- <FontAwesomeIcon icon={faArrowCircleDown} fixedWidth widthAuto />
- </button>
- );
- const renderUpBtn = () => index > 0 && (
- <button
- type="button"
- className="granule-date-item-up-button"
- onClick={(e) => this.moveUp(e, index, date)}
- >
- <FontAwesomeIcon icon={faArrowCircleUp} fixedWidth widthAuto />
- </button>
- );
+ const maxNumItemsNoScrollNeeded = 8;
+ const needsScrollBar = items.length > maxNumItemsNoScrollNeeded;
+ const listStyle = useMemo(
+ () => getListStyle(needsScrollBar, items, screenHeight),
+ [needsScrollBar, items, screenHeight],
+ );
- return (
- <Draggable
- key={date}
- draggableId={date}
- index={index}
- direction="vertical"
- >
- {(provided, snapshot) => (
- <div
- className="granule-date-item"
- onMouseEnter={() => this.handleMouseOverItem(date)}
- onMouseLeave={() => this.handleMouseLeaveItem()}
- ref={provided.innerRef}
- {...provided.draggableProps}
- {...provided.dragHandleProps}
- style={getItemStyle(
- snapshot.isDragging,
- hoveredItem === date,
- lastMovedItem === date,
- provided.draggableProps.style,
- )}
+ return (
+ <div className="layer-granule-date-draggable-list" style={{ paddingLeft: '4px', marginBottom: '14px' }}>
+ <h2 className="wv-header">
+ Granule Layer Date Order
+ <span style={{ float: 'right' }}>
+ <Button
+ onClick={(e) => onClickReset(e)}
+ className="granule-list-reset-button"
+ block={false}
+ style={{ lineHeight: '6px' }}
+ disabled={sorted}
+ color={sorted ? 'secondary' : 'primary'}
>
- <div className="granule-date monospace">
- {date}
- </div>
- <div className="granule-date-buttons">
- {renderDownBtn()}
- {renderUpBtn()}
- </div>
+ RESET
+ </Button>
+ </span>
+ </h2>
+ {items.length > 0
+ ? (
+ <DndContext
+ collisionDetection={closestCenter}
+ onDragEnd={onDragEnd}
+ >
+ <SortableContext
+ items={items}
+ strategy={verticalListSortingStrategy}
+ >
+ <div style={listStyle}>
+ {items.map((date, index) => (
+ <SortableGranuleItem
+ key={date}
+ date={date}
+ index={index}
+ itemsLength={items.length}
+ hoveredItem={hoveredItem}
+ lastMovedItem={lastMovedItem}
+ onMouseEnter={handleMouseOverItem}
+ onMouseLeave={handleMouseLeaveItem}
+ onMoveUp={moveUp}
+ onMoveDown={moveDown}
+ />
+ ))}
+ </div>
+ </SortableContext>
+ </DndContext>
+ )
+ : (
+ <div style={{ marginBottom: '14px', color: '#a0a0a0' }}>
+ <p className="granule-date-item-no-granules-available">No granules available.</p>
+ <br />
</div>
)}
- </Draggable>
- );
- };
-
- render() {
- const { items, sorted } = this.state;
- const { screenHeight, def } = this.props;
- const maxNumItemsNoScrollNeeded = 8;
- const granuleDateLength = items.length;
- const needsScrollBar = granuleDateLength > maxNumItemsNoScrollNeeded;
- const droppableId = `droppable-granule-date-list-${def.id}`;
-
- return (
- <div className="layer-granule-date-draggable-list" style={{ paddingLeft: '4px', marginBottom: '14px' }}>
- <h2 className="wv-header">
- Granule Layer Date Order
- <span style={{ float: 'right' }}>
- <Button
- onClick={(e) => this.onClickReset(e)}
- className="granule-list-reset-button"
- block={false}
- style={{ lineHeight: '6px' }}
- disabled={sorted}
- color={sorted ? 'secondary' : 'primary'}
- >
- RESET
- </Button>
- </span>
- </h2>
- {items.length > 0
- ? (
-
- <DragDropContext onDragEnd={this.onDragEnd}>
- <Droppable
- droppableId={droppableId}
- direction="vertical"
- type="granule"
- >
- {(provided, snapshot) => (
- <div
- ref={provided.innerRef}
- style={getListStyle(needsScrollBar, items, screenHeight)}
- {...provided.droppableProps}
- >
- {items.map(this.renderDraggableGranule)}
- {provided.placeholder}
- </div>
- )}
- </Droppable>
- </DragDropContext>
-
- )
- : (
- <div style={{ marginBottom: '14px', color: '#a0a0a0' }}>
- <p className="granule-date-item-no-granules-available">No granules available.</p>
- <br />
- </div>
- )}
- </div>
- );
- }
+ </div>
+ );
}
GranuleDateList.propTypes = {
@@ -305,8 +273,19 @@ GranuleDateList.propTypes = {
resetGranuleLayerDates: PropTypes.func,
granulePlatform: PropTypes.string,
screenHeight: PropTypes.number,
- toggleHoveredGranule: PropTypes.func,
updateGranuleLayerOptions: PropTypes.func,
};
export default GranuleDateList;
+
+SortableGranuleItem.propTypes = {
+ date: PropTypes.string,
+ index: PropTypes.number,
+ itemsLength: PropTypes.number,
+ hoveredItem: PropTypes.string,
+ lastMovedItem: PropTypes.string,
+ onMouseEnter: PropTypes.func,
+ onMouseLeave: PropTypes.func,
+ onMoveUp: PropTypes.func,
+ onMoveDown: PropTypes.func,
+};
diff --git a/web/js/containers/sidebar/layer-list.js b/web/js/containers/sidebar/layer-list.js
index 899f2f2aa..fa3fc116f 100644
--- a/web/js/containers/sidebar/layer-list.js
+++ b/web/js/containers/sidebar/layer-list.js
@@ -2,13 +2,15 @@
import { useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
-import { Droppable, DragDropContext } from 'react-beautiful-dnd';
+import { DndContext, closestCenter } from '@dnd-kit/core';
+import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
} from 'reactstrap';
import { get as lodashGet } from 'lodash';
import LayerRow from './layer-row';
+import util from '../../util/util';
import {
replaceSubGroup,
getZotsForActiveLayers,
@@ -22,13 +24,6 @@ import {
toggleGroupVisibility as toggleGroupVisibilityAction,
} from '../../modules/layers/actions';
-const reorder = (list, startIndex, endIndex) => {
- const result = Array.from(list);
- const [removed] = result.splice(startIndex, 1);
- result.splice(endIndex, 0, removed);
- return result;
-};
-
function LayerList(props) {
const {
activeLayers,
@@ -58,6 +53,20 @@ function LayerList(props) {
const [showDropdownBtn, setDropdownBtnVisible] = useState(false);
const [showDropdownMenu, setDropdownMenuVisible] = useState(false);
+ const orderedLayers = [...layers]
+ .sort((layerA, layerB) => {
+ const shouldHideLayerB = layerB.shouldHide ? -1 : 1;
+ const shouldHideLayers = layerB.shouldHide === layerA.shouldHide
+ ? 0
+ : shouldHideLayerB;
+ return isChartingActive
+ ? shouldHideLayers
+ : 0;
+ });
+
+ const sortableLayerIds = orderedLayers
+ .map((layer) => `${util.encodeId(layer.id)}-${compareState}`);
+
const toggleDropdownMenuVisible = () => {
if (showDropdownMenu) {
setDropdownBtnVisible(false);
@@ -71,23 +80,36 @@ function LayerList(props) {
setDropdownBtnVisible(false);
};
+ // Prevent pointer/mouse events on controls from bubbling up and activating group drag.
+ const stopDndActivation = (e) => {
+ if (e?.nativeEvent?.stopImmediatePropagation) {
+ e.nativeEvent.stopImmediatePropagation();
+ }
+ if (e?.stopPropagation) {
+ e.stopPropagation();
+ }
+ };
+
/**
* Update Layer order after drag
* @param {Object} result | Result of layer drag
*/
- const onDragEnd = (result) => {
- const { destination, source, draggableId } = result;
- if (!destination || source.index === destination.index) {
- return;
- }
+ const onDragEnd = (event) => {
+ const { active, over } = event;
+ if (!over || active.id === over.id) return;
+
+ const sourceIndex = sortableLayerIds.indexOf(active.id);
+ const destinationIndex = sortableLayerIds.indexOf(over.id);
+ if (sourceIndex < 0 || destinationIndex < 0 || sourceIndex === destinationIndex) return;
+
const regex = new RegExp(`-${compareState}$`);
- const layerId = draggableId.replace(regex, '');
- const newLayerArray = reorder(layers, source.index, destination.index);
+ const layerId = `${active.id}`.replace(regex, '');
+ const newLayerArray = arrayMove(orderedLayers, sourceIndex, destinationIndex);
// In the new ordering, find the layer right after the layer that just
// moved. Leave null if moved to the end of the list.
let nextLayerId = null;
- const nextIndex = destination.index + 1;
+ const nextIndex = destinationIndex + 1;
if (nextIndex < newLayerArray.length) {
nextLayerId = newLayerArray[nextIndex].id;
}
@@ -121,21 +143,39 @@ function LayerList(props) {
const renderDropdownMenu = () => (!isAnimating && !isChartingActive) && (
<Dropdown className="layer-group-more-options" isOpen={showDropdownMenu} toggle={toggleDropdownMenuVisible}>
- <DropdownToggle>
+ <DropdownToggle
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
+ >
<FontAwesomeIcon
className="layer-group-more"
icon="ellipsis-v"
widthAuto
/>
</DropdownToggle>
- <DropdownMenu>
- <DropdownItem id="show-all" onClick={() => toggleVisibility(groupLayerIds, true)}>
+ <DropdownMenu onPointerDown={stopDndActivation} onMouseDown={stopDndActivation}>
+ <DropdownItem
+ id="show-all"
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
+ onClick={() => toggleVisibility(groupLayerIds, true)}
+ >
Show All Layers
</DropdownItem>
- <DropdownItem id="hide-all" onClick={() => toggleVisibility(groupLayerIds, false)}>
+ <DropdownItem
+ id="hide-all"
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
+ onClick={() => toggleVisibility(groupLayerIds, false)}
+ >
Hide All Layers
</DropdownItem>
- <DropdownItem id="remove-group" onClick={() => removeGroup(groupLayerIds)}>
+ <DropdownItem
+ id="remove-group"
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
+ onClick={() => removeGroup(groupLayerIds)}
+ >
Remove Group
</DropdownItem>
</DropdownMenu>
@@ -160,6 +200,8 @@ function LayerList(props) {
className="layer-group-collapse"
icon={!collapsed ? 'caret-down' : 'caret-left'}
onClick={() => toggleCollapse(groupId, !collapsed)}
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
widthAuto
/>
)}
@@ -175,32 +217,19 @@ function LayerList(props) {
{renderHeader()}
- <DragDropContext onDragEnd={onDragEnd}>
- <Droppable
- droppableId={`${compareState}-${groupId}`}
- type={`layerGroup${groupId}`}
- direction="vertical"
+ <DndContext
+ collisionDetection={closestCenter}
+ onDragEnd={onDragEnd}
+ >
+ <SortableContext
+ items={sortableLayerIds}
+ strategy={verticalListSortingStrategy}
>
- {(provided, snapshot) => (
- <ul
- className={collapsed ? 'category hidden' : 'category'}
- ref={provided.innerRef}
- {...provided.droppableProps}
- >
- {layers.sort((layerA, layerB) => {
- const shouldHideLayerB = layerB.shouldHide ? -1 : 1;
- const shouldHideLayers = layerB.shouldHide === layerA.shouldHide
- ? 0
- : shouldHideLayerB;
- return isChartingActive
- ? shouldHideLayers
- : 0;
- }).map(renderLayer)}
- {provided.placeholder}
- </ul>
- )}
- </Droppable>
- </DragDropContext>
+ <ul className={collapsed ? 'category hidden' : 'category'}>
+ {orderedLayers.map(renderLayer)}
+ </ul>
+ </SortableContext>
+ </DndContext>
</div>
);
}
diff --git a/web/js/containers/sidebar/layer-row.js b/web/js/containers/sidebar/layer-row.js
index f69c7b6bf..d9095933a 100644
--- a/web/js/containers/sidebar/layer-row.js
+++ b/web/js/containers/sidebar/layer-row.js
@@ -3,7 +3,8 @@
import { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
-import { Draggable } from 'react-beautiful-dnd';
+import { useSortable } from '@dnd-kit/sortable';
+import { CSS } from '@dnd-kit/utilities';
import { isEmpty as lodashIsEmpty, get as lodashGet } from 'lodash';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
@@ -48,12 +49,11 @@ import {
const { events } = util;
const { vectorModalProps, granuleModalProps, zoomModalProps } = MODAL_PROPERTIES;
-const getItemStyle = (isDragging, draggableStyle) => ({
- // some basic styles to make the items look a bit nicer
- ...draggableStyle,
- position: isDragging ? 'absolute' : 'relative',
+const getItemStyle = (isDragging, sortableStyle) => ({
+ ...sortableStyle,
top: null,
left: null,
+ zIndex: isDragging ? 1 : undefined,
});
function LayerRow (props) {
@@ -87,7 +87,6 @@ function LayerRow (props) {
toggleVisibility,
isDisabled,
isVisible,
- index,
hasPalette,
isInProjection,
tracksForLayer,
@@ -109,6 +108,7 @@ function LayerRow (props) {
} = props;
const encodedLayerId = util.encodeId(layer.id);
+ const sortableId = `${encodedLayerId}-${compareState}`;
const { title } = names;
const removeLayerBtnId = `close-${compareState}${encodedLayerId}`;
const removeLayerBtnTitle = 'Remove Layer';
@@ -130,6 +130,23 @@ function LayerRow (props) {
const [hideZoomAlert, setHideZoomAlert] = useState(false);
const [hideGranuleAlert, setHideGranuleAlert] = useState(false);
+ const {
+ attributes,
+ listeners,
+ setNodeRef,
+ transform,
+ transition,
+ isDragging,
+ } = useSortable({
+ id: sortableId,
+ disabled: isEmbedModeActive || isAnimating,
+ });
+
+ const sortableStyle = {
+ transform: CSS.Transform.toString(transform),
+ transition,
+ };
+
const ddvLayerZoomNoticeActive = ddvZoomAlerts.includes(layer.title);
const ddvLayerLocationNoticeActive = ddvLocationAlerts.includes(layer.title);
// All DDV layer notices are dismissable + Reflectance (Nadir BRDF-Adjusted) + DSWx-HLS
@@ -300,6 +317,17 @@ function LayerRow (props) {
return 'No data on selected date for this layer';
};
+ // Prevent pointer/mouse events on controls from bubbling up and activating drag.
+ // Intentionally does NOT call preventDefault so clicks still fire (esp. on touch).
+ const stopDndActivation = (e) => {
+ if (e?.nativeEvent?.stopImmediatePropagation) {
+ e.nativeEvent.stopImmediatePropagation();
+ }
+ if (e?.stopPropagation) {
+ e.stopPropagation();
+ }
+ };
+
const stopPropagation = (e) => {
e.nativeEvent.stopImmediatePropagation();
e.stopPropagation();
@@ -323,7 +351,10 @@ function LayerRow (props) {
const renderDropdownMenu = () => (
<Dropdown className="layer-group-more-options" isOpen={showDropdownMenu} toggle={toggleDropdownMenuVisible}>
- <DropdownToggle>
+ <DropdownToggle
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
+ >
<FontAwesomeIcon
className="layer-group-more"
icon="ellipsis-v"
@@ -367,6 +398,8 @@ function LayerRow (props) {
id={removeLayerBtnId}
aria-label={removeLayerBtnTitle}
className={isMobile ? 'hidden wv-layers-options' : 'button wv-layers-close'}
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
onClick={() => removeLayer()}
>
<UncontrolledTooltip id="center-align-tooltip" placement="top" target={removeLayerBtnId}>
@@ -380,6 +413,7 @@ function LayerRow (props) {
id={layerOptionsBtnId}
aria-label={layerOptionsBtnTitle}
className={isMobile ? 'hidden wv-layers-options' : 'button wv-layers-options'}
+ onPointerDown={stopDndActivation}
onMouseDown={stopPropagation}
onClick={() => onOptionsClick(layer, title, zot)}
>
@@ -393,6 +427,7 @@ function LayerRow (props) {
id={layerInfoBtnId}
aria-label={layerInfoBtnTitle}
className={isMobile ? 'hidden wv-layers-info' : 'button wv-layers-info'}
+ onPointerDown={stopDndActivation}
onMouseDown={stopPropagation}
onClick={() => onInfoClick(layer, title, measurementDescriptionPath, describeDomainsUrl)}
>
@@ -418,6 +453,7 @@ function LayerRow (props) {
id={layerVectorBtnId}
aria-label={titleStr}
className={runningDataObj ? `${classNames} running` : classNames}
+ onPointerDown={stopDndActivation}
onMouseDown={stopPropagation}
onClick={openVectorAlertModal}
>
@@ -438,6 +474,7 @@ function LayerRow (props) {
id={layerChartableBtnId}
aria-label={titleStr}
className="layer-chartable-icon"
+ onPointerDown={stopDndActivation}
onMouseDown={stopPropagation}
>
<UncontrolledTooltip id="center-align-tooltip" placement="top" target={layerChartableBtnId}>
@@ -517,6 +554,8 @@ function LayerRow (props) {
id={`hide${encodedLayerId}`}
className={getVisibilityToggleClass()}
aria-label={visibilityTitle}
+ onPointerDown={stopDndActivation}
+ onMouseDown={stopDndActivation}
onClick={() => !isAnimating && !disabled && toggleVisibility(layer.id, !isVisible)}
>
{!isAnimating && (
@@ -648,37 +687,30 @@ function LayerRow (props) {
</>
);
+ if (isInProjection) {
+ return (
+ <li
+ id={`${compareState}-${encodedLayerId}`}
+ className={getLayerItemClasses()}
+ style={getItemStyle(isDragging, { ...sortableStyle })}
+ ref={setNodeRef}
+ onMouseOver={mouseOver}
+ onMouseLeave={mouseLeave}
+ {...attributes}
+ {...listeners}
+ >
+ {renderLayerRow()}
+ </li>
+ );
+ }
+
return (
- <Draggable
- isDragDisabled={isEmbedModeActive || isAnimating}
- draggableId={`${encodedLayerId}-${compareState}`}
- index={index}
- direction="vertical"
- >
- {(provided, snapshot) => (isInProjection
- ? (
- <li
- id={`${compareState}-${encodedLayerId}`}
- className={getLayerItemClasses()}
- style={getItemStyle(snapshot.isDragging, provided.draggableProps.style)}
- ref={provided.innerRef}
- onMouseOver={mouseOver}
- onMouseLeave={mouseLeave}
- {...provided.draggableProps}
- {...provided.dragHandleProps}
- >
- {renderLayerRow()}
- </li>
- )
- : (
- <li
- className="layer-list-placeholder"
- ref={provided.innerRef}
- {...provided.draggableProps}
- {...provided.dragHandleProps}
- />
- ))}
- </Draggable>
+ <li
+ className="layer-list-placeholder"
+ ref={setNodeRef}
+ {...attributes}
+ {...listeners}
+ />
);
}
@@ -852,7 +884,6 @@ LayerRow.propTypes = {
hasPalette: PropTypes.bool,
globalTemperatureUnit: PropTypes.string,
hover: PropTypes.func,
- index: PropTypes.number,
isCustomPalette: PropTypes.bool,
isDisabled: PropTypes.bool,
isDistractionFreeModeActive: PropTypes.bool,
diff --git a/web/js/containers/sidebar/layers-container.js b/web/js/containers/sidebar/layers-container.js
index e7a485f44..f3505f855 100644
--- a/web/js/containers/sidebar/layers-container.js
+++ b/web/js/containers/sidebar/layers-container.js
@@ -1,7 +1,13 @@
-/* eslint-disable react/jsx-props-no-spreading */
import { useState } from 'react';
import { connect } from 'react-redux';
-import { Draggable, Droppable, DragDropContext } from 'react-beautiful-dnd';
+import { DndContext, closestCenter } from '@dnd-kit/core';
+import {
+ SortableContext,
+ verticalListSortingStrategy,
+ arrayMove,
+ useSortable,
+} from '@dnd-kit/sortable';
+import { CSS } from '@dnd-kit/utilities';
import PropTypes from 'prop-types';
import { isMobileOnly, isTablet } from 'react-device-detect';
import LayerList from './layer-list';
@@ -20,6 +26,63 @@ import SearchUiProvider from '../../components/layer/product-picker/search-ui-pr
import { openCustomContent } from '../../modules/modal/actions';
import { stop as stopAnimationAction } from '../../modules/animation/actions';
+function SortableOverlayGroup(props) {
+ const {
+ group,
+ compareState,
+ activeLayersMap,
+ overlaysLength,
+ toggleCollapse,
+ isEmbedModeActive,
+ isAnimating,
+ } = props;
+
+ const { groupName, layers, collapsed } = group;
+ const {
+ attributes,
+ listeners,
+ setNodeRef,
+ setActivatorNodeRef,
+ transform,
+ transition,
+ isDragging,
+ } = useSortable({
+ id: groupName,
+ disabled: isEmbedModeActive || isAnimating,
+ });
+
+ const style = {
+ transform: CSS.Transform.toString(transform),
+ transition,
+ opacity: isDragging ? 0.8 : undefined,
+ };
+
+ if (!layers) return null;
+ const layersForGroup = layers.map((id) => activeLayersMap[id]);
+ return (
+ <li
+ id={`${compareState}-${util.cleanId(groupName)}`}
+ ref={setNodeRef}
+ style={style}
+ >
+ <LayerList
+ title={groupName}
+ groupId={groupName}
+ collapsed={collapsed}
+ toggleCollapse={toggleCollapse}
+ compareState={compareState}
+ layerSplit={overlaysLength}
+ layers={layersForGroup}
+ dragHandleProps={{
+ ...attributes,
+ ...listeners,
+ ref: setActivatorNodeRef,
+ }}
+ />
+ </li>
+ );
+}
+
function LayersContainer (props) {
const {
activeLayersMap,
@@ -43,14 +106,15 @@ function LayersContainer (props) {
* Update layer group order after drag/drop
* @param {*} result
*/
- const onDragEnd = (result) => {
- const { destination, source } = result;
- if (!destination || source.index === destination.index) {
- return;
- }
- const newGroups = Array.from(overlayGroups);
- const [removed] = newGroups.splice(source.index, 1);
- newGroups.splice(destination.index, 0, removed);
+ const onDragEnd = (event) => {
+ const { active, over } = event;
+ if (!over || active.id === over.id) return;
+
+ const sourceIndex = overlayGroups.findIndex(({ groupName }) => groupName === active.id);
+ const destinationIndex = overlayGroups.findIndex(({ groupName }) => groupName === over.id);
+ if (sourceIndex < 0 || destinationIndex < 0 || sourceIndex === destinationIndex) return;
+
+ const newGroups = arrayMove(overlayGroups, sourceIndex, destinationIndex);
const newLayers = newGroups
.flatMap(({ layers }) => layers)
.map((id) => activeLayersMap[id])
@@ -58,56 +122,31 @@ function LayersContainer (props) {
reorderOverlayGroups(newLayers, newGroups);
};
- const renderLayerList = (group, idx) => {
- const { groupName, layers, collapsed } = group;
- const layersForGroup = layers.map((id) => activeLayersMap[id]);
- return layers && (
- <Draggable
- key={groupName}
- draggableId={groupName}
- index={idx}
- isDragDisabled={isEmbedModeActive || isAnimating}
+ const renderOverlayGroups = () => (
+ <DndContext
+ collisionDetection={closestCenter}
+ onDragEnd={onDragEnd}
+ >
+ <SortableContext
+ items={overlayGroups.map(({ groupName }) => groupName)}
+ strategy={verticalListSortingStrategy}
>
- {(provided) => (
- <li
- id={`${compareState}-${util.cleanId(groupName)}`}
- ref={provided.innerRef}
- {...provided.draggableProps}
- >
- <LayerList
- title={groupName}
- groupId={groupName}
- collapsed={collapsed}
- toggleCollapse={toggleCollapse}
+ <ul>
+ {overlayGroups.map((group, idx) => (
+ <SortableOverlayGroup
+ key={group.groupName}
+ group={group}
compareState={compareState}
- layerSplit={overlays.length}
- layers={layersForGroup}
- dragHandleProps={provided.dragHandleProps}
+ activeLayersMap={activeLayersMap}
+ overlaysLength={overlays.length}
+ toggleCollapse={toggleCollapse}
+ isEmbedModeActive={isEmbedModeActive}
+ isAnimating={isAnimating}
/>
- </li>
- )}
- </Draggable>
- );
- };
-
- const renderOverlayGroups = () => (
- <DragDropContext onDragEnd={onDragEnd}>
- <Droppable
- droppableId="layerGroup"
- type="overlayGroups"
- direction="vertical"
- >
- {(provided, snapshot) => (
- <ul
- ref={provided.innerRef}
- {...provided.droppableProps}
- >
- {overlayGroups.map(renderLayerList)}
- {provided.placeholder}
- </ul>
- )}
- </Droppable>
- </DragDropContext>
+ ))}
+ </ul>
+ </SortableContext>
+ </DndContext>
);
let minHeight = '100px';
@@ -226,6 +265,16 @@ export default connect(
mapDispatchToProps,
)(LayersContainer);
+SortableOverlayGroup.propTypes = {
+ group: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
+ compareState: PropTypes.string,
+ activeLayersMap: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
+ overlaysLength: PropTypes.number,
+ toggleCollapse: PropTypes.func,
+ isEmbedModeActive: PropTypes.bool,
+ isAnimating: PropTypes.bool,
+};
+
LayersContainer.propTypes = {
activeLayersMap: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
baselayers: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['null'])]),
@@ -234,15 +283,9 @@ LayersContainer.propTypes = {
height: PropTypes.number,
isActive: PropTypes.bool,
isAnimating: PropTypes.bool,
- isCompareActive: PropTypes.bool,
isEmbedModeActive: PropTypes.bool,
- isMobile: PropTypes.bool,
overlayGroups: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['null'])]),
overlays: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['null'])]),
reorderOverlayGroups: PropTypes.func,
toggleCollapse: PropTypes.func,
- breakpoints: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
- isPlaying: PropTypes.bool,
- screenWidth: PropTypes.number,
- addLayers: PropTypes.func,
};