mirror of
https://github.com/penpot/penpot.git
synced 2025-06-10 20:51:45 +02:00
Compute tokens from each activated set
This commit is contained in:
parent
8e02dced2f
commit
6049c32839
8 changed files with 63 additions and 67 deletions
|
@ -16,12 +16,12 @@
|
||||||
[app.main.data.workspace.shapes :as dwsh]
|
[app.main.data.workspace.shapes :as dwsh]
|
||||||
[app.main.refs :as refs]
|
[app.main.refs :as refs]
|
||||||
[app.main.ui.workspace.tokens.token :as wtt]
|
[app.main.ui.workspace.tokens.token :as wtt]
|
||||||
|
[app.main.ui.workspace.tokens.token-set :as wtts]
|
||||||
|
[app.main.ui.workspace.tokens.update :as wtu]
|
||||||
[beicon.v2.core :as rx]
|
[beicon.v2.core :as rx]
|
||||||
[clojure.data :as data]
|
[clojure.data :as data]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[potok.v2.core :as ptk]
|
[potok.v2.core :as ptk]))
|
||||||
[app.main.ui.workspace.tokens.changes :as wdt]
|
|
||||||
[app.main.ui.workspace.tokens.token-set :as wtts]))
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Helpers
|
;; Helpers
|
||||||
|
@ -126,7 +126,9 @@
|
||||||
new-themes (wtts/toggle-active-theme-id token-theme-id state)
|
new-themes (wtts/toggle-active-theme-id token-theme-id state)
|
||||||
changes (-> (pcb/empty-changes it)
|
changes (-> (pcb/empty-changes it)
|
||||||
(pcb/update-active-token-themes new-themes themes))]
|
(pcb/update-active-token-themes new-themes themes))]
|
||||||
(rx/of (dch/commit-changes changes))))))
|
(rx/of
|
||||||
|
(dch/commit-changes changes)
|
||||||
|
(wtu/update-workspace-tokens))))))
|
||||||
|
|
||||||
(defn delete-token-theme [token-theme-id]
|
(defn delete-token-theme [token-theme-id]
|
||||||
(ptk/reify ::delete-token-theme
|
(ptk/reify ::delete-token-theme
|
||||||
|
@ -136,7 +138,9 @@
|
||||||
changes (-> (pcb/empty-changes it)
|
changes (-> (pcb/empty-changes it)
|
||||||
(pcb/with-library-data data)
|
(pcb/with-library-data data)
|
||||||
(pcb/delete-token-theme token-theme-id))]
|
(pcb/delete-token-theme token-theme-id))]
|
||||||
(rx/of (dch/commit-changes changes))))))
|
(rx/of
|
||||||
|
(dch/commit-changes changes)
|
||||||
|
(wtu/update-workspace-tokens))))))
|
||||||
|
|
||||||
(defn create-token-set [token-set]
|
(defn create-token-set [token-set]
|
||||||
(let [new-token-set (merge
|
(let [new-token-set (merge
|
||||||
|
@ -165,7 +169,9 @@
|
||||||
(wtts/toggle-token-set-to-token-theme token-set-id theme)
|
(wtts/toggle-token-set-to-token-theme token-set-id theme)
|
||||||
theme)
|
theme)
|
||||||
(pcb/update-active-token-themes #{(wtts/update-theme-id state)} (wtts/get-active-theme-ids state)))]
|
(pcb/update-active-token-themes #{(wtts/update-theme-id state)} (wtts/get-active-theme-ids state)))]
|
||||||
(rx/of (dch/commit-changes changes))))))
|
(rx/of
|
||||||
|
(dch/commit-changes changes)
|
||||||
|
(wtu/update-workspace-tokens))))))
|
||||||
|
|
||||||
(defn delete-token-set [token-set-id]
|
(defn delete-token-set [token-set-id]
|
||||||
(ptk/reify ::delete-token-set
|
(ptk/reify ::delete-token-set
|
||||||
|
@ -175,7 +181,9 @@
|
||||||
changes (-> (pcb/empty-changes it)
|
changes (-> (pcb/empty-changes it)
|
||||||
(pcb/with-library-data data)
|
(pcb/with-library-data data)
|
||||||
(pcb/delete-token-set token-set-id))]
|
(pcb/delete-token-set token-set-id))]
|
||||||
(rx/of (dch/commit-changes changes))))))
|
(rx/of
|
||||||
|
(dch/commit-changes changes)
|
||||||
|
(wtu/update-workspace-tokens))))))
|
||||||
|
|
||||||
(defn update-create-token
|
(defn update-create-token
|
||||||
[token]
|
[token]
|
||||||
|
|
|
@ -259,6 +259,9 @@
|
||||||
st/state
|
st/state
|
||||||
=))
|
=))
|
||||||
|
|
||||||
|
(def get-active-theme-sets-tokens
|
||||||
|
(l/derived wtts/get-active-theme-sets-tokens-names-map st/state =))
|
||||||
|
|
||||||
(def workspace-tokens
|
(def workspace-tokens
|
||||||
(l/derived
|
(l/derived
|
||||||
(fn [data]
|
(fn [data]
|
||||||
|
|
|
@ -101,7 +101,7 @@
|
||||||
selection-parents-ref (mf/use-memo (mf/deps ids) #(refs/parents-by-ids ids))
|
selection-parents-ref (mf/use-memo (mf/deps ids) #(refs/parents-by-ids ids))
|
||||||
selection-parents (mf/deref selection-parents-ref)
|
selection-parents (mf/deref selection-parents-ref)
|
||||||
|
|
||||||
tokens (-> (mf/deref refs/workspace-tokens)
|
tokens (-> (mf/deref refs/get-active-theme-sets-tokens)
|
||||||
(sd/use-resolved-tokens))
|
(sd/use-resolved-tokens))
|
||||||
tokens-by-type (mf/use-memo (mf/deps tokens) #(wtc/group-tokens-by-type tokens))
|
tokens-by-type (mf/use-memo (mf/deps tokens) #(wtc/group-tokens-by-type tokens))
|
||||||
|
|
||||||
|
|
|
@ -21,15 +21,12 @@
|
||||||
[app.main.ui.workspace.tokens.core :as wtc]
|
[app.main.ui.workspace.tokens.core :as wtc]
|
||||||
[app.main.ui.workspace.tokens.style-dictionary :as sd]
|
[app.main.ui.workspace.tokens.style-dictionary :as sd]
|
||||||
[app.main.ui.workspace.tokens.token :as wtt]
|
[app.main.ui.workspace.tokens.token :as wtt]
|
||||||
[app.main.ui.workspace.tokens.token-set :as wtts]
|
|
||||||
[app.main.ui.workspace.tokens.token-types :as wtty]
|
[app.main.ui.workspace.tokens.token-types :as wtty]
|
||||||
[app.main.ui.workspace.tokens.update :as wtu]
|
|
||||||
[app.util.dom :as dom]
|
[app.util.dom :as dom]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[okulary.core :as l]
|
[okulary.core :as l]
|
||||||
[rumext.v2 :as mf]
|
[rumext.v2 :as mf]
|
||||||
[shadow.resource]
|
[shadow.resource]))
|
||||||
[clojure.set :as set]))
|
|
||||||
|
|
||||||
(def lens:token-type-open-status
|
(def lens:token-type-open-status
|
||||||
(l/derived (l/in [:workspace-tokens :open-status]) st/state))
|
(l/derived (l/in [:workspace-tokens :open-status]) st/state))
|
||||||
|
@ -223,8 +220,7 @@
|
||||||
[:li {:class [(when (= selected-token-set-id id) "selected")]
|
[:li {:class [(when (= selected-token-set-id id) "selected")]
|
||||||
:on-click (fn []
|
:on-click (fn []
|
||||||
(st/emit!
|
(st/emit!
|
||||||
(wdt/set-selected-token-set-id id)
|
(wdt/set-selected-token-set-id id)))}
|
||||||
(wtu/update-workspace-tokens)))}
|
|
||||||
[:div.spaced
|
[:div.spaced
|
||||||
name
|
name
|
||||||
[:div.spaced
|
[:div.spaced
|
||||||
|
@ -248,9 +244,9 @@
|
||||||
selected (mf/deref refs/selected-shapes)
|
selected (mf/deref refs/selected-shapes)
|
||||||
selected-shapes (into [] (keep (d/getf objects)) selected)
|
selected-shapes (into [] (keep (d/getf objects)) selected)
|
||||||
|
|
||||||
tokens (-> (mf/deref refs/workspace-tokens)
|
tokens (sd/use-resolved-workspace-tokens)
|
||||||
(sd/use-resolved-tokens))
|
|
||||||
token-groups (mf/with-memo [tokens]
|
token-groups (mf/with-memo [tokens]
|
||||||
|
(js/console.log "tokens" tokens)
|
||||||
(sorted-token-groups tokens))]
|
(sorted-token-groups tokens))]
|
||||||
[:article
|
[:article
|
||||||
[:& token-sets]
|
[:& token-sets]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
(:require
|
(:require
|
||||||
["@tokens-studio/sd-transforms" :as sd-transforms]
|
["@tokens-studio/sd-transforms" :as sd-transforms]
|
||||||
["style-dictionary$default" :as sd]
|
["style-dictionary$default" :as sd]
|
||||||
|
[app.common.data :refer [ordered-map]]
|
||||||
[app.main.refs :as refs]
|
[app.main.refs :as refs]
|
||||||
[app.main.ui.workspace.tokens.token :as wtt]
|
[app.main.ui.workspace.tokens.token :as wtt]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
|
@ -79,8 +80,10 @@
|
||||||
(resolve-sd-tokens+ config))]
|
(resolve-sd-tokens+ config))]
|
||||||
(let [resolved-tokens (reduce
|
(let [resolved-tokens (reduce
|
||||||
(fn [acc ^js cur]
|
(fn [acc ^js cur]
|
||||||
(let [id (uuid (.-uuid (.-id cur)))
|
(let [identifier (if (uuid? (ffirst tokens))
|
||||||
origin-token (get tokens id)
|
(uuid (.-uuid (.-id cur)))
|
||||||
|
(.. cur -original -name))
|
||||||
|
origin-token (get tokens identifier)
|
||||||
parsed-value (wtt/parse-token-value (.-value cur))
|
parsed-value (wtt/parse-token-value (.-value cur))
|
||||||
resolved-token (if (not parsed-value)
|
resolved-token (if (not parsed-value)
|
||||||
(assoc origin-token :errors [:style-dictionary/missing-reference])
|
(assoc origin-token :errors [:style-dictionary/missing-reference])
|
||||||
|
@ -93,11 +96,6 @@
|
||||||
(js/console.log "Resolved tokens" resolved-tokens))
|
(js/console.log "Resolved tokens" resolved-tokens))
|
||||||
resolved-tokens)))
|
resolved-tokens)))
|
||||||
|
|
||||||
(defn resolve-workspace-tokens+
|
|
||||||
[& {:keys [debug?] :as config}]
|
|
||||||
(when-let [workspace-tokens @refs/workspace-tokens]
|
|
||||||
(resolve-tokens+ workspace-tokens)))
|
|
||||||
|
|
||||||
;; Hooks -----------------------------------------------------------------------
|
;; Hooks -----------------------------------------------------------------------
|
||||||
|
|
||||||
(defonce !tokens-cache (atom nil))
|
(defonce !tokens-cache (atom nil))
|
||||||
|
@ -112,7 +110,7 @@
|
||||||
then the state will be updated with the resolved tokens."
|
then the state will be updated with the resolved tokens."
|
||||||
[tokens & {:keys [cache-atom]
|
[tokens & {:keys [cache-atom]
|
||||||
:or {cache-atom !tokens-cache}}]
|
:or {cache-atom !tokens-cache}}]
|
||||||
(let [tokens-state (mf/use-state (get @cache-atom tokens tokens))]
|
(let [tokens-state (mf/use-state (get @cache-atom tokens))]
|
||||||
(mf/use-effect
|
(mf/use-effect
|
||||||
(mf/deps tokens)
|
(mf/deps tokens)
|
||||||
(fn []
|
(fn []
|
||||||
|
@ -133,37 +131,3 @@
|
||||||
(defn use-resolved-workspace-tokens [& {:as config}]
|
(defn use-resolved-workspace-tokens [& {:as config}]
|
||||||
(-> (mf/deref refs/workspace-tokens)
|
(-> (mf/deref refs/workspace-tokens)
|
||||||
(use-resolved-tokens config)))
|
(use-resolved-tokens config)))
|
||||||
|
|
||||||
;; Testing ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
(comment
|
|
||||||
(defonce !output (atom nil))
|
|
||||||
|
|
||||||
(-> @refs/workspace-tokens
|
|
||||||
(resolve-tokens+ {:debug? false})
|
|
||||||
(.then js/console.log))
|
|
||||||
|
|
||||||
(-> (resolve-workspace-tokens+ {:debug? true})
|
|
||||||
(p/then #(reset! !output %)))
|
|
||||||
|
|
||||||
@!output
|
|
||||||
|
|
||||||
(->> @refs/workspace-tokens
|
|
||||||
(resolve-tokens+)
|
|
||||||
(#(doto % js/console.log)))
|
|
||||||
|
|
||||||
(->
|
|
||||||
(clj->js {"a" {:name "a" :value "5"}
|
|
||||||
"b" {:name "b" :value "{a} * 2"}})
|
|
||||||
|
|
||||||
(#(resolve-sd-tokens+ % {:debug? true})))
|
|
||||||
|
|
||||||
(defonce output (atom nil))
|
|
||||||
(require '[shadow.resource])
|
|
||||||
(let [example (-> (shadow.resource/inline "./data/example-tokens-set.json")
|
|
||||||
(js/JSON.parse)
|
|
||||||
.-core)]
|
|
||||||
(.then (resolve-sd-tokens+ example {:debug? true})
|
|
||||||
#(reset! output %)))
|
|
||||||
|
|
||||||
nil)
|
|
||||||
|
|
|
@ -4,6 +4,14 @@
|
||||||
[clojure.set :as set]
|
[clojure.set :as set]
|
||||||
[cuerdas.core :as str]))
|
[cuerdas.core :as str]))
|
||||||
|
|
||||||
|
(defn get-workspace-tokens
|
||||||
|
[state]
|
||||||
|
(get-in state [:workspace-data :tokens] {}))
|
||||||
|
|
||||||
|
(defn get-workspace-token
|
||||||
|
[token-id state]
|
||||||
|
(get-in state [:workspace-data :tokens token-id]))
|
||||||
|
|
||||||
(def parseable-token-value-regexp
|
(def parseable-token-value-regexp
|
||||||
"Regexp that can be used to parse a number value out of resolved token value.
|
"Regexp that can be used to parse a number value out of resolved token value.
|
||||||
This regexp also trims whitespace around the value."
|
This regexp also trims whitespace around the value."
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
(ns app.main.ui.workspace.tokens.token-set
|
(ns app.main.ui.workspace.tokens.token-set
|
||||||
(:require
|
(:require
|
||||||
[clojure.set :as set]))
|
[app.common.data :refer [ordered-map]]
|
||||||
|
[app.main.ui.workspace.tokens.token :as wtt]
|
||||||
|
[clojure.set :as set]))
|
||||||
|
|
||||||
;; Themes ----------------------------------------------------------------------
|
;; Themes ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -102,9 +104,6 @@
|
||||||
|
|
||||||
;; Sets ------------------------------------------------------------------------
|
;; Sets ------------------------------------------------------------------------
|
||||||
|
|
||||||
(defn get-workspace-tokens [state]
|
|
||||||
(get-in state [:workspace-data :tokens]))
|
|
||||||
|
|
||||||
(defn get-workspace-sets [state]
|
(defn get-workspace-sets [state]
|
||||||
(get-in state [:workspace-data :token-sets-index]))
|
(get-in state [:workspace-data :token-sets-index]))
|
||||||
|
|
||||||
|
@ -112,6 +111,10 @@
|
||||||
(some-> (get-workspace-sets state)
|
(some-> (get-workspace-sets state)
|
||||||
(get set-id)))
|
(get set-id)))
|
||||||
|
|
||||||
|
(defn get-workspace-token-set-tokens [set-id state]
|
||||||
|
(-> (get-token-set set-id state)
|
||||||
|
:tokens))
|
||||||
|
|
||||||
(def default-token-set-name "Global")
|
(def default-token-set-name "Global")
|
||||||
|
|
||||||
(defn create-global-set [])
|
(defn create-global-set [])
|
||||||
|
@ -129,11 +132,22 @@
|
||||||
|
|
||||||
(defn get-selected-token-set-tokens [state]
|
(defn get-selected-token-set-tokens [state]
|
||||||
(when-let [token-set (get-selected-token-set state)]
|
(when-let [token-set (get-selected-token-set state)]
|
||||||
(let [tokens (or (get-workspace-tokens state) {})]
|
(let [tokens (or (wtt/get-workspace-tokens state) {})]
|
||||||
|
(js/console.log "token-set" token-set (select-keys tokens (:tokens token-set)))
|
||||||
(select-keys tokens (:tokens token-set)))))
|
(select-keys tokens (:tokens token-set)))))
|
||||||
|
|
||||||
(defn get-token-set-tokens [{:keys [tokens] :as token-set} file]
|
|
||||||
(map #(get-in file [:data :tokens %]) tokens))
|
|
||||||
|
|
||||||
(defn assoc-selected-token-set-id [state id]
|
(defn assoc-selected-token-set-id [state id]
|
||||||
(assoc-in state [:workspace-local :selected-token-set-id] id))
|
(assoc-in state [:workspace-local :selected-token-set-id] id))
|
||||||
|
|
||||||
|
(defn get-active-theme-sets-tokens-names-map [state]
|
||||||
|
(let [active-set-ids (get-active-set-ids state)]
|
||||||
|
(reduce
|
||||||
|
(fn [names-map-acc set-id]
|
||||||
|
(let [token-ids (get-workspace-token-set-tokens set-id state)]
|
||||||
|
(reduce
|
||||||
|
(fn [acc token-id]
|
||||||
|
(if-let [token (wtt/get-workspace-token token-id state)]
|
||||||
|
(assoc acc (wtt/token-identifier token) token)
|
||||||
|
acc))
|
||||||
|
names-map-acc token-ids)))
|
||||||
|
(ordered-map) active-set-ids)))
|
||||||
|
|
|
@ -121,7 +121,10 @@
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state _]
|
(watch [_ state _]
|
||||||
(->>
|
(->>
|
||||||
(rx/from (wtsd/resolve-tokens+ (wtts/get-selected-token-set-tokens state)))
|
(rx/from
|
||||||
|
(->
|
||||||
|
(wtts/get-active-theme-sets-tokens-names-map state)
|
||||||
|
(wtsd/resolve-tokens+)))
|
||||||
(rx/mapcat
|
(rx/mapcat
|
||||||
(fn [sd-tokens]
|
(fn [sd-tokens]
|
||||||
(let [undo-id (js/Symbol)]
|
(let [undo-id (js/Symbol)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue