From ddbdc2a27fb2a2f9bedf80cfe47d271be26a963d Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 5 Jul 2021 18:14:49 +0200 Subject: [PATCH] :sparkles: Import/export folders in library elements --- common/src/app/common/file_builder.cljc | 2 + frontend/src/app/main/exports.cljs | 7 +-- .../src/app/main/ui/dashboard/import.cljs | 6 ++- frontend/src/app/util/import/parser.cljs | 6 ++- frontend/src/app/worker/export.cljs | 6 +-- frontend/src/app/worker/import.cljs | 44 +++++++++++++------ 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index 13b009128..348c4fdd3 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -393,6 +393,7 @@ (let [selrect init/empty-selrect name (:name data) + path (:path data) obj (-> (init/make-minimal-group nil selrect name) (merge data) (check-name file :group) @@ -402,6 +403,7 @@ {:type :add-component :id (:id obj) :name name + :path path :shapes [obj]}) (assoc :last-id (:id obj)) diff --git a/frontend/src/app/main/exports.cljs b/frontend/src/app/main/exports.cljs index 1642f3118..b6e6e9894 100644 --- a/frontend/src/app/main/exports.cljs +++ b/frontend/src/app/main/exports.cljs @@ -247,7 +247,7 @@ (mf/defc component-symbol [{:keys [id data] :as props}] - (let [{:keys [name objects]} data + (let [{:keys [name path objects]} data root (get objects id) {:keys [width height]} (:selrect root) @@ -267,8 +267,9 @@ (mf/deps objects) #(group-wrapper-factory objects))] - [:symbol {:id (str id) - :viewBox vbox} + [:> "symbol" #js {:id (str id) + :viewBox vbox + "penpot:path" path} [:title name] [:> shape-container {:shape root} [:& group-wrapper {:shape root :view-box vbox}]]])) diff --git a/frontend/src/app/main/ui/dashboard/import.cljs b/frontend/src/app/main/ui/dashboard/import.cljs index 342bbbaaa..cd9a97620 100644 --- a/frontend/src/app/main/ui/dashboard/import.cljs +++ b/frontend/src/app/main/ui/dashboard/import.cljs @@ -22,6 +22,8 @@ (log/set-level! :debug) +(def ^:const emit-delay 1000) + (defn use-import-file [project-id on-finish-import] (mf/use-callback @@ -201,7 +203,7 @@ (->> (uw/ask-many! {:cmd :analyze-import :files (->> files (mapv :uri))}) - (rx/delay-emit 1000) + (rx/delay-emit emit-delay) (rx/subs (fn [{:keys [uri data error] :as msg}] (log/debug :msg msg) @@ -216,7 +218,7 @@ {:cmd :import-files :project-id project-id :files files}) - (rx/delay-emit 1000) + (rx/delay-emit emit-delay) (rx/subs (fn [{:keys [file-id status] :as msg}] (log/debug :msg msg) diff --git a/frontend/src/app/util/import/parser.cljs b/frontend/src/app/util/import/parser.cljs index b4d68d0b3..3c46a9218 100644 --- a/frontend/src/app/util/import/parser.cljs +++ b/frontend/src/app/util/import/parser.cljs @@ -73,8 +73,10 @@ (defn get-id [node] - (when-let [id (re-find uuid-regex (get-in node [:attrs :id]))] - (uuid/uuid id))) + (let [attr-id (get-in node [:attrs :id]) + id (when (string? attr-id) (re-find uuid-regex attr-id))] + (when (some? id) + (uuid/uuid id)))) (defn str->bool [val] diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index f2fbe1652..43c2828d5 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -82,14 +82,14 @@ [(-> k str/camel) v])))))) (def ^:const color-keys - [:name :color :opacity :gradient]) + [:name :color :opacity :gradient :path]) (def ^:const typography-keys [:name :font-family :font-id :font-size :font-style :font-variant-id :font-weight - :letter-spacing :line-height :text-transform]) + :letter-spacing :line-height :text-transform :path]) (def ^:const media-keys - [:name :mtype :width :height]) + [:name :mtype :width :height :path]) (defn collect-color [result color] diff --git a/frontend/src/app/worker/import.cljs b/frontend/src/app/worker/import.cljs index c7be805b4..107b09231 100644 --- a/frontend/src/app/worker/import.cljs +++ b/frontend/src/app/worker/import.cljs @@ -21,7 +21,10 @@ [app.worker.impl :as impl] [beicon.core :as rx] [cuerdas.core :as str] - [tubax.core :as tubax])) + [tubax.core :as tubax] + [app.util.logging :as log])) + +(log/set-level! :trace) ;; Upload changes batches size (def change-batch-size 100) @@ -46,20 +49,23 @@ (str file-id "/media/" id "." ext)) :components (str file-id "/components.svg")) - svg? (str/ends-with? path "svg") - json? (str/ends-with? path "json") - other? (not (or svg? json?)) + parse-svg? (and (not= type :media) (str/ends-with? path "svg")) + parse-json? (and (not= type :media) (str/ends-with? path "json")) + no-parse? (or (= type :media) + (not (or parse-svg? parse-json?))) - file-type (if other? "blob" "text")] + file-type (if (or parse-svg? parse-json?) "text" "blob")] + + (log/debug :action "parsing" :path path) (cond->> (uz/get-file (:zip context) path file-type) - svg? + parse-svg? (rx/map (comp tubax/xml->clj :content)) - json? + parse-json? (rx/map (comp json/decode :content)) - other? + no-parse? (rx/map :content))))) (defn resolve-factory @@ -138,6 +144,9 @@ (defn upload-media-files "Upload a image to the backend and returns its id" [file-id name data-uri] + + (log/debug :action "uploading" :file-id file-id :name name) + (->> (http/send! {:uri data-uri :response-type :blob @@ -156,11 +165,18 @@ (->> node (ct/transform-nodes (fn [item] - (-> item - (d/update-when :fill-color-ref-id resolve) - (d/update-when :fill-color-ref-file resolve) - (d/update-when :typography-ref-id resolve) - (d/update-when :typography-ref-file resolve))))))) + (cond-> item + (uuid? (get item :fill-color-ref-id)) + (d/update-when :fill-color-ref-id resolve) + + (uuid? (get item :fill-color-ref-file)) + (d/update-when :fill-color-ref-file resolve) + + (uuid? (get item :typography-ref-id)) + (d/update-when :typography-ref-id resolve) + + (uuid? (get item :typography-ref-file)) + (d/update-when :typography-ref-file resolve))))))) (defn resolve-data-ids [data type context] @@ -273,7 +289,9 @@ file-id (:id file) old-id (cip/get-id node) id (resolve old-id) + path (get-in node [:attrs :penpot:path] "") data (-> (cip/parse-data :group content) + (assoc :path path) (assoc :id id)) file (-> file (fb/start-component data))