<commit_before>(ns chessground.drag
  "Contains functions for drag and drop of pieces"
  (:require [quiescent :as q :include-macros true]
            [jayq.core :as jq :refer [$]]
            [chessground.common :as common :refer [pp square-key push-args!]]))

(def over-class "drag-over")

(defn- drag-move [draggie event pointer]
  (let [$el ($ (common/square-element (.-target event)))]
    (when (not (jq/has-class $el over-class))
      (jq/remove-class (jq/siblings $el) over-class)
      (jq/add-class $el over-class))))

(defn make [channels component]
  "Make a react component draggable"
  (q/wrapper component :onMount (fn [node] (-> (new js/Draggabilly node)
                                               (.on "dragStart" (push-args! (:drag-start channels)))
                                               (.on "dragMove" drag-move)
                                               (.on "dragEnd" (push-args! (:drag-end channels)))))))

(defn end [[draggie event pointer]]
  "Undo damages done by dragging and return origin and destination square keys"
  (jq/css ($ (.-element draggie)) {:top 0 :left 0})
  (when-let [orig (square-key (.-element draggie))]
    (when-let [dest-element (common/square-element (.-target event))]
      (jq/remove-class ($ dest-element) over-class)
      (when-let [dest (square-key dest-element)]
        [orig dest]))))<commit_msg>improve dragging by shifting the piece under the cursor<commit_after>(ns chessground.drag
  "Contains functions for drag and drop of pieces"
  (:require [quiescent :as q :include-macros true]
            [jayq.core :as jq :refer [$]]
            [chessground.common :as common :refer [pp square-key push-args!]]))

(def over-class "drag-over")

(defn- on-start [_ event _]
  "Shift piece right under the cursor"
  (let [$el ($ (.-target event))
        pos (.offset $el)
        center-x (+ (.-left pos) (/ (.width $el) 2))
        center-y (+ (.-top pos) (/ (.height $el) 2))
        decay-x (- (.-pageX event) center-x)
        decay-y (- (.-pageY event) center-y)]
    (jq/css $el {:left (str decay-x "px") :top (str decay-y "px")})))

(defn- on-move [_ event _]
  "Highlight the square under the dragged piece"
  (let [$el ($ (common/square-element (.-target event)))]
    (when (not (jq/has-class $el over-class))
      (jq/remove-class (jq/siblings $el) over-class)
      (jq/add-class $el over-class))))

(defn make [channels component]
  "Make a react component draggable"
  (q/wrapper component
             :onMount (fn [node]
                        (-> (new js/Draggabilly node)
                            (.on "dragStart" on-start)
                            (.on "dragStart" (push-args! (:drag-start channels)))
                            (.on "dragMove" on-move)
                            (.on "dragEnd" (push-args! (:drag-end channels)))))))

(defn end [[draggie event _]]
  "Undo damages done by dragging and return origin and destination square keys"
  (jq/css ($ (.-element draggie)) {:top 0 :left 0})
  (when-let [orig (square-key (.-element draggie))]
    (when-let [dest-element (common/square-element (.-target event))]
      (jq/remove-class ($ dest-element) over-class)
      (when-let [dest (square-key dest-element)]
        [orig dest]))))
{'fraction_non_alphanumeric': 0.1583487369069624, 'fraction_numerical': 0.0018484288354898336, 'mean_word_length': 2.309887869520897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455878', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1006, 'n_words': 310}