<commit_before>(ns ^:figwheel-load blocks-editor.events
  (:require [re-frame.core :as rf]
            [blocks-editor.core :as c]
            
            [Blockly.Xml :as bx]
            [Blockly.Variables :as bv]))

(defonce node-require (js* "require"))

(defonce dialog (-> "electron" node-require .-remote .-dialog))
(defonce fs (node-require "fs"))

(rf/reg-event-db
 :init-db
 (fn [_ _]
   {:robot-name nil}))

(rf/reg-event-db
 :update-robot-name
 (fn [db [_ v]]
   (assoc db :robot-name v)))

(rf/reg-event-db
 :open-file
 (fn [db [_ _]] 
   (.showOpenDialog
    dialog
    #(when %
       (.readFile
        fs (aget % 0) "utf-8"
        (fn [err content]
          (if-not err
            (let [wdom (bx/textToDom content)
                  rname (some-> wdom .-children (aget 0)
                                .-innerHTML)] 
              (bx/domToWorkspace wdom c/workspace)
              (rf/dispatch [:update-robot-name rname]))
            (do (js/alert (str
                           "Sorry, could not open your file: \n"
                           err))))))))
   db))

(rf/reg-event-db
 :save-file
 (fn [db [_ _]] 
   (if (.-getTopBlocks c/workspace) 
     (.showSaveDialog
      dialog
      #(when % 
         (let [wdom (-> c/workspace bx/workspaceToDom)
               vars (.createElement js/document "meta")
               svars  (-> c/workspace bv/allUsedVariables .join)
               tvars (.createTextNode js/document svars)

               rname (.createElement js/document "meta") <commit_msg>Open-file -> clean workspace<commit_after>(ns ^:figwheel-load blocks-editor.events
  (:require [re-frame.core :as rf]
            [blocks-editor.core :as c]
            
            [Blockly.Xml :as bx]
            [Blockly.Variables :as bv]))

(defonce node-require (js* "require"))

(defonce dialog (-> "electron" node-require .-remote .-dialog))
(defonce fs (node-require "fs"))

(rf/reg-event-db
 :init-db
 (fn [_ _]
   {:robot-name nil}))

(rf/reg-event-db
 :update-robot-name
 (fn [db [_ v]]
   (assoc db :robot-name v)))

(rf/reg-event-db
 :open-file
 (fn [db [_ _]] 
   (.showOpenDialog
    dialog
    #(when %
       (.readFile
        fs (aget % 0) "utf-8"
        (fn [err content]
          (if-not err
            (let [wdom (bx/textToDom content)
                  rname (some-> wdom .-children (aget 0)
                                .-innerHTML)] 
              (.clear c/workspace)
              (bx/domToWorkspace wdom c/workspace)
              (rf/dispatch [:update-robot-name rname]))
            (do (js/alert (str
                           "Sorry, could not open your file: \n"
                           err))))))))
   db))

(rf/reg-event-db
 :save-file
 (fn [db [_ _]] 
   (if (.-getTopBlocks c/workspace) 
     (.showSaveDialog
      dialog
      #(when % 
         (let [wdom (-> c/workspace bx/workspaceToDom)
               vars (.createElement js/document "meta")
               svars  (-> c/workspace bv/allUsedVariables .join)
               tvars (.createTextNode js/document svars)

               rname (.createElement js/document "meta") 
{'fraction_non_alphanumeric': 0.1453900709219858, 'fraction_numerical': 0.0019342359767891683, 'mean_word_length': 1.652136752136752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '5190977', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1011, 'n_words': 244}