0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

fix token update

This commit is contained in:
Andrés Moya 2024-09-03 14:20:55 +02:00
parent d147d844fb
commit 0dca047339
6 changed files with 104 additions and 30 deletions

View file

@ -564,6 +564,41 @@
new-elems
(remove p? after))))
(defn addm-at-index
"Insert an element in an ordered map at an arbitrary index"
[coll index key element]
(assert (ordered-map? coll))
(-> (ordered-map)
(into (take index coll))
(assoc key element)
(into (drop index coll))))
(defn insertm-at-index
"Insert a map {k v} of elements in an ordered map at an arbitrary index"
[coll index new-elems]
(assert (ordered-map? coll))
(-> (ordered-map)
(into (take index coll))
(into new-elems)
(into (drop index coll))))
(defn adds-at-index
"Insert an element in an ordered set at an arbitrary index"
[coll index element]
(assert (ordered-set? coll))
(-> (ordered-set)
(into (take index coll))
(conj element)
(into (drop index coll))))
(defn inserts-at-index
"Insert a list of elements in an ordered set at an arbitrary index"
[coll index new-elems]
(assert (ordered-set? coll))
(-> (ordered-set)
(into (take index coll))
(into new-elems)
(into (drop index coll))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Parsing / Conversion

View file

@ -312,6 +312,7 @@
[:set-id ::sm/uuid]
[:set-name :string]
[:id ::sm/uuid]
[:name :string]
[:token ::cto/token]]]
[:del-token
@ -798,15 +799,15 @@
(ctob/add-token-in-set set-name (ctob/make-token token))))))
(defmethod process-change :mod-token
[data {:keys [set-id set-name id token]}]
[data {:keys [set-name id name token]}]
(-> data
(ctol/update-token data set-id id merge token)
(ctol/update-token id merge token)
(update :tokens-lib
#(-> %
(ctob/ensure-tokens-lib)
(ctob/update-token-in-set
set-name
(:name token)
name
(fn [old-token]
(ctob/make-token (merge old-token token))))))))

View file

@ -765,10 +765,10 @@
(apply-changes-local)))
(defn update-token
[changes set-id set-name {:keys [id] :as token} prev-token]
[changes set-id set-name {:keys [id name] :as token} {prev-name :name :as prev-token}]
(-> changes
(update :redo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :token token})
(update :undo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :token (or prev-token token)})
(update :redo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :name prev-name :token token})
(update :undo-changes conj {:type :mod-token :set-id set-id :set-name set-name :id id :name name :token (or prev-token token)})
(apply-changes-local)))
(defn delete-token

View file

@ -75,13 +75,13 @@
(TokenSet. name
description
(dt/now)
(cond-> tokens
(not= (:name token) (:name token'))
(dissoc (:name token))
:always ; TODO: if token is renamed,
(assoc (:name token') token')))) ; it sould remain in
this)) ; the same position
(if (= (:name token) (:name token'))
(assoc tokens (:name token') token')
(let [index (d/index-of (keys tokens) (:name token))]
(-> tokens
(dissoc (:name token))
(d/addm-at-index index (:name token') token'))))))
this))
(delete-token [_ token-name]
(TokenSet. name
@ -180,13 +180,13 @@
(let [set' (-> (make-token-set (f set))
(assoc :modified-at (dt/now)))]
(check-token-set! set')
(TokensLib. (cond-> sets
(not= (:name set) (:name set'))
(dissoc (:name set))
:always ; TODO: if set is renamed,
(assoc (:name set') set')) ; it sould remain in
themes)) ; the same position
(TokensLib. (if (= (:name set) (:name set'))
(assoc sets (:name set') set')
(let [index (d/index-of (keys sets) (:name set))]
(-> sets
(dissoc (:name set))
(d/addm-at-index index (:name set') set'))))
themes))
this))
(delete-set [_ set-name]
@ -220,7 +220,7 @@
#(update-token % token-name f))
themes)
this))
(delete-token-from-set [this set-name token-name]
(if (contains? sets set-name)
(TokensLib. (update sets set-name

View file

@ -6,6 +6,7 @@
(ns common-tests.types.tokens-lib-test
(:require
[app.common.data :as d]
[app.common.fressian :as fres]
[app.common.time :as dt]
[app.common.transit :as tr]
@ -143,18 +144,21 @@
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
(ctob/add-token-in-set "test-token-set"
(ctob/make-token :name "test-token"
(ctob/make-token :name "test-token-1"
:type :boolean
:value true))
(ctob/add-token-in-set "test-token-set"
(ctob/make-token :name "test-token-2"
:type :boolean
:value true)))
tokens-lib' (-> tokens-lib
(ctob/update-token-in-set "test-token-set" "test-token"
(ctob/update-token-in-set "test-token-set" "test-token-1"
(fn [token]
(assoc token
:name "updated-name"
:description "some description"
:value false)))
(ctob/update-token-in-set "not-existing-set" "test-token"
(ctob/update-token-in-set "not-existing-set" "test-token-1"
(fn [token]
(assoc token
:name "no-effect")))
@ -165,14 +169,47 @@
token-set (ctob/get-set tokens-lib "test-token-set")
token-set' (ctob/get-set tokens-lib' "test-token-set")
token (get-in token-set [:tokens "test-token"])
token (get-in token-set [:tokens "test-token-1"])
token' (get-in token-set' [:tokens "test-token-1"])]
(t/is (= (ctob/set-count tokens-lib') 1))
(t/is (= (count (:tokens token-set')) 2))
(t/is (= (d/index-of (keys (:tokens token-set')) "test-token-1") 0))
(t/is (= (:name token') "test-token-1"))
(t/is (= (:description token') "some description"))
(t/is (= (:value token') false))
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))
(t/is (dt/is-after? (:modified-at token') (:modified-at token)))))
(t/deftest rename-token
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
(ctob/add-token-in-set "test-token-set"
(ctob/make-token :name "test-token-1"
:type :boolean
:value true))
(ctob/add-token-in-set "test-token-set"
(ctob/make-token :name "test-token-2"
:type :boolean
:value true)))
tokens-lib' (-> tokens-lib
(ctob/update-token-in-set "test-token-set" "test-token-1"
(fn [token]
(assoc token
:name "updated-name"))))
token-set (ctob/get-set tokens-lib "test-token-set")
token-set' (ctob/get-set tokens-lib' "test-token-set")
token (get-in token-set [:tokens "test-token-1"])
token' (get-in token-set' [:tokens "updated-name"])]
(t/is (= (ctob/set-count tokens-lib') 1))
(t/is (= (count (:tokens token-set')) 1))
(t/is (= (count (:tokens token-set')) 2))
(t/is (= (d/index-of (keys (:tokens token-set')) "updated-name") 0))
(t/is (= (:name token') "updated-name"))
(t/is (= (:description token') "some description"))
(t/is (= (:value token') false))
(t/is (= (:description token') nil))
(t/is (= (:value token') true))
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))
(t/is (dt/is-after? (:modified-at token') (:modified-at token)))))

View file

@ -234,7 +234,8 @@
create-set?
(pcb/add-token-set token-set))
prev-token (d/seek #(= (:id %) (:id token)) (:tokens token-set)) ; TODO
prev-token-id (d/seek #(= % (:id token)) (:tokens token-set))
prev-token (get-token-data-from-token-id prev-token-id)
create-token? (not prev-token)
changes (if create-token?