Browse Source

feat: export as EDN

Tienson Qin 4 years ago
parent
commit
d94da81459

+ 6 - 5
src/main/frontend/components/repo.cljs

@@ -78,16 +78,17 @@
                                   :on-click (fn []
                                               (repo-handler/re-index! nfs-handler/rebuild-index!))}
                  "Re-index"]
-                ;; [:a.control.ml-4 {:title "Export as JSON"
-                ;;                   :on-click (fn []
-                ;;                               (export-handler/export-repo-as-json! (:url repo)))}
-                ;;  "Export as JSON"]
+                [:a.control.ml-4 {:title "Export as EDN"
+                                  :on-click (fn []
+                                              (export-handler/export-repo-as-edn! (:url repo)))}
+                 "Export as EDN"]
                 [:a.text-gray-400.ml-4 {:title "No worries, unlink this graph will clear its cache only, it does not remove your files on the disk."
                                         :on-click (fn []
                                                     (repo-handler/remove-repo! repo))}
                  "Unlink"]]]))]
 
-         [:a#download-as-json.hidden]]
+         [:a#download-as-json.hidden]
+         [:a#download-as-edn.hidden]]
         (widgets/add-graph)))))
 
 (rum/defc sync-status < rum/reactive

+ 1 - 1
src/main/frontend/db.cljs

@@ -30,7 +30,7 @@
   remove-conn!]
 
  [frontend.db.utils
-  date->int db->json db->string get-max-tx-id get-tx-id
+  date->int db->json db->edn-str db->string get-max-tx-id get-tx-id
   group-by-page seq-flatten sort-by-pos
   string->db with-repo
 

+ 10 - 0
src/main/frontend/handler/export.cljs

@@ -44,6 +44,16 @@
         (.setAttribute anchor "download" (str (last (string/split repo #"/")) ".json"))
         (.click anchor)))))
 
+(defn export-repo-as-edn!
+  [repo]
+  (when-let [db (db/get-conn repo)]
+    (let [db-edn (db/db->edn-str db)
+          data-str (str "data:text/edn;charset=utf-8," (js/encodeURIComponent db-edn))]
+      (when-let [anchor (gdom/getElement "download-as-edn")]
+        (.setAttribute anchor "href" data-str)
+        (.setAttribute anchor "download" (str (last (string/split repo #"/")) ".edn"))
+        (.click anchor)))))
+
 (defn download-file!
   [file-path]
   (when-let [repo (state/get-current-repo)]