lifecycle.cljs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (ns frontend.handler.editor.lifecycle
  2. (:require [frontend.state :as state]
  3. [goog.dom :as gdom]
  4. [goog.object :as gobj]
  5. [clojure.string :as string]
  6. [cljs-drag-n-drop.core :as dnd]
  7. [frontend.handler.editor.keyboards :as keyboards-handler]
  8. [frontend.handler.page :as page-handler]
  9. [frontend.handler.editor :as editor-handler :refer [get-state]]
  10. [frontend.handler.notification :as notification]
  11. [frontend.db :as db]
  12. [frontend.date :as date]
  13. [frontend.handler.file :as file]
  14. [promesa.core :as p]))
  15. (defn did-mount!
  16. [state]
  17. (let [[{:keys [dummy? format block-parent-id]} id] (:rum/args state)
  18. content (get-in @state/state [:editor/content id])
  19. input (gdom/getElement id)]
  20. (when block-parent-id
  21. (state/set-editing-block-dom-id! block-parent-id))
  22. (editor-handler/restore-cursor-pos! id content dummy?)
  23. (when input
  24. (dnd/subscribe!
  25. input
  26. :upload-images
  27. {:drop (fn [e files]
  28. (editor-handler/upload-asset id files format editor-handler/*asset-uploading? true))}))
  29. ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
  30. ;; which will hide the editor so no way for editing.
  31. (js/setTimeout #(keyboards-handler/esc-save! state) 100)
  32. (when-let [element (gdom/getElement id)]
  33. (.focus element)))
  34. state)
  35. (defn did-remount!
  36. [_old-state state]
  37. (keyboards-handler/esc-save! state)
  38. state)
  39. (defn will-unmount
  40. [state]
  41. (let [{:keys [id value format block repo dummy? config]} (get-state)
  42. file? (:file? config)]
  43. (when-let [input (gdom/getElement id)]
  44. ;; (.removeEventListener input "paste" (fn [event]
  45. ;; (append-paste-doc! format event)))
  46. (let [s (str "cljs-drag-n-drop." :upload-images)
  47. a (gobj/get input s)
  48. timer (:timer a)]
  49. (and timer
  50. (dnd/unsubscribe!
  51. input
  52. :upload-images))))
  53. (editor-handler/clear-when-saved!)
  54. ;; TODO: ugly
  55. (when-not (contains? #{:insert :indent-outdent :auto-save :undo :redo} (state/get-editor-op))
  56. (editor-handler/save-block! (get-state) value)))
  57. state)
  58. (def lifecycle
  59. {:did-mount did-mount!
  60. :did-remount did-remount!
  61. :will-unmount will-unmount})