🐛 Fix rotation token highlight and its application on the text-editor-v2

This commit is contained in:
Elena Torro 2026-02-26 09:38:35 +01:00
parent 75b1c0c1b1
commit 495371c079
3 changed files with 66 additions and 55 deletions

View File

@ -620,61 +620,68 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
;; We do not allow to apply tokens while text editor is open. ;; We do not allow to apply tokens while text editor is open.
(when (empty? (get state :workspace-editor-state)) ;; The classic text editor sets :workspace-editor-state; the WASM text editor
(let [attributes-to-remove ;; does not, so we also check :workspace-local :edition for text shapes.
;; Remove atomic typography tokens when applying composite and vice-verca (let [edition (get-in state [:workspace-local :edition])
(cond objects (dsh/lookup-page-objects state)
(ctt/typography-token-keys (:type token)) (set/union attributes-to-remove ctt/typography-keys) text-editing? (and (some? edition)
(ctt/typography-keys (:type token)) (set/union attributes-to-remove ctt/typography-token-keys) (= :text (:type (get objects edition))))]
:else attributes-to-remove)] (when (and (empty? (get state :workspace-editor-state))
(when-let [tokens (some-> (dsh/lookup-file-data state) (not text-editing?))
(get :tokens-lib) (let [attributes-to-remove
(ctob/get-tokens-in-active-sets))] ;; Remove atomic typography tokens when applying composite and vice-verca
(->> (if (contains? cf/flags :tokenscript) (cond
(rx/of (ts/resolve-tokens tokens)) (ctt/typography-token-keys (:type token)) (set/union attributes-to-remove ctt/typography-keys)
(sd/resolve-tokens tokens)) (ctt/typography-keys (:type token)) (set/union attributes-to-remove ctt/typography-token-keys)
(rx/mapcat :else attributes-to-remove)]
(fn [resolved-tokens] (when-let [tokens (some-> (dsh/lookup-file-data state)
(let [undo-id (js/Symbol) (get :tokens-lib)
objects (dsh/lookup-page-objects state) (ctob/get-tokens-in-active-sets))]
selected-shapes (select-keys objects shape-ids) (->> (if (contains? cf/flags :tokenscript)
(rx/of (ts/resolve-tokens tokens))
(sd/resolve-tokens tokens))
(rx/mapcat
(fn [resolved-tokens]
(let [undo-id (js/Symbol)
objects (dsh/lookup-page-objects state)
selected-shapes (select-keys objects shape-ids)
shapes (->> selected-shapes shapes (->> selected-shapes
(filter (fn [[_ shape]] (filter (fn [[_ shape]]
(or (or
(and (ctsl/any-layout-immediate-child? objects shape) (and (ctsl/any-layout-immediate-child? objects shape)
(some ctt/spacing-margin-keys attributes)) (some ctt/spacing-margin-keys attributes))
(and (ctt/any-appliable-attr-for-shape? attributes (:type shape) (:layout shape)) (and (ctt/any-appliable-attr-for-shape? attributes (:type shape) (:layout shape))
(all-attrs-appliable-for-token? attributes (:type token))))))) (all-attrs-appliable-for-token? attributes (:type token)))))))
shape-ids (d/nilv (keys shapes) []) shape-ids (d/nilv (keys shapes) [])
any-variant? (->> shapes vals (some ctk/is-variant?) boolean) any-variant? (->> shapes vals (some ctk/is-variant?) boolean)
resolved-value (get-in resolved-tokens [(cfo/token-identifier token) :resolved-value]) resolved-value (get-in resolved-tokens [(cfo/token-identifier token) :resolved-value])
resolved-value (if (contains? cf/flags :tokenscript) resolved-value (if (contains? cf/flags :tokenscript)
(ts/tokenscript-symbols->penpot-unit resolved-value) (ts/tokenscript-symbols->penpot-unit resolved-value)
resolved-value) resolved-value)
tokenized-attributes (cfo/attributes-map attributes token) tokenized-attributes (cfo/attributes-map attributes token)
type (:type token)] type (:type token)]
(rx/concat (rx/concat
(rx/of (rx/of
(st/emit! (ev/event {::ev/name "apply-tokens" (st/emit! (ev/event {::ev/name "apply-tokens"
:type type :type type
:applied-to attributes :applied-to attributes
:applied-to-variant any-variant?})) :applied-to-variant any-variant?}))
(dwu/start-undo-transaction undo-id) (dwu/start-undo-transaction undo-id)
(dwsh/update-shapes shape-ids (fn [shape] (dwsh/update-shapes shape-ids (fn [shape]
(cond-> shape (cond-> shape
attributes-to-remove attributes-to-remove
(update :applied-tokens #(apply (partial dissoc %) attributes-to-remove)) (update :applied-tokens #(apply (partial dissoc %) attributes-to-remove))
:always :always
(update :applied-tokens merge tokenized-attributes))))) (update :applied-tokens merge tokenized-attributes)))))
(when on-update-shape (when on-update-shape
(let [res (on-update-shape resolved-value shape-ids attributes)] (let [res (on-update-shape resolved-value shape-ids attributes)]
;; Composed updates return observables and need to be executed differently ;; Composed updates return observables and need to be executed differently
(if (rx/observable? res) (if (rx/observable? res)
res res
(rx/of res)))) (rx/of res))))
(rx/of (dwu/commit-undo-transaction undo-id))))))))))))) (rx/of (dwu/commit-undo-transaction undo-id))))))))))))))
(defn apply-spacing-token-separated (defn apply-spacing-token-separated
"Handles edge-case for spacing token when applying token via toggle button. "Handles edge-case for spacing token when applying token via toggle button.

View File

@ -548,7 +548,7 @@
modif-tree modif-tree
(dwm/build-modif-tree ids objects get-modifier)] (dwm/build-modif-tree ids objects get-modifier)]
(rx/of (dwm/apply-wasm-modifiers modif-tree))) (rx/of (dwm/apply-wasm-modifiers modif-tree :ignore-touched (:ignore-touched options))))
(let [page-id (or (:page-id options) (let [page-id (or (:page-id options)
(:current-page-id state)) (:current-page-id state))

View File

@ -75,7 +75,11 @@
is-type-unfolded (contains? (set unfolded-token-paths) (name type)) is-type-unfolded (contains? (set unfolded-token-paths) (name type))
editing-ref (mf/deref refs/workspace-editor-state) editing-ref (mf/deref refs/workspace-editor-state)
not-editing? (empty? editing-ref) edition (mf/deref refs/selected-edition)
objects (mf/deref refs/workspace-page-objects)
not-editing? (and (empty? editing-ref)
(not (and (some? edition)
(= :text (:type (get objects edition))))))
can-edit? can-edit?
(mf/use-ctx ctx/can-edit?) (mf/use-ctx ctx/can-edit?)