From 4c1f2cfdedbe9d225d878bed7f5387e6991ad268 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Mon, 28 Nov 2022 09:47:47 +0100 Subject: [PATCH 1/4] :tada: Add support color management via library --- common/src/app/common/file_builder.cljc | 19 +++++++++++++++++-- frontend/src/app/libs/file_builder.cljs | 24 ++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index a45d0d027b..d4520fcb51 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -498,15 +498,30 @@ (defn add-library-color [file color] - (let [id (or (:id color) (uuid/next))] (-> file (commit-change {:type :add-color - :id id :color (assoc color :id id)}) (assoc :last-id id)))) +(defn update-library-color + [file color] + (let [id (uuid/uuid (:id color))] + (-> file + (commit-change + {:type :mod-color + :color (assoc color :id id)}) + (assoc :last-id (:id color))))) + +(defn delete-library-color + [file color-id] + (let [id (uuid/uuid color-id)] + (-> file + (commit-change + {:type :del-color + :id id})))) + (defn add-library-typography [file typography] (let [id (or (:id typography) (uuid/next))] diff --git a/frontend/src/app/libs/file_builder.cljs b/frontend/src/app/libs/file_builder.cljs index 1a4af9fb56..25aec3ffee 100644 --- a/frontend/src/app/libs/file_builder.cljs +++ b/frontend/src/app/libs/file_builder.cljs @@ -27,7 +27,6 @@ key (-> key d/name str/kebab keyword)] [key value])) $))) - (defn export-file [file] (let [file (assoc file @@ -51,6 +50,14 @@ (rx/observe-on :async) (rx/flat-map e/get-page-data) (rx/share)) + + colors-stream + (->> files-stream + (rx/flat-map vals) + (rx/map #(vector (:id %) (get-in % [:data :colors]))) + (rx/filter #(d/not-empty? (second %))) + (rx/map e/parse-library-color)) + pages-stream (->> render-stream (rx/map e/collect-page))] @@ -64,7 +71,8 @@ (->> (rx/merge manifest-stream - pages-stream) + pages-stream + colors-stream) (rx/reduce conj []) (rx/with-latest-from files-stream) (rx/flat-map (fn [[data _]] @@ -133,6 +141,18 @@ (closeSVG [_] (set! file (fb/close-svg-raw file))) + (addLibraryColor [_ data] + (set! file (fb/add-library-color file (parse-data data))) + (str (:last-id file))) + + (updateLibraryColor [_ data] + (set! file (fb/update-library-color file (parse-data data))) + (str (:last-id file))) + + (deleteLibraryColor [_ data] + (set! file (fb/delete-library-color file (parse-data data))) + (str (:last-id file))) + (asMap [_] (clj->js file)) From a19417417a46656e8fa98d2af072b7eb955ff7a6 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 30 Nov 2022 06:25:21 +0100 Subject: [PATCH 2/4] :tada: Add support components managemente via library --- common/src/app/common/file_builder.cljc | 37 +++++++++++++++++++++- frontend/src/app/libs/file_builder.cljs | 41 +++++++++++++++++++++---- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index d4520fcb51..2bd2a5dc00 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -661,6 +661,40 @@ shapes) (dissoc $ :current-component-id)))) +(defn create-component-instance + [file data] + (let [component-id (uuid/uuid (:component-id data)) + x (:x data) + y (:y data) + file (assoc file :current-component-id component-id) + page-id (:current-page-id file) + page (ctpl/get-page (:data file) page-id) + component (ctkl/get-component (:data file) component-id) + ;; main-instance-id (:main-instance-id component) + + [shape shapes] + (ctn/make-component-instance page + component + (:id file) + (gpt/point x + y) + #_{:main-instance? true + :force-id main-instance-id})] + + (as-> file $ + (reduce #(commit-change %1 + {:type :add-obj + :id (:id %2) + :page-id (:id page) + :parent-id (:parent-id %2) + :frame-id (:frame-id %2) + :obj %2}) + $ + shapes) + + (assoc $ :last-id (:id shape)) + (dissoc $ :current-component-id)))) + (defn delete-object [file id] (let [page-id (:current-page-id file)] @@ -687,7 +721,8 @@ {:type :mod-obj :operations (reduce generate-operation [] attrs) :page-id page-id - :id (:id old-obj)})))) + :id (:id old-obj)}) + (assoc :last-id (:id old-obj))))) (defn get-current-page [file] diff --git a/frontend/src/app/libs/file_builder.cljs b/frontend/src/app/libs/file_builder.cljs index 25aec3ffee..4070ed1f85 100644 --- a/frontend/src/app/libs/file_builder.cljs +++ b/frontend/src/app/libs/file_builder.cljs @@ -58,6 +58,12 @@ (rx/filter #(d/not-empty? (second %))) (rx/map e/parse-library-color)) + components-stream + (->> files-stream + (rx/flat-map vals) + (rx/filter #(d/not-empty? (get-in % [:data :components]))) + (rx/flat-map e/parse-library-components)) + pages-stream (->> render-stream (rx/map e/collect-page))] @@ -72,6 +78,7 @@ (->> (rx/merge manifest-stream pages-stream + components-stream colors-stream) (rx/reduce conj []) (rx/with-latest-from files-stream) @@ -153,16 +160,38 @@ (set! file (fb/delete-library-color file (parse-data data))) (str (:last-id file))) + (startComponent [_ data] + (set! file (fb/start-component file (parse-data data))) + (str (:current-component-id file))) + + (finishComponent [_] + (set! file (fb/finish-component file))) + + (createComponentInstance [_ data] + (set! file (fb/create-component-instance file (parse-data data))) + (str (:last-id file))) + + (lookupShape [_ shape-id] + (clj->js (fb/lookup-shape file (uuid/uuid shape-id)))) + + (updateObject [_ id new-obj] + (let [old-obj (fb/lookup-shape file (uuid/uuid id)) + new-obj (d/deep-merge old-obj (parse-data new-obj))] + (set! file (fb/update-object file old-obj new-obj)))) + + (deleteObject [_ id] + (set! file (fb/delete-object file (uuid/uuid id)))) + (asMap [_] (clj->js file)) (export [_] - (->> (export-file file) - (rx/subs - (fn [value] - (when (not (contains? value :type)) - (let [[file export-blob] value] - (dom/trigger-download (:name file) export-blob)))))))) + (->> (export-file file) + (rx/subs + (fn [value] + (when (not (contains? value :type)) + (let [[file export-blob] value] + (dom/trigger-download (:name file) export-blob)))))))) (defn create-file-export [^string name] (File. (fb/create-file name))) From fffacf35528369773e7e341c5bfbe5a1c846bf6a Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 30 Nov 2022 13:29:02 +0100 Subject: [PATCH 3/4] :tada: Add support media management via library --- common/src/app/common/file_builder.cljc | 8 ++++ frontend/src/app/libs/file_builder.cljs | 49 +++++++++++++++++++++++++ frontend/src/app/worker/export.cljs | 17 ++++----- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index 2bd2a5dc00..0f04df9806 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -541,6 +541,14 @@ :object (assoc media :id id)}) (assoc :last-id id)))) +(defn delete-library-media + [file media-id] + (let [id (uuid/uuid media-id)] + (-> file + (commit-change + {:type :del-media + :id id})))) + (defn start-component [file data] diff --git a/frontend/src/app/libs/file_builder.cljs b/frontend/src/app/libs/file_builder.cljs index 4070ed1f85..0d4e723511 100644 --- a/frontend/src/app/libs/file_builder.cljs +++ b/frontend/src/app/libs/file_builder.cljs @@ -8,8 +8,11 @@ (:require [app.common.data :as d] [app.common.file-builder :as fb] + [app.common.media :as cm] [app.common.uuid :as uuid] [app.util.dom :as dom] + [app.util.json :as json] + [app.util.webapi :as wapi] [app.util.zip :as uz] [app.worker.export :as e] [beicon.core :as rx] @@ -27,6 +30,36 @@ key (-> key d/name str/kebab keyword)] [key value])) $))) +(defn data-uri->blob + [data-uri] + (let [[mtype b64-data] (str/split data-uri ";base64,") + mtype (subs mtype (inc (str/index-of mtype ":"))) + decoded (.atob js/window b64-data) + size (.-length ^js decoded) + content (js/Uint8Array. size)] + + (doseq [i (range 0 size)] + (aset content i (.charCodeAt decoded i))) + + (wapi/create-blob content mtype))) + +(defn parse-library-media + [[file-id media]] + (rx/merge + (let [markup + (->> (vals media) + (reduce e/collect-media {}) + (json/encode))] + (rx/of (vector (str file-id "/media.json") markup))) + + (->> (rx/from (vals media)) + (rx/map #(assoc % :file-id file-id)) + (rx/flat-map + (fn [media] + (let [file-path (str/concat file-id "/media/" (:id media) (cm/mtype->extension (:mtype media))) + blob (data-uri->blob (:uri media))] + (rx/of (vector file-path blob)))))))) + (defn export-file [file] (let [file (assoc file @@ -58,6 +91,13 @@ (rx/filter #(d/not-empty? (second %))) (rx/map e/parse-library-color)) + media-stream + (->> files-stream + (rx/flat-map vals) + (rx/map #(vector (:id %) (get-in % [:data :media]))) + (rx/filter #(d/not-empty? (second %))) + (rx/flat-map parse-library-media)) + components-stream (->> files-stream (rx/flat-map vals) @@ -79,6 +119,7 @@ manifest-stream pages-stream components-stream + media-stream colors-stream) (rx/reduce conj []) (rx/with-latest-from files-stream) @@ -160,6 +201,14 @@ (set! file (fb/delete-library-color file (parse-data data))) (str (:last-id file))) + (addLibraryMedia [_ data] + (set! file (fb/add-library-media file (parse-data data))) + (str (:last-id file))) + + (deleteLibraryMedia [_ data] + (set! file (fb/delete-library-media file (parse-data data))) + (str (:last-id file))) + (startComponent [_ data] (set! file (fb/start-component file (parse-data data))) (str (:current-component-id file))) diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index c40a172b90..ac3ecfac4b 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -155,15 +155,14 @@ (->> (r/render-components (:data file) :deleted-components) (rx/map #(vector (str (:id file) "/deleted-components.svg") %)))) -(defn fetch-file-with-libraries - [file-id components-v2] - (let [features (cond-> #{} components-v2 (conj "components/v2"))] - (->> (rx/zip (rp/cmd! :get-file {:id file-id :features features}) - (rp/cmd! :get-file-libraries {:file-id file-id})) - (rx/map - (fn [[file file-libraries]] - (let [libraries-ids (->> file-libraries (map :id) (filterv #(not= (:id file) %)))] - (assoc file :libraries libraries-ids))))))) +(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]] + (let [libraries-ids (->> file-libraries (map :id) (filterv #(not= (:id file) %)))] + (-> file + (assoc :libraries libraries-ids))))))) (defn get-component-ref-file [objects shape] From 32520884948f747fa10bd899404b4fa8eb3edaf5 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 30 Nov 2022 13:56:44 +0100 Subject: [PATCH 4/4] :tada: Add support typography management via library --- common/src/app/common/file_builder.cljc | 8 ++++++++ frontend/src/app/libs/file_builder.cljs | 18 +++++++++++++++++- frontend/src/app/worker/export.cljs | 17 +++++++++-------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index 0f04df9806..1706452c2d 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -532,6 +532,14 @@ :typography (assoc typography :id id)}) (assoc :last-id id)))) +(defn delete-library-typography + [file typography-id] + (let [id (uuid/uuid typography-id)] + (-> file + (commit-change + {:type :del-typography + :id id})))) + (defn add-library-media [file media] (let [id (or (:id media) (uuid/next))] diff --git a/frontend/src/app/libs/file_builder.cljs b/frontend/src/app/libs/file_builder.cljs index 0d4e723511..4c1e42adb6 100644 --- a/frontend/src/app/libs/file_builder.cljs +++ b/frontend/src/app/libs/file_builder.cljs @@ -91,6 +91,13 @@ (rx/filter #(d/not-empty? (second %))) (rx/map e/parse-library-color)) + typographies-stream + (->> files-stream + (rx/flat-map vals) + (rx/map #(vector (:id %) (get-in % [:data :typographies]))) + (rx/filter #(d/not-empty? (second %))) + (rx/map e/parse-library-typographies)) + media-stream (->> files-stream (rx/flat-map vals) @@ -120,7 +127,8 @@ pages-stream components-stream media-stream - colors-stream) + colors-stream + typographies-stream) (rx/reduce conj []) (rx/with-latest-from files-stream) (rx/flat-map (fn [[data _]] @@ -209,6 +217,14 @@ (set! file (fb/delete-library-media file (parse-data data))) (str (:last-id file))) + (addLibraryTypography [_ data] + (set! file (fb/add-library-typography file (parse-data data))) + (str (:last-id file))) + + (deleteLibraryTypography [_ data] + (set! file (fb/delete-library-typography file (parse-data data))) + (str (:last-id file))) + (startComponent [_ data] (set! file (fb/start-component file (parse-data data))) (str (:current-component-id file))) diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index ac3ecfac4b..c40a172b90 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -155,14 +155,15 @@ (->> (r/render-components (:data file) :deleted-components) (rx/map #(vector (str (:id file) "/deleted-components.svg") %)))) -(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]] - (let [libraries-ids (->> file-libraries (map :id) (filterv #(not= (:id file) %)))] - (-> file - (assoc :libraries libraries-ids))))))) +(defn fetch-file-with-libraries + [file-id components-v2] + (let [features (cond-> #{} components-v2 (conj "components/v2"))] + (->> (rx/zip (rp/cmd! :get-file {:id file-id :features features}) + (rp/cmd! :get-file-libraries {:file-id file-id})) + (rx/map + (fn [[file file-libraries]] + (let [libraries-ids (->> file-libraries (map :id) (filterv #(not= (:id file) %)))] + (assoc file :libraries libraries-ids))))))) (defn get-component-ref-file [objects shape]