mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
⚡ Add improved text change detection on viewport text renderer
This commit is contained in:
parent
f82c682421
commit
7f91619075
1 changed files with 19 additions and 22 deletions
|
@ -125,34 +125,32 @@
|
||||||
|
|
||||||
(defn text-properties-equal?
|
(defn text-properties-equal?
|
||||||
[shape other]
|
[shape other]
|
||||||
|
;; FIXME: use dm/get-prop
|
||||||
(or (identical? shape other)
|
(or (identical? shape other)
|
||||||
(and
|
(and (= (:content shape) (:content other))
|
||||||
;; Check if both shapes are equivalent removing their geometry data
|
;; Check if the position and size is close. If any of these changes the shape has changed
|
||||||
(= (dissoc shape :migrate :points :selrect :height :width :x :y :position-data :modifiers)
|
;; and if not there is no geometry relevant change
|
||||||
(dissoc other :migrate :points :selrect :height :width :x :y :position-data :modifiers))
|
(mth/close? (:x shape) (:x other))
|
||||||
|
(mth/close? (:y shape) (:y other))
|
||||||
;; Check if the position and size is close. If any of these changes the shape has changed
|
(mth/close? (:width shape) (:width other))
|
||||||
;; and if not there is no geometry relevant change
|
(mth/close? (:height shape) (:height other)))))
|
||||||
(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/defc text-changes-renderer
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
[props]
|
[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)
|
prev-text-shapes (hooks/use-previous text-shapes)
|
||||||
|
|
||||||
;; We store in the state the texts still pending to be calculated so we can
|
;; We store in the state the texts still pending to be calculated so we can
|
||||||
;; get its position
|
;; get its position
|
||||||
pending-update (mf/use-state {})
|
pending-update* (mf/use-state {})
|
||||||
|
pending-update (deref pending-update*)
|
||||||
|
|
||||||
text-change?
|
text-change?
|
||||||
(fn [id]
|
(fn [id]
|
||||||
(let [new-shape (get text-shapes id)
|
(let [new-shape (get text-shapes id)
|
||||||
old-shape (get prev-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
|
(or (and (not remote?) ;; changes caused by a remote peer are not re-calculated
|
||||||
(not (text-properties-equal? old-shape new-shape)))
|
(not (text-properties-equal? old-shape new-shape)))
|
||||||
|
@ -160,9 +158,8 @@
|
||||||
(nil? (:position-data new-shape)))))
|
(nil? (:position-data new-shape)))))
|
||||||
|
|
||||||
changed-texts
|
changed-texts
|
||||||
(mf/use-memo
|
(mf/with-memo [text-shapes pending-update]
|
||||||
(mf/deps text-shapes @pending-update)
|
(let [pending-shapes (into #{} (vals pending-update))]
|
||||||
#(let [pending-shapes (into #{} (vals @pending-update))]
|
|
||||||
(->> (keys text-shapes)
|
(->> (keys text-shapes)
|
||||||
(filter (fn [id]
|
(filter (fn [id]
|
||||||
(or (contains? pending-shapes id)
|
(or (contains? pending-shapes id)
|
||||||
|
@ -170,18 +167,18 @@
|
||||||
(map (d/getf text-shapes)))))
|
(map (d/getf text-shapes)))))
|
||||||
|
|
||||||
handle-update-shape
|
handle-update-shape
|
||||||
(mf/use-callback
|
(mf/use-fn
|
||||||
(fn [shape node]
|
(fn [shape node]
|
||||||
;; Unique to indentify the pending state
|
;; Unique to indentify the pending state
|
||||||
(let [uid (js/Symbol)]
|
(let [uid (js/Symbol)]
|
||||||
(swap! pending-update assoc uid (:id shape))
|
(swap! pending-update* assoc uid (:id shape))
|
||||||
(p/then
|
(p/then
|
||||||
(update-text-shape shape node)
|
(update-text-shape shape node)
|
||||||
#(swap! pending-update dissoc uid)))))]
|
#(swap! pending-update* dissoc uid)))))]
|
||||||
|
|
||||||
[:.text-changes-renderer
|
[:.text-changes-renderer
|
||||||
(for [{:keys [id] :as shape} changed-texts]
|
(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
|
:shape shape
|
||||||
:on-update handle-update-shape}])]))
|
:on-update handle-update-shape}])]))
|
||||||
|
|
||||||
|
@ -213,7 +210,7 @@
|
||||||
|
|
||||||
[:.text-changes-renderer
|
[:.text-changes-renderer
|
||||||
(for [{:keys [id] :as shape} changed-texts]
|
(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
|
:shape shape
|
||||||
:on-update handle-update-shape}])]))
|
:on-update handle-update-shape}])]))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue