diff --git a/common/src/app/common/data.cljc b/common/src/app/common/data.cljc index a0646bdf7..1d7b4959e 100644 --- a/common/src/app/common/data.cljc +++ b/common/src/app/common/data.cljc @@ -223,20 +223,6 @@ ([data] (into {} (without-nils) data))) -(defn without-nils-deep - "Given a map remove the `nil` values and when a child map is found - recursively removes them as well." - [data] - (let [data (without-nils - (c/update-vals - data - (fn [value] - (cond-> value - (map? value) - (without-nils-deep)))))] - (when (not-empty? data) - data))) - (defn without-qualified ([] (remove (comp qualified-keyword? key))) @@ -251,6 +237,36 @@ (persistent! (reduce dissoc! (transient data) keys)) (reduce dissoc data keys))) +(defn patch-object + "Changes is some attributes that need to change in object. + When the attribute is nil it will be removed. + + For example + - object: {:a 1 :b {:foo 1 :bar 2} :c 10} + - changes: {:a 2 :b {:foo nil :k 3}} + - result: {:a 2 :b {:bar 2 :k 3} :c 10} + " + ([changes] + #(patch-object % changes)) + + ([object changes] + (->> changes + (reduce-kv + (fn [object key value] + (cond + (map? value) + (update object key patch-object value) + + (and (nil? value) (record? object)) + (assoc object key nil) + + (nil? value) + (dissoc object key value) + + :else + (assoc object key value))) + object)))) + (defn remove-at-index "Takes a vector and returns a vector with an element in the specified index removed." diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index d09f257a1..e69d6c914 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -369,7 +369,7 @@ (watch [_ _ _] (let [undo-id (js/Symbol)] (rx/of (dwu/start-undo-transaction undo-id) - (dwc/update-shapes ids #(d/deep-merge % changes)) + (dwc/update-shapes ids (d/patch-object changes)) (ptk/data-event :layout/update ids) (dwu/commit-undo-transaction undo-id)))))) @@ -553,14 +553,15 @@ parent-ids (->> ids (map #(cph/get-parent-id objects %))) undo-id (js/Symbol)] (rx/of (dwu/start-undo-transaction undo-id) - (dwc/update-shapes ids #(d/without-nils-deep (d/deep-merge (or % {}) changes))) + (dwc/update-shapes ids (d/patch-object changes)) (dwc/update-shapes children-ids (partial fix-child-sizing objects changes)) - (dwc/update-shapes parent-ids - (fn [parent] - (-> parent - (fix-parent-sizing objects (set ids) changes) - (cond-> (ctl/grid-layout? parent) - (ctl/assign-cells))))) + (dwc/update-shapes + parent-ids + (fn [parent] + (-> parent + (fix-parent-sizing objects (set ids) changes) + (cond-> (ctl/grid-layout? parent) + (ctl/assign-cells))))) (ptk/data-event :layout/update ids) (dwu/commit-undo-transaction undo-id)))))) @@ -577,11 +578,13 @@ [layout-id] (fn [shape] (->> ids - (reduce (fn [shape cell-id] - (-> shape - (d/update-in-when [:layout-grid-cells cell-id] - #(d/without-nils (merge % props))))) - shape)))) + (reduce + (fn [shape cell-id] + (d/update-in-when + shape + [:layout-grid-cells cell-id] + d/patch-object props)) + shape)))) (ptk/data-event :layout/update [layout-id]) (dwu/commit-undo-transaction undo-id)))))) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/layout_container.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/layout_container.cljs index dd62d15a5..8820a30bf 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/layout_container.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/layout_container.cljs @@ -525,6 +525,7 @@ :on-focus #(do (dom/select-target %) (select-paddings true false true false)) + :nillable true :min 0 :value p1}]] [:div {:class (stl/css :padding-simple) @@ -539,6 +540,7 @@ :on-focus #(do (dom/select-target %) (select-paddings false true false true)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value p2}]]] (= padding-type :multiple) @@ -555,6 +557,7 @@ :on-focus #(do (dom/select-target %) (select-padding :p1)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value (:p1 (:layout-padding values))}]] @@ -569,6 +572,7 @@ :on-focus #(do (dom/select-target %) (select-padding :p2)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value (:p2 (:layout-padding values))}]] @@ -583,6 +587,7 @@ :on-focus #(do (dom/select-target %) (select-padding :p3)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value (:p3 (:layout-padding values))}]] @@ -597,6 +602,7 @@ :on-focus #(do (dom/select-target %) (select-padding :p4)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value (:p4 (:layout-padding values))}]]])] [:button {:class (stl/css-case :padding-toggle true @@ -618,6 +624,7 @@ :on-focus #(do (dom/select-target %) (select-paddings true false true false)) + :nillable true :min 0 :value p1}]] @@ -630,6 +637,7 @@ :on-focus #(do (dom/select-target %) (select-paddings false true false true)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value p2}]]] @@ -650,6 +658,7 @@ :on-focus #(do (dom/select-target %) (select-padding num)) :on-blur #(select-paddings false false false false) + :nillable true :min 0 :value (num (:layout-padding values))}]]])]) @@ -691,6 +700,7 @@ :on-blur (fn [_] (select-gap nil) (reset! gap-selected? :none)) + :nillable true :min 0 :value (:row-gap gap-value) :disabled (and (= :nowrap wrap-type) (not is-col?))}]] @@ -710,6 +720,7 @@ :on-blur (fn [_] (select-gap nil) (reset! gap-selected? :none)) + :nillable true :min 0 :value (:column-gap gap-value) :disabled (and (= :nowrap wrap-type) is-col?)}]]] @@ -731,6 +742,7 @@ :on-blur (fn [_] (select-gap nil) (reset! gap-selected? :none)) + :nillable true :min 0 :value (:column-gap gap-value) :disabled (and (= :nowrap wrap-type) is-col?)}]] @@ -749,6 +761,7 @@ :on-blur (fn [_] (select-gap nil) (reset! gap-selected? :none)) + :nillable true :min 0 :value (:row-gap gap-value) :disabled (and (= :nowrap wrap-type) (not is-col?))}]]]])))