<commit_before>import {
  TOOL_NONE, TOOL_PAN, TOOL_AUTO, TOOL_ZOOM_IN, TOOL_ZOOM_OUT,
  MODE_IDLE, MODE_PANNING, MODE_ZOOMING
} from '../constants';
import {resetMode} from './common';
import {onMouseDown, onMouseMove, onMouseUp} from './interactions';

export function onTouchStart(event, ViewerDOM, tool, value, props) {
  let x, y;
  if (event.touches.length === 1) {
    let touchPosition = event.touches[0];
    let {left, top} = ViewerDOM.getBoundingClientRect();
    x = touchPosition.clientX - Math.round(left);
    y = touchPosition.clientY - Math.round(top);
  } else {
    if ([MODE_PANNING, MODE_ZOOMING].includes(value.mode)) return resetMode(value);
  }

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseDown(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchMove(event, ViewerDOM, tool, value, props) {
  if (![MODE_PANNING, MODE_ZOOMING].includes(value.mode)) return value;

  let touchPosition = event.touches[0];
  let {left, top} = ViewerDOM.getBoundingClientRect();
  let x = touchPosition.clientX - Math.round(left);
  let y = touchPosition.clientY - Math.round(top);

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseMove(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchEnd(event, ViewerDOM, tool, value, props) {
  if (![MODE_PANNING, MODE_ZOOMING].includes(value.mode)) return value;

  let touchPosition = event.changedTouches[0];
  let {left, top} = ViewerDOM.getBoundingClientRect();
  let x = touchPosition.clientX - Math.round(left);
  let y = touchPosition.clientY - Math.round(top);

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseUp(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchCancel(event, ViewerDOM, tool, value, props) {
  event.stopPropagation();
  event.preventDefault();

  return resetMode(value);
}

<commit_msg>Fixed double touch events<commit_after>import {
  TOOL_NONE, TOOL_PAN, TOOL_AUTO, TOOL_ZOOM_IN, TOOL_ZOOM_OUT,
  MODE_IDLE, MODE_PANNING, MODE_ZOOMING
} from '../constants';
import {resetMode} from './common';
import {onMouseDown, onMouseMove, onMouseUp} from './interactions';

export function onTouchStart(event, ViewerDOM, tool, value, props) {
  let x, y;
  if (event.touches.length === 1) {
    let touchPosition = event.touches[0];
    let {left, top} = ViewerDOM.getBoundingClientRect();
    x = touchPosition.clientX - Math.round(left);
    y = touchPosition.clientY - Math.round(top);
  } else {
    if ([MODE_PANNING, MODE_ZOOMING].includes(value.mode)){
      return resetMode(value);
    }else if([MODE_IDLE].includes(value.mode)){
      return value;
    }
  }

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseDown(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchMove(event, ViewerDOM, tool, value, props) {
  if (![MODE_PANNING, MODE_ZOOMING].includes(value.mode)) return value;

  let touchPosition = event.touches[0];
  let {left, top} = ViewerDOM.getBoundingClientRect();
  let x = touchPosition.clientX - Math.round(left);
  let y = touchPosition.clientY - Math.round(top);

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseMove(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchEnd(event, ViewerDOM, tool, value, props) {
  if (![MODE_PANNING, MODE_ZOOMING].includes(value.mode)) return value;

  let touchPosition = event.changedTouches[0];
  let {left, top} = ViewerDOM.getBoundingClientRect();
  let x = touchPosition.clientX - Math.round(left);
  let y = touchPosition.clientY - Math.round(top);

  switch (tool) {
    case TOOL_ZOOM_OUT:
    case TOOL_ZOOM_IN:
    case TOOL_AUTO:
    case TOOL_PAN:
      event.stopPropagation();
      event.preventDefault();
      return onMouseUp(event, ViewerDOM, tool, value, props, {x, y});

    default:
      return value;
  }
}

export function onTouchCancel(event, ViewerDOM, tool, value, props) {
  event.stopPropagation();
  event.preventDefault();

  return resetMode(value);
}


{'fraction_non_alphanumeric': 0.12434909393876276, 'fraction_numerical': 0.0016663195167673402, 'mean_word_length': 3.759167492566898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2118287', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1687, 'n_words': 381}