<commit_before>import {MODE_IDLE, MODE_PANNING} from '../constants';
import {getDefaultValue, set} from './common';
import {Matrix} from 'transformation-matrix-js';

/**
 *
 * @param value
 * @param viewerDeltaX
 * @param viewerDeltaY
 * @returns {Object}
 */
export function pan(value, viewerDeltaX, viewerDeltaY) {
  let {a, b, c, d, e, f} = value;
  let matrix = Matrix.from(a, b, c, d, e, f);

  let act = new Matrix();
  act = act.translate(viewerDeltaX, viewerDeltaY);

  matrix = matrix.multiply(act);

  return set(value, {
    mode: MODE_IDLE,
    a: matrix.a,
    b: matrix.b,
    c: matrix.c,
    d: matrix.d,
    e: matrix.e,
    f: matrix.f
  });
}

export function startPanning(value, viewerX, viewerY) {
  return set(value, {
    mode: MODE_PANNING,
    startX: viewerX,
    startY: viewerY
  });
}

export function updatePanning(value, viewerX, viewerY, panLimit) {
  if (value.mode !== MODE_PANNING) throw new Error('update pan not allowed in this mode ' + value.mode);

  let {a, b, c, d, e, f, startX, startY} = value;
  let matrix = Matrix.from(a, b, c, d, e, f);
  let zoomLevel = matrix.decompose(false).scale.x;

  let deltaX = (startX - viewerX) / zoomLevel;
  let deltaY = (startY - viewerY) / zoomLevel;

  let act = new Matrix();
  act = act.translate(-deltaX, -deltaY);

  matrix = matrix.multiply(act);

  //apply pan limits
  matrix.e = Math.min(matrix.e, value.viewerWidth - panLimit);
  matrix.e = Math.max(matrix.e, panLimit - value.SVGWidth * zoomLevel);

  matrix.f = Math.min(matrix.f, value.viewerHeight - panLimit);
  matrix.f = Math.max(matrix.f, panLimit - value.SVGHeight * zoomLevel);

  return set(value, {
    mode: MODE_PANNING,
    startX: viewerX,
    startY: viewerY,
    a: matrix.a,
    b: matrix.b,
    c: matrix.c,
    d: matrix.d,
    e: matrix.e,
    f: matrix.f

  });
}

export function stopPanning(value) {
  return set(value, {
      mode: MODE_IDLE,
      viewerStartX: null,
      viewerStartY: null
    }<commit_msg>improve pan feature<commit_after>import {MODE_IDLE, MODE_PANNING} from '../constants';
import {getDefaultValue, set, getSVGPoint} from './common';
import {Matrix} from 'transformation-matrix-js';

/**
 *
 * @param value
 * @param SVGDeltaX
 * @param SVGDeltaY
 * @param panLimit
 * @returns {Object}
 */
export function pan(value, SVGDeltaX, SVGDeltaY, panLimit = undefined) {
  let {a, b, c, d, e, f} = value;
  let matrix = Matrix.from(a, b, c, d, e, f);

  let act = new Matrix();
  act = act.translate(SVGDeltaX, SVGDeltaY);

  matrix = matrix.multiply(act);

  // apply pan limits
  if(panLimit) {
    let zoomLevel = matrix.decompose(false).scale.x;
    matrix.e = Math.min(matrix.e, value.viewerWidth - panLimit);
    matrix.e = Math.max(matrix.e, panLimit - value.SVGWidth * zoomLevel);

    matrix.f = Math.min(matrix.f, value.viewerHeight - panLimit);
    matrix.f = Math.max(matrix.f, panLimit - value.SVGHeight * zoomLevel);
  }

  return set(value, {
    mode: MODE_IDLE,
    a: matrix.a,
    b: matrix.b,
    c: matrix.c,
    d: matrix.d,
    e: matrix.e,
    f: matrix.f
  });
}

export function startPanning(value, viewerX, viewerY) {
  return set(value, {
    mode: MODE_PANNING,
    startX: viewerX,
    startY: viewerY
  });
}

export function updatePanning(value, viewerX, viewerY, panLimit) {
  if (value.mode !== MODE_PANNING) throw new Error('update pan not allowed in this mode ' + value.mode);

  let {startX, startY} = value;

  let start = getSVGPoint(value, startX, startY);
  let end = getSVGPoint(value, viewerX, viewerY);

  let deltaX = end.x - start.x;
  let deltaY = end.y - start.y;

  let nextValue = pan(value, deltaX, deltaY, panLimit);
  return set(nextValue, {
    mode: MODE_PANNING,
    startX: viewerX,
    startY: viewerY,
  });
}

export function stopPanning(value) {
  return set(value, {
      mode: MODE_IDLE,
      viewerStartX: null,
      viewerStartY: null
    }
{'fraction_non_alphanumeric': 0.14135183757388847, 'fraction_numerical': 0.0, 'mean_word_length': 3.2489082969432315, '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': '4697229', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1427, 'n_words': 371}