<commit_before>(ns middlebrow-example-notepad.core
  (:require [dommy.core :as d :refer-macros [sel sel1]]
            [ajax.core :refer [GET POST]]))

; Let's make FileList ISeqable so we can use the standard Clojure core
; functions on them.
(extend-type js/FileList
  ISeqable
  (-seq [array] (array-seq array 0)))

; Here we're handling the reading of the file on the client side. This
; could easily be done on the server side as well. We're just showing
; both techniques in this sample project for learning purposes.
(defn read-file [file on-load]
  (let [reader (js/FileReader.)]
    (set! (.-onload reader) on-load)
    (.readAsText reader file)))

(defn set-char-count! [char-count]
  (d/set-text! (sel1 :#characterCount) char-count))

(let [inputTextArea (sel1 :#inputTextArea)]
  (d/listen! (sel1 :#open) :click
    (fn [e] (.click (sel1 :#openFile))))

  (d/listen! (sel1 :#openFile) :change
    (fn [e]
      (let [file (first (aget e "target" "files"))]
        (read-file file
          (fn [e]
            (let [content (aget e "target" "result")]
              (d/set-value! inputTextArea content)
              (set-char-count! (count content))))))))

  ; I would consider learning how to use core.async as it can be
  ; used in cases like this. You can avoid having to nest callbacks like
  ; this, which can make reasoning about your code much easier.
  (d/listen! (sel1 :#save) :click
    (fn [e]
      (GET "/save-dialog" :handler
        (fn [path]
          (when-not (empty? path)
            ; Contrast to the way we're reading files (which is being done on the client side),
            ; here we are savings files on the server side.
            (POST "/save" {:params {:path path
                                    :text (d/value (sel1 :#inputTextArea))}
                           :format :url}))))))

  (doseq [eventType [:keyup :change :paste]]
    (d/listen! inputTextArea eventType
      (fn [e]
        (set-char-count! (count (d/value inputTextArea)))))))<commit_msg>Match comments with clj-thrust-example-notepad version of this file<commit_after>(ns middlebrow-example-notepad.core
  (:require [dommy.core :as d :refer-macros [sel sel1]]
            [ajax.core :refer [GET POST]]))

; Let's make FileList ISeqable so we can use the standard Clojure core
; functions on it.
(extend-type js/FileList
  ISeqable
  (-seq [array] (array-seq array 0)))

; Here we're handling the reading of the file on the client side. This
; could easily be done on the server side as well. We're just showing
; both techniques in this sample project for learning purposes.
(defn read-file [file on-load]
  (let [reader (js/FileReader.)]
    (set! (.-onload reader) on-load)
    (.readAsText reader file)))

(defn set-char-count! [char-count]
  (d/set-text! (sel1 :#characterCount) char-count))

(let [inputTextArea (sel1 :#inputTextArea)]
  (d/listen! (sel1 :#open) :click
    (fn [e] (.click (sel1 :#openFile))))

  (d/listen! (sel1 :#openFile) :change
    (fn [e]
      (let [file (first (aget e "target" "files"))]
        (read-file file
          (fn [e]
            (let [content (aget e "target" "result")]
              (d/set-value! inputTextArea content)
              (set-char-count! (count content))))))))

  ; I would consider learning how to use core.async as it can be
  ; used in cases like this. You can avoid having to nest callbacks like
  ; this, which can make reasoning about your code easier.
  (d/listen! (sel1 :#save) :click
    (fn [e]
      (GET "/save-dialog" :handler
        (fn [path]
          (when-not (empty? path)
            ; Contrast this to the way we're reading files (which is being done on the client side),
            ; here we are savings files on the server side.
            (POST "/save" {:params {:path path
                                    :text (d/value (sel1 :#inputTextArea))}
                           :format :url}))))))

  (doseq [eventType [:keyup :change :paste]]
    (d/listen! inputTextArea eventType
      (fn [e]
        (set-char-count! (count (d/value inputTextArea)))))))
{'fraction_non_alphanumeric': 0.13170851797144265, 'fraction_numerical': 0.004431314623338257, 'mean_word_length': 2.5987599645704162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '399418', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1275, 'n_words': 470}