From a8738b44a185be419b36fd22970afb4fac96a881 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Fri, 19 Apr 2024 15:37:42 +0200 Subject: [PATCH] :recycle: Merge libraries_common_helpers into libraries_helpers --- backend/src/app/features/components_v2.clj | 4 +- .../app/common/files/libraries_helpers.cljc | 94 ++++++++++++++++++- .../app/main/data/workspace/libraries.cljs | 3 +- .../app/main/data/workspace/selection.cljs | 3 +- .../test/frontend_tests/helpers/pages.cljs | 5 +- 5 files changed, 97 insertions(+), 12 deletions(-) diff --git a/backend/src/app/features/components_v2.clj b/backend/src/app/features/components_v2.clj index 34fb01f88..05cd1a084 100644 --- a/backend/src/app/features/components_v2.clj +++ b/backend/src/app/features/components_v2.clj @@ -12,7 +12,7 @@ [app.common.files.changes :as cp] [app.common.files.changes-builder :as fcb] [app.common.files.helpers :as cfh] - [app.common.files.libraries-common-helpers :as cflch] + [app.common.files.libraries-helpers :as cflh] [app.common.files.migrations :as fmg] [app.common.files.shapes-helpers :as cfsh] [app.common.files.validate :as cfv] @@ -1451,7 +1451,7 @@ (cons shape children)) [_ _ changes2] - (cflch/generate-add-component nil + (cflh/generate-add-component nil [shape] (:objects page) (:id page) diff --git a/common/src/app/common/files/libraries_helpers.cljc b/common/src/app/common/files/libraries_helpers.cljc index 0be04e5d6..37d893b12 100644 --- a/common/src/app/common/files/libraries_helpers.cljc +++ b/common/src/app/common/files/libraries_helpers.cljc @@ -25,8 +25,9 @@ [app.common.types.shape-tree :as ctst] [app.common.types.shape.layout :as ctl] [app.common.types.typography :as cty] - [cljs.spec.alpha :as s] - [clojure.set :as set])) + [app.common.uuid :as uuid] + [clojure.set :as set] + [clojure.spec.alpha :as s])) ;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default (log/set-level! :warn) @@ -1662,3 +1663,92 @@ (if (cfh/page? container) (assoc change :page-id (:id container)) (assoc change :component-id (:id container)))) + +(defn generate-add-component-changes + [changes root objects file-id page-id components-v2] + (let [name (:name root) + [path name] (cfh/parse-path-name name) + + [root-shape new-shapes updated-shapes] + (if-not components-v2 + (ctn/make-component-shape root objects file-id components-v2) + (ctn/convert-shape-in-component root objects file-id)) + + changes (-> changes + (pcb/add-component (:id root-shape) + path + name + new-shapes + updated-shapes + (:id root) + page-id))] + [root-shape changes])) + +(defn generate-add-component + "If there is exactly one id, and it's a frame (or a group in v1), and not already a component, + use it as root. Otherwise, create a frame (v2) or group (v1) 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 prepare-create-group prepare-create-board] + + (let [changes (pcb/empty-changes it page-id) + shapes-count (count shapes) + first-shape (first shapes) + + from-singe-frame? + (and (= 1 shapes-count) + (cfh/frame-shape? first-shape)) + + [root changes old-root-ids] + (if (and (= shapes-count 1) + (or (and (cfh/group-shape? first-shape) + (not components-v2)) + (cfh/frame-shape? first-shape)) + (not (ctk/instance-head? first-shape))) + [first-shape + (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects)) + (:shapes first-shape)] + + (let [root-name (if (= 1 shapes-count) + (:name first-shape) + "Component 1") + + shape-ids (into (d/ordered-set) (map :id) shapes) + + [root changes] + (if-not components-v2 + (prepare-create-group it ; These functions needs to be passed as argument + objects ; to avoid a circular dependence + page-id + shapes + root-name + (not (ctk/instance-head? first-shape))) + (prepare-create-board changes + (uuid/next) + (:parent-id first-shape) + objects + shape-ids + nil + root-name + true))] + + [root changes shape-ids])) + + changes + (cond-> changes + (not from-singe-frame?) + (pcb/update-shapes + (:shapes root) + (fn [shape] + (assoc shape :constraints-h :scale :constraints-v :scale)))) + + objects' (assoc objects (:id root) root) + + [root-shape changes] (generate-add-component-changes changes root objects' file-id page-id components-v2) + + changes (pcb/update-shapes changes + old-root-ids + #(dissoc % :component-root) + [:component-root])] + + [root (:id root-shape) changes])) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 8ad2a90be..9e944f9f4 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -11,7 +11,6 @@ [app.common.files.changes :as ch] [app.common.files.changes-builder :as pcb] [app.common.files.helpers :as cfh] - [app.common.files.libraries-common-helpers :as cflch] [app.common.files.libraries-helpers :as cflh] [app.common.files.shapes-helpers :as cfsh] [app.common.geom.point :as gpt] @@ -352,7 +351,7 @@ parents (into #{} (map :parent-id) shapes)] (when-not (empty? shapes) (let [[root _ changes] - (cflch/generate-add-component it shapes objects page-id file-id components-v2 + (cflh/generate-add-component it shapes objects page-id file-id components-v2 dwg/prepare-create-group cfsh/prepare-create-artboard-from-selection)] (when-not (empty? (:redo-changes changes)) diff --git a/frontend/src/app/main/data/workspace/selection.cljs b/frontend/src/app/main/data/workspace/selection.cljs index 38e1ec51a..048479e5e 100644 --- a/frontend/src/app/main/data/workspace/selection.cljs +++ b/frontend/src/app/main/data/workspace/selection.cljs @@ -11,7 +11,6 @@ [app.common.files.changes-builder :as pcb] [app.common.files.focus :as cpf] [app.common.files.helpers :as cfh] - [app.common.files.libraries-common-helpers :as cflch] [app.common.files.libraries-helpers :as cflh] [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] @@ -498,7 +497,7 @@ regenerate-component (fn [changes shape] (let [components-v2 (dm/get-in library-data [:options :components-v2]) - [_ changes] (cflch/generate-add-component-changes changes shape objects file-id (:id page) components-v2)] + [_ changes] (cflh/generate-add-component-changes changes shape objects file-id (:id page) components-v2)] changes)) new-obj diff --git a/frontend/test/frontend_tests/helpers/pages.cljs b/frontend/test/frontend_tests/helpers/pages.cljs index 8f28aebb4..cb5a080cc 100644 --- a/frontend/test/frontend_tests/helpers/pages.cljs +++ b/frontend/test/frontend_tests/helpers/pages.cljs @@ -6,7 +6,6 @@ (ns frontend-tests.helpers.pages (:require - [app.common.data :as d] [app.common.files.changes :as cp] [app.common.files.changes-builder :as pcb] [app.common.files.helpers :as cfh] @@ -17,8 +16,6 @@ [app.common.uuid :as uuid] [app.main.data.workspace.groups :as dwg] [app.main.data.workspace.layout :as layout] - [app.main.data.workspace.libraries-helpers :as dwlh] - [app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.state-helpers :as wsh])) ;; ---- Helpers to manage pages and objects @@ -161,7 +158,7 @@ (pcb/with-objects objects)) [new-shape changes] - (dwlh/generate-instantiate-component changes + (cflh/generate-instantiate-component changes objects file-id component-id