diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 930b7fc999..b6cf52a649 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -13,6 +13,7 @@ [app.common.math :as mth] [app.common.pages.helpers :as cph] [app.common.text :as txt] + [app.common.uuid :as uuid] [app.main.data.workspace.changes :as dch] [app.main.data.workspace.common :as dwc] [app.main.data.workspace.selection :as dws] @@ -379,3 +380,45 @@ ptk/UpdateEvent (update [_ state] (d/dissoc-in state [:workspace-text-modifier id])))) + +(defn commit-position-data + [] + (ptk/reify ::commit-position-data + ptk/WatchEvent + (watch [_ state _] + (let [position-data (::update-position-data state)] + (rx/concat + (rx/of (dch/update-shapes + (keys position-data) + (fn [shape] + (-> shape + (assoc :position-data (get position-data (:id shape)))) + ) + {:save-undo? false :reg-objects? false})) + (rx/of (fn [state] + (dissoc state ::update-position-data-debounce ::update-position-data)))))))) + +(defn update-position-data + [id position-data] + + (let [start (uuid/next)] + (ptk/reify ::update-position-data + ptk/UpdateEvent + (update [_ state] + (if (nil? (::update-position-data-debounce state)) + (assoc state ::update-position-data-debounce start) + (assoc-in state [::update-position-data id] position-data))) + + ptk/WatchEvent + (watch [_ state stream] + (if (= (::update-position-data-debounce state) start) + (let [stopper (->> stream (rx/filter (ptk/type? :app.main.data.workspace/finalize)))] + (rx/merge + (->> stream + (rx/filter (ptk/type? ::update-position-data)) + (rx/debounce 50) + (rx/take 1) + (rx/map #(commit-position-data)) + (rx/take-until stopper)) + (rx/of (update-position-data id position-data)))) + (rx/empty)))))) diff --git a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts.cljs b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts.cljs index 17e1aaae63..f225161f18 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts.cljs @@ -12,7 +12,6 @@ [app.common.math :as mth] [app.common.pages.helpers :as cph] [app.common.text :as txt] - [app.main.data.workspace.changes :as dch] [app.main.data.workspace.texts :as dwt] [app.main.fonts :as fonts] [app.main.refs :as refs] @@ -41,7 +40,7 @@ (assoc :content (attrs/merge content editor-content))))) (defn- update-text-shape - [{:keys [grow-type id]} node] + [{:keys [grow-type id] :as shape} node] ;; Check if we need to update the size because it's auto-width or auto-height (when (contains? #{:auto-height :auto-width} grow-type) (let [{:keys [width height]} @@ -54,12 +53,7 @@ ;; Update the position-data of every text fragment (let [position-data (utp/calc-position-data node)] - (st/emit! (dch/update-shapes - [id] - (fn [shape] - (-> shape - (assoc :position-data position-data))) - {:save-undo? false})))) + (st/emit! (dwt/update-position-data id position-data)))) (defn- update-text-modifier [{:keys [grow-type id]} node]