Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Chore] remove dnd-kit from dependency of utils and reducer, remove use of withState in dnd context (#3067)

Shan Hed5ef5f713643
diff --git a/src/common-utils/src/data.ts b/src/common-utils/src/data.ts
index 44c62dc27..cade710c5 100644
--- a/src/common-utils/src/data.ts
+++ b/src/common-utils/src/data.ts
@@ -50,6 +50,16 @@ export function toArray<T>(item: T | T[]): T[] {
return [item];
}
+/**
+ * Move an array item to a different position. Returns a new array with the item moved to the new position.
+ */
+export function arrayMove<T>(array: T[], from: number, to: number): T[] {
+ const newArray = array.slice();
+ newArray.splice(to < 0 ? newArray.length + to : to, 0, newArray.splice(from, 1)[0]);
+
+ return newArray;
+}
+
/**
* Check whether geojson linestring's 4th coordinate is 1) not timestamp 2) unix time stamp 3) real date time
* @param timestamps array to be tested if its elements are timestamp
diff --git a/src/constants/src/dnd-layer-items.ts b/src/components/src/common/dnd-layer-items.ts
similarity index 100%
rename from src/constants/src/dnd-layer-items.ts
rename to src/components/src/common/dnd-layer-items.ts
diff --git a/src/components/src/common/index.ts b/src/components/src/common/index.ts
index 7bc2f525e..3d613f6d6 100644
--- a/src/components/src/common/index.ts
+++ b/src/components/src/common/index.ts
@@ -42,6 +42,7 @@ export {
StyledPanelDropdown,
StyledPanelHeader,
StyledType,
+ shouldForwardProp,
TextArea,
TextAreaLight,
Tooltip,
diff --git a/src/components/src/common/item-selector/chickleted-input.tsx b/src/components/src/common/item-selector/chickleted-input.tsx
index 7c4f8c647..a3d3e3f44 100644
--- a/src/components/src/common/item-selector/chickleted-input.tsx
+++ b/src/components/src/common/item-selector/chickleted-input.tsx
@@ -5,11 +5,13 @@ import React, {MouseEventHandler, ReactNode, useMemo, useCallback} from 'react';
import styled, {IStyledComponent} from 'styled-components';
import {DndContext, DragOverlay, pointerWithin} from '@dnd-kit/core';
-import {SortableContext, useSortable, arrayMove} from '@dnd-kit/sortable';
+import {SortableContext, useSortable} from '@dnd-kit/sortable';
import {restrictToParentElement} from '@dnd-kit/modifiers';
-import Delete from '../icons/delete';
import {FormattedMessage} from '@kepler.gl/localization';
+import {arrayMove} from '@kepler.gl/common-utils';
+
+import Delete from '../icons/delete';
import {BaseComponentProps} from '../../types';
import {shouldForwardProp} from '../styled-components';
diff --git a/src/components/src/dnd-context.tsx b/src/components/src/dnd-context.tsx
index 59cae3cf4..0ef76060f 100644
--- a/src/components/src/dnd-context.tsx
+++ b/src/components/src/dnd-context.tsx
@@ -4,22 +4,21 @@
import React, {useCallback, useMemo, PropsWithChildren} from 'react';
import styled from 'styled-components';
import {DndContext as DndKitContext, DragOverlay} from '@dnd-kit/core';
-import Console from 'global/console';
-import {
- DND_EMPTY_MODIFIERS,
- DND_MODIFIERS,
- SORTABLE_LAYER_TYPE,
- SORTABLE_EFFECT_TYPE
-} from '@kepler.gl/constants';
-import {visStateLens} from '@kepler.gl/reducers';
+import Console from 'global/console';
import {VisState} from '@kepler.gl/schemas';
import LayerPanelHeaderFactory from './side-panel/layer-panel/layer-panel-header';
-import {withState} from './injector';
import useDndLayers from './hooks/use-dnd-layers';
import useDndEffects from './hooks/use-dnd-effects';
+import {
+ DND_MODIFIERS,
+ DND_EMPTY_MODIFIERS,
+ SORTABLE_LAYER_TYPE,
+ SORTABLE_EFFECT_TYPE
+} from './common/dnd-layer-items';
+
export type DndContextProps = PropsWithChildren<{
visState: VisState;
}>;
@@ -39,7 +38,7 @@ DndContextFactory.deps = [LayerPanelHeaderFactory];
function DndContextFactory(
LayerPanelHeader: ReturnType<typeof LayerPanelHeaderFactory>
-): React.FC<PropsWithChildren> {
+): React.FC<DndContextProps> {
const LayerPanelOverlay = ({layer, datasets}) => {
const color =
layer.config.dataId && datasets[layer.config.dataId]
@@ -116,7 +115,7 @@ function DndContextFactory(
},
[onLayerDragEnd, onEffectDragEnd]
);
-
+ console.log('dndContext', {activeLayer});
return (
<DndKitContext onDragStart={onDragStart} onDragEnd={onDragEnd} modifiers={dndModifiers}>
{children}
@@ -131,7 +130,7 @@ function DndContextFactory(
);
};
- return withState([visStateLens], state => state)(DndContext) as React.FC<PropsWithChildren>;
+ return DndContext;
}
export default DndContextFactory;
diff --git a/src/components/src/effects/effect-list.tsx b/src/components/src/effects/effect-list.tsx
index f935d4eec..ddf1409e4 100644
--- a/src/components/src/effects/effect-list.tsx
+++ b/src/components/src/effects/effect-list.tsx
@@ -7,7 +7,7 @@ import classnames from 'classnames';
import {CSS} from '@dnd-kit/utilities';
import {useSortable, SortableContext, verticalListSortingStrategy} from '@dnd-kit/sortable';
-import {dataTestIds, SORTABLE_EFFECT_TYPE, SORTABLE_EFFECT_PANEL_TYPE} from '@kepler.gl/constants';
+import {dataTestIds} from '@kepler.gl/constants';
import {findById} from '@kepler.gl/utils';
import {Effect} from '@kepler.gl/types';
import {
@@ -18,6 +18,7 @@ import {
ActionHandler
} from '@kepler.gl/actions';
+import {SORTABLE_EFFECT_TYPE, SORTABLE_EFFECT_PANEL_TYPE} from '../common/dnd-layer-items';
import EffectPanelFactory from './effect-panel';
export type EffectListProps = {
diff --git a/src/components/src/hooks/use-dnd-effects.tsx b/src/components/src/hooks/use-dnd-effects.tsx
index fff2c0897..9840ff05d 100644
--- a/src/components/src/hooks/use-dnd-effects.tsx
+++ b/src/components/src/hooks/use-dnd-effects.tsx
@@ -6,9 +6,9 @@ import {useDispatch} from 'react-redux';
import {DragEndEvent, DragStartEvent} from '@dnd-kit/core';
import {reorderEffect, updateEffect} from '@kepler.gl/actions';
-import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '@kepler.gl/constants';
import {reorderEffectOrder} from '@kepler.gl/utils';
import {Effect} from '@kepler.gl/types';
+import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '../common/dnd-layer-items';
type DndEffectsHook = {
activeEffect: Effect | undefined;
diff --git a/src/components/src/hooks/use-dnd-layers.tsx b/src/components/src/hooks/use-dnd-layers.tsx
index 262a5c3be..f4d8780fe 100644
--- a/src/components/src/hooks/use-dnd-layers.tsx
+++ b/src/components/src/hooks/use-dnd-layers.tsx
@@ -9,7 +9,7 @@ import {
DROPPABLE_MAP_CONTAINER_TYPE,
SORTABLE_LAYER_TYPE,
SORTABLE_SIDE_PANEL_TYPE
-} from '@kepler.gl/constants';
+} from '../common/dnd-layer-items';
import {reorderLayerOrder} from '@kepler.gl/reducers';
import {Layer} from '@kepler.gl/layers';
diff --git a/src/components/src/index.ts b/src/components/src/index.ts
index 4e15b2fd9..a7e9aa6b9 100644
--- a/src/components/src/index.ts
+++ b/src/components/src/index.ts
@@ -437,6 +437,7 @@ export {
StyledPanelDropdown,
StyledPanelHeader,
StyledType,
+ shouldForwardProp,
TextArea,
TextAreaLight,
Tooltip,
diff --git a/src/components/src/kepler-gl.tsx b/src/components/src/kepler-gl.tsx
index 3749e8dd8..44c6443a5 100644
--- a/src/components/src/kepler-gl.tsx
+++ b/src/components/src/kepler-gl.tsx
@@ -608,7 +608,7 @@ function KeplerGlFactory(
ref={this.root}
>
<NotificationPanel {...notificationPanelFields} />
- <DndContext>
+ <DndContext visState={visState}>
{!uiState.readOnly && !readOnly && <SidePanel {...sideFields} />}
<MapsLayout className="maps" mapState={this.props.mapState}>
{mapContainers}
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index 297445024..518d708b1 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -70,10 +70,10 @@ import {
THROTTLE_NOTIFICATION_TIME,
DEFAULT_PICKING_RADIUS,
NO_MAP_ID,
- EMPTY_MAPBOX_STYLE,
- DROPPABLE_MAP_CONTAINER_TYPE
+ EMPTY_MAPBOX_STYLE
} from '@kepler.gl/constants';
+import {DROPPABLE_MAP_CONTAINER_TYPE} from './common/dnd-layer-items';
// Contexts
import {MapViewStateContext} from './map-view-state-context';
diff --git a/src/components/src/side-panel/layer-panel/layer-list.tsx b/src/components/src/side-panel/layer-panel/layer-list.tsx
index d774a58d3..3e9f90400 100644
--- a/src/components/src/side-panel/layer-panel/layer-list.tsx
+++ b/src/components/src/side-panel/layer-panel/layer-list.tsx
@@ -13,8 +13,9 @@ import {useSortable, SortableContext, verticalListSortingStrategy} from '@dnd-ki
import {CSS} from '@dnd-kit/utilities';
import LayerPanelFactory from './layer-panel';
import {findById} from '@kepler.gl/utils';
-import {dataTestIds, SORTABLE_LAYER_TYPE, SORTABLE_SIDE_PANEL_TYPE} from '@kepler.gl/constants';
+import {dataTestIds} from '@kepler.gl/constants';
import {SplitMap} from '@kepler.gl/types';
+import {SORTABLE_LAYER_TYPE, SORTABLE_SIDE_PANEL_TYPE} from '../../common/dnd-layer-items';
export type LayerListProps = {
datasets: Datasets;
diff --git a/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx b/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx
index f5058d9ad..691176734 100644
--- a/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx
+++ b/src/components/src/side-panel/layer-panel/radius-by-zoom-input.tsx
@@ -8,7 +8,7 @@ import {sortableContainer, sortableElement, sortableHandle} from 'react-sortable
import styled, {css} from 'styled-components';
import {ZoomStops, ZoomStopsConfig} from '@kepler.gl/types';
-import {arrayMove} from '@kepler.gl/utils';
+import {arrayMove} from '@kepler.gl/common-utils';
import {Icons, Input, PanelLabel, Button} from '../../common';
diff --git a/src/constants/package.json b/src/constants/package.json
index 074cf5c72..15da06217 100644
--- a/src/constants/package.json
+++ b/src/constants/package.json
@@ -30,8 +30,6 @@
"umd"
],
"dependencies": {
- "@dnd-kit/core": "^6.1.0",
- "@dnd-kit/modifiers": "^7.0.0",
"@kepler.gl/types": "3.1.2",
"@types/d3-scale": "^3.2.2",
"@types/keymirror": "^0.1.1",
diff --git a/src/constants/src/index.ts b/src/constants/src/index.ts
index 629b35a7d..fcda42781 100644
--- a/src/constants/src/index.ts
+++ b/src/constants/src/index.ts
@@ -12,5 +12,4 @@ export {default as KeyEvent} from './keyevent';
export * from './tooltip';
export * from './user-feedbacks';
export * from './user-guides';
-export * from './dnd-layer-items';
export * from './time';
diff --git a/src/reducers/package.json b/src/reducers/package.json
index c98a32bb4..4d06aacb9 100644
--- a/src/reducers/package.json
+++ b/src/reducers/package.json
@@ -30,7 +30,6 @@
"umd"
],
"dependencies": {
- "@dnd-kit/sortable": "^8.0.0",
"@kepler.gl/actions": "3.1.2",
"@kepler.gl/cloud-providers": "3.1.2",
"@kepler.gl/common-utils": "3.1.2",
diff --git a/src/reducers/src/layer-utils.ts b/src/reducers/src/layer-utils.ts
index 46d013889..36139c102 100644
--- a/src/reducers/src/layer-utils.ts
+++ b/src/reducers/src/layer-utils.ts
@@ -3,7 +3,6 @@
import Console from 'global/console';
-import {arrayMove} from '@dnd-kit/sortable';
import {GEOCODER_LAYER_ID} from '@kepler.gl/constants';
import {Layer as DeckLayer, LayerProps as DeckLayerProps} from '@deck.gl/core/typed';
import {
@@ -30,6 +29,8 @@ import {
import KeplerTable from '@kepler.gl/table';
import {VisState} from '@kepler.gl/schemas';
import {isFunction, getMapLayersFromSplitMaps, DataRow} from '@kepler.gl/utils';
+import {arrayMove} from '@kepler.gl/common-utils';
+
import {ThreeDBuildingLayer} from '@kepler.gl/deckgl-layers';
export type LayersToRender = {
diff --git a/src/utils/package.json b/src/utils/package.json
index 80bd1107a..f86ccff15 100644
--- a/src/utils/package.json
+++ b/src/utils/package.json
@@ -31,7 +31,6 @@
],
"dependencies": {
"@deck.gl/core": "^8.9.27",
- "@dnd-kit/sortable": "^8.0.0",
"@kepler.gl/common-utils": "3.1.2",
"@kepler.gl/constants": "3.1.2",
"@kepler.gl/types": "3.1.2",
diff --git a/src/utils/src/color-utils.ts b/src/utils/src/color-utils.ts
index d1027befe..92d12447b 100644
--- a/src/utils/src/color-utils.ts
+++ b/src/utils/src/color-utils.ts
@@ -7,6 +7,7 @@ import {
DEFAULT_CUSTOM_PALETTE,
colorPaletteToColorRange
} from '@kepler.gl/constants';
+import {arrayMove} from '@kepler.gl/common-utils';
import {
ColorMap,
ColorRange,
@@ -17,7 +18,7 @@ import {
} from '@kepler.gl/types';
import {rgb as d3Rgb} from 'd3-color';
import {interpolate} from 'd3-interpolate';
-import {arrayInsert, arrayMove} from './utils';
+import {arrayInsert} from './utils';
import Console from 'global/console';
import {KEPLER_COLOR_PALETTES, PALETTE_TYPES, SCALE_TYPES} from '@kepler.gl/constants';
diff --git a/src/utils/src/effect-utils.ts b/src/utils/src/effect-utils.ts
index acd2e3672..9834f6329 100644
--- a/src/utils/src/effect-utils.ts
+++ b/src/utils/src/effect-utils.ts
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import {arrayMove} from '@dnd-kit/sortable';
import SunCalc from 'suncalc';
import cloneDeep from 'lodash.clonedeep';
@@ -13,7 +12,7 @@ import {
FILTER_TYPES,
FILTER_VIEW_TYPES
} from '@kepler.gl/constants';
-// import {VisState} from '@kepler.gl/schemas';
+import {arrayMove} from '@kepler.gl/common-utils';
import {MapState, Effect, EffectProps, EffectDescription} from '@kepler.gl/types';
import {findById} from './utils';
import {clamp} from './data-utils';
diff --git a/src/utils/src/utils.ts b/src/utils/src/utils.ts
index f9646d563..5a8395fa5 100644
--- a/src/utils/src/utils.ts
+++ b/src/utils/src/utils.ts
@@ -177,23 +177,6 @@ export function arrayInsert<T>(arr: T[], index: number, val: T): T[] {
return [...arr.slice(0, index), val, ...arr.slice(index)];
}
-const arrayMoveMutate = (array, from, to) => {
- array.splice(to < 0 ? array.length + to : to, 0, array.splice(from, 1)[0]);
-};
-
-/**
- *
- * @param {any[]} array
- * @param {number} from
- * @param {number} to
- * @returns {any[]}
- */
-export const arrayMove = <T>(array: T[], from: number, to: number): T[] => {
- array = array.slice();
- arrayMoveMutate(array, from, to);
- return array;
-};
-
export function hasMobileWidth(breakPointValues: {palm: number; desk: number}): boolean {
const mobileWidth = Window.matchMedia(`(max-width: ${breakPointValues.palm}px)`);
return mobileWidth.matches;
diff --git a/test/browser/components/hooks/use-dnd-effects.spec.js b/test/browser/components/hooks/use-dnd-effects.spec.js
index 743ad03f0..5448e219d 100644
--- a/test/browser/components/hooks/use-dnd-effects.spec.js
+++ b/test/browser/components/hooks/use-dnd-effects.spec.js
@@ -5,7 +5,10 @@ import {renderHook, act} from '@testing-library/react';
import {useDispatch} from 'react-redux';
import {useDndEffects} from '@kepler.gl/components';
import {reorderEffect, updateEffect} from '@kepler.gl/actions';
-import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '@kepler.gl/constants';
+import {
+ SORTABLE_EFFECT_PANEL_TYPE,
+ SORTABLE_EFFECT_TYPE
+} from '@kepler.gl/components/common/dnd-layer-items';
import {reorderEffectOrder} from '@kepler.gl/utils';
// Mock useDispatch hook
diff --git a/test/browser/components/hooks/use-dnd-layers.spec.js b/test/browser/components/hooks/use-dnd-layers.spec.js
index 83cb549a4..0315d024d 100644
--- a/test/browser/components/hooks/use-dnd-layers.spec.js
+++ b/test/browser/components/hooks/use-dnd-layers.spec.js
@@ -9,7 +9,7 @@ import {
DROPPABLE_MAP_CONTAINER_TYPE,
SORTABLE_LAYER_TYPE,
SORTABLE_SIDE_PANEL_TYPE
-} from '@kepler.gl/constants';
+} from '@kepler.gl/components/common/dnd-layer-items';
import {reorderLayerOrder} from '@kepler.gl/reducers';
// Mock useDispatch hook
diff --git a/test/node/utils/util-test.js b/test/node/utils/util-test.js
index 0bc7d450f..ed099c15d 100644
--- a/test/node/utils/util-test.js
+++ b/test/node/utils/util-test.js
@@ -3,14 +3,13 @@
import {
arrayInsert,
- arrayMove,
camelToTitle,
camelize,
capitalizeFirstLetter,
getError,
set
} from '@kepler.gl/utils';
-import {toArray} from '@kepler.gl/common-utils';
+import {toArray, arrayMove} from '@kepler.gl/common-utils';
import test from 'tape';
test('Utils -> set', t => {
diff --git a/yarn.lock b/yarn.lock
index 59838f1c8..0b9879230 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3326,8 +3326,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@kepler.gl/constants@workspace:src/constants"
dependencies:
- "@dnd-kit/core": "npm:^6.1.0"
- "@dnd-kit/modifiers": "npm:^7.0.0"
"@kepler.gl/types": "npm:3.1.2"
"@types/d3-scale": "npm:^3.2.2"
"@types/d3-time": "npm:^2"
@@ -3532,7 +3530,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@kepler.gl/reducers@workspace:src/reducers"
dependencies:
- "@dnd-kit/sortable": "npm:^8.0.0"
"@kepler.gl/actions": "npm:3.1.2"
"@kepler.gl/cloud-providers": "npm:3.1.2"
"@kepler.gl/common-utils": "npm:3.1.2"
@@ -3660,7 +3657,6 @@ __metadata:
resolution: "@kepler.gl/utils@workspace:src/utils"
dependencies:
"@deck.gl/core": "npm:^8.9.27"
- "@dnd-kit/sortable": "npm:^8.0.0"
"@kepler.gl/common-utils": "npm:3.1.2"
"@kepler.gl/constants": "npm:3.1.2"
"@kepler.gl/types": "npm:3.1.2"