draw.cljs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. (ns frontend.handler.draw
  2. (:refer-clojure :exclude [load-file])
  3. (:require [frontend.util :as util :refer-macros [profile]]
  4. [frontend.fs :as fs]
  5. [promesa.core :as p]
  6. [frontend.state :as state]
  7. [frontend.db :as db]
  8. [frontend.handler.file :as file-handler]
  9. [frontend.date :as date]
  10. [frontend.config :as config]))
  11. (defn create-draws-directory!
  12. [repo]
  13. (when repo
  14. (let [repo-dir (config/get-repo-dir repo)]
  15. (util/p-handle
  16. (fs/mkdir! (str repo-dir (str "/" config/default-draw-directory)))
  17. (fn [_result] nil)
  18. (fn [_error] nil)))))
  19. (defn save-excalidraw!
  20. [file data]
  21. (let [path file
  22. repo (state/get-current-repo)]
  23. (when repo
  24. (let [repo-dir (config/get-repo-dir repo)]
  25. (->
  26. (p/do!
  27. (create-draws-directory! repo)
  28. (fs/write-file! repo repo-dir path data nil)
  29. (db/transact! repo
  30. [{:file/path path
  31. :block/name file
  32. :block/file {:file/path path}
  33. :block/journal? false}]))
  34. (p/catch (fn [error]
  35. (prn "Write file failed, path: " path ", data: " data)
  36. (js/console.dir error))))))))
  37. (defn load-excalidraw-file
  38. [file ok-handler]
  39. (when-let [repo (state/get-current-repo)]
  40. (util/p-handle
  41. (file-handler/load-file repo file)
  42. (fn [content]
  43. (ok-handler content))
  44. (fn [error]
  45. (println "Error loading " file ": "
  46. error)))))
  47. (defonce default-content
  48. (util/format
  49. "{\n \"type\": \"excalidraw\",\n \"version\": 2,\n \"source\": \"%s\",\n \"elements\": [],\n \"appState\": {\n \"viewBackgroundColor\": \"#FFF\",\n \"gridSize\": null\n }\n}"
  50. config/website))
  51. (defn file-name
  52. []
  53. (str (date/get-date-time-string-2) ".excalidraw"))
  54. (defn create-draw-with-default-content
  55. [current-file]
  56. (when-let [repo (state/get-current-repo)]
  57. (p/let [exists? (fs/file-exists? (config/get-repo-dir repo)
  58. (str config/default-draw-directory current-file))]
  59. (when-not exists?
  60. (save-excalidraw! current-file default-content)))))