<commit_before>#= require trix/dom

{DOM} = Trix

class Trix.AttachmentController
  imageEditorSelector = "div.image-editor"
  imageResizeHandleSelector = "#{imageEditorSelector} div.resize-handle"

  constructor: (@element, @responder) ->
    DOM.on(@element, "click", "img", @didClickImage)
    DOM.on(@element, "mousedown", imageResizeHandleSelector, @didMouseDownResizeHandle)

  didClickImage: (event, image) =>
    if DOM.closest(image, imageEditorSelector)
      uninstallImageEditor(image)
    else
      installImageEditor(image)

  didMouseDownResizeHandle: (event, handleElement) =>
    event.preventDefault()

    @element.style["cursor"] = window.getComputedStyle(handleElement)["cursor"]
    @element.addEventListener("mousemove", @didMoveMouseToResize)
    document.addEventListener("mouseup", @didMouseUpToEndResize)

    @resizing =
      editor: editor = handleElement.parentElement
      image: editor.firstChild
      startWidth: parseInt(getDimensions(editor).width, 10)
      startClientX: event.clientX

  didMoveMouseToResize: (event) =>
    width = (@resizing.startWidth + event.clientX - @resizing.startClientX) + "px"
    height = getDimensions(@resizing.image).height
    setStyle(@resizing.editor, {width, height})

  didMouseUpToEndResize: (event) =>
    @element.style["cursor"] = "auto"
    @element.removeEventListener("mousemove", @didMoveMouseToResize)
    document.removeEventListener("mouseup", @didMouseUpToEndResize)

    {image} = @resizing
    delete @resizing

    uninstallImageEditor(image)
    # Use offsets to avoid possible sub-pixel dimensions from the computed style
    attributes = { width: image.offsetWidth, height: image.offsetHeight }
    @delegate?.attachmentControllerDidChangeAttributesAtPosition(attributes, image.trixPosition)

  installImageEditor = (image) ->
    editor = document.createElement("div")
    editor.setAttribute("contenteditable", false)
    editor.classList.add("image-editor")
    setStyle(editor, getDimensions(image))

    handle = document.createElement("div")
    handle.classList.add("resize-handle")
    handle.classList.add("se")

    image.parentElement.insertBefore(editor, image)
    editor.appendChild(image)
    editor.appendChild(handle)
    setStyle(image, width: "100%", height: "auto")

  uninstallImageEditor = (image) ->
    setStyle(image, getDimensions(image))
    editor = DOM.closest(image, imageEditorSelector)
    editor.parentElement.replaceChild(image, editor)

  getDimensions = (element) ->
    style = window.getComputedStyle(element)
    dimensions = {}
    dimensions[key] = style[key] for key in ["width", "height"]
    dimensions

  setStyle = (element, attributes) ->
    element.style[key] = value for key, value of attributes
<commit_msg>Remove responder left over from testing<commit_after>#= require trix/dom

{DOM} = Trix

class Trix.AttachmentController
  imageEditorSelector = "div.image-editor"
  imageResizeHandleSelector = "#{imageEditorSelector} div.resize-handle"

  constructor: (@element) ->
    DOM.on(@element, "click", "img", @didClickImage)
    DOM.on(@element, "mousedown", imageResizeHandleSelector, @didMouseDownResizeHandle)

  didClickImage: (event, image) =>
    if DOM.closest(image, imageEditorSelector)
      uninstallImageEditor(image)
    else
      installImageEditor(image)

  didMouseDownResizeHandle: (event, handleElement) =>
    event.preventDefault()

    @element.style["cursor"] = window.getComputedStyle(handleElement)["cursor"]
    @element.addEventListener("mousemove", @didMoveMouseToResize)
    document.addEventListener("mouseup", @didMouseUpToEndResize)

    @resizing =
      editor: editor = handleElement.parentElement
      image: editor.firstChild
      startWidth: parseInt(getDimensions(editor).width, 10)
      startClientX: event.clientX

  didMoveMouseToResize: (event) =>
    width = (@resizing.startWidth + event.clientX - @resizing.startClientX) + "px"
    height = getDimensions(@resizing.image).height
    setStyle(@resizing.editor, {width, height})

  didMouseUpToEndResize: (event) =>
    @element.style["cursor"] = "auto"
    @element.removeEventListener("mousemove", @didMoveMouseToResize)
    document.removeEventListener("mouseup", @didMouseUpToEndResize)

    {image} = @resizing
    delete @resizing

    uninstallImageEditor(image)
    # Use offsets to avoid possible sub-pixel dimensions from the computed style
    attributes = { width: image.offsetWidth, height: image.offsetHeight }
    @delegate?.attachmentControllerDidChangeAttributesAtPosition(attributes, image.trixPosition)

  installImageEditor = (image) ->
    editor = document.createElement("div")
    editor.setAttribute("contenteditable", false)
    editor.classList.add("image-editor")
    setStyle(editor, getDimensions(image))

    handle = document.createElement("div")
    handle.classList.add("resize-handle")
    handle.classList.add("se")

    image.parentElement.insertBefore(editor, image)
    editor.appendChild(image)
    editor.appendChild(handle)
    setStyle(image, width: "100%", height: "auto")

  uninstallImageEditor = (image) ->
    setStyle(image, getDimensions(image))
    editor = DOM.closest(image, imageEditorSelector)
    editor.parentElement.replaceChild(image, editor)

  getDimensions = (element) ->
    style = window.getComputedStyle(element)
    dimensions = {}
    dimensions[key] = style[key] for key in ["width", "height"]
    dimensions

  setStyle = (element, attributes) ->
    element.style[key] = value for key, value of attributes

{'fraction_non_alphanumeric': 0.11956521739130435, 'fraction_numerical': 0.0018115942028985507, 'mean_word_length': 4.709410548086867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '7542219', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1592, 'n_words': 304}