handleClick()

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


  handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
    const picked = event.picks && event.picks[0];
    const selectedFeatureIndex = props.selectedIndexes && props.selectedIndexes[0];
    if (!picked || !picked.object || picked.featureIndex !== selectedFeatureIndex) {
      return;
    }

    const { type: objectType, featureIndex, index } = picked;
    const feature = this.getSelectedFeature(props, featureIndex);

    if (
      feature &&
      (feature.geometry.type === GEOJSON_TYPE.POLYGON ||
        feature.geometry.type === GEOJSON_TYPE.LINE_STRING) &&
      objectType === ELEMENT_TYPE.SEGMENT
    ) {
      const coordinates = getFeatureCoordinates(feature);
      if (!coordinates) {
        return;
      }

      // @ts-expect-error narrow type of coordinates
      const insertIndex = (index + 1) % coordinates.length;
      const positionIndexes =
        feature.geometry.type === SHAPE.POLYGON ? [0, insertIndex] : [insertIndex];
      const insertMapCoords = this._getPointOnSegment(feature, picked, event.mapCoords);

      const updatedData = new ImmutableFeatureCollection(props.data)
        .addPosition(featureIndex, positionIndexes, insertMapCoords)
        .getObject();

      props.onEdit({
        editType: EDIT_TYPE.ADD_POSITION,
        updatedData,
        editContext: [
          {
            featureIndex,
            editHandleIndex: insertIndex,
            screenCoords:
              props.viewport && props.viewport.project(insertMapCoords as [number, number]),
            mapCoords: insertMapCoords,
          },
        ],
      });
    }
  }