diff --git a/common/src/app/common/logic/bool.cljc b/common/src/app/common/logic/bool.cljc new file mode 100644 index 0000000000..3637af2885 --- /dev/null +++ b/common/src/app/common/logic/bool.cljc @@ -0,0 +1,108 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns app.common.logic.bool + "The changes of the boolean actions." + (:require + [app.common.data :as d] + [app.common.files.changes-builder :as pcb] + [app.common.files.helpers :as cfh] + [app.common.geom.shapes :as gsh] + [app.common.types.component :as ctk] + [app.common.types.container :as ctn] + [app.common.types.path :as path] + [app.common.types.path.bool :as bool] + [app.common.types.shape :as cts] + [app.common.types.shape.layout :as ctl] + [app.common.uuid :as uuid] + [cuerdas.core :as str])) + +(defn- create-bool-shape + [id type name shapes objects] + (let [shape-id + (or id (uuid/next)) + + head + (if (= type :difference) (first shapes) (last shapes)) + + head + (cond-> head + (and (contains? head :svg-attrs) (empty? (:fills head))) + (assoc :fills (path/get-default-bool-fills))) + + shape + {:id shape-id + :type :bool + :bool-type type + :frame-id (:frame-id head) + :parent-id (:parent-id head) + :name name + :shapes (into [] d/xf:map-id shapes)} + + shape + (-> shape + (merge (select-keys head path/bool-style-properties)) + (cts/setup-shape) + (path/update-bool-shape objects))] + + [shape (cfh/get-position-on-parent objects (:id head))])) + +(defn group->bool + [type group objects] + (let [shapes (->> (:shapes group) + (map (d/getf objects))) + head (if (= type :difference) (first shapes) (last shapes)) + head (cond-> head + (and (contains? head :svg-attrs) (empty? (:fills head))) + (assoc :fills (path/get-default-bool-fills)))] + (-> group + (assoc :type :bool) + (assoc :bool-type type) + (merge (select-keys head bool/style-properties)) + (path/update-bool-shape objects)))) + +(defn bool->group + [shape objects] + (-> shape + (assoc :type :group) + (dissoc :bool-type) + (d/without-keys path/bool-group-style-properties) + (gsh/update-group-selrect + (mapv (d/getf objects) + (:shapes shape))))) + +(defn generate-create-bool + "The bool of `type` that combines the shapes of `ids`, and the changes + that create it; nil when none of them can take part: boards, variants + and shapes inside a copy do not." + [it page-id objects ids type force-shape-id] + (let [name + (-> type d/name str/capital) + + xform + (comp + (map (d/getf objects)) + (remove cfh/frame-shape?) + (remove ctk/is-variant?) + (remove #(ctn/has-any-copy-parent? objects %))) + + shapes + (->> (cfh/order-by-indexed-shapes objects ids) + (into [] xform) + (not-empty))] + + (when shapes + (let [[shape index] + (create-bool-shape force-shape-id type name (reverse shapes) objects) + + shape-id + (get shape :id)] + [shape + (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects) + (pcb/add-object shape {:index (inc index)}) + (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) + (pcb/change-parent shape-id shapes))])))) diff --git a/common/src/app/common/logic/groups.cljc b/common/src/app/common/logic/groups.cljc new file mode 100644 index 0000000000..38a13b172c --- /dev/null +++ b/common/src/app/common/logic/groups.cljc @@ -0,0 +1,266 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns app.common.logic.groups + "The changes of the group, ungroup, mask and unmask actions." + (:require + [app.common.data :as d] + [app.common.data.macros :as dm] + [app.common.files.changes-builder :as pcb] + [app.common.files.helpers :as cfh] + [app.common.geom.shapes :as gsh] + [app.common.types.component :as ctk] + [app.common.types.container :as ctn] + [app.common.types.shape :as cts] + [app.common.types.shape.layout :as ctl])) + +(defn shapes-for-grouping + [objects selected] + (->> selected + (cfh/order-by-indexed-shapes objects) + reverse + (map #(get objects %)))) + +(defn shapes-for-group-or-mask + "Shapes allowed for Group and Mask after the shared variant gates. + + `ids-for-structure-op` drops mains before `clean-loops` so a selected + variant main does not swallow nested children." + [objects ids] + (shapes-for-grouping objects (ctn/ids-for-structure-op objects ids))) + +(defn- get-empty-groups-after-group-creation + "An auxiliary function that finds and returns a set of ids that + corresponds to groups that should be deleted after a group creation. + + The corner case happens when you selects two (or more) shapes that + belongs each one to different groups, and after creating the new + group, one (or many) groups can become empty because they have had a + single shape which is moved to the created group." + [objects parent-id shapes] + (let [ids (cfh/clean-loops objects (into #{} (map :id) shapes)) + parents (into #{} (map #(cfh/get-parent-id objects %)) ids)] + (loop [current-id (first parents) + to-check (rest parents) + removed-id? ids + result #{}] + + (if-not current-id + ;; Base case, no next element + result + + (let [group (get objects current-id)] + (if (and (not= :frame (:type group)) + (not= current-id parent-id) + (empty? (remove removed-id? (:shapes group)))) + + ;; Adds group to the remove and check its parent + (let [to-check (concat to-check [(cfh/get-parent-id objects current-id)])] + (recur (first to-check) + (rest to-check) + (conj removed-id? current-id) + (conj result current-id))) + + ;; otherwise recur + (recur (first to-check) + (rest to-check) + removed-id? + result))))))) + +(defn prepare-create-group + [changes id 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? + (= (count shapes) 1) + (= (:type (first shapes)) :group)) + (:name (first shapes)) + base-name) + + selrect (gsh/shapes->rect shapes) + group-idx (->> shapes + last + :id + (cfh/get-position-on-parent objects) + inc) + + group (cts/setup-shape {:id id + :type :group + :name gname + :shapes (mapv :id shapes) + :selrect selrect + :x (:x selrect) + :y (:y selrect) + :width (:width selrect) + :height (:height selrect) + :parent-id parent-id + :frame-id frame-id + :index group-idx}) + + ;; Shapes that are in a component, but are not root, must be detached, + ;; because they will be now children of a non instance group. + shapes-to-detach (filter ctk/in-component-copy-not-head? 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) + + target-cell + (when (ctl/grid-layout? objects parent-id) + (ctl/get-cell-by-shape-id (get objects parent-id) (-> shapes last :id))) + + grid-parents + (into [] + (comp (map :parent-id) + (filter (partial ctl/grid-layout? objects))) + shapes) + + changes (-> changes + (pcb/with-page-id page-id) + (pcb/with-objects objects) + (pcb/add-object group {:index group-idx}) + + ;; Create a group needs to reset the constraints to scale/scale + (pcb/update-shapes + (map :id shapes) + (fn [shape] + (-> shape + (d/assoc-when :constraints-h :scale) + (d/assoc-when :constraints-v :scale)))) + (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) + (pcb/change-parent (:id group) (reverse shapes)) + (pcb/update-shapes (map :id shapes-to-detach) ctk/detach-shape) + (cond-> target-cell + (pcb/update-shapes + [parent-id] + (fn [parent] + (assoc-in parent [:layout-grid-cells (:id target-cell) :shapes] [(:id group)])))) + (pcb/update-shapes grid-parents ctl/assign-cells {:with-objects? true}) + (pcb/remove-objects ids-to-delete))] + + [group changes])) + +(defn remove-group-changes + [it page-id group objects] + (let [children (->> (:shapes group) + (cfh/order-by-indexed-shapes objects) + (mapv #(get objects %))) + parent-id (cfh/get-parent-id objects (:id group)) + parent (get objects parent-id) + + index-in-parent + (->> (:shapes parent) + (map-indexed vector) + (filter #(#{(:id group)} (second %))) + (ffirst) + inc)] + + (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects) + (pcb/change-parent parent-id children index-in-parent) + (pcb/remove-objects [(:id group)])))) + +(defn remove-frame-changes + [it page-id frame objects] + (let [children (->> (:shapes frame) + (cfh/order-by-indexed-shapes objects) + (mapv #(get objects %))) + parent-id (cfh/get-parent-id objects (:id frame)) + idx-in-parent (->> (:id frame) + (cfh/get-position-on-parent objects) + inc)] + + (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects) + (cond-> (ctl/any-layout? frame) + (pcb/update-shapes (:shapes frame) ctl/remove-layout-item-data)) + (pcb/change-parent parent-id children idx-in-parent) + (pcb/remove-objects [(:id frame)])))) + +(defn ungroup-ids + "The shapes of `ids` an ungroup takes apart: not inside a copy, not a + copy head and not a variant container." + [objects ids] + (->> ids + (remove #(ctn/has-any-copy-parent? objects (get objects %))) + (remove #(ctk/instance-head? (get objects %))) + (remove #(ctk/is-variant-container? (get objects %))))) + +(defn generate-ungroup-changes + "The changes that ungroup the groups, bools and boards of `ids`, which + `ungroup-ids` gives." + [it page-id objects ids] + (let [prepare + (fn [shape-id] + (let [shape (get objects shape-id) + changes + (cond + (or (cfh/group-shape? shape) (cfh/bool-shape? shape)) + (remove-group-changes it page-id shape objects) + + (cfh/frame-shape? shape) + (remove-frame-changes it page-id shape objects))] + + (cond-> changes + (ctl/grid-layout? objects (:parent-id shape)) + (pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true})))) + + changes-list (sequence (keep prepare) ids)] + {:redo-changes (vec (mapcat :redo-changes changes-list)) + :undo-changes (vec (mapcat :undo-changes changes-list)) + :origin it})) + +(defn generate-mask-changes + "The group that masks `shapes`, which `shapes-for-group-or-mask` gives, + and the changes that make it: a lone group becomes the mask, other + shapes are grouped into a new one with id `id`." + [it id page-id objects shapes] + (let [first-shape (first shapes) + [group changes] + (if (and (= (count shapes) 1) + (= (:type (first shapes)) :group)) + [first-shape (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects))] + (prepare-create-group (pcb/empty-changes it) id objects page-id shapes "Mask" true))] + [group + (-> 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)]))])) + +(defn generate-unmask-changes + "The changes that stop the groups and bools of `ids` masking." + [it page-id objects ids] + (let [masked-groups (->> ids + (map #(get objects %)) + (filter #(or (= :bool (:type %)) (= :group (:type %)))))] + (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))) + +(defn ungrouped-children + "The children the ungroup of `ids` leaves in place of them." + [objects ids] + (into (d/ordered-set) + (mapcat #(dm/get-in objects [% :shapes])) + ids)) diff --git a/common/test/common_tests/logic/groups_test.cljc b/common/test/common_tests/logic/groups_test.cljc new file mode 100644 index 0000000000..c8d18bd6b6 --- /dev/null +++ b/common/test/common_tests/logic/groups_test.cljc @@ -0,0 +1,81 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.logic.groups-test + (:require + [app.common.files.changes-builder :as pcb] + [app.common.logic.bool :as clb] + [app.common.logic.groups :as clg] + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.test-helpers.shapes :as ths] + [app.common.uuid :as uuid] + [clojure.test :as t])) + +(t/use-fixtures :each thi/test-fixture) + +(defn- two-rects + [] + (-> (thf/sample-file :file1) + (ths/add-sample-shape :rect-a) + (ths/add-sample-shape :rect-b))) + +(defn- objects + [file] + (:objects (thf/current-page file))) + +(defn- group + [file labels] + (let [page (thf/current-page file) + shapes (clg/shapes-for-group-or-mask (:objects page) (map thi/id labels)) + [group changes] (clg/prepare-create-group (pcb/empty-changes nil) (uuid/next) (:objects page) + (:id page) shapes "Group" false)] + [(:id group) (thf/apply-changes file changes)])) + +(t/deftest grouping-puts-the-shapes-in-a-new-group + (let [[id file] (group (two-rects) [:rect-a :rect-b]) + group (get (objects file) id)] + (t/is (= :group (:type group))) + (t/is (= #{(thi/id :rect-a) (thi/id :rect-b)} (set (:shapes group)))) + (t/is (every? #(= id (:parent-id (get (objects file) %))) (:shapes group))))) + +(t/deftest ungrouping-gives-the-children-back-to-the-parent + (let [[id file] (group (two-rects) [:rect-a :rect-b]) + page (thf/current-page file) + ids (clg/ungroup-ids (:objects page) [id]) + file (thf/apply-changes file (clg/generate-ungroup-changes nil (:id page) (:objects page) ids))] + (t/is (nil? (get (objects file) id))) + (t/is (= #{(thi/id :rect-a) (thi/id :rect-b)} (clg/ungrouped-children (:objects page) ids))) + (t/is (= uuid/zero (:parent-id (get (objects file) (thi/id :rect-a))))))) + +(t/deftest ungrouping-leaves-copy-heads-whole + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component :main-root :main-child :copy-root))] + (t/is (empty? (clg/ungroup-ids (objects file) [(thi/id :copy-root)]))))) + +(t/deftest masking-a-lone-group-masks-it-and-unmasking-takes-the-mask-off + (let [[id file] (group (two-rects) [:rect-a :rect-b]) + page (thf/current-page file) + shapes (clg/shapes-for-group-or-mask (:objects page) [id]) + [mask changes] (clg/generate-mask-changes nil (uuid/next) (:id page) (:objects page) shapes) + masked (thf/apply-changes file changes) + page (thf/current-page masked) + unmasked (thf/apply-changes masked (clg/generate-unmask-changes nil (:id page) (:objects page) [id]))] + (t/is (= id (:id mask))) + (t/is (true? (:masked-group (get (objects masked) id)))) + (t/is (nil? (:masked-group (get (objects unmasked) id)))))) + +(t/deftest a-bool-combines-shapes-but-not-boards + (let [file (-> (two-rects) + (ths/add-sample-shape :board :type :frame)) + page (thf/current-page file) + ids (map thi/id [:rect-a :rect-b :board]) + [shape changes] (clb/generate-create-bool nil (:id page) (:objects page) ids :union nil) + file (thf/apply-changes file changes)] + (t/is (= :bool (:type (get (objects file) (:id shape))))) + (t/is (= #{(thi/id :rect-a) (thi/id :rect-b)} (set (:shapes (get (objects file) (:id shape)))))) + (t/is (nil? (clb/generate-create-bool nil (:id page) (:objects page) [(thi/id :board)] :union nil))))) diff --git a/frontend/src/app/main/data/workspace/bool.cljs b/frontend/src/app/main/data/workspace/bool.cljs index a961299f5d..0107410859 100644 --- a/frontend/src/app/main/data/workspace/bool.cljs +++ b/frontend/src/app/main/data/workspace/bool.cljs @@ -7,68 +7,15 @@ (ns app.main.data.workspace.bool (:require [app.common.data :as d] - [app.common.files.changes-builder :as pcb] - [app.common.files.helpers :as cph] - [app.common.geom.shapes :as gsh] - [app.common.types.component :as ctc] + [app.common.logic.bool :as clb] [app.common.types.container :as ctn] - [app.common.types.path :as path] - [app.common.types.path.bool :as bool] - [app.common.types.shape :as cts] - [app.common.types.shape.layout :as ctl] - [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.helpers :as dsh] [app.main.data.workspace.selection :as dws] [app.main.data.workspace.shapes :as dwsh] [beicon.v2.core :as rx] - [cuerdas.core :as str] [potok.v2.core :as ptk])) -(defn- create-bool-shape - [id type name shapes objects] - (let [shape-id - (or id (uuid/next)) - - head - (if (= type :difference) (first shapes) (last shapes)) - - head - (cond-> head - (and (contains? head :svg-attrs) (empty? (:fills head))) - (assoc :fills (path/get-default-bool-fills))) - - shape - {:id shape-id - :type :bool - :bool-type type - :frame-id (:frame-id head) - :parent-id (:parent-id head) - :name name - :shapes (into [] d/xf:map-id shapes)} - - shape - (-> shape - (merge (select-keys head path/bool-style-properties)) - (cts/setup-shape) - (path/update-bool-shape objects))] - - [shape (cph/get-position-on-parent objects (:id head))])) - -(defn- group->bool - [type group objects] - (let [shapes (->> (:shapes group) - (map (d/getf objects))) - head (if (= type :difference) (first shapes) (last shapes)) - head (cond-> head - (and (contains? head :svg-attrs) (empty? (:fills head))) - (assoc :fills (path/get-default-bool-fills)))] - (-> group - (assoc :type :bool) - (assoc :bool-type type) - (merge (select-keys head bool/style-properties)) - (path/update-bool-shape objects)))) - (defn create-bool [type & {:keys [ids force-shape-id]}] @@ -79,42 +26,11 @@ (watch [it state _] (let [page-id (:current-page-id state) objects (dsh/lookup-page-objects state page-id) - - name - (-> type d/name str/capital) - - ids - (->> (or ids (dsh/get-selected-ids state)) - (dsh/process-selected objects)) - - xform - (comp - (map (d/getf objects)) - (remove cph/frame-shape?) - (remove ctc/is-variant?) - (remove #(ctn/has-any-copy-parent? objects %))) - - shapes - (->> (cph/order-by-indexed-shapes objects ids) - (into [] xform) - (not-empty))] - - (when shapes - (let [[shape index] - (create-bool-shape force-shape-id type name (reverse shapes) objects) - - shape-id - (get shape :id) - - changes - (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/add-object shape {:index (inc index)}) - (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) - (pcb/change-parent shape-id shapes))] - - (rx/of (dch/commit-changes changes) - (dws/select-shapes (d/ordered-set shape-id))))))))) + ids (->> (or ids (dsh/get-selected-ids state)) + (dsh/process-selected objects))] + (when-let [[shape changes] (clb/generate-create-bool it page-id objects ids type force-shape-id)] + (rx/of (dch/commit-changes changes) + (dws/select-shapes (d/ordered-set (:id shape))))))))) (defn group-to-bool [shape-id type] @@ -122,20 +38,10 @@ ptk/WatchEvent (watch [_ state _] (let [objects (dsh/lookup-page-objects state) - update-fn (partial group->bool type)] + update-fn (partial clb/group->bool type)] (when-not (ctn/has-any-copy-parent? objects (get objects shape-id)) (rx/of (dwsh/update-shapes [shape-id] update-fn {:with-objects? true :reg-objects? true}))))))) -(defn- bool->group - [shape objects] - (-> shape - (assoc :type :group) - (dissoc :bool-type) - (d/without-keys path/bool-group-style-properties) - (gsh/update-group-selrect - (mapv (d/getf objects) - (:shapes shape))))) - (defn bool-to-group [shape-id] (ptk/reify ::bool-to-group @@ -143,7 +49,7 @@ (watch [_ state _] (let [objects (dsh/lookup-page-objects state)] (when-not (ctn/has-any-copy-parent? objects (get objects shape-id)) - (rx/of (dwsh/update-shapes [shape-id] bool->group {:with-objects? true :reg-objects? true}))))))) + (rx/of (dwsh/update-shapes [shape-id] clb/bool->group {:with-objects? true :reg-objects? true}))))))) (defn change-bool-type [shape-id type] diff --git a/frontend/src/app/main/data/workspace/groups.cljs b/frontend/src/app/main/data/workspace/groups.cljs index 53175a61f2..857a34e01b 100644 --- a/frontend/src/app/main/data/workspace/groups.cljs +++ b/frontend/src/app/main/data/workspace/groups.cljs @@ -7,14 +7,9 @@ (ns app.main.data.workspace.groups (:require [app.common.data :as d] - [app.common.data.macros :as dm] [app.common.files.changes-builder :as pcb] [app.common.files.helpers :as cfh] - [app.common.geom.shapes :as gsh] - [app.common.types.component :as ctk] - [app.common.types.container :as ctn] - [app.common.types.shape :as cts] - [app.common.types.shape.layout :as ctl] + [app.common.logic.groups :as clg] [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.helpers :as dsh] @@ -23,170 +18,6 @@ [beicon.v2.core :as rx] [potok.v2.core :as ptk])) -(defn shapes-for-grouping - [objects selected] - (->> selected - (cfh/order-by-indexed-shapes objects) - reverse - (map #(get objects %)))) - -(defn- shapes-for-group-or-mask - "Shapes allowed for Group and Mask after the shared variant gates. - - `ids-for-structure-op` drops mains before `clean-loops` so a selected - variant main does not swallow nested children." - [objects ids] - (shapes-for-grouping objects (ctn/ids-for-structure-op objects ids))) - -(defn- get-empty-groups-after-group-creation - "An auxiliary function that finds and returns a set of ids that - corresponds to groups that should be deleted after a group creation. - - The corner case happens when you selects two (or more) shapes that - belongs each one to different groups, and after creating the new - group, one (or many) groups can become empty because they have had a - single shape which is moved to the created group." - [objects parent-id shapes] - (let [ids (cfh/clean-loops objects (into #{} (map :id) shapes)) - parents (into #{} (map #(cfh/get-parent-id objects %)) ids)] - (loop [current-id (first parents) - to-check (rest parents) - removed-id? ids - result #{}] - - (if-not current-id - ;; Base case, no next element - result - - (let [group (get objects current-id)] - (if (and (not= :frame (:type group)) - (not= current-id parent-id) - (empty? (remove removed-id? (:shapes group)))) - - ;; Adds group to the remove and check its parent - (let [to-check (concat to-check [(cfh/get-parent-id objects current-id)])] - (recur (first to-check) - (rest to-check) - (conj removed-id? current-id) - (conj result current-id))) - - ;; otherwise recur - (recur (first to-check) - (rest to-check) - removed-id? - result))))))) - -(defn prepare-create-group - [changes id 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? - (= (count shapes) 1) - (= (:type (first shapes)) :group)) - (:name (first shapes)) - base-name) - - selrect (gsh/shapes->rect shapes) - group-idx (->> shapes - last - :id - (cfh/get-position-on-parent objects) - inc) - - group (cts/setup-shape {:id id - :type :group - :name gname - :shapes (mapv :id shapes) - :selrect selrect - :x (:x selrect) - :y (:y selrect) - :width (:width selrect) - :height (:height selrect) - :parent-id parent-id - :frame-id frame-id - :index group-idx}) - - ;; Shapes that are in a component, but are not root, must be detached, - ;; because they will be now children of a non instance group. - shapes-to-detach (filter ctk/in-component-copy-not-head? 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) - - target-cell - (when (ctl/grid-layout? objects parent-id) - (ctl/get-cell-by-shape-id (get objects parent-id) (-> shapes last :id))) - - grid-parents - (into [] - (comp (map :parent-id) - (filter (partial ctl/grid-layout? objects))) - shapes) - - changes (-> changes - (pcb/with-page-id page-id) - (pcb/with-objects objects) - (pcb/add-object group {:index group-idx}) - - ;; Create a group needs to reset the constraints to scale/scale - (pcb/update-shapes - (map :id shapes) - (fn [shape] - (-> shape - (d/assoc-when :constraints-h :scale) - (d/assoc-when :constraints-v :scale)))) - (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) - (pcb/change-parent (:id group) (reverse shapes)) - (pcb/update-shapes (map :id shapes-to-detach) ctk/detach-shape) - (cond-> target-cell - (pcb/update-shapes - [parent-id] - (fn [parent] - (assoc-in parent [:layout-grid-cells (:id target-cell) :shapes] [(:id group)])))) - (pcb/update-shapes grid-parents ctl/assign-cells {:with-objects? true}) - (pcb/remove-objects ids-to-delete))] - - [group changes])) - -(defn remove-group-changes - [it page-id group objects] - (let [children (->> (:shapes group) - (cfh/order-by-indexed-shapes objects) - (mapv #(get objects %))) - parent-id (cfh/get-parent-id objects (:id group)) - parent (get objects parent-id) - - index-in-parent - (->> (:shapes parent) - (map-indexed vector) - (filter #(#{(:id group)} (second %))) - (ffirst) - inc)] - - (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/change-parent parent-id children index-in-parent) - (pcb/remove-objects [(:id group)])))) - -(defn remove-frame-changes - [it page-id frame objects] - (let [children (->> (:shapes frame) - (cfh/order-by-indexed-shapes objects) - (mapv #(get objects %))) - parent-id (cfh/get-parent-id objects (:id frame)) - idx-in-parent (->> (:id frame) - (cfh/get-position-on-parent objects) - inc)] - - (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (cond-> (ctl/any-layout? frame) - (pcb/update-shapes (:shapes frame) ctl/remove-layout-item-data)) - (pcb/change-parent parent-id children idx-in-parent) - (pcb/remove-objects [(:id frame)])))) - - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; GROUPS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -199,11 +30,11 @@ (let [id (d/nilv id (uuid/next)) page-id (:current-page-id state) objects (dsh/lookup-page-objects state page-id) - shapes (shapes-for-group-or-mask objects ids) + shapes (clg/shapes-for-group-or-mask objects ids) parents (into #{} (map :parent-id) shapes)] (when-not (empty? shapes) (let [[group changes] - (prepare-create-group (pcb/empty-changes it) id objects page-id shapes "Group" false)] + (clg/prepare-create-group (pcb/empty-changes it) id objects page-id shapes "Group" false)] (rx/of (dch/commit-changes changes) (when change-selection? (dws/select-shapes (d/ordered-set (:id group)))) @@ -227,42 +58,16 @@ (let [page-id (:current-page-id state) objects (dsh/lookup-page-objects state page-id) - prepare - (fn [shape-id] - (let [shape (get objects shape-id) - changes - (cond - (or (cfh/group-shape? shape) (cfh/bool-shape? shape)) - (remove-group-changes it page-id shape objects) - - (cfh/frame-shape? shape) - (remove-frame-changes it page-id shape objects))] - - (cond-> changes - (ctl/grid-layout? objects (:parent-id shape)) - (pcb/update-shapes [(:parent-id shape)] ctl/assign-cells {:with-objects? true})))) - - ids (->> ids - (remove #(ctn/has-any-copy-parent? objects (get objects %))) - ;; components can't be ungrouped - (remove #(ctk/instance-head? (get objects %))) - (remove #(ctk/is-variant-container? (get objects %)))) - - changes-list (sequence (keep prepare) ids) + ids (clg/ungroup-ids objects ids) parents (into #{} (comp (map #(cfh/get-parent objects %)) (keep :id)) ids) - child-ids - (into (d/ordered-set) - (mapcat #(dm/get-in objects [% :shapes])) - ids) + child-ids (clg/ungrouped-children objects ids) - changes {:redo-changes (vec (mapcat :redo-changes changes-list)) - :undo-changes (vec (mapcat :undo-changes changes-list)) - :origin it} + changes (clg/generate-ungroup-changes it page-id objects ids) undo-id (js/Symbol)] (when-not (empty? ids) @@ -290,35 +95,11 @@ (watch [it state _] (let [page-id (:current-page-id state) objects (dsh/lookup-page-objects state page-id) - shapes (shapes-for-group-or-mask + shapes (clg/shapes-for-group-or-mask objects - (or ids (dsh/get-selected-ids state))) - first-shape (first shapes)] + (or ids (dsh/get-selected-ids state)))] (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 changes] - (if (and (= (count shapes) 1) - (= (:type (first shapes)) :group)) - [first-shape (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects))] - (prepare-create-group (pcb/empty-changes it) (uuid/next) objects page-id shapes "Mask" true)) - - 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)])) + (let [[group changes] (clg/generate-mask-changes it (uuid/next) page-id objects shapes) undo-id (js/Symbol)] (rx/of (dwu/start-undo-transaction undo-id) @@ -338,18 +119,7 @@ (let [page-id (:current-page-id state) objects (dsh/lookup-page-objects state page-id) - masked-groups (->> (d/nilv ids (dsh/lookup-selected state)) - (map #(get objects %)) - (filter #(or (= :bool (:type %)) (= :group (:type %))))) - - 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)] + changes (clg/generate-unmask-changes it page-id objects + (d/nilv ids (dsh/lookup-selected state)))] (rx/of (dch/commit-changes changes))))))) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 9d70b5cee6..b00068dad0 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -15,6 +15,7 @@ [app.common.geom.point :as gpt] [app.common.logging :as log] [app.common.logic.duplicate :as cld] + [app.common.logic.groups :as clg] [app.common.logic.instances :as cli] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] @@ -41,7 +42,6 @@ [app.main.data.notifications :as ntf] [app.main.data.persistence :as dps] [app.main.data.workspace :as-alias dw] - [app.main.data.workspace.groups :as dwg] [app.main.data.workspace.notifications :as-alias dwn] [app.main.data.workspace.pages :as-alias dwpg] [app.main.data.workspace.reflow :as wrf] @@ -354,7 +354,7 @@ (let [file-id (:current-file-id state) page-id (:current-page-id state) objects (dsh/lookup-page-objects state file-id page-id) - shapes (dwg/shapes-for-grouping objects selected) + shapes (clg/shapes-for-grouping objects selected) parents (into #{} (map :parent-id) shapes)] (when-not (empty? shapes) (let [[root component-id changes] diff --git a/frontend/test/frontend_tests/helpers/pages.cljs b/frontend/test/frontend_tests/helpers/pages.cljs index 0581ca4af0..c548db22d3 100644 --- a/frontend/test/frontend_tests/helpers/pages.cljs +++ b/frontend/test/frontend_tests/helpers/pages.cljs @@ -14,13 +14,13 @@ [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] [app.common.logic.duplicate :as cld] + [app.common.logic.groups :as clg] [app.common.logic.instances :as cli] [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] [app.common.uuid :as uuid] [app.main.data.helpers :as dsh] [app.main.data.workspace.clipboard :as dwc] - [app.main.data.workspace.groups :as dwg] [app.main.data.workspace.layout :as layout])) ;; ---- Helpers to manage pages and objects @@ -102,11 +102,11 @@ ([state label ids] (group-shapes state label ids "Group")) ([state label ids prefix] (let [page (current-page state) - shapes (dwg/shapes-for-grouping (:objects page) ids)] + shapes (clg/shapes-for-grouping (:objects page) ids)] (if (empty? shapes) state (let [[group changes] - (dwg/prepare-create-group (pcb/empty-changes) nil (:objects page) (:id page) shapes prefix true)] + (clg/prepare-create-group (pcb/empty-changes) nil (:objects page) (:id page) shapes prefix true)] (swap! idmap assoc label (:id group)) (apply-changes state (:redo-changes changes))))))) @@ -115,7 +115,7 @@ ([state label ids] (frame-shapes state label ids "Board")) ([state label ids frame-name] (let [page (current-page state) - shapes (dwg/shapes-for-grouping (:objects page) ids) + shapes (clg/shapes-for-grouping (:objects page) ids) changes (pcb/empty-changes nil (:id page))] (if (empty? shapes) state @@ -136,7 +136,7 @@ [state instance-label component-label shape-ids] (let [page (current-page state) objects (dsh/lookup-page-objects state (:id page)) - shapes (dwg/shapes-for-grouping objects shape-ids) + shapes (clg/shapes-for-grouping objects shape-ids) [group component-id changes] (cli/generate-add-component (pcb/empty-changes nil)