dnd.cljs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (ns frontend.handler.dnd
  2. "Provides fns for drag and drop"
  3. (:require [frontend.db :as db]
  4. [frontend.handler.block :as block-handler]
  5. [frontend.handler.editor :as editor-handler]
  6. [frontend.handler.property :as property-handler]
  7. [frontend.modules.outliner.op :as outliner-op]
  8. [frontend.modules.outliner.ui :as ui-outliner-tx]
  9. [frontend.ref :as ref]
  10. [frontend.state :as state]
  11. [logseq.db :as ldb]))
  12. (defn move-blocks
  13. [^js event blocks target-block original-block move-to]
  14. (let [target-block (db/entity (:db/id target-block))
  15. blocks' (map #(db/entity (:db/id %)) blocks)
  16. first-block (first blocks')
  17. top? (= move-to :top)
  18. nested? (= move-to :nested)
  19. alt-key? (and event (.-altKey event))
  20. current-format (get first-block :block/format :markdown)
  21. target-format (get target-block :block/format :markdown)
  22. target-block (if nested? target-block
  23. (or original-block target-block))]
  24. (cond
  25. ;; alt pressed, make a block-ref
  26. (and alt-key? (= (count blocks) 1))
  27. (do
  28. (property-handler/file-persist-block-id! (state/get-current-repo) (:block/uuid first-block))
  29. (editor-handler/api-insert-new-block!
  30. (ref/->block-ref (:block/uuid first-block))
  31. {:block-uuid (:block/uuid target-block)
  32. :sibling? (not nested?)
  33. :before? top?}))
  34. ;; format mismatch
  35. (and current-format target-format (not= current-format target-format))
  36. (state/pub-event! [:notification/show
  37. {:content [:div "Those two pages have different formats."]
  38. :status :warning
  39. :clear? true}])
  40. (every? map? (conj blocks' target-block))
  41. (let [blocks' (block-handler/get-top-level-blocks blocks')]
  42. (ui-outliner-tx/transact!
  43. {:outliner-op :move-blocks}
  44. (editor-handler/save-current-block!)
  45. (if top?
  46. (let [first-child?
  47. (= (:block/uuid (:block/parent target-block))
  48. (:block/uuid (ldb/get-left-sibling target-block)))]
  49. (if first-child?
  50. (when-let [parent (:block/parent target-block)]
  51. (outliner-op/move-blocks! blocks' parent false))
  52. (if-let [before-node (ldb/get-left-sibling target-block)]
  53. (outliner-op/move-blocks! blocks' before-node true)
  54. (when-let [parent (:block/parent target-block)]
  55. (outliner-op/move-blocks! blocks' parent false)))))
  56. (outliner-op/move-blocks! blocks' target-block (not nested?)))))
  57. :else
  58. nil)))