mirror of
https://github.com/penpot/penpot.git
synced 2025-01-07 15:39:42 -05:00
Split path/selector for disallowing creating tokens at path segments
This commit is contained in:
parent
a98f59469e
commit
4a85ef3608
2 changed files with 32 additions and 15 deletions
|
@ -9,6 +9,18 @@
|
||||||
[token-name]
|
[token-name]
|
||||||
(str/split token-name #"\.+"))
|
(str/split token-name #"\.+"))
|
||||||
|
|
||||||
|
(defn token-name->path-selector
|
||||||
|
"Splits token-name into map with `:path` and `:selector` using `token-name->path`.
|
||||||
|
|
||||||
|
`:selector` is the last item of the names path
|
||||||
|
`:path` is everything leading up the the `:selector`."
|
||||||
|
[token-name]
|
||||||
|
(let [path-segments (token-name->path token-name)
|
||||||
|
last-idx (dec (count path-segments))
|
||||||
|
[path [selector]] (split-at last-idx path-segments)]
|
||||||
|
{:path (seq path)
|
||||||
|
:selector selector}))
|
||||||
|
|
||||||
(defn token-names-tree
|
(defn token-names-tree
|
||||||
"Convert tokens into a nested tree with their `:name` as the path."
|
"Convert tokens into a nested tree with their `:name` as the path."
|
||||||
[tokens]
|
[tokens]
|
||||||
|
@ -31,18 +43,22 @@
|
||||||
|
|
||||||
{\"foo\" {:name \"other\"}}"
|
{\"foo\" {:name \"other\"}}"
|
||||||
[token-name token-names-tree]
|
[token-name token-names-tree]
|
||||||
(let [name-path (token-name->path token-name)
|
(let [{:keys [path selector]} (token-name->path-selector token-name)
|
||||||
result (reduce
|
path-target (reduce
|
||||||
(fn [acc cur]
|
(fn [acc cur]
|
||||||
(let [target (get acc cur)]
|
(let [target (get acc cur)]
|
||||||
(cond
|
(prn target cur)
|
||||||
;; Path segment doesn't exist yet
|
(cond
|
||||||
(nil? target) (reduced false)
|
;; Path segment doesn't exist yet
|
||||||
;; A token exists at this path
|
(nil? target) (reduced false)
|
||||||
(:name target) (reduced true)
|
;; A token exists at this path
|
||||||
;; Continue traversing the true
|
(:name target) (reduced true)
|
||||||
:else target)))
|
;; Continue traversing the true
|
||||||
token-names-tree name-path)]
|
:else target)))
|
||||||
(if (map? result)
|
token-names-tree path)]
|
||||||
(some? (:name result))
|
(cond
|
||||||
result)))
|
(boolean? path-target) path-target
|
||||||
|
(get path-target :name) true
|
||||||
|
:else (-> (get path-target selector)
|
||||||
|
(seq)
|
||||||
|
(boolean)))))
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
:value "{foo.bar.baz}"}}))))
|
:value "{foo.bar.baz}"}}))))
|
||||||
|
|
||||||
(t/deftest token-name-path-exists?-test
|
(t/deftest token-name-path-exists?-test
|
||||||
|
(t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {"sm" {:name "sm"}}})))
|
||||||
(t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {:name "sm"}})))
|
(t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {:name "sm"}})))
|
||||||
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm" {"border-radius" {:name "sm"}})))
|
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm" {"border-radius" {:name "sm"}})))
|
||||||
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm.x" {"border-radius" {:name "sm"}})))
|
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm.x" {"border-radius" {:name "sm"}})))
|
||||||
|
|
Loading…
Reference in a new issue