-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 新增 swipe 控件 #216
Open
zhaoqc-max
wants to merge
2
commits into
main
Choose a base branch
from
feat/swipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: 新增 swipe 控件 #216
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { LarkMap, RasterLayer, SwipeControl, useLayer } from '@antv/larkmap'; | ||
import React from 'react'; | ||
|
||
const CustomComponent = () => { | ||
const rightRasterLayer = useLayer('rightRasterLayer'); | ||
return ( | ||
<SwipeControl orientation="vertical" ratio={0.5} layers={['leftRasterLayer']} rightLayers={[rightRasterLayer]} /> | ||
); | ||
}; | ||
|
||
export default () => { | ||
return ( | ||
<LarkMap mapType="Gaode" style={{ height: '400px' }}> | ||
<RasterLayer | ||
id="rightRasterLayer" | ||
source={{ | ||
data: 'https://tiles{1-3}.geovisearth.com/base/v1/ter/{z}/{x}/{y}?format=webp&tmsIds=w&token=b2a0cfc132cd60b61391b9dd63c15711eadb9b38a9943e3f98160d5710aef788', | ||
parser: { maxZoom: 21, minZoom: 3, type: 'rasterTile', tileSize: 256, zoomOffset: 0 }, | ||
}} | ||
/> | ||
<RasterLayer | ||
id="leftRasterLayer" | ||
source={{ | ||
data: 'https://tiles{1-3}.geovisearth.com/base/v1/img/{z}/{x}/{y}?format=webp&tmsIds=w&token=b2a0cfc132cd60b61391b9dd63c15711eadb9b38a9943e3f98160d5710aef788', | ||
parser: { | ||
type: 'rasterTile', | ||
tileSize: 256, | ||
zoomOffset: 0, | ||
}, | ||
}} | ||
/> | ||
<CustomComponent /> | ||
</LarkMap> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
toc: content | ||
order: 2 | ||
group: | ||
title: 控件组件 | ||
order: 2 | ||
nav: | ||
title: 组件 | ||
path: /components | ||
--- | ||
|
||
## 卷帘 - SwipeControl | ||
|
||
### 介绍 | ||
|
||
该控件用于分屏对比两个地图上叠加图层,图层可以添加到地图的左侧(顶部)或右侧(底部)。未添加到卷帘上的图层将显示在两侧。 | ||
|
||
### 代码演示 | ||
|
||
<code src="./demos/default.tsx" defaultShowCode compact></code> | ||
|
||
### API | ||
|
||
| 参数 | 说明 | 类型 | | ||
| --- | --- | --- | | ||
| orientation | 卷帘方向设置,默认 'vertical' | `vertical|horizontal` | | ||
| ratio | 卷帘的位置,值域为 0 到 1, 默认正中间为 0.5 | `number` | | ||
| layers | 卷帘左侧的图层 | `Array<ILayer|string>` | | ||
| rightLayers | 卷帘左侧的图层 | `Array<ILayer|string>` | | ||
| className | 自定义样式名 | `string` | | ||
| style | 自定义样式 | `CSSProperties` | | ||
| onAdd | 组件被添加时的回调 | `(this) => void` | | ||
| onRemove | 组件被移除时的回调 | `(this) => void` | | ||
| onShow | 组件显示时的回调 | `(this) => void` | | ||
| onHide | 组件隐藏时的回调 | `(this) => void` | | ||
| onMoving | 卷帘移动事件 | `(data: {size: number[], ratio: number[]}) => void` | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { IZoomControlOption } from '@antv/l7'; | ||
import { Swipe as L7Swipe } from '@antv/l7'; | ||
import { omitBy } from 'lodash-es'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { getStyleText } from '../../../utils'; | ||
import { useLayerList, useScene } from '../../LarkMap/hooks'; | ||
import { useL7ComponentEvent, useL7ComponentUpdate } from '../hooks'; | ||
import type { SwipeControlProps } from './types'; | ||
|
||
export const SwipeControl: React.FC<SwipeControlProps> = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还是与 L7 对应一致叫 Swipe,放分析组件栏目里面吧 |
||
onShow, | ||
onHide, | ||
onAdd, | ||
onRemove, | ||
onMoving, | ||
className, | ||
style, | ||
rightLayers, | ||
layers: layerItems, | ||
ratio, | ||
orientation, | ||
}) => { | ||
const scene = useScene(); | ||
const [control, setControl] = useState<L7Swipe | undefined>(); | ||
const styleText = useMemo(() => getStyleText(style), [style]); | ||
const fullLayerList = useLayerList(); | ||
|
||
const layers = (defaultLayers) => { | ||
return defaultLayers?.length | ||
? defaultLayers | ||
.map((layerItem) => { | ||
if (typeof layerItem === 'string') { | ||
return fullLayerList.find((layer) => layer.id === layerItem); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(layerItem, 'isComposite')) { | ||
//@ts-ignore | ||
const { layer } = layerItem; | ||
|
||
if (typeof layer === 'string') { | ||
const targetLayer = fullLayerList.find((item) => item.id === layer); | ||
return targetLayer ? { ...layerItem, layer: targetLayer } : undefined; | ||
} | ||
return layerItem; | ||
} | ||
|
||
return layerItem; | ||
}) | ||
.filter((layer) => !!layer) | ||
.map((layerItem) => { | ||
//@ts-ignore | ||
return layerItem.subLayers ? layerItem.subLayers.getLayers().map((layer) => layer.layer) : layerItem.layer; | ||
}) | ||
.flat() | ||
: fullLayerList; | ||
}; | ||
|
||
const controlOptions: Partial<IZoomControlOption> = useMemo(() => { | ||
return { | ||
rightLayers: layers(rightLayers), | ||
layers: layers(layerItems), | ||
ratio, | ||
orientation, | ||
style: styleText, | ||
}; | ||
}, [className, styleText, rightLayers, layers, ratio, orientation]); | ||
Check warning on line 65 in src/components/Control/SwipeControl/index.tsx GitHub Actions / lint (16.x)
|
||
|
||
useEffect(() => { | ||
const swipe = new L7Swipe(omitBy(controlOptions, (value) => value === undefined)); | ||
setControl(swipe); | ||
scene.addControl(swipe); | ||
return () => { | ||
scene.removeControl(swipe); | ||
setControl(undefined); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
useL7ComponentUpdate(control, controlOptions); | ||
|
||
useL7ComponentEvent(control, { | ||
add: onAdd, | ||
remove: onRemove, | ||
show: onShow, | ||
hide: onHide, | ||
moving: onMoving, | ||
}); | ||
|
||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { ISwipeControlOption, Zoom as L7Zoom } from '@antv/l7'; | ||
import type { CSSProperties } from 'react'; | ||
import type { IControlCallback, Layer } from '../../../types'; | ||
|
||
export interface SwipeControlProps | ||
extends Omit<Partial<ISwipeControlOption>, 'style' | 'layers'>, | ||
IControlCallback<L7Zoom> { | ||
style?: CSSProperties; | ||
onMoving?: (data: { size: number[]; ratio: number[] }) => void; | ||
layers: (Layer | string)[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
放分析组件里面吧