0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-04 13:50:12 -05:00

Adapt shapes d&d ordering to new data structure.

This commit is contained in:
Andrey Antukh 2020-03-09 12:22:31 +01:00
parent 0a30c8efdf
commit 5e91f05a7b
5 changed files with 55 additions and 143 deletions

View file

@ -35,7 +35,7 @@ Mozilla Public License Version 2.0
means any form of the work other than Source Code Form. means any form of the work other than Source Code Form.
1.7. "Larger Work" 1.7. "Larger Work"
means a work that combines Covered Software with other material, in means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software. a separate file or files, that is not Covered Software.
1.8. "License" 1.8. "License"

View file

@ -119,7 +119,7 @@
(defmethod operation-spec-impl :set [_] (defmethod operation-spec-impl :set [_]
(s/keys :req-un [::attr ::val])) (s/keys :req-un [::attr ::val]))
(defmethod operation-spec-impl :mov [_] (defmethod operation-spec-impl :order [_]
(s/keys :req-un [::id ::index])) (s/keys :req-un [::id ::index]))
(s/def ::operation (s/multi-spec operation-spec-impl :type)) (s/def ::operation (s/multi-spec operation-spec-impl :type))
@ -179,6 +179,9 @@
(defmulti process-change (defmulti process-change
(fn [data change] (:type change))) (fn [data change] (:type change)))
(defmulti process-operation
(fn [_ op] (:type op)))
(defn process-changes (defn process-changes
[data items] [data items]
(->> (us/verify ::changes items) (->> (us/verify ::changes items)
@ -205,24 +208,11 @@
(let [[before after] (split-at index shapes)] (let [[before after] (split-at index shapes)]
(d/concat [] before [id] after)))))))) (d/concat [] before [id] after))))))))
(defn- process-obj-operation
[shape op]
(case (:type op)
:set
(let [attr (:attr op)
val (:val op)]
(if (nil? val)
(dissoc shape attr)
(assoc shape attr val)))
(ex/raise :type :not-implemented
:hint "TODO")))
(defmethod process-change :mod-obj (defmethod process-change :mod-obj
[data {:keys [id operations] :as change}] [data {:keys [id operations] :as change}]
(assert (contains? (:objects data) id) "process-change/mod-obj") (assert (contains? (:objects data) id) "process-change/mod-obj")
(update-in data [:objects id] (update-in data [:objects id]
#(reduce process-obj-operation % operations))) #(reduce process-operation % operations)))
(defmethod process-change :mov-obj (defmethod process-change :mov-obj
[data {:keys [id frame-id] :as change}] [data {:keys [id frame-id] :as change}]
@ -242,102 +232,26 @@
(update-in [:objects frame-id :shapes] (update-in [:objects frame-id :shapes]
(fn [s] (filterv #(not= % id) s)))))) (fn [s] (filterv #(not= % id) s))))))
;; (defn- process-change (defmethod process-operation :set
;; [data {:keys [type] :as change}] [shape op]
;; (case type (let [attr (:attr op)
;; :add-obj (process-add-obj data change) val (:val op)]
;; :mod-obj (process-mod-obj data change) (if (nil? val)
(dissoc shape attr)
(assoc shape attr val))))
;; ;; :add-shape (process-add-shape data change) (defmethod process-operation :order
;; ;; :add-canvas (process-add-canvas data change) [obj {:keys [id index]}]
;; ;; :mod-shape (process-mod-shape data change) (prn "process-operation" :order obj)
;; ;; :mov-shape (process-mov-shape data change) (assert (vector? (:shapes obj)) ":shapes should be a vector")
;; ;; :del-shape (process-del-shape data change) (update obj :shapes (fn [items]
;; ;; :del-canvas (process-del-canvas data change) (let [[b a] (->> (remove #(= % id) items)
;; ;; :mod-opts (process-mod-opts data change) (split-at index))]
;; )) (vec (concat b [id] a))))))
;; (defn- process-add-obj (defmethod process-operation :default
[shape op]
(ex/raise :type :operation-not-implemented
:context {:type (:type op)}))
;; (defn- process-add-shape
;; [data {:keys [id index shape] :as change}]
;; (-> data
;; (update :shapes (fn [shapes]
;; (cond
;; (some #{id} shapes)
;; shapes
;; (nil? index)
;; (conj shapes id)
;; :else
;; (let [[before after] (split-at index shapes)]
;; (d/concat [] before [id] after)))))
;; (update :shapes-by-id assoc id shape)))
;; (defn- process-add-canvas
;; [data {:keys [id shape index] :as change}]
;; (-> data
;; (update :canvas (fn [shapes]
;; (cond
;; (some #{id} shapes)
;; shapes
;; (nil? index)
;; (conj shapes id)
;; :else
;; (let [[before after] (split-at index shapes)]
;; (d/concat [] before [id] after)))))
;; (update :shapes-by-id assoc id shape)))
;; (defn- process-mod-shape
;; [data {:keys [id operations] :as change}]
;; (if (get-in data [:shapes-by-id id])
;; (update-in data [:shapes-by-id id]
;; #(reduce (fn [shape [_ att val]]
;; (if (nil? val)
;; (dissoc shape att)
;; (assoc shape att val)))
;; % operations))
;; data))
;; (defn- process-mod-opts
;; [data {:keys [operations]}]
;; (update data :options
;; #(reduce (fn [options [_ att val]]
;; (if (nil? val)
;; (dissoc options att)
;; (assoc options att val)))
;; % operations)))
;; (defn- process-mov-shape
;; [data {:keys [id index]}]
;; (let [shapes (:shapes data)
;; current-index (d/index-of shapes id)
;; shapes' (into [] (remove #(= % id) shapes))]
;; (cond
;; (= index current-index)
;; data
;; (nil? current-index)
;; (assoc data :shapes (d/concat [id] shapes'))
;; :else
;; (let [[before after] (split-at index shapes')]
;; (assoc data :shapes (d/concat [] before [id] after))))))
;; (defn- process-del-shape
;; [data {:keys [id] :as change}]
;; (-> data
;; (update :shapes (fn [s] (filterv #(not= % id) s)))
;; (update :shapes-by-id dissoc id)))
;; (defn- process-del-canvas
;; [data {:keys [id] :as change}]
;; (-> data
;; (update :canvas (fn [s] (filterv #(not= % id) s)))
;; (update :shapes-by-id dissoc id)))

View file

@ -981,6 +981,7 @@
:stroke-opacity 1 :stroke-opacity 1
:frame-id uuid/zero :frame-id uuid/zero
:fill-color "#ffffff" :fill-color "#ffffff"
:shapes []
:fill-opacity 1}) :fill-opacity 1})
(defn add-frame (defn add-frame
@ -1376,23 +1377,28 @@
(ptk/reify ::commit-shape-order-change (ptk/reify ::commit-shape-order-change
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [page-id (get-in state [:workspace-page :id])
curr-shapes (get-in state [:workspace-data :shapes])
prev-shapes (get-in state [:pages-data page-id :shapes])
curr-index (d/index-of curr-shapes id) (let [obj (get-in state [:workspace-data :objects id])
prev-index (d/index-of prev-shapes id) pid (get-in state [:workspace-page :id])
cfrm (get-in state [:workspace-data :objects (:frame-id obj)])
pfrm (get-in state [:pages-data pid :objects (:frame-id obj)])
cindex (d/index-of (:shapes cfrm) id)
pindex (d/index-of (:shapes pfrm) id)
session-id (:session-id state) session-id (:session-id state)
change {:type :mov-shape rchange {:type :mod-obj
:session-id session-id :session-id session-id
:id id :id (:id cfrm)
:index curr-index} :operations [{:type :order :id id :index cindex}]}
uchange {:type :mov-shape uchange {:type :mod-obj
:session-id session-id :session-id session-id
:id id :id (:id cfrm)
:index prev-index}] :operations [{:type :order :id id :index pindex}]}]
(rx/of (commit-changes [change] [uchange])))))) (prn "commit-shape-order-change3" rchange)
(rx/of (commit-changes [rchange] [uchange]))))))
;; --- Change Frame Order (D&D Ordering) ;; --- Change Frame Order (D&D Ordering)
@ -1400,7 +1406,7 @@
[{:keys [id index] :as params}] [{:keys [id index] :as params}]
(us/verify ::us/uuid id) (us/verify ::us/uuid id)
(us/verify ::us/number index) (us/verify ::us/number index)
(ptk/reify ::change-frame-order #_(ptk/reify ::change-frame-order
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [shapes (get-in state [:workspace-data :frame]) (let [shapes (get-in state [:workspace-data :frame])
@ -1458,14 +1464,11 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [[rch uch] (impl-diff state)] (let [[rch uch] (impl-diff state)]
;; (prn "rehash-shape-frame-relationship" rch)
;; (prn "rehash-shape-frame-relationship" uch)
(when-not (empty? rch) (when-not (empty? rch)
(rx/of (commit-changes rch uch {:commit-local? true})))))))) (rx/of (commit-changes rch uch {:commit-local? true}))))))))
(defn assoc-resize-modifier-in-bulk (defn assoc-resize-modifier-in-bulk
[ids xfmt] [ids xfmt]
;; (prn "assoc-resize-modifier-in-bulk" ids)
(us/verify ::set-of-uuid ids) (us/verify ::set-of-uuid ids)
(us/verify gmt/matrix? xfmt) (us/verify gmt/matrix? xfmt)
(ptk/reify ::assoc-resize-modifier-in-bulk (ptk/reify ::assoc-resize-modifier-in-bulk
@ -1484,14 +1487,10 @@
(ptk/reify ::materialize-resize-modifier-in-bulk (ptk/reify ::materialize-resize-modifier-in-bulk
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
;; (prn "materialize-resize-modifier-in-bulk$update" ids)
(reduce process-shape state ids)) (reduce process-shape state ids))
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
;; (prn "materialize-resize-modifier-in-bulk$watch" ids)
(rx/of diff-and-commit-changes (rx/of diff-and-commit-changes
(rehash-shape-frame-relationship ids)))))) (rehash-shape-frame-relationship ids))))))

View file

@ -120,15 +120,15 @@
on-drop on-drop
(fn [item monitor] (fn [item monitor]
#_(st/emit! (dw/commit-shape-order-change (:shape-id item)))) (st/emit! (dw/commit-shape-order-change (:obj-id item))))
on-hover on-hover
(fn [item monitor] (fn [item monitor]
(st/emit! (dw/shape-order-change (:shape-id item) index))) (st/emit! (dw/shape-order-change (:obj-id item) index)))
[dprops dnd-ref] (use-sortable [dprops dnd-ref] (use-sortable
{:type "layer-item" {:type (str "layer-item" (:frame-id item))
:data {:shape-id (:id item) :data {:obj-id (:id item)
:page-id (:page item) :page-id (:page item)
:index index} :index index}
:on-hover on-hover :on-hover on-hover
@ -197,16 +197,15 @@
on-drop on-drop
(fn [item monitor] (fn [item monitor]
(st/emit! ::dw/page-data-update)) (st/emit! (dw/commit-shape-order-change (:obj-id item))))
on-hover on-hover
(fn [item monitor] (fn [item monitor]
(st/emit! (dw/change-frame-order {:id (:frame-id item) (st/emit! (dw/shape-order-change (:obj-id item) index)))
:index index})))
[dprops dnd-ref] (use-sortable [dprops dnd-ref] (use-sortable
{:type "frame-item" {:type (str "layer-item" (:frame-id item))
:data {:frame-id (:id item) :data {:obj-id (:id item)
:page-id (:page item) :page-id (:page item)
:index index} :index index}
:on-hover on-hover :on-hover on-hover

View file

@ -151,7 +151,7 @@
shapes (->> (:shapes root) shapes (->> (:shapes root)
(map #(get objects %)))] (map #(get objects %)))]
[:g.shapes [:g.shapes
(for [item shapes] (for [item (reverse shapes)]
(if (= (:type item) :frame) (if (= (:type item) :frame)
[:& frame-wrapper {:shape item [:& frame-wrapper {:shape item
:key (:id item) :key (:id item)