From 0667089833300153455cdde6c7a966525583c15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 28 Jul 2022 15:10:11 +0200 Subject: [PATCH] :wrench: Some style enhancements and mini bug fix --- backend/src/app/rpc/mutations/files.clj | 18 +--- common/src/app/common/geom/shapes.cljc | 4 +- common/src/app/common/pages/changes.cljc | 6 +- common/src/app/common/types/color.cljc | 86 +++++++++---------- .../src/app/common/types/components_list.cljc | 5 +- common/src/app/common/types/container.cljc | 2 +- common/src/app/common/types/file.cljc | 8 +- common/test/app/common/types/file_test.cljc | 2 +- .../app/main/data/workspace/libraries.cljs | 21 ++--- .../src/app/main/ui/dashboard/export.cljs | 5 +- frontend/src/app/worker/export.cljs | 16 ++-- 11 files changed, 83 insertions(+), 90 deletions(-) diff --git a/backend/src/app/rpc/mutations/files.clj b/backend/src/app/rpc/mutations/files.clj index e4768e78e7..ff45fb6c6f 100644 --- a/backend/src/app/rpc/mutations/files.clj +++ b/backend/src/app/rpc/mutations/files.clj @@ -123,17 +123,13 @@ (db/with-atomic [conn pool] (files/check-edition-permissions! conn profile-id id) (when-not is-shared - (absorb-library conn params) - (unlink-files conn params)) + (absorb-library conn params) + (unlink-files conn params)) (set-file-shared conn params))) -(def sql:unlink-files - "delete from file_library_rel - where library_file_id = ?") - (defn- unlink-files [conn {:keys [id] :as params}] - (db/exec-one! conn [sql:unlink-files id])) + (db/delete! conn :file-library-rel {:library-file-id id})) (defn- set-file-shared [conn {:keys [id is-shared] :as params}] @@ -162,11 +158,6 @@ {:id id}) nil) -(def sql:find-files - "select file_id - from file_library_rel - where library_file_id=?") - (defn absorb-library "Find all files using a shared library, and absorb all library assets into the file local libraries" @@ -189,8 +180,7 @@ :modified-at ts} {:id (:id file)})))] - (dorun (->> (db/exec! conn [sql:find-files id]) - (map process-file))))))) + (run! process-file (db/query conn :file-library-rel {:library-file-id id})))))) ;; --- Mutation: Link file to library diff --git a/common/src/app/common/geom/shapes.cljc b/common/src/app/common/geom/shapes.cljc index c75f47e52d..a08fe8a21a 100644 --- a/common/src/app/common/geom/shapes.cljc +++ b/common/src/app/common/geom/shapes.cljc @@ -52,12 +52,12 @@ ; TODO: perhaps some day we want after transformations, but for the ; moment it's enough as is now. [shape] - (get shape :x (:x (:selrect shape)))) ; Paths don't have :x attribute + (or (:x shape) (:x (:selrect shape)))) ; Paths don't have :x attribute (defn top-bound "Returns the lowest y coord of the shape BEFORE applying transformations." [shape] - (get shape :y (:y (:selrect shape)))) ; Paths don't have :y attribute + (or (:y shape) (:y (:selrect shape)))) ; Paths don't have :y attribute (defn fully-contained? "Checks if one rect is fully inside the other" diff --git a/common/src/app/common/pages/changes.cljc b/common/src/app/common/pages/changes.cljc index 6916500a17..152612769b 100644 --- a/common/src/app/common/pages/changes.cljc +++ b/common/src/app/common/pages/changes.cljc @@ -477,9 +477,9 @@ (component-sync-attrs (:attr operation)))) any-sync? (some need-sync? operations)] (when any-sync? - (into #{} (->> shape-and-parents - (filter #(:main-instance? %)) ; Select shapes that are main component instances - (map :id))))))) + (let [xform (comp (filter :main-instance?) ; Select shapes that are main component instances + (map :id))] + (into #{} xform shape-and-parents)))))) (defmethod components-changed :default [_ _] diff --git a/common/src/app/common/types/color.cljc b/common/src/app/common/types/color.cljc index 5f462796c3..06c5c497ba 100644 --- a/common/src/app/common/types/color.cljc +++ b/common/src/app/common/types/color.cljc @@ -246,38 +246,38 @@ (defn- process-shape-colors "Execute an update function on all colors of a shape." - [shape func] + [shape process-fn] (let [process-fill (fn [shape [position fill]] - (func shape - position - (fill->shape-color fill) - set-fill-color - attach-fill-color - detach-fill-color)) + (process-fn shape + position + (fill->shape-color fill) + set-fill-color + attach-fill-color + detach-fill-color)) process-stroke (fn [shape [position stroke]] - (func shape - position - (stroke->shape-color stroke) - set-stroke-color - attach-stroke-color - detach-stroke-color)) + (process-fn shape + position + (stroke->shape-color stroke) + set-stroke-color + attach-stroke-color + detach-stroke-color)) process-shadow (fn [shape [position shadow]] - (func shape - position - (shadow->shape-color shadow) - set-shadow-color - attach-shadow-color - detach-shadow-color)) + (process-fn shape + position + (shadow->shape-color shadow) + set-shadow-color + attach-shadow-color + detach-shadow-color)) process-grid (fn [shape [position grid]] - (func shape - position - (grid->shape-color grid) - set-grid-color - attach-grid-color - detach-grid-color)) + (process-fn shape + position + (grid->shape-color grid) + set-grid-color + attach-grid-color + detach-grid-color)) process-text-node (fn [node] (as-> node $ @@ -302,13 +302,13 @@ "Change the shape so that any use of the given color now points to the given library." [shape library-id color] - (let [remap-color (fn [shape position shape-color _ attach-fn _] - (if (= (:ref-id shape-color) (:id color)) - (attach-fn shape - position - (:id color) - library-id) - shape))] + (letfn [(remap-color [shape position shape-color _ attach-fn _] + (if (= (:ref-id shape-color) (:id color)) + (attach-fn shape + position + (:id color) + library-id) + shape))] (process-shape-colors shape remap-color))) @@ -316,17 +316,17 @@ "Look for usage of any color of the given library inside the shape, and, in this case, copy the library color into the shape." [shape library-id library-colors] - (let [sync-color (fn [shape position shape-color set-fn _ detach-fn] - (if (= (:ref-file shape-color) library-id) - (let [library-color (get library-colors (:ref-id shape-color))] - (if (some? library-color) - (set-fn shape - position - (:color library-color) - (:opacity library-color) - (:gradient library-color)) - (detach-fn shape position))) - shape))] + (letfn [(sync-color [shape position shape-color set-fn _ detach-fn] + (if (= (:ref-file shape-color) library-id) + (let [library-color (get library-colors (:ref-id shape-color))] + (if (some? library-color) + (set-fn shape + position + (:color library-color) + (:opacity library-color) + (:gradient library-color)) + (detach-fn shape position))) + shape))] (process-shape-colors shape sync-color))) diff --git a/common/src/app/common/types/components_list.cljc b/common/src/app/common/types/components_list.cljc index cb4bd70ae2..d2bbff58f2 100644 --- a/common/src/app/common/types/components_list.cljc +++ b/common/src/app/common/types/components_list.cljc @@ -24,9 +24,8 @@ :objects (d/index-by :id shapes)}) components-v2 - (update-in [:components id] #(assoc % - :main-instance-id main-instance-id - :main-instance-page main-instance-page))))) + (update-in [:components id] assoc :main-instance-id main-instance-id + :main-instance-page main-instance-page)))) (defn get-component [file-data component-id] diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index d58c05e3a4..b6ea7b9176 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -96,7 +96,7 @@ :component-file file-id :component-root? true) - components-v2 + (and (nil? (:parent-id new-shape)) components-v2) (assoc :main-instance? true) (some? (:parent-id new-shape)) diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index d81361a023..7653d99545 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -165,10 +165,10 @@ assets-seq))) (defn get-or-add-library-page - "If exists a page named 'Library page', get the id and calculate the position to start + "If exists a page named 'Library backup', get the id and calculate the position to start adding new components. If not, create it and start at (0, 0)." [file-data grid-gap] - (let [library-page (d/seek #(= (:name %) "Library page") (ctpl/pages-seq file-data))] + (let [library-page (d/seek #(= (:name %) "Library backup") (ctpl/pages-seq file-data))] (if (some? library-page) (let [compare-pos (fn [pos shape] (let [bounds (gsh/bounding-box shape)] @@ -180,11 +180,11 @@ (gpt/point 0 0) (ctn/shapes-seq library-page))] [file-data (:id library-page) position]) - (let [library-page (ctp/make-empty-page (uuid/next) "Library page")] + (let [library-page (ctp/make-empty-page (uuid/next) "Library backup")] [(ctpl/add-page file-data library-page) (:id library-page) (gpt/point 0 0)])))) (defn migrate-to-components-v2 - "If there is any component in the file library, add a new 'Library Page' and generate + "If there is any component in the file library, add a new 'Library backup' and generate main instances for all components there. Mark the file with the :comonents-v2 option." [file-data] (let [components (ctkl/components-seq file-data)] diff --git a/common/test/app/common/types/file_test.cljc b/common/test/app/common/types/file_test.cljc index 908c2b3e1c..70f14a36a6 100644 --- a/common/test/app/common/types/file_test.cljc +++ b/common/test/app/common/types/file_test.cljc @@ -104,7 +104,7 @@ (t/is (= (count pages) 2)) (t/is (= (:name (first pages)) "Page-1")) - (t/is (= (:name (second pages)) "Library page")) + (t/is (= (:name (second pages)) "Library backup")) (t/is (= (count components) 1)) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 4ea4eab752..d460cbf896 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -770,12 +770,12 @@ (rx/filter #(or (= :app.main.data.workspace/finalize-page (ptk/type %)) (= ::watch-component-changes (ptk/type %))))) - workspace-data-str + workspace-data-s (->> (rx/concat (rx/of nil) (rx/from-atom refs/workspace-data {:emit-current-value? true}))) - change-str + change-s (->> stream (rx/filter #(or (dch/commit-changes? %) (= (ptk/type %) :app.main.data.workspace.notifications/handle-file-change))) @@ -788,13 +788,13 @@ #{} changes)] (when (d/not-empty? components-changed) - (apply st/emit! + (run! st/emit! (map #(update-component-sync % (:id data)) components-changed)))))] (when components-v2 - (->> change-str - (rx/with-latest-from workspace-data-str) + (->> change-s + (rx/with-latest-from workspace-data-s) (rx/map check-changes) (rx/take-until stopper))))))) @@ -844,12 +844,13 @@ [file-id library-id] (ptk/reify ::attach-library ptk/WatchEvent - (watch [_ _ _] - (let [fetched #(assoc-in %2 [:workspace-libraries (:id %1)] %1) - params {:file-id file-id - :library-id library-id}] + (watch [_ state _] + (let [components-v2 (features/active-feature? state :components-v2) + fetched #(assoc-in %2 [:workspace-libraries (:id %1)] %1) + params {:file-id file-id + :library-id library-id}] (->> (rp/mutation :link-file-to-library params) - (rx/mapcat #(rp/query :file {:id library-id})) + (rx/mapcat #(rp/query :file {:id library-id :components-v2 components-v2})) (rx/map #(partial fetched %))))))) (defn unlink-file-from-library diff --git a/frontend/src/app/main/ui/dashboard/export.cljs b/frontend/src/app/main/ui/dashboard/export.cljs index 58f8d957d5..998e558039 100644 --- a/frontend/src/app/main/ui/dashboard/export.cljs +++ b/frontend/src/app/main/ui/dashboard/export.cljs @@ -8,6 +8,7 @@ (:require [app.common.data :as d] [app.main.data.modal :as modal] + [app.main.features :as features] [app.main.store :as st] [app.main.ui.icons :as i] [app.main.worker :as uw] @@ -56,6 +57,8 @@ :files (->> files (mapv #(assoc % :loading? true)))}) selected-option (mf/use-state :all) + components-v2 (features/use-feature :components-v2) + start-export (fn [] (swap! state assoc :status :exporting) @@ -64,7 +67,7 @@ :team-id team-id :export-type @selected-option :files files - }) + :components-v2 components-v2}) (rx/delay-emit 1000) (rx/subs (fn [msg] diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index bd55211100..1427ebd8a4 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -149,8 +149,8 @@ (->> (r/render-components (:data file)) (rx/map #(vector (str (:id file) "/components.svg") %)))) -(defn fetch-file-with-libraries [file-id] - (->> (rx/zip (rp/query :file {:id file-id}) +(defn fetch-file-with-libraries [file-id components-v2] + (->> (rx/zip (rp/query :file {:id file-id :components-v2 components-v2}) (rp/query :file-libraries {:file-id file-id})) (rx/map (fn [[file file-libraries]] @@ -351,7 +351,7 @@ (update file-id dissoc :libraries)))) (defn collect-files - [file-id export-type] + [file-id export-type components-v2] (letfn [(fetch-dependencies [[files pending]] (if (empty? pending) @@ -365,7 +365,7 @@ ;; The file is already in the result (rx/of [files pending]) - (->> (fetch-file-with-libraries next) + (->> (fetch-file-with-libraries next components-v2) (rx/map (fn [file] [(-> files @@ -381,9 +381,9 @@ (rx/map #(process-export file-id export-type %)))))) (defn export-file - [team-id file-id export-type] + [team-id file-id export-type components-v2] - (let [files-stream (->> (collect-files file-id export-type) + (let [files-stream (->> (collect-files file-id export-type components-v2) (rx/share)) manifest-stream @@ -471,12 +471,12 @@ :file-id (:id file)})))))))) (defmethod impl/handler :export-standard-file - [{:keys [team-id files export-type] :as message}] + [{:keys [team-id files export-type components-v2] :as message}] (->> (rx/from files) (rx/mapcat (fn [file] - (->> (export-file team-id (:id file) export-type) + (->> (export-file team-id (:id file) export-type components-v2) (rx/map (fn [value] (if (contains? value :type)