_updateFeature()

in modules/react-map-gl-draw/src/edit-modes/editing-mode.ts [164:238]


  _updateFeature(props: ModeProps<FeatureCollection>, type: string, options: any = {}) {
    const { data, selectedIndexes, viewport } = props;

    const featureIndex = selectedIndexes && selectedIndexes[0];
    const feature = this.getSelectedFeature(props, featureIndex);

    let geometry = null;
    const coordinates = getFeatureCoordinates(feature);
    if (!coordinates) {
      return null;
    }

    // @ts-expect-error narrow coordinates' type
    let newCoordinates = [...coordinates];

    switch (type) {
      case 'editHandle':
        const positionIndexes =
          feature.geometry.type === GEOJSON_TYPE.POLYGON
            ? [0, options.editHandleIndex]
            : [options.editHandleIndex];

        return new ImmutableFeatureCollection(data)
          .replacePosition(featureIndex, positionIndexes, options.mapCoords)
          .getObject();

      case 'feature':
        const { dx, dy } = options;

        newCoordinates = newCoordinates
          .map((mapCoords) => {
            const pixels = viewport && viewport.project(mapCoords);
            if (pixels) {
              pixels[0] += dx;
              pixels[1] += dy;
              return viewport && viewport.unproject(pixels);
            }
            return null;
          })
          .filter(Boolean);
        geometry = {
          type: feature.geometry.type,
          coordinates:
            feature.geometry.type === GEOJSON_TYPE.POLYGON
              ? [newCoordinates]
              : feature.geometry.type === GEOJSON_TYPE.POINT
              ? newCoordinates[0]
              : newCoordinates,
        };

        return new ImmutableFeatureCollection(data)
          .replaceGeometry(featureIndex, geometry)
          .getObject();

      case 'rectangle':
        // moved editHandleIndex and destination mapCoords
        newCoordinates = updateRectanglePosition(
          // @ts-expect-error turf type diff
          feature,
          options.editHandleIndex,
          options.mapCoords
        );
        geometry = {
          type: GEOJSON_TYPE.POLYGON,
          coordinates: newCoordinates,
        };

        return new ImmutableFeatureCollection(data)
          .replaceGeometry(featureIndex, geometry)
          .getObject();

      default:
        return data && new ImmutableFeatureCollection(data).getObject();
    }
  }