<commit_before>import { Chain, UiFinder, NamedChain } from '@ephox/agar';
import { Arr } from '@ephox/katamari';
import { Body, Element, Node, Attr } from '@ephox/sugar';
import { dispatchDndEvent, createDragstartEvent, createDragEvent, createDragenterEvent, createDragoverEvent, createDropEvent, createDragendEvent, isDefaultPrevented, getWindowFromElement } from '../dragndrop/DndEvents';
import { File, DragEvent } from '@ephox/dom-globals';
import { createDataTransfer, getDragImage } from '../datatransfer/DataTransfer';

const isDraggable = (element: Element) => {
  if (Node.name(element) === 'img') {
    return true;
  } else if (Node.name(element) === 'a' && Attr.has(element, 'href')) {
    return true;
  } else if (Attr.get(element, 'draggable') === 'true') {
    return true;
  } else {
    return false;
  }
};

const checkDefaultPrevented = (evt: DragEvent) => {
  if (isDefaultPrevented(evt) === false) {
    throw new Error(`preventDefault was not called on drag event: ${evt.type}`);
  }
};

const dragnDrop = (from: Element, to: Element) => {
  const fromWin = getWindowFromElement(from);
  const toWin = getWindowFromElement(to);
  const fromRect = from.dom().getBoundingClientRect();
  const toRect = from.dom().getBoundingClientRect();
  const transfer = createDataTransfer();

  if (isDraggable(from) === false) {
    throw new Error('Can not drag a non draggable element.');
  }

  dispatchDndEvent(createDragstartEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
  dispatchDndEvent(createDragEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
  checkDefaultPrevented(dispatchDndEvent(createDragenterEvent(toWin, toRect.left, toRect.top, transfer), to));
  checkDefaultPrevented(dispatchDndEvent(createDragoverEvent(toWin, toRect.left, toRect.top, transfer), to));
  checkDefaultPrevented(dispatchDndEvent(createDropEvent(toWin, toRect.left, toRect.top, transfer), to));
  dispatchDndEvent(createDragendEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
};

const dropFiles = (files: File[], to: Element) => {
  const toWin = getWindowFromElement(to);<commit_msg>Refactor DragnDrop.isDraggable<commit_after>import { Chain, UiFinder, NamedChain } from '@ephox/agar';
import { Arr } from '@ephox/katamari';
import { Body, Element, Node, Attr } from '@ephox/sugar';
import { dispatchDndEvent, createDragstartEvent, createDragEvent, createDragenterEvent, createDragoverEvent, createDropEvent, createDragendEvent, isDefaultPrevented, getWindowFromElement } from '../dragndrop/DndEvents';
import { File, DragEvent } from '@ephox/dom-globals';
import { createDataTransfer, getDragImage } from '../datatransfer/DataTransfer';

const isDraggable = (element: Element) => {
  const name = Node.name(element);
  return (
    name === 'img' ||
    name === 'a' && Attr.has(element, 'href') ||
    Attr.get(element, 'draggable') === 'true'
  );
};

const checkDefaultPrevented = (evt: DragEvent) => {
  if (isDefaultPrevented(evt) === false) {
    throw new Error(`preventDefault was not called on drag event: ${evt.type}`);
  }
};

const dragnDrop = (from: Element, to: Element) => {
  const fromWin = getWindowFromElement(from);
  const toWin = getWindowFromElement(to);
  const fromRect = from.dom().getBoundingClientRect();
  const toRect = from.dom().getBoundingClientRect();
  const transfer = createDataTransfer();

  if (isDraggable(from) === false) {
    throw new Error('Can not drag a non draggable element.');
  }

  dispatchDndEvent(createDragstartEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
  dispatchDndEvent(createDragEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
  checkDefaultPrevented(dispatchDndEvent(createDragenterEvent(toWin, toRect.left, toRect.top, transfer), to));
  checkDefaultPrevented(dispatchDndEvent(createDragoverEvent(toWin, toRect.left, toRect.top, transfer), to));
  checkDefaultPrevented(dispatchDndEvent(createDropEvent(toWin, toRect.left, toRect.top, transfer), to));
  dispatchDndEvent(createDragendEvent(fromWin, fromRect.left, fromRect.top, transfer), from);
};

const dropFiles = (files: File[], to: Element) => {
  const toWin = getWindowFromElement(to);
{'fraction_non_alphanumeric': 0.1390950442901604, 'fraction_numerical': 0.0, 'mean_word_length': 5.905785123966942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6434672', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1336, 'n_words': 300}