handleClick()

in modules/edit-modes/src/lib/draw-polygon-mode.ts [82:139]


  handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
    const { picks } = event;
    const clickedEditHandle = getPickedEditHandle(picks);
    const clickSequence = this.getClickSequence();

    let overlappingLines = false;
    if (clickSequence.length > 2 && props.modeConfig && props.modeConfig.preventOverlappingLines) {
      const currentLine = turfLineString([
        clickSequence[clickSequence.length - 1],
        event.mapCoords,
      ]);
      const otherLines = turfLineString([...clickSequence.slice(0, clickSequence.length - 1)]);
      const intersectingPoints = lineIntersect(currentLine, otherLines);
      if (intersectingPoints.features.length > 0) {
        overlappingLines = true;
      }
    }

    let positionAdded = false;
    if (!clickedEditHandle && !overlappingLines) {
      // Don't add another point right next to an existing one
      this.addClickSequence(event);
      positionAdded = true;
    }

    if (
      clickSequence.length > 2 &&
      clickedEditHandle &&
      Array.isArray(clickedEditHandle.properties.positionIndexes) &&
      (clickedEditHandle.properties.positionIndexes[0] === 0 ||
        clickedEditHandle.properties.positionIndexes[0] === clickSequence.length - 1)
    ) {
      // They clicked the first or last point (or double-clicked), so complete the polygon

      // Remove the hovered position
      const polygonToAdd: Polygon = {
        type: 'Polygon',
        coordinates: [[...clickSequence, clickSequence[0]]],
      };

      this.resetClickSequence();

      const editAction = this.getAddFeatureOrBooleanPolygonAction(polygonToAdd, props);
      if (editAction) {
        props.onEdit(editAction);
      }
    } else if (positionAdded) {
      // new tentative point
      props.onEdit({
        // data is the same
        updatedData: props.data,
        editType: 'addTentativePosition',
        editContext: {
          position: event.mapCoords,
        },
      });
    }
  }