From 96540af2b1866b335e1b0409ef99192815d21309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Wed, 22 Mar 2023 11:07:34 +0100 Subject: [PATCH] :tada: Instantiate component with duplicate --- frontend/src/app/main/data/workspace.cljs | 2 +- .../app/main/data/workspace/libraries.cljs | 6 ++- .../data/workspace/libraries_helpers.cljs | 19 +++++----- .../app/main/data/workspace/selection.cljs | 37 ++++++++++++++----- .../test/frontend_tests/helpers/pages.cljs | 8 +++- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index aaa2f3936..f4dc5c1a6 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1651,7 +1651,7 @@ all-objects (merge (:objects page) paste-objects) - changes (-> (dws/prepare-duplicate-changes all-objects page selected delta it) + changes (-> (dws/prepare-duplicate-changes all-objects page selected delta it nil) (pcb/amend-changes (partial process-rchange media-idx)) (pcb/amend-changes (partial change-add-obj-index paste-objects selected index))) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 289c25c8a..268f0f61d 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -317,7 +317,7 @@ shapes (dwg/shapes-for-grouping objects selected)] (when-not (empty? shapes) (let [[group _ changes] - (dwlh/generate-add-component it shapes objects page-id file-id components-v2)] + (dwlh/generate-add-component it shapes objects page-id file-id components-v2 dwg/prepare-create-group)] (when-not (empty? (:redo-changes changes)) (rx/of (dch/commit-changes changes) (dws/select-shapes (d/ordered-set (:id group))))))))))) @@ -462,8 +462,10 @@ (let [page (wsh/lookup-page state) libraries (wsh/get-libraries state) + changes (pcb/empty-changes it (:id page)) + [new-shape changes] - (dwlh/generate-instantiate-component it + (dwlh/generate-instantiate-component changes file-id component-id position diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 861add6f3..c7c2ca3c4 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -24,7 +24,6 @@ [app.common.types.shape-tree :as ctst] [app.common.types.typography :as cty] [app.common.uuid :as uuid] - [app.main.data.workspace.groups :as dwg] [app.main.data.workspace.state-helpers :as wsh] [cljs.spec.alpha :as s] [clojure.set :as set])) @@ -66,7 +65,7 @@ "If there is exactly one id, and it's a group or a frame, and not already a component, 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." - [it shapes objects page-id file-id components-v2] + [it shapes objects page-id file-id components-v2 prepare-create-group] (let [[group changes] (if (and (= (count shapes) 1) (or (= (:type (first shapes)) :group) @@ -77,12 +76,12 @@ (let [group-name (if (= 1 (count shapes)) (:name (first shapes)) "Component 1")] - (dwg/prepare-create-group it - objects - page-id - shapes - group-name - (not (ctk/instance-root? (first shapes)))))) + (prepare-create-group it ; This function needs to be passed as argument + objects ; to avoid a circular dependence + page-id + shapes + group-name + (not (ctk/instance-root? (first shapes)))))) name (:name group) [path name] (cph/parse-path-name name) @@ -147,7 +146,7 @@ (defn generate-instantiate-component "Generate changes to create a new instance from a component." - [it file-id component-id position page libraries] + [changes file-id component-id position page libraries] (let [component (ctf/get-component libraries file-id component-id) library (get libraries file-id) @@ -161,7 +160,7 @@ components-v2) changes (reduce #(pcb/add-object %1 %2 {:ignore-touched true}) - (pcb/empty-changes it (:id page)) + changes new-shapes)] [new-shape changes])) diff --git a/frontend/src/app/main/data/workspace/selection.cljs b/frontend/src/app/main/data/workspace/selection.cljs index c3de20ec9..fecbfe2cf 100644 --- a/frontend/src/app/main/data/workspace/selection.cljs +++ b/frontend/src/app/main/data/workspace/selection.cljs @@ -14,12 +14,14 @@ [app.common.pages.changes-builder :as pcb] [app.common.pages.helpers :as cph] [app.common.spec :as us] + [app.common.types.component :as ctk] [app.common.types.page :as ctp] [app.common.types.shape.interactions :as ctsi] [app.common.uuid :as uuid] [app.main.data.modal :as md] [app.main.data.workspace.changes :as dch] [app.main.data.workspace.collapse :as dwc] + [app.main.data.workspace.libraries-helpers :as dwlh] [app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.undo :as dwu] [app.main.data.workspace.zoom :as dwz] @@ -330,7 +332,7 @@ (defn prepare-duplicate-changes "Prepare objects to duplicate: generate new id, give them unique names, move to the desired position, and recalculate parents and frames as needed." - [all-objects page ids delta it] + [all-objects page ids delta it libraries] (let [shapes (map (d/getf all-objects) ids) unames (volatile! (cp/retrieve-used-names (:objects page))) update-unames! (fn [new-name] (vswap! unames conj new-name)) @@ -351,7 +353,8 @@ update-unames! ids-map %2 - delta) + delta + libraries) init-changes))] (-> changes @@ -359,11 +362,25 @@ (prepare-duplicate-guides shapes page ids-map delta)))) (defn- prepare-duplicate-shape-change - ([changes objects page unames update-unames! ids-map obj delta] - (prepare-duplicate-shape-change changes objects page unames update-unames! ids-map obj delta (:frame-id obj) (:parent-id obj))) + ([changes objects page unames update-unames! ids-map obj delta libraries] + (prepare-duplicate-shape-change changes objects page unames update-unames! ids-map obj delta libraries (:frame-id obj) (:parent-id obj))) - ([changes objects page unames update-unames! ids-map obj delta frame-id parent-id] - (if (some? obj) + ([changes objects page unames update-unames! ids-map obj delta libraries frame-id parent-id] + (cond + (nil? obj) + changes + + (ctk/main-instance? obj) + (let [[_new-shape changes] + (dwlh/generate-instantiate-component changes + (:component-file obj) + (:component-id obj) + (gpt/point (:x obj) (:y obj)) + page + libraries)] + changes) + + :else (let [frame? (cph/frame-shape? obj) new-id (ids-map (:id obj)) parent-id (or parent-id frame-id) @@ -392,11 +409,11 @@ ids-map child delta + libraries (if frame? new-id frame-id) new-id)) changes - (map (d/getf objects) (:shapes obj)))) - changes))) + (map (d/getf objects) (:shapes obj))))))) (defn- prepare-duplicate-flows [changes shapes page ids-map] @@ -545,7 +562,9 @@ (calc-duplicate-delta obj state objects) (gpt/point 0 0)) - changes (->> (prepare-duplicate-changes objects page selected delta it) + libraries (wsh/get-libraries state) + + changes (->> (prepare-duplicate-changes objects page selected delta it libraries) (duplicate-changes-update-indices objects selected)) changes (cond-> changes add-group-id? (assoc :group-id (uuid/random))) diff --git a/frontend/test/frontend_tests/helpers/pages.cljs b/frontend/test/frontend_tests/helpers/pages.cljs index cc565e59f..94cd86ec7 100644 --- a/frontend/test/frontend_tests/helpers/pages.cljs +++ b/frontend/test/frontend_tests/helpers/pages.cljs @@ -8,6 +8,7 @@ (:require [app.common.geom.point :as gpt] [app.common.pages :as cp] + [app.common.pages.changes-builder :as pcb] [app.common.pages.helpers :as cph] [app.common.types.shape :as cts] [app.common.uuid :as uuid] @@ -113,7 +114,8 @@ (:objects page) (:id page) current-file-id - true)] + true + dwg/prepare-create-group)] (swap! idmap assoc instance-label (:id group) component-label component-id) @@ -127,8 +129,10 @@ (let [page (current-page state) libraries (wsh/get-libraries state) + changes (pcb/empty-changes nil (:id page)) + [new-shape changes] - (dwlh/generate-instantiate-component nil + (dwlh/generate-instantiate-component changes file-id component-id (gpt/point 100 100)