<commit_before>import _ from "lodash"
import MouseHandler, { defaultActionFactory } from "./NoteMouseHandler"
import { createNoteAction } from "./PencilMouseHandler"
import Rect from "../model/Rect"
import { pointSub } from "../helpers/point"

export default class SelectionMouseHandler extends MouseHandler {
  constructor(emitter) {
    super(emitter, [defaultActionFactory, actionFactory], getCursor)
  }
}

function actionFactory(local, ctx, e) {
  if (e.relatedTarget) {
    return null
  }

  const selectionRect = ctx.selection.getBounds(ctx.transform)
  const clicked = selectionRect && selectionRect.contains(local.x, local.y)
  if (!clicked) {
    if (e.nativeEvent.detail == 2) {
      return createNoteAction(ctx)
    }
    return createSelectionAction(ctx)
  }
  const type = getDragPositionType(local.x - selectionRect.x, selectionRect.width)
  switch (type) {
    case "center": return moveSelectionAction(ctx)
    case "right": return dragSelectionRightEdgeAction(ctx)
    case "left": return dragSelectionLeftEdgeAction(ctx)
  }
}

function getCursor(local, ctx) {
  const selectionRect = ctx.selection.getBounds(ctx.transform)
  const hover = selectionRect && selectionRect.contains(local.x, local.y)
  if (hover) {
    const type = getDragPositionType(local.x - selectionRect.x, selectionRect.width)
    return cursorForPositionType(type)
  }
  return "crosshair"
}

const createSelectionAction = ctx => (onMouseDown, onMouseMove, onMouseUp) => {
  const { selection, quantizer, transform } = ctx
  let startPosition

  // 選択範囲外でクリックした場合は選択範囲をリセット
  onMouseDown(local => {
    startPosition = local
    selection.reset()
    // TODO: カーソルを移動<commit_msg>fix selection<commit_after>import _ from "lodash"
import MouseHandler, { defaultActionFactory } from "./NoteMouseHandler"
import { createNoteAction } from "./PencilMouseHandler"
import Rect from "../model/Rect"
import { pointSub } from "../helpers/point"

export default class SelectionMouseHandler extends MouseHandler {
  constructor(emitter) {
    super(emitter, [defaultActionFactory, actionFactory], getCursor)
  }
}

function actionFactory(local, ctx, e) {
  if (e.relatedTarget) {
    return null
  }

  const { selection, transform } = ctx
  if (selection && selection.enabled) {
    const selectionRect = selection.getBounds(transform)
    const clicked = selectionRect.contains(local.x, local.y)
    if (clicked) {
      const type = getDragPositionType(local.x - selectionRect.x, selectionRect.width)
      switch (type) {
        case "center": return moveSelectionAction(ctx)
        case "right": return dragSelectionRightEdgeAction(ctx)
        case "left": return dragSelectionLeftEdgeAction(ctx)
      }
    }
  }

  if (e.nativeEvent.detail == 2) {
    return createNoteAction(ctx)
  }
  return createSelectionAction(ctx)
}

function getCursor(local, ctx) {
  const selectionRect = ctx.selection.getBounds(ctx.transform)
  const hover = selectionRect && selectionRect.contains(local.x, local.y)
  if (hover) {
    const type = getDragPositionType(local.x - selectionRect.x, selectionRect.width)
    return cursorForPositionType(type)
  }
  return "crosshair"
}

const createSelectionAction = ctx => (onMouseDown, onMouseMove, onMouseUp) => {
  const { selection, quantizer, transform } = ctx
  let startPosition

  // 選択範囲外でクリックした場合は選択範囲をリセット
  onMouseDown(local => {
    startPosition = local
    selection.reset()
    // TODO: カーソルを移動
{'fraction_non_alphanumeric': 0.10624635143023935, 'fraction_numerical': 0.0005837711617046118, 'mean_word_length': 4.20030349013657, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 9, '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': '5442163', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1050, 'n_words': 240}