0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-27 15:11:26 -05:00

Add improved text change detection on viewport text renderer

This commit is contained in:
Andrey Antukh 2023-08-28 11:16:13 +02:00
parent f82c682421
commit 7f91619075

View file

@ -125,34 +125,32 @@
(defn text-properties-equal?
[shape other]
;; FIXME: use dm/get-prop
(or (identical? shape other)
(and
;; Check if both shapes are equivalent removing their geometry data
(= (dissoc shape :migrate :points :selrect :height :width :x :y :position-data :modifiers)
(dissoc other :migrate :points :selrect :height :width :x :y :position-data :modifiers))
;; Check if the position and size is close. If any of these changes the shape has changed
;; and if not there is no geometry relevant change
(mth/close? (:x shape) (:x other))
(mth/close? (:y shape) (:y other))
(mth/close? (:width shape) (:width other))
(mth/close? (:height shape) (:height other)))))
(and (= (:content shape) (:content other))
;; Check if the position and size is close. If any of these changes the shape has changed
;; and if not there is no geometry relevant change
(mth/close? (:x shape) (:x other))
(mth/close? (:y shape) (:y other))
(mth/close? (:width shape) (:width other))
(mth/close? (:height shape) (:height other)))))
(mf/defc text-changes-renderer
{::mf/wrap-props false}
[props]
(let [text-shapes (obj/get props "text-shapes")
(let [text-shapes (unchecked-get props "text-shapes")
prev-text-shapes (hooks/use-previous text-shapes)
;; We store in the state the texts still pending to be calculated so we can
;; get its position
pending-update (mf/use-state {})
pending-update* (mf/use-state {})
pending-update (deref pending-update*)
text-change?
(fn [id]
(let [new-shape (get text-shapes id)
old-shape (get prev-text-shapes id)
remote? (some? (-> new-shape meta :session-id))]
remote? (some? (-> new-shape meta :session-id))]
(or (and (not remote?) ;; changes caused by a remote peer are not re-calculated
(not (text-properties-equal? old-shape new-shape)))
@ -160,9 +158,8 @@
(nil? (:position-data new-shape)))))
changed-texts
(mf/use-memo
(mf/deps text-shapes @pending-update)
#(let [pending-shapes (into #{} (vals @pending-update))]
(mf/with-memo [text-shapes pending-update]
(let [pending-shapes (into #{} (vals pending-update))]
(->> (keys text-shapes)
(filter (fn [id]
(or (contains? pending-shapes id)
@ -170,18 +167,18 @@
(map (d/getf text-shapes)))))
handle-update-shape
(mf/use-callback
(mf/use-fn
(fn [shape node]
;; Unique to indentify the pending state
(let [uid (js/Symbol)]
(swap! pending-update assoc uid (:id shape))
(swap! pending-update* assoc uid (:id shape))
(p/then
(update-text-shape shape node)
#(swap! pending-update dissoc uid)))))]
#(swap! pending-update* dissoc uid)))))]
[:.text-changes-renderer
(for [{:keys [id] :as shape} changed-texts]
[:& text-container {:key (str (dm/str "text-container-" id))
[:& text-container {:key (dm/str "text-container-" id)
:shape shape
:on-update handle-update-shape}])]))
@ -213,7 +210,7 @@
[:.text-changes-renderer
(for [{:keys [id] :as shape} changed-texts]
[:& text-container {:key (str (dm/str "text-container-" id))
[:& text-container {:key (dm/str "text-container-" id)
:shape shape
:on-update handle-update-shape}])]))