Remove token when applying tyopgraphic asset style

This commit is contained in:
Florian Schroedl 2025-07-24 15:38:32 +02:00 committed by Andrés Moya
parent 57330f53e2
commit 4189d01844
2 changed files with 31 additions and 46 deletions

View File

@ -21,9 +21,12 @@
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[clojure.set :as set])) [clojure.set :as set]))
(def text-typography-attrs (set ctt/text-typography-attrs))
(defn- generate-unapply-tokens (defn- generate-unapply-tokens
"When updating attributes that have a token applied, we must unapply it, because the value "When updating attributes that have a token applied, we must unapply it, because the value
of the attribute now has been given directly, and does not come from the token." of the attribute now has been given directly, and does not come from the token.
When applying a typography asset style we also unapply any typographic tokens."
[changes objects changed-sub-attr] [changes objects changed-sub-attr]
(let [new-objects (pcb/get-objects changes) (let [new-objects (pcb/get-objects changes)
mod-obj-changes (->> (:redo-changes changes) mod-obj-changes (->> (:redo-changes changes)
@ -32,7 +35,11 @@
text-changed-attrs text-changed-attrs
(fn [shape] (fn [shape]
(let [new-shape (get new-objects (:id shape)) (let [new-shape (get new-objects (:id shape))
attrs (ctt/get-diff-attrs (:content shape) (:content new-shape))] attrs (ctt/get-diff-attrs (:content shape) (:content new-shape))
;; Unapply token when applying typography asset style
attrs (if (set/intersection text-typography-attrs attrs)
(into attrs cto/typography-keys)
attrs)]
(apply set/union (map cto/shape-attr->token-attrs attrs)))) (apply set/union (map cto/shape-attr->token-attrs attrs))))
check-attr (fn [shape changes attr] check-attr (fn [shape changes attr]

View File

@ -252,37 +252,31 @@
(dwsl/update-layout-child shape-ids props {:ignore-touched true (dwsl/update-layout-child shape-ids props {:ignore-touched true
:page-id page-id})))))))) :page-id page-id}))))))))
(defn generate-text-shape-update
[txt-attrs shape-ids page-id]
(let [update-node? (fn [node]
(or (txt/is-text-node? node)
(txt/is-paragraph-node? node)))
update-fn (fn [node _]
(-> node
(d/txt-merge txt-attrs)
(cty/remove-typography-from-node)))]
(dwsh/update-shapes shape-ids
#(txt/update-text-content % update-node? update-fn nil)
{:ignore-touched true
:page-id page-id})))
(defn update-line-height (defn update-line-height
([value shape-ids attributes] (update-line-height value shape-ids attributes nil)) ([value shape-ids attributes] (update-line-height value shape-ids attributes nil))
([value shape-ids _attributes page-id] ([value shape-ids _attributes page-id]
(let [update-node? (fn [node] (when (number? value)
(or (txt/is-text-node? node) (generate-text-shape-update {:line-height value} shape-ids page-id))))
(txt/is-paragraph-node? node)))
update-fn (fn [node _]
(-> node
(d/txt-merge {:line-height value})
(cty/remove-typography-from-node)))]
(when (number? value)
(dwsh/update-shapes shape-ids
#(txt/update-text-content % update-node? update-fn nil)
{:ignore-touched true
:page-id page-id})))))
(defn update-letter-spacing (defn update-letter-spacing
([value shape-ids attributes] (update-letter-spacing value shape-ids attributes nil)) ([value shape-ids attributes] (update-letter-spacing value shape-ids attributes nil))
([value shape-ids _attributes page-id] ([value shape-ids _attributes page-id]
(let [update-node? (fn [node] (when (number? value)
(or (txt/is-text-node? node) (generate-text-shape-update {:letter-spacing (str value)} shape-ids page-id))))
(txt/is-paragraph-node? node)))
update-fn (fn [node _]
(-> node
(d/txt-merge {:letter-spacing (str value)})
(cty/remove-typography-from-node)))]
(when (number? value)
(dwsh/update-shapes shape-ids
#(txt/update-text-content % update-node? update-fn nil)
{:ignore-touched true
:page-id page-id})))))
(defn update-font-family (defn update-font-family
([value shape-ids attributes] (update-font-family value shape-ids attributes nil)) ([value shape-ids attributes] (update-font-family value shape-ids attributes nil))
@ -294,31 +288,15 @@
{:font-id (:id font) {:font-id (:id font)
:font-family (:family font)} :font-family (:family font)}
{:font-id (str uuid/zero) {:font-id (str uuid/zero)
:font-family font-family}) :font-family font-family})]
update-node? (fn [node]
(or (txt/is-text-node? node)
(txt/is-paragraph-node? node)))]
(when text-attrs (when text-attrs
(dwsh/update-shapes shape-ids (generate-text-shape-update text-attrs shape-ids page-id)))))
#(txt/update-text-content % update-node? d/txt-merge text-attrs)
{:ignore-touched true
:page-id page-id})))))
(defn update-font-size (defn update-font-size
([value shape-ids attributes] (update-font-size value shape-ids attributes nil)) ([value shape-ids attributes] (update-font-size value shape-ids attributes nil))
([value shape-ids _attributes page-id] ([value shape-ids _attributes page-id]
(let [update-node? (fn [node] (when (number? value)
(or (txt/is-text-node? node) (generate-text-shape-update {:font-size (str value)} shape-ids page-id))))
(txt/is-paragraph-node? node)))
update-fn (fn [node _]
(-> node
(d/txt-merge {:font-size (str value)})
(cty/remove-typography-from-node)))]
(when (number? value)
(dwsh/update-shapes shape-ids
#(txt/update-text-content % update-node? update-fn nil)
{:ignore-touched true
:page-id page-id})))))
;; Events to apply / unapply tokens to shapes ------------------------------------------------------------ ;; Events to apply / unapply tokens to shapes ------------------------------------------------------------