mirror of
https://github.com/penpot/penpot.git
synced 2025-06-16 20:31:37 +02:00
Extract token validation
This commit is contained in:
parent
6e9623153c
commit
eb123bf8ef
1 changed files with 30 additions and 24 deletions
|
@ -58,6 +58,29 @@
|
||||||
|
|
||||||
;; Component -------------------------------------------------------------------
|
;; Component -------------------------------------------------------------------
|
||||||
|
|
||||||
|
(defn validate-token-value+ [{:keys [input name-value token tokens]}]
|
||||||
|
(let [token-references (sd/find-token-references input)
|
||||||
|
;; When creating a new token we dont have a token name yet,
|
||||||
|
;; so we use a temporary token name that hopefully doesn't clash with any of the users token names.
|
||||||
|
token-name (if (str/empty? name-value) "__TOKEN_STUDIO_SYSTEM.TEMP" name-value)
|
||||||
|
direct-self-reference? (get token-references token-name)
|
||||||
|
empty-input? (empty? (str/trim input))]
|
||||||
|
(cond
|
||||||
|
empty-input? (p/rejected nil)
|
||||||
|
direct-self-reference? (p/rejected :error/token-direct-self-reference)
|
||||||
|
:else (let [token-id (or (:id token) (random-uuid))
|
||||||
|
new-tokens (update tokens token-id merge {:id token-id
|
||||||
|
:value input
|
||||||
|
:name token-name})]
|
||||||
|
(-> (sd/resolve-tokens+ new-tokens)
|
||||||
|
(p/then
|
||||||
|
(fn [resolved-tokens]
|
||||||
|
(let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-id)]
|
||||||
|
(cond
|
||||||
|
resolved-value (p/resolved resolved-token)
|
||||||
|
(= #{:style-dictionary/missing-reference} errors) (p/rejected :error/token-missing-reference)
|
||||||
|
:else (p/rejected :error/unknown-error))))))))))
|
||||||
|
|
||||||
(defn use-debonced-resolve-callback
|
(defn use-debonced-resolve-callback
|
||||||
[name-ref token tokens callback & {:keys [timeout] :or {timeout 160}}]
|
[name-ref token tokens callback & {:keys [timeout] :or {timeout 160}}]
|
||||||
(let [timeout-id-ref (mf/use-ref nil)
|
(let [timeout-id-ref (mf/use-ref nil)
|
||||||
|
@ -73,30 +96,13 @@
|
||||||
(js/setTimeout
|
(js/setTimeout
|
||||||
(fn []
|
(fn []
|
||||||
(when (not (timeout-outdated-cb?))
|
(when (not (timeout-outdated-cb?))
|
||||||
(let [token-references (sd/find-token-references input)
|
(-> (validate-token-value+ {:input input
|
||||||
;; When creating a new token we dont have a token name yet,
|
:name-value @name-ref
|
||||||
;; so we use a temporary token name that hopefully doesn't clash with any of the users token names.
|
:token token
|
||||||
token-name (if (empty? @name-ref) "__TOKEN_STUDIO_SYSTEM.TEMP" @name-ref)
|
:tokens tokens})
|
||||||
direct-self-reference? (get token-references token-name)
|
(p/finally (fn [x err]
|
||||||
empty-input? (empty? (str/trim input))]
|
|
||||||
(cond
|
|
||||||
empty-input? (callback nil)
|
|
||||||
direct-self-reference? (callback :error/token-direct-self-reference)
|
|
||||||
:else
|
|
||||||
(let [token-id (or (:id token) (random-uuid))
|
|
||||||
new-tokens (update tokens token-id merge {:id token-id
|
|
||||||
:value input
|
|
||||||
:name token-name})]
|
|
||||||
(-> (sd/resolve-tokens+ new-tokens)
|
|
||||||
(p/finally
|
|
||||||
(fn [resolved-tokens _err]
|
|
||||||
(when-not (timeout-outdated-cb?)
|
(when-not (timeout-outdated-cb?)
|
||||||
(let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-id)]
|
(callback (or err x))))))))
|
||||||
(cond
|
|
||||||
resolved-value (callback resolved-token)
|
|
||||||
(= #{:style-dictionary/missing-reference} errors) (callback :error/token-missing-reference)
|
|
||||||
:else (callback :error/unknown-error))))))))))))
|
|
||||||
|
|
||||||
timeout))))]
|
timeout))))]
|
||||||
debounced-resolver-callback))
|
debounced-resolver-callback))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue