Skip to content

Commit 00465d0

Browse files
authored
feat(core): Add View.clone() method (#9588)
1 parent b04187d commit 00465d0

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

docs/api-reference/core/view.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ Returns:
115115
Note: For speed, deck.gl uses shallow equality. This means that a value of `false` does not guarantee that the views are not equivalent.
116116

117117

118+
#### `clone` {#clone}
119+
120+
<img src="https://img.shields.io/badge/from-v9.2-green.svg?style=flat-square" alt="from v9.2" />
121+
122+
```js
123+
view.clone(newProps)
124+
```
125+
126+
Creates a new `View` instance by merging the existing view's props with the provided `newProps`.
127+
128+
Parameters:
129+
130+
* `newProps` (Object) - Partial view props to override on the cloned view.
131+
132+
Returns:
133+
134+
* `View` - A new view instance with merged props.
135+
118136
#### `makeViewport` {#makeviewport}
119137

120138
```js

docs/whats-new.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
This page contains highlights of each deck.gl release. Also check our [vis.gl blog](https://medium.com/vis-gl) for news about new releases and features in deck.gl.
44

5+
## deck.gl v9.2
6+
7+
Target release date: Q2, 2025
8+
59
### Widgets
610

711
- React: Pre-wrapped React components for the new deck.gl widgets are available via the [`@deck.gl/react`](./api-reference/react/overview.md) package, including: `ResetViewWidget`, `ScaleWidget`, `GeolocateWidget`, `ScreenshotWidget`, `LoadingWidget`, `ThemeWidget`, `InfoWidget`, and `SplitterWidget` (in addition to the `ZoomWidget`, `CompassWidget`, `FullscreenWidget` from v9.1)
812

13+
## Core
14+
15+
- [`View.clone()`](./api-reference/core/view.md) - New method that simplifies creating new Views with modified props, similar to `Layer.clone()`.
16+
917
## deck.gl v9.1
1018

1119
Release date: Jan 21, 2025

modules/core/src/views/view.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ export default abstract class View<
105105
return this.constructor === view.constructor && deepEqual(this.props, view.props, 2);
106106
}
107107

108+
/** Clone this view with modified props */
109+
clone(newProps: Partial<ViewProps>): this {
110+
const ViewConstructor = this.constructor as new (props: ViewProps) => this;
111+
return new ViewConstructor({...this.props, ...newProps});
112+
}
113+
108114
/** Make viewport from canvas dimensions and view state */
109115
makeViewport({width, height, viewState}: {width: number; height: number; viewState: ViewState}) {
110116
viewState = this.filterViewState(viewState);

test/modules/core/views/view.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ test('View#imports', t => {
1919
t.end();
2020
});
2121

22+
test('View#clone', t => {
23+
const view = new MapView({
24+
id: 'test-view',
25+
latitude: 0,
26+
longitude: 0,
27+
zoom: 1
28+
});
29+
const identicalClone = view.clone({});
30+
t.ok(identicalClone instanceof MapView, 'identical clone is an instance of MapView');
31+
t.ok(identicalClone !== view, 'identical clone is a new instance');
32+
t.ok(identicalClone.equals(view), 'identical clone.equals() is true');
33+
34+
const clone = view.clone({
35+
id: 'cloned-view',
36+
zoom: 5
37+
});
38+
t.is(clone.id, 'cloned-view', 'modified clone id is overridden');
39+
t.is(clone.props.zoom, 5, 'modified clone prop zoom is overridden');
40+
t.is(clone.props.latitude, view.props.latitude, 'other props are preserved');
41+
t.is(clone.props.longitude, view.props.longitude, 'other props are preserved');
42+
t.end();
43+
});
44+
2245
test('View#equals', t => {
2346
const mapView1 = new MapView({
2447
id: 'default-view',

0 commit comments

Comments
 (0)