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,12 +125,9 @@
(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
(= (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 ;; 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 ;; and if not there is no geometry relevant change
(mth/close? (:x shape) (:x other)) (mth/close? (:x shape) (:x other))
@ -141,12 +138,13 @@
(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]
@ -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}])]))