<commit_before>const {Selection, NodeSelection, TextSelection} = require("prosemirror-state")

const browser = require("./browser")
const {captureKeyDown} = require("./capturekeys")
const {DOMChange} = require("./domchange")
const {fromClipboard, toClipboard, canUpdateClipboard} = require("./clipboard")
const {TrackMappings} = require("./trackmappings")

// A collection of DOM events that occur within the editor, and callback functions
// to invoke when the event fires.
const handlers = {}

function initInput(view) {
  view.shiftKey = false
  view.mouseDown = null
  view.dragging = null
  view.inDOMChange = null
  view.mutationObserver = window.MutationObserver &&
    new window.MutationObserver(mutations => registerMutations(view, mutations))
  startObserving(view)

  for (let event in handlers) {
    let handler = handlers[event]
    view.content.addEventListener(event, event => {
      if (eventBelongsToView(view, event) && !view.someProp("handleDOMEvent", f => f(view, event)))
        handler(view, event)
    })
  }
}
exports.initInput = initInput

function eventBelongsToView(view, event) {
  if (!event.bubbles) return true
  if (event.defaultPrevented) return false
  for (let node = event.target; node != view.content; node = node.parentNode)
    if (!node || node.nodeType == 11 ||
        (node.pmViewDesc && node.pmViewDesc.stopEvent(event)))
      return false
  return true
}

function dispatchEvent(view, event) {
  let handler = handlers[event.type]
  if (handler && !view.someProp("handleDOMEvent", f => f(view, event))) handler(view, event)
}
exports.dispatchEvent = dispatchEvent

handlers.keydown = (view, event) => {
  if (event.keyCode == 16) view.shiftKey = true
  if (view.inDOMChange) return
  if (view.someProp("handleKeyDown", f => f(view, event)) || captureKeyDown(view, event))
    event.preventDefault()
  else
    view.selectionReader.poll()
}

handlers.keyup = (view, e) => {
  if (e.keyCode == 16) view.shiftKey = false
}

handlers.keypress = (view, event) => {
  if (view.inDOMChange || !event.charCode ||
      event.ctrlKey && !event.altKey || browser.mac && event.metaKey) return

  if (view.someProp("handleKeyPress", f => f(view, event))) {
    event.preventDefault()
    return
  }
}

function eventCoords(event) { return {left: event.clientX, top: event.clientY} }

let lastClick = {time: 0, x: 0, y: 0}, oneButLastClick = lastClick

function isNear(event, click) {
  let dx = click.x - event.clientX, dy = click.y - event.clientY
  return dx * dx + dy * dy < 100
}

function runHandlerOnContext(view, propName, pos, inside, event) {
  if (inside == -1) return false
  let $pos = view.state.doc.resolve(inside)
  for (let i = $pos.depth + 1; i > 0; i--) {
    if (view.someProp(propName, f => i > $pos.depth ? f(view, pos, $pos.nodeAfter, $pos.before(i), event, true)
                                                    : f(view, pos, $pos.node(i), $pos.before(i), event, false)))
      return true
  }
  return false
}

function updateSelection(view, selection, origin) {
  view.focus()
  view.props.onAction(selection.action({origin}))<commit_msg>Do override keypress events when the selection isn't a simple text selection<commit_after>const {Selection, NodeSelection, TextSelection} = require("prosemirror-state")

const browser = require("./browser")
const {captureKeyDown} = require("./capturekeys")
const {DOMChange} = require("./domchange")
const {fromClipboard, toClipboard, canUpdateClipboard} = require("./clipboard")
const {TrackMappings} = require("./trackmappings")

// A collection of DOM events that occur within the editor, and callback functions
// to invoke when the event fires.
const handlers = {}

function initInput(view) {
  view.shiftKey = false
  view.mouseDown = null
  view.dragging = null
  view.inDOMChange = null
  view.mutationObserver = window.MutationObserver &&
    new window.MutationObserver(mutations => registerMutations(view, mutations))
  startObserving(view)

  for (let event in handlers) {
    let handler = handlers[event]
    view.content.addEventListener(event, event => {
      if (eventBelongsToView(view, event) && !view.someProp("handleDOMEvent", f => f(view, event)))
        handler(view, event)
    })
  }
}
exports.initInput = initInput

function eventBelongsToView(view, event) {
  if (!event.bubbles) return true
  if (event.defaultPrevented) return false
  for (let node = event.target; node != view.content; node = node.parentNode)
    if (!node || node.nodeType == 11 ||
        (node.pmViewDesc && node.pmViewDesc.stopEvent(event)))
      return false
  return true
}

function dispatchEvent(view, event) {
  let handler = handlers[event.type]
  if (handler && !view.someProp("handleDOMEvent", f => f(view, event))) handler(view, event)
}
exports.dispatchEvent = dispatchEvent

handlers.keydown = (view, event) => {
  if (event.keyCode == 16) view.shiftKey = true
  if (view.inDOMChange) return
  if (view.someProp("handleKeyDown", f => f(view, event)) || captureKeyDown(view, event))
    event.preventDefault()
  else
    view.selectionReader.poll()
}

handlers.keyup = (view, e) => {
  if (e.keyCode == 16) view.shiftKey = false
}

handlers.keypress = (view, event) => {
  if (view.inDOMChange || !event.charCode ||
      event.ctrlKey && !event.altKey || browser.mac && event.metaKey) return

  if (view.someProp("handleKeyPress", f => f(view, event))) {
    event.preventDefault()
    return
  }

  let {node, $from, $to} = view.state.selection
  if (node || !$from.sameParent($to)) {
    let text = String.fromCharCode(event.charCode)
    if (!view.someProp("handleTextInput", f => f(view, $from.pos, $to.pos, text)))
      view.props.onAction(view.state.tr.insertText(text).scrollAction())
    event.preventDefault()
  }
}

function eventCoords(event) { return {left: event.clientX, top: event.clientY} }

let lastClick = {time: 0, x: 0, y: 0}, oneButLastClick = lastClick

function isNear(event, click) {
  let dx = click.x - event.clientX, dy = click.y - event.clientY
  return dx * dx + dy * dy < 100
}

function runHandlerOnContext(view, propName, pos, inside, event) {
  if (inside == -1) return false
  let $pos = view.state.doc.resolve(inside)
  for (let i = $pos.depth + 1; i > 0; i--) {
    if (view.someProp(propName, f => i > $pos.depth ? f(view, pos, $pos.nodeAfter, $pos.before(i), event, true)
                                                    : f(view, pos, $pos.node(i), $pos.before(i), event, false)))
      return true
  }
  return false
}

function updateSelection(view, selection, origin) {
  view.focus()
  view.props.onAction(selection.action({origin}))
{'fraction_non_alphanumeric': 0.14363636363636365, 'fraction_numerical': 0.004545454545454545, 'mean_word_length': 3.8323572474377747, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3162413', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2091, 'n_words': 540}