mirror of
https://github.com/penpot/penpot.git
synced 2025-01-25 07:58:49 -05:00
🎉 Add support media management via library
This commit is contained in:
parent
a19417417a
commit
fffacf3552
3 changed files with 65 additions and 9 deletions
|
@ -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]
|
||||
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Reference in a new issue