mirror of
https://github.com/penpot/penpot.git
synced 2026-02-02 10:41:56 -05:00
Compare commits
1 Commits
develop
...
xaviju-132
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14d173c5b0 |
@@ -6,6 +6,7 @@
|
||||
|
||||
(ns app.main.data.workspace.tokens.library-edit
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.files.helpers :as cfh]
|
||||
@@ -27,6 +28,7 @@
|
||||
[potok.v2.core :as ptk]))
|
||||
|
||||
(declare set-selected-token-set-id)
|
||||
(declare toggle-token-path)
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; TOKENS Getters
|
||||
@@ -62,6 +64,41 @@
|
||||
(watch [_ _ _]
|
||||
(rx/of (dwsh/update-shapes [id] #(merge % attrs)))))))
|
||||
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Toggle tree nodes
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(defn clean-tokens-paths
|
||||
[]
|
||||
|
||||
(ptk/reify ::clean-tokens-paths
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(assoc-in state [:workspace-tokens :unfolded-token-paths] []))))
|
||||
|
||||
(defn toggle-token-path
|
||||
[path]
|
||||
(ptk/reify ::toggle-token-path
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(update-in state [:workspace-tokens :unfolded-token-paths]
|
||||
(fn [paths]
|
||||
(let [paths (or paths [])]
|
||||
(if (some #(= % path) paths)
|
||||
(vec (remove #(str/starts-with? % (str path))
|
||||
paths))
|
||||
(let [split-path (cpn/split-path path :separator ".")
|
||||
partial-paths (reduce
|
||||
(fn [acc segment]
|
||||
(let [new-acc (if (empty? acc)
|
||||
segment
|
||||
(str (last acc) "." segment))]
|
||||
(conj acc new-acc)))
|
||||
[]
|
||||
split-path)]
|
||||
(vec (distinct (into paths partial-paths)))))))))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; TOKENS Actions
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
@@ -439,11 +476,14 @@
|
||||
token (-> (get-tokens-lib state)
|
||||
(ctob/get-token (ctob/get-id token-set) token-id))
|
||||
token-type (:type token)
|
||||
token-name (:name token)
|
||||
token-path (str (d/name token-type) "." token-name)
|
||||
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-library-data data)
|
||||
(pcb/set-token set-id token-id nil))]
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(toggle-token-path token-path)
|
||||
(ptk/data-event ::ev/event {::ev/name "delete-token" :type token-type}))))))
|
||||
|
||||
(defn bulk-delete-tokens
|
||||
@@ -486,35 +526,7 @@
|
||||
;; TOKEN UI OPS
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(defn clean-tokens-paths
|
||||
[]
|
||||
(ptk/reify ::clean-tokens-paths
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(assoc-in state [:workspace-tokens :unfolded-token-paths] []))))
|
||||
|
||||
(defn toggle-token-path
|
||||
[path]
|
||||
(ptk/reify ::toggle-token-path
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(update-in state [:workspace-tokens :unfolded-token-paths]
|
||||
(fn [paths]
|
||||
(let [paths (or paths [])]
|
||||
(if (some #(= % path) paths)
|
||||
(vec (remove #(or (= % path)
|
||||
(str/starts-with? % (str path ".")))
|
||||
paths))
|
||||
(let [split-path (cpn/split-path path :separator ".")
|
||||
partial-paths (reduce
|
||||
(fn [acc segment]
|
||||
(let [new-acc (if (empty? acc)
|
||||
segment
|
||||
(str (last acc) "." segment))]
|
||||
(conj acc new-acc)))
|
||||
[]
|
||||
split-path)]
|
||||
(into paths partial-paths)))))))))
|
||||
|
||||
(defn assign-token-context-menu
|
||||
[{:keys [position] :as params}]
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
tokens
|
||||
(sd/use-resolved-tokens* tokens)
|
||||
|
||||
;; Group tokens by their type
|
||||
tokens-by-type
|
||||
(mf/with-memo [tokens selected-token-set-tokens]
|
||||
(let [tokens (reduce-kv (fn [tokens k _]
|
||||
@@ -139,12 +140,46 @@
|
||||
(let [[_ token-value] token]
|
||||
(:id token-value)))))))
|
||||
|
||||
remaining-tokens-of-type?
|
||||
(mf/use-fn
|
||||
(fn [tokens-by-type tokens-in-path-ids]
|
||||
(let [token-ids (set tokens-in-path-ids)
|
||||
remaining-tokens (filter (fn [token]
|
||||
(not (contains? token-ids (:id token))))
|
||||
tokens-by-type)]
|
||||
(seq remaining-tokens))))
|
||||
|
||||
delete-token
|
||||
(mf/with-memo [tokens selected-token-set-id]
|
||||
(fn [token]
|
||||
(let [id (:id token)
|
||||
type (:type token)
|
||||
path (:name token)
|
||||
tokens-by-type (ctob/group-by-type tokens)
|
||||
tokens-filtered-by-type (get tokens-by-type type)
|
||||
tokens-in-path-ids (filter-tokens-by-path-ids type path)
|
||||
remaining-tokens? (remaining-tokens-of-type? tokens-filtered-by-type tokens-in-path-ids)]
|
||||
;; Delete the token
|
||||
(st/emit! (dwtl/delete-token selected-token-set-id id))
|
||||
;; Remove from unfolded tree path
|
||||
(if remaining-tokens?
|
||||
(st/emit! (dwtl/toggle-token-path (str (name type) "." path)))
|
||||
(st/emit! (dwtl/toggle-token-path (name type)))))))
|
||||
|
||||
delete-node
|
||||
(mf/with-memo [tokens selected-token-set-id]
|
||||
(fn [node type]
|
||||
(let [path (:path node)
|
||||
tokens-in-path-ids (filter-tokens-by-path-ids type path)]
|
||||
(st/emit! (dwtl/bulk-delete-tokens selected-token-set-id tokens-in-path-ids)))))]
|
||||
tokens-by-type (ctob/group-by-type tokens)
|
||||
tokens-filtered-by-type (get tokens-by-type type)
|
||||
tokens-in-path-ids (filter-tokens-by-path-ids type path)
|
||||
remaining-tokens? (remaining-tokens-of-type? tokens-filtered-by-type tokens-in-path-ids)]
|
||||
;; Delete tokens in path
|
||||
(st/emit! (dwtl/bulk-delete-tokens selected-token-set-id tokens-in-path-ids))
|
||||
;; Remove from unfolded tree path
|
||||
(if remaining-tokens?
|
||||
(st/emit! (dwtl/toggle-token-path (str (name type) "." path)))
|
||||
(st/emit! (dwtl/toggle-token-path (name type)))))))]
|
||||
|
||||
(mf/with-effect [tokens-lib selected-token-set-id]
|
||||
(when (and tokens-lib
|
||||
@@ -157,7 +192,7 @@
|
||||
(st/emit! (dwtl/set-selected-token-set-id (ctob/get-id match)))))))
|
||||
|
||||
[:*
|
||||
[:& token-context-menu]
|
||||
[:& token-context-menu {:on-delete-token delete-token}]
|
||||
[:> token-node-context-menu* {:on-delete-node delete-node}]
|
||||
|
||||
[:> selected-set-info* {:tokens-lib tokens-lib
|
||||
|
||||
@@ -316,8 +316,9 @@
|
||||
(generic-attribute-actions #{:y} "Y" (assoc context-data :on-update-shape dwta/update-shape-position)))
|
||||
(clean-separators)))}))
|
||||
|
||||
(defn default-actions [{:keys [token selected-token-set-id]}]
|
||||
(let [{:keys [modal]} (dwta/get-token-properties token)]
|
||||
(defn default-actions [{:keys [token selected-token-set-id on-delete-token]}]
|
||||
(let [{:keys [modal]} (dwta/get-token-properties token)
|
||||
on-duplicate-token #(st/emit! (dwtl/duplicate-token (:id token)))]
|
||||
[{:title (tr "workspace.tokens.edit")
|
||||
:no-selectable true
|
||||
:action (fn [event]
|
||||
@@ -333,12 +334,10 @@
|
||||
:token token}))))}
|
||||
{:title (tr "workspace.tokens.duplicate")
|
||||
:no-selectable true
|
||||
:action #(st/emit! (dwtl/duplicate-token (:id token)))}
|
||||
:action on-duplicate-token}
|
||||
{:title (tr "workspace.tokens.delete")
|
||||
:no-selectable true
|
||||
:action #(st/emit! (dwtl/delete-token
|
||||
selected-token-set-id
|
||||
(:id token)))}]))
|
||||
:action #(on-delete-token token)}]))
|
||||
|
||||
(defn- allowed-shape-attributes [shapes]
|
||||
(reduce into #{} (map #(ctt/shape-type->attributes (:type %) (:layout %)) shapes)))
|
||||
@@ -464,7 +463,7 @@
|
||||
:selected? selected?}])])))
|
||||
|
||||
(mf/defc token-context-menu-tree
|
||||
[{:keys [width errors] :as mdata}]
|
||||
[{:keys [width errors on-delete-token] :as mdata}]
|
||||
(let [objects (mf/deref refs/workspace-page-objects)
|
||||
selected (mf/deref refs/selected-shapes)
|
||||
|
||||
@@ -488,10 +487,11 @@
|
||||
:errors errors
|
||||
:selected-token-set-id selected-token-set-id
|
||||
:selected-shapes selected-shapes
|
||||
:is-selected-inside-layout is-selected-inside-layout}]]))
|
||||
:is-selected-inside-layout is-selected-inside-layout
|
||||
:on-delete-token on-delete-token}]]))
|
||||
|
||||
(mf/defc token-context-menu
|
||||
[]
|
||||
[{:keys [on-delete-token]}]
|
||||
(let [mdata (mf/deref tokens-menu-ref)
|
||||
is-open? (boolean mdata)
|
||||
width (mf/use-state 0)
|
||||
@@ -538,5 +538,5 @@
|
||||
:left (dm/str left "px")}
|
||||
:on-context-menu prevent-default}
|
||||
(when mdata
|
||||
[:& token-context-menu-tree (assoc mdata :width @width)])]])
|
||||
[:& token-context-menu-tree (assoc mdata :width @width :on-delete-token on-delete-token)])]])
|
||||
(dom/get-body)))))
|
||||
|
||||
Reference in New Issue
Block a user