mirror of
https://github.com/penpot/penpot.git
synced 2025-04-10 14:01:29 -05:00
🔧 Use changes-builder in many places
This commit is contained in:
parent
a7b455fb9a
commit
e609670a41
17 changed files with 174 additions and 407 deletions
|
@ -26,6 +26,10 @@
|
|||
:undo-changes []
|
||||
:origin origin}))
|
||||
|
||||
(defn set-save-undo?
|
||||
[changes save-undo?]
|
||||
(assoc changes :save-undo? save-undo?))
|
||||
|
||||
(defn with-page [changes page]
|
||||
(vary-meta changes assoc
|
||||
::page page
|
||||
|
@ -266,10 +270,10 @@
|
|||
(d/preconj
|
||||
change-set
|
||||
{:type :add-obj
|
||||
:id id
|
||||
:page-id page-id
|
||||
:parent-id (:frame-id shape)
|
||||
:frame-id (:frame-id shape)
|
||||
:id id
|
||||
:index (cph/get-position-on-parent objects id)
|
||||
:obj (cond-> shape
|
||||
(contains? shape :shapes)
|
||||
|
|
|
@ -194,15 +194,6 @@
|
|||
(conj done (:id current))
|
||||
(concat (rest pending) (:shapes current))))))
|
||||
|
||||
(defn get-index-in-parent
|
||||
"Retrieves the index in the parent"
|
||||
[objects shape-id]
|
||||
(let [shape (get objects shape-id)
|
||||
parent (get objects (:parent-id shape))
|
||||
[parent-idx _] (d/seek (fn [[_idx child-id]] (= child-id shape-id))
|
||||
(d/enumerate (:shapes parent)))]
|
||||
parent-idx))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; COMPONENTS HELPERS
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
"scripts": {
|
||||
"compile-test": "clojure -M:dev:shadow-cljs compile test --config-merge '{:autorun false}'",
|
||||
"lint-scss": "yarn run prettier -c resources/styles",
|
||||
"run-test": "node target/test.js",
|
||||
"run-test": "node target/tests.js",
|
||||
"test": "yarn run compile-test && yarn run run-test",
|
||||
"watch-gulp": "gulp watch",
|
||||
"watch-main": "shadow-cljs watch main",
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.pages.changes-builder :as cb]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.path.shapes-to-path :as stp]
|
||||
[app.common.uuid :as uuid]
|
||||
|
@ -96,10 +96,10 @@
|
|||
(when-not (empty? shapes)
|
||||
(let [[boolean-data index] (create-bool-data bool-type name shapes objects)
|
||||
shape-id (:id boolean-data)
|
||||
changes (-> (cb/empty-changes it page-id)
|
||||
(cb/with-objects objects)
|
||||
(cb/add-obj boolean-data {:index index})
|
||||
(cb/change-parent shape-id shapes))]
|
||||
changes (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/add-obj boolean-data {:index index})
|
||||
(pcb/change-parent shape-id shapes))]
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(dwc/select-shapes (d/ordered-set shape-id)))))))))
|
||||
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
(ns app.main.data.workspace.changes
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.logging :as log]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.spec :as us]
|
||||
[app.common.spec.change :as spec.change]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
|
@ -31,78 +31,38 @@
|
|||
|
||||
(def commit-changes? (ptk/type? ::commit-changes))
|
||||
|
||||
(defn- generate-operation
|
||||
"Given an object old and new versions and an attribute will append into changes
|
||||
the set and undo operations"
|
||||
[changes attr old new ignore-geometry?]
|
||||
(let [old-val (get old attr)
|
||||
new-val (get new attr)]
|
||||
(if (= old-val new-val)
|
||||
changes
|
||||
(-> changes
|
||||
(update :rops conj {:type :set :attr attr :val new-val :ignore-geometry ignore-geometry?})
|
||||
(update :uops conj {:type :set :attr attr :val old-val :ignore-touched true})))))
|
||||
|
||||
(defn- update-shape-changes
|
||||
"Calculate the changes and undos to be done when a function is applied to a
|
||||
single object"
|
||||
[changes page-id objects update-fn attrs id ignore-geometry?]
|
||||
(let [old-obj (get objects id)
|
||||
new-obj (update-fn old-obj)
|
||||
|
||||
attrs (or attrs (d/concat-set (keys old-obj) (keys new-obj)))
|
||||
|
||||
{rops :rops uops :uops}
|
||||
(reduce #(generate-operation %1 %2 old-obj new-obj ignore-geometry?)
|
||||
{:rops [] :uops []}
|
||||
attrs)
|
||||
|
||||
uops (cond-> uops
|
||||
(seq uops)
|
||||
(conj {:type :set-touched :touched (:touched old-obj)}))
|
||||
|
||||
change {:type :mod-obj :page-id page-id :id id}]
|
||||
|
||||
(cond-> changes
|
||||
(seq rops)
|
||||
(update :redo-changes conj (assoc change :operations rops))
|
||||
|
||||
(seq uops)
|
||||
(update :undo-changes conj (assoc change :operations uops)))))
|
||||
|
||||
(defn update-shapes
|
||||
([ids f] (update-shapes ids f nil nil))
|
||||
([ids f keys] (update-shapes ids f nil keys))
|
||||
([ids f page-id {:keys [reg-objects? save-undo? attrs ignore-tree]
|
||||
:or {reg-objects? false save-undo? true attrs nil}}]
|
||||
([ids update-fn] (update-shapes ids update-fn nil nil))
|
||||
([ids update-fn keys] (update-shapes ids update-fn nil keys))
|
||||
([ids update-fn page-id {:keys [reg-objects? save-undo? attrs ignore-tree]
|
||||
:or {reg-objects? false save-undo? true attrs nil}}]
|
||||
|
||||
(us/assert ::coll-of-uuid ids)
|
||||
(us/assert fn? f)
|
||||
(us/assert fn? update-fn)
|
||||
|
||||
(ptk/reify ::update-shapes
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [page-id (or page-id (:current-page-id state))
|
||||
objects (wsh/lookup-page-objects state page-id)
|
||||
changes {:redo-changes []
|
||||
:undo-changes []
|
||||
:origin it
|
||||
:save-undo? save-undo?}
|
||||
|
||||
objects (wsh/lookup-page-objects state)
|
||||
ids (into [] (filter some?) ids)
|
||||
|
||||
changes (reduce
|
||||
#(update-shape-changes %1 page-id objects f attrs %2 (get ignore-tree %2))
|
||||
changes ids)]
|
||||
(fn [changes id]
|
||||
(pcb/update-shapes changes
|
||||
[id]
|
||||
update-fn
|
||||
{:attrs attrs
|
||||
:ignore-geometry? (get ignore-tree id)}))
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/set-save-undo? save-undo?)
|
||||
(pcb/with-objects objects))
|
||||
ids)]
|
||||
|
||||
(when-not (empty? (:redo-changes changes))
|
||||
(let [reg-objs {:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes ids}
|
||||
changes (cond-> changes
|
||||
(when (seq (:redo-changes changes))
|
||||
(let [changes (cond-> changes
|
||||
reg-objects?
|
||||
(-> (update :redo-changes conj reg-objs)
|
||||
(update :undo-changes conj reg-objs)))]
|
||||
(pcb/resize-parents ids))]
|
||||
(rx/of (commit-changes changes)))))))))
|
||||
|
||||
(defn update-indices
|
||||
|
@ -125,6 +85,7 @@
|
|||
(ptk/reify ::commit-changes
|
||||
cljs.core/IDeref
|
||||
(-deref [_]
|
||||
|
||||
{:file-id file-id
|
||||
:hint-events @st/last-events
|
||||
:hint-origin (ptk/type origin)
|
||||
|
|
|
@ -8,8 +8,10 @@
|
|||
(:require
|
||||
[app.common.colors :as clr]
|
||||
[app.common.data :as d]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.spec :as us]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
[beicon.core :as rx]
|
||||
[potok.core :as ptk]))
|
||||
|
||||
|
@ -72,15 +74,8 @@
|
|||
(ptk/reify ::set-default-grid
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [pid (:current-page-id state)
|
||||
prev-value (get-in state [:workspace-data :pages-index pid :options :saved-grids type])]
|
||||
(let [page (wsh/lookup-page state)]
|
||||
(rx/of (dch/commit-changes
|
||||
{:redo-changes [{:type :set-option
|
||||
:page-id pid
|
||||
:option [:saved-grids type]
|
||||
:value params}]
|
||||
:undo-changes [{:type :set-option
|
||||
:page-id pid
|
||||
:option [:saved-grids type]
|
||||
:value prev-value}]
|
||||
:origin it}))))))
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-page page)
|
||||
(pcb/set-page-option [:saved-grids type] params))))))))
|
||||
|
|
|
@ -9,9 +9,8 @@
|
|||
[app.common.data :as d]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.pages.changes-builder :as cb]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.spec :as us]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
[app.main.data.workspace.common :as dwc]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
|
@ -65,7 +64,7 @@
|
|||
result)))))))
|
||||
|
||||
(defn prepare-create-group
|
||||
[objects page-id shapes base-name keep-name?]
|
||||
[it objects page-id shapes base-name keep-name?]
|
||||
(let [frame-id (:frame-id (first shapes))
|
||||
parent-id (:parent-id (first shapes))
|
||||
gname (if (and keep-name?
|
||||
|
@ -78,62 +77,22 @@
|
|||
selrect (gsh/selection-rect shapes)
|
||||
group (-> (cp/make-minimal-group frame-id selrect gname)
|
||||
(gsh/setup selrect)
|
||||
(assoc :shapes (mapv :id shapes)))
|
||||
|
||||
rchanges [{:type :add-obj
|
||||
:id (:id group)
|
||||
:page-id page-id
|
||||
:frame-id frame-id
|
||||
:parent-id parent-id
|
||||
:obj group
|
||||
:index (::index (first shapes))}
|
||||
|
||||
{:type :mov-objects
|
||||
:page-id page-id
|
||||
:parent-id (:id group)
|
||||
:shapes (mapv :id shapes)}]
|
||||
|
||||
uchanges (-> (mapv (fn [obj]
|
||||
{:type :mov-objects
|
||||
:page-id page-id
|
||||
:parent-id (:parent-id obj)
|
||||
:index (::index obj)
|
||||
:shapes [(:id obj)]})
|
||||
shapes)
|
||||
(conj {:type :del-obj
|
||||
:id (:id group)
|
||||
:page-id page-id}))
|
||||
(assoc :shapes (mapv :id shapes)
|
||||
:parent-id parent-id
|
||||
:frame-id frame-id
|
||||
:index (::index (first shapes))))
|
||||
|
||||
;; Look at the `get-empty-groups-after-group-creation`
|
||||
;; docstring to understand the real purpose of this code
|
||||
ids-to-delete (get-empty-groups-after-group-creation objects parent-id shapes)
|
||||
|
||||
delete-group
|
||||
(fn [changes id]
|
||||
(conj changes {:type :del-obj
|
||||
:id id
|
||||
:page-id page-id}))
|
||||
changes (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/add-obj group)
|
||||
(pcb/change-parent (:id group) shapes)
|
||||
(pcb/remove-objects ids-to-delete))]
|
||||
|
||||
add-deleted-group
|
||||
(fn [changes id]
|
||||
(let [obj (-> (get objects id)
|
||||
(dissoc :shapes))]
|
||||
(into [{:type :add-obj
|
||||
:id id
|
||||
:page-id page-id
|
||||
:frame-id (:frame-id obj)
|
||||
:parent-id (:parent-id obj)
|
||||
:obj obj
|
||||
:index (::index obj)}]
|
||||
changes)))
|
||||
|
||||
rchanges (->> ids-to-delete
|
||||
(reduce delete-group rchanges))
|
||||
|
||||
uchanges (->> ids-to-delete
|
||||
(reduce add-deleted-group uchanges))]
|
||||
|
||||
[group rchanges uchanges]))
|
||||
[group changes]))
|
||||
|
||||
(defn prepare-remove-group
|
||||
[it page-id group objects]
|
||||
|
@ -159,36 +118,13 @@
|
|||
:shape-ref
|
||||
:touched))]
|
||||
|
||||
(cond-> (-> (cb/empty-changes it page-id)
|
||||
(cb/with-objects objects)
|
||||
(cb/change-parent parent-id children index-in-parent)
|
||||
(cb/remove-objects [(:id group)]))
|
||||
(cond-> (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/change-parent parent-id children index-in-parent)
|
||||
(pcb/remove-objects [(:id group)]))
|
||||
|
||||
(some? ids-to-detach)
|
||||
(cb/update-shapes ids-to-detach detach-fn))))
|
||||
|
||||
(defn prepare-remove-mask
|
||||
[page-id mask]
|
||||
(let [rchanges [{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id mask)
|
||||
:operations [{:type :set
|
||||
:attr :masked-group?
|
||||
:val nil}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [(:id mask)]}]
|
||||
uchanges [{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id mask)
|
||||
:operations [{:type :set
|
||||
:attr :masked-group?
|
||||
:val (:masked-group? mask)}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [(:id mask)]}]]
|
||||
[rchanges uchanges]))
|
||||
|
||||
(pcb/update-shapes ids-to-detach detach-fn))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; GROUPS
|
||||
|
@ -204,11 +140,9 @@
|
|||
selected (cph/clean-loops objects selected)
|
||||
shapes (shapes-for-grouping objects selected)]
|
||||
(when-not (empty? shapes)
|
||||
(let [[group rchanges uchanges]
|
||||
(prepare-create-group objects page-id shapes "Group-1" false)]
|
||||
(rx/of (dch/commit-changes {:redo-changes rchanges
|
||||
:undo-changes uchanges
|
||||
:origin it})
|
||||
(let [[group changes]
|
||||
(prepare-create-group it objects page-id shapes "Group-1" false)]
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(dwc/select-shapes (d/ordered-set (:id group))))))))))
|
||||
|
||||
(def ungroup-selected
|
||||
|
@ -237,85 +171,39 @@
|
|||
(ptk/reify ::mask-group
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [page-id (:current-page-id state)
|
||||
objects (wsh/lookup-page-objects state page-id)
|
||||
selected (wsh/lookup-selected state)
|
||||
selected (cph/clean-loops objects selected)
|
||||
shapes (shapes-for-grouping objects selected)]
|
||||
(let [page-id (:current-page-id state)
|
||||
objects (wsh/lookup-page-objects state page-id)
|
||||
selected (wsh/lookup-selected state)
|
||||
selected (cph/clean-loops objects selected)
|
||||
shapes (shapes-for-grouping objects selected)
|
||||
first-shape (first shapes)]
|
||||
(when-not (empty? shapes)
|
||||
(let [;; If the selected shape is a group, we can use it. If not,
|
||||
;; create a new group and set it as masked.
|
||||
[group rchanges uchanges]
|
||||
[group changes]
|
||||
(if (and (= (count shapes) 1)
|
||||
(= (:type (first shapes)) :group))
|
||||
[(first shapes) [] []]
|
||||
(prepare-create-group objects page-id shapes "Group-1" true))
|
||||
[first-shape (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects))]
|
||||
(prepare-create-group it objects page-id shapes "Group-1" true))
|
||||
|
||||
;; Assertions just for documentation purposes
|
||||
_ (us/assert vector? rchanges)
|
||||
_ (us/assert vector? uchanges)
|
||||
changes (-> changes
|
||||
(pcb/update-shapes (:shapes group)
|
||||
(fn [shape]
|
||||
(assoc shape
|
||||
:constraints-h :scale
|
||||
:constraints-v :scale)))
|
||||
(pcb/update-shapes [(:id group)]
|
||||
(fn [group]
|
||||
(assoc group
|
||||
:masked-group? true
|
||||
:selrect (:selrect first-shape)
|
||||
:points (:points first-shape)
|
||||
:transform (:transform first-shape)
|
||||
:transform-inverse (:transform-inverse first-shape))))
|
||||
(pcb/resize-parents [(:id group)]))]
|
||||
|
||||
children (map #(get objects %) (:shapes group))
|
||||
|
||||
rchanges (d/concat-vec
|
||||
rchanges
|
||||
(for [child children]
|
||||
{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id child)
|
||||
:operations [{:type :set
|
||||
:attr :constraints-h
|
||||
:val :scale}
|
||||
{:type :set
|
||||
:attr :constraints-v
|
||||
:val :scale}]})
|
||||
[{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id group)
|
||||
:operations [{:type :set
|
||||
:attr :masked-group?
|
||||
:val true}
|
||||
{:type :set
|
||||
:attr :selrect
|
||||
:val (-> shapes first :selrect)}
|
||||
{:type :set
|
||||
:attr :points
|
||||
:val (-> shapes first :points)}
|
||||
{:type :set
|
||||
:attr :transform
|
||||
:val (-> shapes first :transform)}
|
||||
{:type :set
|
||||
:attr :transform-inverse
|
||||
:val (-> shapes first :transform-inverse)}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [(:id group)]}])
|
||||
|
||||
uchanges (d/concat-vec
|
||||
uchanges
|
||||
(for [child children]
|
||||
{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id child)
|
||||
:operations [{:type :set
|
||||
:attr :constraints-h
|
||||
:val (:constraints-h child)}
|
||||
{:type :set
|
||||
:attr :constraints-v
|
||||
:val (:constraints-v child)}]})
|
||||
[{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id (:id group)
|
||||
:operations [{:type :set
|
||||
:attr :masked-group?
|
||||
:val nil}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [(:id group)]}])]
|
||||
|
||||
(rx/of (dch/commit-changes {:redo-changes rchanges
|
||||
:undo-changes uchanges
|
||||
:origin it})
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(dwc/select-shapes (d/ordered-set (:id group))))))))))
|
||||
|
||||
(def unmask-group
|
||||
|
@ -325,13 +213,18 @@
|
|||
(let [page-id (:current-page-id state)
|
||||
objects (wsh/lookup-page-objects state page-id)
|
||||
|
||||
changes-in-bulk (->> (wsh/lookup-selected state)
|
||||
(map #(get objects %))
|
||||
(filter #(or (= :bool (:type %)) (= :group (:type %))))
|
||||
(map #(prepare-remove-mask page-id %)))
|
||||
rchanges-in-bulk (into [] (mapcat first) changes-in-bulk)
|
||||
uchanges-in-bulk (into [] (mapcat second) changes-in-bulk)]
|
||||
masked-groups (->> (wsh/lookup-selected state)
|
||||
(map #(get objects %))
|
||||
(filter #(or (= :bool (:type %)) (= :group (:type %)))))
|
||||
|
||||
(rx/of (dch/commit-changes {:redo-changes rchanges-in-bulk
|
||||
:undo-changes uchanges-in-bulk
|
||||
:origin it}))))))
|
||||
changes (reduce (fn [changes mask]
|
||||
(-> changes
|
||||
(pcb/update-shapes [(:id mask)]
|
||||
(fn [shape]
|
||||
(dissoc shape :masked-group?)))
|
||||
(pcb/resize-parents [(:id mask)])))
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects))
|
||||
masked-groups)]
|
||||
|
||||
(rx/of (dch/commit-changes changes))))))
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.spec :as us]
|
||||
[app.common.spec.interactions :as csi]
|
||||
|
@ -27,12 +28,8 @@
|
|||
(ptk/reify ::add-flow
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [page-id (:current-page-id state)
|
||||
flows (get-in state [:workspace-data
|
||||
:pages-index
|
||||
page-id
|
||||
:options
|
||||
:flows] [])
|
||||
(let [page (wsh/lookup-page state)
|
||||
flows (get-in page [:options :flows] [])
|
||||
|
||||
unames (into #{} (map :name flows))
|
||||
name (dwc/generate-unique-name unames "Flow-1")
|
||||
|
@ -42,15 +39,10 @@
|
|||
:starting-frame starting-frame}]
|
||||
|
||||
(rx/of (dch/commit-changes
|
||||
{:redo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value (csp/add-flow flows new-flow)}]
|
||||
:undo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value flows}]
|
||||
:origin it}))))))
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-page page)
|
||||
(pcb/set-page-option :flows
|
||||
(csp/add-flow flows new-flow)))))))))
|
||||
|
||||
(defn add-flow-selected-frame
|
||||
[]
|
||||
|
@ -66,22 +58,13 @@
|
|||
(ptk/reify ::remove-flow
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [page-id (:current-page-id state)
|
||||
flows (get-in state [:workspace-data
|
||||
:pages-index
|
||||
page-id
|
||||
:options
|
||||
:flows] [])]
|
||||
(let [page (wsh/lookup-page state)
|
||||
flows (get-in page [:options :flows] [])]
|
||||
(rx/of (dch/commit-changes
|
||||
{:redo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value (csp/remove-flow flows flow-id)}]
|
||||
:undo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value flows}]
|
||||
:origin it}))))))
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-page page)
|
||||
(pcb/set-page-option :flows
|
||||
(csp/remove-flow flows flow-id)))))))))
|
||||
|
||||
(defn rename-flow
|
||||
[flow-id name]
|
||||
|
@ -90,24 +73,14 @@
|
|||
(ptk/reify ::rename-flow
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [page-id (:current-page-id state)
|
||||
flows (get-in state [:workspace-data
|
||||
:pages-index
|
||||
page-id
|
||||
:options
|
||||
:flows] [])]
|
||||
(let [page (wsh/lookup-page state)
|
||||
flows (get-in page [:options :flows] [])]
|
||||
(rx/of (dch/commit-changes
|
||||
{:redo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value (csp/update-flow flows flow-id
|
||||
#(csp/rename-flow % name))}]
|
||||
:undo-changes [{:type :set-option
|
||||
:page-id page-id
|
||||
:option :flows
|
||||
:value flows}]
|
||||
:origin it}))))))
|
||||
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-page page)
|
||||
(pcb/set-page-option :flows
|
||||
(csp/update-flow flows flow-id
|
||||
#(csp/rename-flow % name))))))))))
|
||||
|
||||
(defn start-rename-flow
|
||||
[id]
|
||||
|
|
|
@ -288,7 +288,7 @@
|
|||
shapes (dwg/shapes-for-grouping objects selected)]
|
||||
(when-not (empty? shapes)
|
||||
(let [[group rchanges uchanges]
|
||||
(dwlh/generate-add-component shapes objects page-id file-id)]
|
||||
(dwlh/generate-add-component it shapes objects page-id file-id)]
|
||||
(when-not (empty? rchanges)
|
||||
(rx/of (dch/commit-changes {:redo-changes rchanges
|
||||
:undo-changes uchanges
|
||||
|
|
|
@ -131,7 +131,7 @@
|
|||
"If there is exactly one id, and it's a group, use it as root. Otherwise,
|
||||
create a group that contains all ids. Then, make a component with it,
|
||||
and link all shapes to their corresponding one in the component."
|
||||
[shapes objects page-id file-id]
|
||||
[it shapes objects page-id file-id]
|
||||
(if (and (= (count shapes) 1)
|
||||
(:component-id (first shapes)))
|
||||
empty-changes
|
||||
|
@ -140,7 +140,8 @@
|
|||
(if (and (= (count shapes) 1)
|
||||
(= (:type (first shapes)) :group))
|
||||
[(first shapes) [] []]
|
||||
(dwg/prepare-create-group objects page-id shapes name true))
|
||||
(let [[group changes] (dwg/prepare-create-group it objects page-id shapes name true)]
|
||||
[group (:redo-changes changes) (:undo-changes changes)]))
|
||||
|
||||
;; Asserts for documentation purposes
|
||||
_ (us/assert vector? rchanges)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
(ns app.main.data.workspace.path.changes
|
||||
(:require
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.spec :as us]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
[app.main.data.workspace.path.helpers :as helpers]
|
||||
|
@ -17,70 +17,37 @@
|
|||
[potok.core :as ptk]))
|
||||
|
||||
(defn generate-path-changes
|
||||
"Generates content changes and the undos for the content given"
|
||||
[objects page-id shape old-content new-content]
|
||||
"Generates changes to update the new content of the shape"
|
||||
[it objects page-id shape old-content new-content]
|
||||
(us/verify ::spec/content old-content)
|
||||
(us/verify ::spec/content new-content)
|
||||
(let [shape-id (:id shape)
|
||||
frame-id (:frame-id shape)
|
||||
parent-id (:parent-id shape)
|
||||
parent-index (cph/get-position-on-parent objects shape-id)
|
||||
(let [shape-id (:id shape)
|
||||
|
||||
[old-points old-selrect] (helpers/content->points+selrect shape old-content)
|
||||
[new-points new-selrect] (helpers/content->points+selrect shape new-content)
|
||||
[new-points new-selrect]
|
||||
(helpers/content->points+selrect shape new-content)
|
||||
|
||||
rch (cond
|
||||
;; https://tree.taiga.io/project/penpot/issue/2366
|
||||
(nil? shape-id)
|
||||
[]
|
||||
changes (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects))]
|
||||
|
||||
(empty? new-content)
|
||||
[{:type :del-obj
|
||||
:id shape-id
|
||||
:page-id page-id}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [shape-id]}]
|
||||
(cond
|
||||
;; https://tree.taiga.io/project/penpot/issue/2366
|
||||
(nil? shape-id)
|
||||
changes
|
||||
|
||||
:else
|
||||
[{:type :mod-obj
|
||||
:id shape-id
|
||||
:page-id page-id
|
||||
:operations [{:type :set :attr :content :val new-content}
|
||||
{:type :set :attr :selrect :val new-selrect}
|
||||
{:type :set :attr :points :val new-points}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [shape-id]}])
|
||||
(empty? new-content)
|
||||
(-> changes
|
||||
(pcb/remove-objects [shape-id])
|
||||
(pcb/resize-parents [shape-id]))
|
||||
|
||||
uch (cond
|
||||
;; https://tree.taiga.io/project/penpot/issue/2366
|
||||
(nil? shape-id)
|
||||
[]
|
||||
|
||||
(empty? new-content)
|
||||
[{:type :add-obj
|
||||
:id shape-id
|
||||
:obj shape
|
||||
:page-id page-id
|
||||
:frame-id frame-id
|
||||
:parent-id parent-id
|
||||
:index parent-index}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [shape-id]}]
|
||||
|
||||
:else
|
||||
[{:type :mod-obj
|
||||
:id shape-id
|
||||
:page-id page-id
|
||||
:operations [{:type :set :attr :content :val old-content}
|
||||
{:type :set :attr :selrect :val old-selrect}
|
||||
{:type :set :attr :points :val old-points}]}
|
||||
{:type :reg-objects
|
||||
:page-id page-id
|
||||
:shapes [shape-id]}])]
|
||||
[rch uch]))
|
||||
:else
|
||||
(-> changes
|
||||
(pcb/update-shapes [shape-id]
|
||||
(fn [shape]
|
||||
(assoc shape
|
||||
:content new-content
|
||||
:selrect new-selrect
|
||||
:points new-points)))
|
||||
(pcb/resize-parents [shape-id])))))
|
||||
|
||||
(defn save-path-content
|
||||
([]
|
||||
|
@ -105,10 +72,8 @@
|
|||
old-content (get-in state [:workspace-local :edit-path id :old-content])
|
||||
shape (st/get-path state)]
|
||||
(if (and (some? old-content) (some? (:id shape)))
|
||||
(let [[rch uch] (generate-path-changes objects page-id shape old-content (:content shape))]
|
||||
(rx/of (dch/commit-changes {:redo-changes rch
|
||||
:undo-changes uch
|
||||
:origin it})))
|
||||
(let [changes (generate-path-changes it objects page-id shape old-content (:content shape))]
|
||||
(rx/of (dch/commit-changes changes)))
|
||||
(rx/empty)))))))
|
||||
|
||||
|
||||
|
|
|
@ -61,15 +61,11 @@
|
|||
point-change (->> (map hash-map old-points new-points) (reduce merge))]
|
||||
|
||||
(when (and (some? new-content) (some? shape))
|
||||
(let [[rch uch] (changes/generate-path-changes objects page-id shape (:content shape) new-content)]
|
||||
(let [changes (changes/generate-path-changes it objects page-id shape (:content shape) new-content)]
|
||||
(if (empty? new-content)
|
||||
(rx/of (dch/commit-changes {:redo-changes rch
|
||||
:undo-changes uch
|
||||
:origin it})
|
||||
(rx/of (dch/commit-changes changes)
|
||||
dwc/clear-edition-mode)
|
||||
(rx/of (dch/commit-changes {:redo-changes rch
|
||||
:undo-changes uch
|
||||
:origin it})
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(selection/update-selection point-change)
|
||||
(fn [state] (update-in state [:workspace-local :edit-path id] dissoc :content-modifiers :moving-nodes :moving-handler))))))))))
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
(ns app.main.data.workspace.path.shapes-to-path
|
||||
(:require
|
||||
[app.common.pages.changes-builder :as cb]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.path.shapes-to-path :as upsp]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
|
@ -28,9 +28,9 @@
|
|||
selected)
|
||||
|
||||
changes
|
||||
(-> (cb/empty-changes it page-id)
|
||||
(cb/with-objects objects)
|
||||
(cb/remove-objects children-ids)
|
||||
(cb/update-shapes selected #(upsp/convert-to-path % objects)))]
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/remove-objects children-ids)
|
||||
(pcb/update-shapes selected #(upsp/convert-to-path % objects)))]
|
||||
|
||||
(rx/of (dch/commit-changes changes))))))
|
||||
|
|
|
@ -34,13 +34,11 @@
|
|||
(when (and (seq points) (some? shape))
|
||||
(let [new-content (-> (tool-fn (:content shape) points)
|
||||
(ups/close-subpaths))
|
||||
[rch uch] (changes/generate-path-changes objects page-id shape (:content shape) new-content)]
|
||||
changes (changes/generate-path-changes it objects page-id shape (:content shape) new-content)]
|
||||
|
||||
(rx/concat
|
||||
(rx/of (dch/update-shapes [id] upsp/convert-to-path))
|
||||
(rx/of (dch/commit-changes {:redo-changes rch
|
||||
:undo-changes uch
|
||||
:origin it})
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(when (empty? new-content)
|
||||
dwc/clear-edition-mode))))))))))
|
||||
|
||||
|
|
|
@ -407,7 +407,7 @@
|
|||
changes (-> changes
|
||||
(pcb/with-objects objects)
|
||||
(pcb/add-obj new-shape)
|
||||
(pcb/change-parent parent-id [new-shape]))
|
||||
(pcb/change-parent parent-id [new-shape] index))
|
||||
|
||||
unames (conj unames (:name new-shape))
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.math :as mth]
|
||||
[app.common.pages.changes-builder :as pcb]
|
||||
[app.common.pages.common :as cpc]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.spec :as us]
|
||||
|
@ -673,26 +674,13 @@
|
|||
(remove #(or (nil? %)
|
||||
(= (:frame-id %) frame-id))))
|
||||
|
||||
rch [{:type :mov-objects
|
||||
:page-id page-id
|
||||
:parent-id frame-id
|
||||
:shapes (mapv :id moving-shapes)}]
|
||||
changes (-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects)
|
||||
(pcb/change-parent frame-id moving-shapes))]
|
||||
|
||||
|
||||
uch (->> moving-shapes
|
||||
(reverse)
|
||||
(mapv (fn [shape]
|
||||
{:type :mov-objects
|
||||
:page-id page-id
|
||||
:parent-id (:parent-id shape)
|
||||
:index (cph/get-index-in-parent objects (:id shape))
|
||||
:shapes [(:id shape)]})))]
|
||||
|
||||
(when-not (empty? uch)
|
||||
(when-not (empty? changes)
|
||||
(rx/of dwu/pop-undo-into-transaction
|
||||
(dch/commit-changes {:redo-changes rch
|
||||
:undo-changes uch
|
||||
:origin it})
|
||||
(dch/commit-changes changes)
|
||||
(dwu/commit-undo-transaction)
|
||||
(dwc/expand-collapse frame-id)))))))
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
[app.main.data.workspace :as dw]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
[app.main.data.workspace.groups :as dwg]
|
||||
[app.main.data.workspace.layout :as layout]
|
||||
[app.main.data.workspace.libraries-helpers :as dwlh]))
|
||||
|
||||
;; ---- Helpers to manage pages and objects
|
||||
|
@ -21,8 +22,8 @@
|
|||
(def initial-state
|
||||
{:current-file-id current-file-id
|
||||
:current-page-id nil
|
||||
:workspace-global dw/default-workspace-global
|
||||
:workspace-local dw/default-workspace-local
|
||||
:workspace-layout layout/default-layout
|
||||
:workspace-global layout/default-global
|
||||
:workspace-data {:id current-file-id
|
||||
:components {}
|
||||
:pages []
|
||||
|
@ -87,12 +88,12 @@
|
|||
shapes (dwg/shapes-for-grouping (:objects page) ids)]
|
||||
(if (empty? shapes)
|
||||
state
|
||||
(let [[group rchanges uchanges]
|
||||
(dwg/prepare-create-group (:objects page) (:id page) shapes prefix true)]
|
||||
(let [[group changes]
|
||||
(dwg/prepare-create-group nil (:objects page) (:id page) shapes prefix true)]
|
||||
|
||||
(swap! idmap assoc label (:id group))
|
||||
(update state :workspace-data
|
||||
cp/process-changes rchanges))))))
|
||||
cp/process-changes (:redo-changes changes)))))))
|
||||
|
||||
(defn make-component
|
||||
[state label ids]
|
||||
|
@ -101,7 +102,8 @@
|
|||
shapes (dwg/shapes-for-grouping objects ids)
|
||||
|
||||
[group rchanges uchanges]
|
||||
(dwlh/generate-add-component shapes
|
||||
(dwlh/generate-add-component nil
|
||||
shapes
|
||||
(:objects page)
|
||||
(:id page)
|
||||
current-file-id)]
|
||||
|
|
Loading…
Add table
Reference in a new issue