From 42d2315f37111bd045bb7c544a2efdae88b03f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Mon, 13 Jan 2025 11:25:20 +0100 Subject: [PATCH] :tada: Include tokens library in .penpot files --- backend/src/app/binfile/v3.clj | 44 +++++++++++++++++++-- common/src/app/common/types/tokens_lib.cljc | 9 ++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/backend/src/app/binfile/v3.clj b/backend/src/app/binfile/v3.clj index ff2d620b5..0f96a55e0 100644 --- a/backend/src/app/binfile/v3.clj +++ b/backend/src/app/binfile/v3.clj @@ -23,6 +23,7 @@ [app.common.types.page :as ctp] [app.common.types.plugins :as ctpg] [app.common.types.shape :as cts] + [app.common.types.tokens-lib :as cto] [app.common.types.typography :as cty] [app.common.uuid :as uuid] [app.config :as cf] @@ -106,6 +107,9 @@ (def encode-typography (sm/encoder ::cty/typography sm/json-transformer)) +(def encode-tokens-lib + (sm/encoder ::cto/tokens-lib sm/json-transformer)) + (def encode-plugin-data (sm/encoder ::ctpg/plugin-data sm/json-transformer)) @@ -141,6 +145,9 @@ (def decode-typography (sm/decoder ::cty/typography sm/json-transformer)) +(def decode-tokens-lib + (sm/decoder ::cto/tokens-lib sm/json-transformer)) + (def decode-plugin-data (sm/decoder ::ctpg/plugin-data sm/json-transformer)) @@ -176,6 +183,9 @@ (def validate-typography (sm/check-fn ::cty/typography)) +(def validate-tokens-lib + (sm/check-fn ::cto/tokens-lib)) + (def validate-plugin-data (sm/check-fn ::ctpg/plugin-data)) @@ -260,6 +270,7 @@ typographies (:typographies data) components (:components data) colors (:colors data) + tokens-lib (:tokens-lib data) pages (:pages data) pages-index (:pages-index data) @@ -333,9 +344,14 @@ (write-entry! output path color))) (doseq [[id object] typographies] - (let [path (str "files/" file-id "/typographies/" id ".json") - color (encode-typography object)] - (write-entry! output path color))))) + (let [path (str "files/" file-id "/typographies/" id ".json") + typography (encode-typography object)] + (write-entry! output path typography))) + + (when tokens-lib + (let [path (str "files/" file-id "/tokens.json") + encoded-tokens (encode-tokens-lib tokens-lib)] + (write-entry! output path encoded-tokens))))) (defn- export-files [{:keys [::ids ::include-libraries ::output] :as cfg}] @@ -471,6 +487,14 @@ {:entry entry :id (parse-uuid id)})))) +(defn- match-tokens-lib-entry-fn + [file-id] + (let [pattern (str "^files/" file-id "/tokens.json$") + pattern (re-pattern pattern)] + (fn [entry] + (when-let [[_] (re-matches pattern (zip-entry-name entry))] + {:entry entry})))) + (defn- match-thumbnail-entry-fn [file-id] (let [pattern (str "^files/" file-id "/thumbnails/([^/]+)/([^/]+)/([^/]+).json$") @@ -516,6 +540,11 @@ (with-open [reader (zip-entry-reader input entry)] (json/read reader :key-fn json/read-kebab-key))) +(defn- read-plain-entry + [^ZipFile input entry] + (with-open [reader (zip-entry-reader input entry)] + (json/read reader))) + (defn- read-file [{:keys [::input ::file-id]}] (let [path (str "files/" file-id ".json") @@ -586,6 +615,13 @@ {}) (not-empty))) +(defn- read-file-tokens-lib + [{:keys [::input ::file-id ::entries]}] + (when-let [entry (d/seek (match-tokens-lib-entry-fn file-id) entries)] + (->> (read-plain-entry input entry) + (decode-tokens-lib) + (validate-tokens-lib)))) + (defn- read-file-shapes [{:keys [::input ::file-id ::page-id ::entries] :as cfg}] (->> (keep (match-shape-entry-fn file-id page-id) entries) @@ -634,6 +670,7 @@ [cfg] (let [colors (read-file-colors cfg) typographies (read-file-typographies cfg) + tokens-lib (read-file-tokens-lib cfg) components (read-file-components cfg) plugin-data (read-file-plugin-data cfg) pages (read-file-pages cfg)] @@ -642,6 +679,7 @@ :pages-index (into {} pages) :colors colors :typographies typographies + :tokens-lib tokens-lib :components components :plugin-data plugin-data})) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index daa72841d..72e659a8e 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -1093,9 +1093,16 @@ Will return a value that matches this schema: [tokens-lib] (or tokens-lib (make-tokens-lib))) +(defn decode-dtcg + [encoded-json] + (-> (make-tokens-lib) + (decode-dtcg-json encoded-json))) + (def type:tokens-lib {:type ::tokens-lib - :pred valid-tokens-lib?}) + :pred valid-tokens-lib? + :type-properties {:encode/json encode-dtcg + :decode/json decode-dtcg}}) (sm/register! ::tokens-lib type:tokens-lib)