handleClick()

in modules/edit-modes/src/lib/measure-distance-mode.ts [46:108]


  handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
    const { modeConfig, data, onEdit } = props;
    const { centerTooltipsOnLine = false } = modeConfig || {};

    // restart measuring session
    if (this._isMeasuringSessionFinished) {
      this._isMeasuringSessionFinished = false;
      this.resetClickSequence();
      this._currentTooltips = [];
      this._currentDistance = 0;
    }

    const { picks } = event;
    const clickedEditHandle = getPickedEditHandle(picks);

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

    if (
      clickSequence.length > 1 &&
      clickedEditHandle &&
      Array.isArray(clickedEditHandle.properties.positionIndexes) &&
      clickedEditHandle.properties.positionIndexes[0] === clickSequence.length - 1
    ) {
      // They clicked the last point (or double-clicked), so add the LineString
      this._isMeasuringSessionFinished = true;
    } else if (positionAdded) {
      if (clickSequence.length > 1) {
        this._currentDistance += this._calculateDistanceForTooltip({
          positionA: clickSequence[clickSequence.length - 2],
          positionB: clickSequence[clickSequence.length - 1],
          modeConfig,
        });

        const tooltipPosition = centerTooltipsOnLine
          ? turfMidpoint(
              clickSequence[clickSequence.length - 2],
              clickSequence[clickSequence.length - 1]
            ).geometry.coordinates
          : event.mapCoords;

        this._currentTooltips.push({
          position: tooltipPosition,
          text: this._formatTooltip(this._currentDistance, modeConfig),
        });
      }

      // new tentative point
      onEdit({
        // data is the same
        updatedData: data,
        editType: 'addTentativePosition',
        editContext: {
          position: event.mapCoords,
        },
      });
    }
  }