mirror of
https://github.com/penpot/penpot.git
synced 2025-05-05 09:45:54 +02:00
Add color token
This commit is contained in:
parent
1a9d703bb1
commit
0b29767c95
10 changed files with 57 additions and 11 deletions
|
@ -35,8 +35,8 @@
|
||||||
(def token-types
|
(def token-types
|
||||||
#{:boolean
|
#{:boolean
|
||||||
:border-radius
|
:border-radius
|
||||||
:stroke-width
|
|
||||||
:box-shadow
|
:box-shadow
|
||||||
|
:color
|
||||||
:dimensions
|
:dimensions
|
||||||
:numeric
|
:numeric
|
||||||
:opacity
|
:opacity
|
||||||
|
@ -45,6 +45,7 @@
|
||||||
:sizing
|
:sizing
|
||||||
:spacing
|
:spacing
|
||||||
:string
|
:string
|
||||||
|
:stroke-width
|
||||||
:typography})
|
:typography})
|
||||||
|
|
||||||
(defn valid-token-type?
|
(defn valid-token-type?
|
||||||
|
@ -66,6 +67,12 @@
|
||||||
[:description {:optional true} :string]
|
[:description {:optional true} :string]
|
||||||
[:modified-at {:optional true} ::sm/inst]])
|
[:modified-at {:optional true} ::sm/inst]])
|
||||||
|
|
||||||
|
(sm/register! ::color
|
||||||
|
[:map
|
||||||
|
[:color {:optional true} token-name-ref]])
|
||||||
|
|
||||||
|
(def color-keys (schema-keys ::color))
|
||||||
|
|
||||||
(sm/register! ::border-radius
|
(sm/register! ::border-radius
|
||||||
[:map
|
[:map
|
||||||
[:rx {:optional true} token-name-ref]
|
[:rx {:optional true} token-name-ref]
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.types.shape.radius :as ctsr]
|
[app.common.types.shape.radius :as ctsr]
|
||||||
[app.common.types.token :as ctt]
|
[app.common.types.token :as ctt]
|
||||||
|
[app.main.data.workspace.colors :as wdc]
|
||||||
[app.main.data.workspace :as udw]
|
[app.main.data.workspace :as udw]
|
||||||
[app.main.data.workspace.shape-layout :as dwsl]
|
[app.main.data.workspace.shape-layout :as dwsl]
|
||||||
[app.main.data.workspace.shapes :as dwsh]
|
[app.main.data.workspace.shapes :as dwsh]
|
||||||
|
@ -123,6 +124,10 @@
|
||||||
{:reg-objects? true
|
{:reg-objects? true
|
||||||
:attrs [:strokes]}))
|
:attrs [:strokes]}))
|
||||||
|
|
||||||
|
(defn update-color
|
||||||
|
[value shape-ids]
|
||||||
|
(wdc/change-fill shape-ids {:color (str "#" value)} 0))
|
||||||
|
|
||||||
(defn update-shape-dimensions [value shape-ids attributes]
|
(defn update-shape-dimensions [value shape-ids attributes]
|
||||||
(ptk/reify ::update-shape-dimensions
|
(ptk/reify ::update-shape-dimensions
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
|
|
|
@ -222,10 +222,10 @@
|
||||||
|
|
||||||
(defn selection-actions [{:keys [type token] :as context-data}]
|
(defn selection-actions [{:keys [type token] :as context-data}]
|
||||||
(let [with-actions (get shape-attribute-actions-map (or type (:type token)))
|
(let [with-actions (get shape-attribute-actions-map (or type (:type token)))
|
||||||
attribute-actions (with-actions context-data)]
|
attribute-actions (if with-actions (with-actions context-data) [])]
|
||||||
(concat
|
(concat
|
||||||
attribute-actions
|
attribute-actions
|
||||||
[:separator]
|
(when (seq attribute-actions) [:separator])
|
||||||
(default-actions context-data))))
|
(default-actions context-data))))
|
||||||
|
|
||||||
;; Components ------------------------------------------------------------------
|
;; Components ------------------------------------------------------------------
|
||||||
|
|
|
@ -101,15 +101,19 @@ Token names should only contain letters and digits separated by . characters.")}
|
||||||
:else (let [token-id (or (:id token) (random-uuid))
|
:else (let [token-id (or (:id token) (random-uuid))
|
||||||
new-tokens (update tokens token-name merge {:id token-id
|
new-tokens (update tokens token-name merge {:id token-id
|
||||||
:value input
|
:value input
|
||||||
:name token-name})]
|
:name token-name
|
||||||
(-> (sd/resolve-tokens+ new-tokens {:names-map? true})
|
:type (:type token)})]
|
||||||
|
(-> (sd/resolve-tokens+ new-tokens {:names-map? true
|
||||||
|
:debug? true})
|
||||||
(p/then
|
(p/then
|
||||||
(fn [resolved-tokens]
|
(fn [resolved-tokens]
|
||||||
|
(js/console.log "resolved-tokens" resolved-tokens)
|
||||||
(let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-name)]
|
(let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-name)]
|
||||||
(cond
|
(cond
|
||||||
resolved-value (p/resolved resolved-token)
|
resolved-value (p/resolved resolved-token)
|
||||||
(sd/missing-reference-error? errors) (p/rejected :error/token-missing-reference)
|
(sd/missing-reference-error? errors) (p/rejected :error/token-missing-reference)
|
||||||
:else (p/rejected :error/unknown-error))))))))))
|
:else (p/rejected :error/unknown-error)))))
|
||||||
|
(p/catch js/console.log))))))
|
||||||
|
|
||||||
(defn use-debonced-resolve-callback
|
(defn use-debonced-resolve-callback
|
||||||
"Resolves a token values using `StyleDictionary`.
|
"Resolves a token values using `StyleDictionary`.
|
||||||
|
@ -143,8 +147,8 @@ Token names should only contain letters and digits separated by . characters.")}
|
||||||
|
|
||||||
(mf/defc form
|
(mf/defc form
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
[{:keys [token token-type] :as _args}]
|
[{:keys [token token-type]}]
|
||||||
(let [tokens (mf/deref refs/workspace-ordered-token-sets-tokens)
|
(let [token (or token {:type token-type})
|
||||||
selected-set-tokens (mf/deref refs/workspace-selected-token-set-tokens)
|
selected-set-tokens (mf/deref refs/workspace-selected-token-set-tokens)
|
||||||
active-theme-tokens (mf/deref refs/workspace-active-theme-sets-tokens)
|
active-theme-tokens (mf/deref refs/workspace-active-theme-sets-tokens)
|
||||||
resolved-tokens (sd/use-resolved-tokens active-theme-tokens {:names-map? true
|
resolved-tokens (sd/use-resolved-tokens active-theme-tokens {:names-map? true
|
||||||
|
@ -153,7 +157,7 @@ Token names should only contain letters and digits separated by . characters.")}
|
||||||
(mf/deps (:name token))
|
(mf/deps (:name token))
|
||||||
#(wtt/token-name->path (:name token)))
|
#(wtt/token-name->path (:name token)))
|
||||||
selected-set-tokens-tree (mf/use-memo
|
selected-set-tokens-tree (mf/use-memo
|
||||||
(mf/deps token-path tokens)
|
(mf/deps token-path selected-set-tokens)
|
||||||
(fn []
|
(fn []
|
||||||
(-> (wtt/token-names-tree selected-set-tokens)
|
(-> (wtt/token-names-tree selected-set-tokens)
|
||||||
;; Allow setting editing token to it's own path
|
;; Allow setting editing token to it's own path
|
||||||
|
|
|
@ -68,6 +68,12 @@
|
||||||
[properties]
|
[properties]
|
||||||
[:& token-update-create-modal properties])
|
[:& token-update-create-modal properties])
|
||||||
|
|
||||||
|
(mf/defc color-modal
|
||||||
|
{::mf/register modal/components
|
||||||
|
::mf/register-as :tokens/color}
|
||||||
|
[properties]
|
||||||
|
[:& token-update-create-modal properties])
|
||||||
|
|
||||||
(mf/defc stroke-width-modal
|
(mf/defc stroke-width-modal
|
||||||
{::mf/register modal/components
|
{::mf/register modal/components
|
||||||
::mf/register-as :tokens/stroke-width}
|
::mf/register-as :tokens/stroke-width}
|
||||||
|
|
|
@ -77,6 +77,7 @@
|
||||||
(case type
|
(case type
|
||||||
:border-radius i/corner-radius
|
:border-radius i/corner-radius
|
||||||
:numeric [:span {:class (stl/css :section-text-icon)} "123"]
|
:numeric [:span {:class (stl/css :section-text-icon)} "123"]
|
||||||
|
:color i/drop-icon
|
||||||
:boolean i/boolean-difference
|
:boolean i/boolean-difference
|
||||||
:opacity [:span {:class (stl/css :section-text-icon)} "%"]
|
:opacity [:span {:class (stl/css :section-text-icon)} "%"]
|
||||||
:rotation i/rotation
|
:rotation i/rotation
|
||||||
|
|
|
@ -52,6 +52,7 @@
|
||||||
(.buildAllPlatforms "json")
|
(.buildAllPlatforms "json")
|
||||||
(.catch js/console.error)
|
(.catch js/console.error)
|
||||||
(.then (fn [^js resp]
|
(.then (fn [^js resp]
|
||||||
|
(js/console.log "resp" resp)
|
||||||
(let [performance-end (js/performance.now)
|
(let [performance-end (js/performance.now)
|
||||||
duration-ms (- performance-end performance-start)
|
duration-ms (- performance-end performance-start)
|
||||||
resolved-tokens (.-allTokens resp)]
|
resolved-tokens (.-allTokens resp)]
|
||||||
|
@ -82,8 +83,11 @@
|
||||||
(let [identifier (if names-map?
|
(let [identifier (if names-map?
|
||||||
(.. cur -original -name)
|
(.. cur -original -name)
|
||||||
(uuid (.-uuid (.-id cur))))
|
(uuid (.-uuid (.-id cur))))
|
||||||
origin-token (get tokens identifier)
|
{:keys [type] :as origin-token} (get tokens identifier)
|
||||||
parsed-value (wtt/parse-token-value (.-value cur))
|
value (.-value cur)
|
||||||
|
parsed-value (case type
|
||||||
|
:color (wtt/parse-token-color-value value)
|
||||||
|
(wtt/parse-token-value value))
|
||||||
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])
|
||||||
(assoc origin-token
|
(assoc origin-token
|
||||||
|
@ -132,6 +136,7 @@
|
||||||
|
|
||||||
(defn use-resolved-workspace-tokens [& {:as config}]
|
(defn use-resolved-workspace-tokens [& {:as config}]
|
||||||
(-> (mf/deref refs/workspace-selected-token-set-tokens)
|
(-> (mf/deref refs/workspace-selected-token-set-tokens)
|
||||||
|
(doto js/console.log)
|
||||||
(use-resolved-tokens config)))
|
(use-resolved-tokens config)))
|
||||||
|
|
||||||
(defn use-active-theme-sets-tokens [& {:as config}]
|
(defn use-active-theme-sets-tokens [& {:as config}]
|
||||||
|
|
|
@ -12,6 +12,15 @@
|
||||||
[token-id state]
|
[token-id state]
|
||||||
(get-in state [:workspace-data :tokens token-id]))
|
(get-in state [:workspace-data :tokens token-id]))
|
||||||
|
|
||||||
|
(def parseable-token-color-value-regexp
|
||||||
|
"Regexp that can be used to parse a hex value out of resolved token value.
|
||||||
|
This regexp also trims whitespace around the value."
|
||||||
|
#"^\s*#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})\s*$")
|
||||||
|
|
||||||
|
(defn parse-token-color-value [value]
|
||||||
|
(when-let [[_ hex] (re-find parseable-token-color-value-regexp value)]
|
||||||
|
{:value hex :unit :hex}))
|
||||||
|
|
||||||
(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."
|
||||||
|
|
|
@ -20,6 +20,14 @@
|
||||||
:modal {:key :tokens/border-radius
|
:modal {:key :tokens/border-radius
|
||||||
:fields [{:label "Border Radius"
|
:fields [{:label "Border Radius"
|
||||||
:key :border-radius}]}}
|
:key :border-radius}]}}
|
||||||
|
|
||||||
|
:color
|
||||||
|
{:title "Color"
|
||||||
|
:attributes ctt/color-keys
|
||||||
|
:on-update-shape wtch/update-color
|
||||||
|
:modal {:key :tokens/color
|
||||||
|
:fields [{:label "Color" :key :color}]}}
|
||||||
|
|
||||||
:stroke-width
|
:stroke-width
|
||||||
{:title "Stroke Width"
|
{:title "Stroke Width"
|
||||||
:attributes ctt/stroke-width-keys
|
:attributes ctt/stroke-width-keys
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
(def attributes->shape-update
|
(def attributes->shape-update
|
||||||
{#{:rx :ry} (fn [v ids _] (wtch/update-shape-radius-all v ids))
|
{#{:rx :ry} (fn [v ids _] (wtch/update-shape-radius-all v ids))
|
||||||
#{:r1 :r2 :r3 :r4} wtch/update-shape-radius-single-corner
|
#{:r1 :r2 :r3 :r4} wtch/update-shape-radius-single-corner
|
||||||
|
ctt/color-keys wtch/update-color
|
||||||
ctt/stroke-width-keys wtch/update-stroke-width
|
ctt/stroke-width-keys wtch/update-stroke-width
|
||||||
ctt/sizing-keys wtch/update-shape-dimensions
|
ctt/sizing-keys wtch/update-shape-dimensions
|
||||||
ctt/opacity-keys wtch/update-opacity
|
ctt/opacity-keys wtch/update-opacity
|
||||||
|
|
Loading…
Add table
Reference in a new issue