🐛 Fix group undo/redo.

This commit is contained in:
Andrey Antukh 2020-04-15 09:24:29 +02:00
parent 527acac92e
commit 5fb0c5c312
3 changed files with 33 additions and 22 deletions

View file

@ -139,7 +139,7 @@
(defmethod change-spec-impl :add-obj [_] (defmethod change-spec-impl :add-obj [_]
(s/keys :req-un [::id ::frame-id ::obj] (s/keys :req-un [::id ::frame-id ::obj]
:opt-un [::session-id])) :opt-un [::session-id ::parent-id]))
(defmethod change-spec-impl :mod-obj [_] (defmethod change-spec-impl :mod-obj [_]
(s/keys :req-un [::id ::operations] (s/keys :req-un [::id ::operations]

View file

@ -7,16 +7,15 @@
;; ;;
;; Copyright (c) 2020 UXBOX Labs SL ;; Copyright (c) 2020 UXBOX Labs SL
(ns uxbox.main.data.helpers) (ns uxbox.main.data.helpers
(:require [uxbox.common.data :as d]))
(defn get-children (defn get-children
"Retrieve all children ids recursively for a given shape" "Retrieve all children ids recursively for a given shape"
[shape-id objects] [shape-id objects]
(let [shapes (get-in objects [shape-id :shapes])] (let [shapes (get-in objects [shape-id :shapes])]
(if shapes (if shapes
(concat (d/concat shapes (mapcat #(get-children % objects) shapes))
shapes
(mapcat #(get-children % objects) shapes))
[]))) [])))
(defn is-shape-grouped (defn is-shape-grouped
@ -29,7 +28,7 @@
(some contains-shape-fn shapes))) (some contains-shape-fn shapes)))
(defn get-parent (defn get-parent
"Retrieve the id of the parent for the shape-id (if exists" "Retrieve the id of the parent for the shape-id (if exists)"
[shape-id objects] [shape-id objects]
(let [check-parenthood (let [check-parenthood
(fn [shape] (when (and (:shapes shape) (fn [shape] (when (and (:shapes shape)
@ -55,9 +54,10 @@
(rec-fn shape-id []))) (rec-fn shape-id [])))
(defn replace-shapes (defn replace-shapes
"Replace inside shapes the value `to-replace-id` for the value in items keeping the same order. "Replace inside shapes the value `to-replace-id` for the value in
`to-replace-id` can be a set, a sequable or a single value. Any of these will be changed into a items keeping the same order. `to-replace-id` can be a set, a
set to make the replacement" sequable or a single value. Any of these will be changed into a set
to make the replacement"
[shape to-replace-id items] [shape to-replace-id items]
(let [should-replace (let [should-replace
(cond (cond

View file

@ -1318,17 +1318,28 @@
(let [page-id (::page-id state) (let [page-id (::page-id state)
session-id (:session-id state) session-id (:session-id state)
objects (get-in state [:workspace-data page-id :objects]) objects (get-in state [:workspace-data page-id :objects])
rchanges (mapv #(array-map :type :del-obj :id %) ids) cpindex (helpers/calculate-child-parent-map objects)
uchanges (mapv (fn [id]
(let [obj (get objects id) del-change #(array-map :type :del-obj :id %)
frm (get objects (:frame-id obj))
idx (d/index-of (:shapes frm) id)] rchanges
(reduce (fn [res id]
(let [chd (helpers/get-children id objects)]
(into res (d/concat
(mapv del-change (reverse chd))
[(del-change id)]))))
[]
ids)
uchanges
(mapv (fn [id]
(let [obj (get objects id)]
{:type :add-obj {:type :add-obj
:id id :id id
:frame-id (:id frm) :frame-id (:frame-id obj)
:index idx :parent-id (get cpindex id)
:obj obj})) :obj obj}))
(reverse ids))] (reverse (map :id rchanges)))]
(rx/of (commit-changes rchanges uchanges {:commit-local? true})))))) (rx/of (commit-changes rchanges uchanges {:commit-local? true}))))))
(defn- delete-frame (defn- delete-frame