From 239c99948dc7ee3d2b8361cc7814aa49f3ad13ef Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Fri, 3 Apr 2020 14:25:34 +0800 Subject: [PATCH] refactor: ts error --- .../draw/__tests__/util/move_featrues.spec.ts | 6 +++--- packages/draw/src/modes/draw_mode.ts | 2 +- packages/draw/src/modes/draw_polygon.ts | 15 ++++++++------- packages/draw/src/render/renderFeature.ts | 2 +- packages/maps/src/mapbox/map.ts | 3 ++- packages/scene/src/IMapController.ts | 1 + yarn.lock | 2 +- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/draw/__tests__/util/move_featrues.spec.ts b/packages/draw/__tests__/util/move_featrues.spec.ts index e53fdd0ced..0c84905860 100644 --- a/packages/draw/__tests__/util/move_featrues.spec.ts +++ b/packages/draw/__tests__/util/move_featrues.spec.ts @@ -51,13 +51,13 @@ describe('moveFeature', () => { const res = moveFeature([pointFeature], delta) as Array< Feature >; - expect(res[0].geometry.coordinates).toEqual([131, 48]); + expect(res[0]?.geometry?.coordinates).toEqual([131, 48]); }); it('move BBox', () => { const res = moveFeature([polyon], delta) as Array< Feature >; - expect(res[0].geometry.coordinates).toEqual([ + expect(res[0]?.geometry?.coordinates).toEqual([ [ [50.5703125, 46.583289756006316], [72.3671875, 46.583289756006316], @@ -71,7 +71,7 @@ describe('moveFeature', () => { const res = moveFeature([line], delta) as Array< Feature >; - expect(res[0].geometry.coordinates).toEqual([ + expect(res[0]?.geometry?.coordinates).toEqual([ [55.31640625, 63.91523303947614], [72.015625, 63.59334083012024], [71.48828125, 59.07787626787517], diff --git a/packages/draw/src/modes/draw_mode.ts b/packages/draw/src/modes/draw_mode.ts index 930d32d7e9..1f4191ca64 100644 --- a/packages/draw/src/modes/draw_mode.ts +++ b/packages/draw/src/modes/draw_mode.ts @@ -80,7 +80,7 @@ export default abstract class DrawMode extends EventEmitter { throw new Error('子类未实现该方法'); } - public getCurrentVertex(feature: Feature) { + public getCurrentVertex() { return this.currentVertex; } public getCurrentFeature() { diff --git a/packages/draw/src/modes/draw_polygon.ts b/packages/draw/src/modes/draw_polygon.ts index 049689a50b..8cd6b0b252 100644 --- a/packages/draw/src/modes/draw_polygon.ts +++ b/packages/draw/src/modes/draw_polygon.ts @@ -59,12 +59,12 @@ export default class DrawPolygon extends DrawFeature { public addVertex(vertex: Feature) { // @ts-ignore const id = vertex.properties.id; - const coord = vertex.geometry.coordinates as Position; + const coord = vertex?.geometry?.coordinates as Position; const feature = this.currentFeature as Feature; - const type = feature.geometry.type; + const type = feature?.geometry?.type; const points = []; if (type === 'Polygon') { - const coords = feature.geometry.coordinates as Position[][]; + const coords = feature?.geometry?.coordinates as Position[][]; coords[0].splice(id + 1, 0, coord); for (let i = 0; i < coords[0].length - 1; i++) { points.push({ @@ -73,7 +73,7 @@ export default class DrawPolygon extends DrawFeature { }); } } else { - const coords = feature.geometry.coordinates as Position[]; + const coords = feature?.geometry?.coordinates as Position[]; coords.splice(id + 1, 0, coord); for (const coor of coords) { points.push({ @@ -169,6 +169,7 @@ export default class DrawPolygon extends DrawFeature { } else { // @ts-ignore const id = selectVertexed.properties.id * 1; + // @ts-ignore selectVertexed.geometry.coordinates = [vertex.lng, vertex.lat]; // @ts-ignore this.pointFeatures[id].geometry.coordinates = [vertex.lng, vertex.lat]; @@ -226,15 +227,15 @@ export default class DrawPolygon extends DrawFeature { private editPolygonVertex(id: number, vertex: ILngLat) { const feature = this.currentFeature as Feature; - const type = feature.geometry.type; + const type = feature?.geometry?.type; if (type === 'Polygon') { - const coords = feature.geometry.coordinates as Position[][]; + const coords = feature?.geometry?.coordinates as Position[][]; coords[0][id] = [vertex.lng, vertex.lat]; if (-id === 0) { coords[0][coords[0].length - 1] = [vertex.lng, vertex.lat]; } } else { - const coords = feature.geometry.coordinates as Position[]; + const coords = feature?.geometry?.coordinates as Position[]; coords[id] = [vertex.lng, vertex.lat]; } this.setCurrentFeature(feature); diff --git a/packages/draw/src/render/renderFeature.ts b/packages/draw/src/render/renderFeature.ts index 6cb0b80717..894d2acc6d 100644 --- a/packages/draw/src/render/renderFeature.ts +++ b/packages/draw/src/render/renderFeature.ts @@ -1,7 +1,7 @@ import { ILayer, LineLayer, PointLayer, PolygonLayer } from '@antv/l7'; import { FeatureCollection } from '@turf/helpers'; export function renderFeature(fe: FeatureCollection, style: any): ILayer[] { - const type = fe.features[0].geometry.type; + const type = fe.features[0]?.geometry?.type; let layers; switch (type) { case 'Point': diff --git a/packages/maps/src/mapbox/map.ts b/packages/maps/src/mapbox/map.ts index 0e5cc36962..6c6b1668c9 100644 --- a/packages/maps/src/mapbox/map.ts +++ b/packages/maps/src/mapbox/map.ts @@ -91,8 +91,9 @@ export default class MapboxService public getContainer(): HTMLElement | null { return this.map.getContainer(); } + public getMapCanvasContainer(): HTMLElement { - return this.map.getCanvasContainer(); + return this.map.getCanvasContainer() as HTMLElement; } public getSize(): [number, number] { diff --git a/packages/scene/src/IMapController.ts b/packages/scene/src/IMapController.ts index fe223f09ba..6930502c59 100644 --- a/packages/scene/src/IMapController.ts +++ b/packages/scene/src/IMapController.ts @@ -59,6 +59,7 @@ export default interface IMapController { // get map params getType(): string; getMapContainer(): HTMLElement | null; + getMapCanvasContainer(): HTMLElement; // control with raw map setRotation(rotation: number): void; diff --git a/yarn.lock b/yarn.lock index ff67ea93ea..bed22e09e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4179,7 +4179,7 @@ dependencies: "@turf/helpers" "^5.1.5" -"@turf/midpoint@5.1.x": +"@turf/midpoint@5.1.x", "@turf/midpoint@^5.1.5": version "5.1.5" resolved "https://registry.npmjs.org/@turf/midpoint/-/midpoint-5.1.5.tgz#e261f6b2b0ea8124cceff552a262dd465c9d05f0" integrity sha1-4mH2srDqgSTM7/VSomLdRlydBfA=