0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 21:09:00 -05:00

🔧 Refactor page actions to use changes-builder

This commit is contained in:
Andrés Moya 2022-02-21 16:31:32 +01:00
parent e139cba621
commit 96870c3fee
2 changed files with 74 additions and 44 deletions

View file

@ -31,6 +31,57 @@
(defn with-objects [changes objects] (defn with-objects [changes objects]
(vary-meta changes assoc ::objects objects)) (vary-meta changes assoc ::objects objects))
;; Page changes
(defn add-empty-page
[chdata id name]
(-> chdata
(update :redo-changes conj {:type :add-page :id id :name name})
(update :undo-changes conj {:type :del-page :id id})))
(defn add-page
[chdata id page]
(-> chdata
(update :redo-changes conj {:type :add-page :id id :page page})
(update :undo-changes conj {:type :del-page :id id})))
(defn mod-page
[chdata page new-name]
(-> chdata
(update :redo-changes conj {:type :mod-page :id (:id page) :name new-name})
(update :undo-changes conj {:type :mod-page :id (:id page) :name (:name page)})))
(defn del-page
[chdata page]
(-> chdata
(update :redo-changes conj {:type :del-page :id (:id page)})
(update :undo-changes conj {:type :add-page :id (:id page) :page page})))
(defn move-page
[chdata index prev-index]
(let [page-id (::page-id (meta chdata))]
(-> chdata
(update :redo-changes conj {:type :mov-page :id page-id :index index})
(update :undo-changes conj {:type :mov-page :id page-id :index prev-index}))))
(defn set-page-option
[chdata option-key option-val]
(let [page-id (::page-id (meta chdata))
page (::page (meta chdata))
old-val (get-in page [:options option-key])]
(-> chdata
(update :redo-changes conj {:type :set-option
:page-id page-id
:option option-key
:value option-val})
(update :undo-changes conj {:type :set-option
:page-id page-id
:option option-key
:value old-val}))))
;; Shape changes
(defn add-obj (defn add-obj
([changes obj] ([changes obj]
(add-obj changes obj nil)) (add-obj changes obj nil))

View file

@ -320,14 +320,10 @@
unames (dwc/retrieve-used-names pages) unames (dwc/retrieve-used-names pages)
name (dwc/generate-unique-name unames "Page-1") name (dwc/generate-unique-name unames "Page-1")
rchange {:type :add-page changes (-> (pcb/empty-changes it)
:id id (pcb/add-empty-page id name))]
:name name}
uchange {:type :del-page (rx/of (dch/commit-changes changes)))))))
:id id}]
(rx/of (dch/commit-changes {:redo-changes [rchange]
:undo-changes [uchange]
:origin it})))))))
(defn duplicate-page (defn duplicate-page
[page-id] [page-id]
@ -342,13 +338,10 @@
page (-> page (assoc :name name :id id)) page (-> page (assoc :name name :id id))
rchange {:type :add-page changes (-> (pcb/empty-changes it)
:page page} (pcb/add-page id page))]
uchange {:type :del-page
:id id}] (rx/of (dch/commit-changes changes))))))
(rx/of (dch/commit-changes {:redo-changes [rchange]
:undo-changes [uchange]
:origin it}))))))
(s/def ::rename-page (s/def ::rename-page
(s/keys :req-un [::id ::name])) (s/keys :req-un [::id ::name]))
@ -360,33 +353,26 @@
(ptk/reify ::rename-page (ptk/reify ::rename-page
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page (get-in state [:workspace-data :pages-index id]) (let [page (get-in state [:workspace-data :pages-index id])
rchg {:type :mod-page changes (-> (pcb/empty-changes it)
:id id (pcb/mod-page page name))]
:name name}
uchg {:type :mod-page (rx/of (dch/commit-changes changes))))))
:id id
:name (:name page)}]
(rx/of (dch/commit-changes {:redo-changes [rchg]
:undo-changes [uchg]
:origin it}))))))
(declare purge-page) (declare purge-page)
(declare go-to-file) (declare go-to-file)
;; TODO: for some reason, the page-id here in some circumstances is `nil`
(defn delete-page (defn delete-page
[id] [id]
(ptk/reify ::delete-page (ptk/reify ::delete-page
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page (get-in state [:workspace-data :pages-index id]) (let [page (get-in state [:workspace-data :pages-index id])
rchg {:type :del-page :id id}
uchg {:type :add-page :page page}]
(rx/of (dch/commit-changes {:redo-changes [rchg] changes (-> (pcb/empty-changes it)
:undo-changes [uchg] (pcb/del-page page))]
:origin it})
(rx/of (dch/commit-changes changes)
(when (= id (:current-page-id state)) (when (= id (:current-page-id state))
go-to-file)))))) go-to-file))))))
@ -1962,19 +1948,12 @@
(ptk/reify ::change-canvas-color (ptk/reify ::change-canvas-color
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page-id (get state :current-page-id) (let [page (wsh/lookup-page state)
options (wsh/lookup-page-options state page-id) changes (-> (pcb/empty-changes it)
previous-color (:background options)] (pcb/with-page page)
(rx/of (dch/commit-changes (pcb/set-page-option :background (:color color)))]
{:redo-changes [{:type :set-option
:page-id page-id (rx/of (dch/commit-changes changes))))))
:option :background
:value (:color color)}]
:undo-changes [{:type :set-option
:page-id page-id
:option :background
:value previous-color}]
:origin it}))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Artboard ;; Artboard