From 4764674374f3b091bcfc7d7daf49d7557919fcd1 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 27 May 2022 15:51:18 +0200 Subject: [PATCH 1/2] :bug: Fix thumbnails. Add safety text position --- .../app/main/ui/workspace/shapes/frame.cljs | 4 +- .../shapes/frame/thumbnail_render.cljs | 29 ++++++++++---- .../workspace/shapes/text/viewport_texts.cljs | 38 ++++++++++++------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/shapes/frame.cljs b/frontend/src/app/main/ui/workspace/shapes/frame.cljs index 9d420f3fb3..ec70154f7e 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame.cljs @@ -84,7 +84,7 @@ disable-thumbnail? (d/not-empty? (dm/get-in modifiers [(:id shape) :modifiers])) [on-load-frame-dom render-frame? thumbnail-renderer] - (ftr/use-render-thumbnail page-id shape node-ref rendered? disable-thumbnail?) + (ftr/use-render-thumbnail page-id shape node-ref rendered? disable-thumbnail? @force-render) on-frame-load (fns/use-node-store thumbnail? node-ref rendered? render-frame?)] @@ -129,5 +129,5 @@ [:g.frame-thumbnail-wrapper {:id (dm/str "thumbnail-container-" (:id shape)) ;; Hide the thumbnail when not displaying - :opacity (when (and @rendered? (not thumbnail?)) 0)} + :opacity (when (and @rendered? (not thumbnail?) (not render-frame?)) 0)} thumbnail-renderer]]])))) diff --git a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs index 35bd2d49ce..5fd7d4eeb3 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs @@ -53,7 +53,7 @@ (defn use-render-thumbnail "Hook that will create the thumbnail thata" - [page-id {:keys [id x y width height] :as shape} node-ref rendered? disable?] + [page-id {:keys [id x y width height] :as shape} node-ref rendered? disable? force-render] (let [frame-canvas-ref (mf/use-ref nil) frame-image-ref (mf/use-ref nil) @@ -85,12 +85,13 @@ img-node (mf/ref-val frame-image-ref)] (when (draw-thumbnail-canvas! canvas-node img-node) (reset! image-url nil) - (reset! render-frame? false)) - ;; If we don't have the thumbnail data saved (normaly the first load) we update the data - ;; when available - (when (not @thumbnail-data-ref) - (st/emit! (dwt/update-thumbnail page-id id) )))))) + ;; If we don't have the thumbnail data saved (normaly the first load) we update the data + ;; when available + (when (not @thumbnail-data-ref) + (st/emit! (dwt/update-thumbnail page-id id) )) + + (reset! render-frame? false)))))) generate-thumbnail (mf/use-callback @@ -140,6 +141,18 @@ (.observe observer node #js {:childList true :attributes true :attributeOldValue true :characterData true :subtree true}) (reset! observer-ref observer)))))] + (mf/use-effect + (mf/deps @render-frame? thumbnail-data) + (fn [] + (if (and (some? thumbnail-data) @render-frame?) + (reset! render-frame? false)))) + + (mf/use-effect + (mf/deps force-render) + (fn [] + (when force-render + (rx/push! updates-str :update)))) + (mf/use-effect (fn [] (let [subid (->> updates-str @@ -190,7 +203,9 @@ :width fixed-width :height fixed-height ;; DEBUG - :style {:filter (when (debug? :thumbnails) "invert(1)")}}]] + :style {:filter (when (debug? :thumbnails) "invert(1)") + :width "100%" + :height "100%"}}]] (when (some? @image-url) [:image {:ref frame-image-ref 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 77f9eb29f8..f823fb8808 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 @@ -9,6 +9,7 @@ [app.common.data :as d] [app.common.data.macros :as dm] [app.common.geom.shapes :as gsh] + [app.common.geom.shapes.rect :as gshr] [app.common.math :as mth] [app.common.pages.helpers :as cph] [app.common.text :as txt] @@ -61,25 +62,34 @@ (and (some? shape) (some? editor-content)) (assoc :content (d/txt-merge content editor-content))))) +(defn overlaps-position-data? + [bounding-box position-data] + (let [fix-rect #(assoc % :y (- (:y %) (:height %)))] + (->> position-data + (some #(gshr/overlaps-rects? bounding-box (fix-rect %))) + (boolean)))) + (defn- update-text-shape - [{:keys [grow-type id migrate]} node] + [{:keys [grow-type id migrate points]} node] ;; Check if we need to update the size because it's auto-width or auto-height ;; Update the position-data of every text fragment (p/let [position-data (utp/calc-position-data node)] - (st/emit! (dwt/update-position-data id position-data)) + (let [bounding-box (gsh/points->selrect points)] + ;; At least one paragraph needs to be inside the bounding box + (when (overlaps-position-data? bounding-box position-data) + (st/emit! (dwt/update-position-data id position-data))) - (when (contains? #{:auto-height :auto-width} grow-type) - (let [{:keys [width height]} - (-> (dom/query node ".paragraph-set") - (dom/get-client-size)) - width (mth/ceil width) - height (mth/ceil height)] - (when (and (not (mth/almost-zero? width)) - (not (mth/almost-zero? height)) - (not migrate)) - (st/emit! (dwt/resize-text id width height)))))) - - (st/emit! (dwt/clean-text-modifier id))) + (when (contains? #{:auto-height :auto-width} grow-type) + (let [{:keys [width height]} + (-> (dom/query node ".paragraph-set") + (dom/get-client-size)) + width (mth/ceil width) + height (mth/ceil height)] + (when (and (not (mth/almost-zero? width)) + (not (mth/almost-zero? height)) + (not migrate)) + (st/emit! (dwt/resize-text id width height)))))) + (st/emit! (dwt/clean-text-modifier id)))) (defn- update-text-modifier [{:keys [grow-type id]} node] From 321c3fb34b1a6e372c2e927b774814890cf807aa Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 30 May 2022 12:08:38 +0200 Subject: [PATCH 2/2] :bug: Fix problem with missplaced texts --- common/src/app/common/geom/shapes/text.cljc | 8 ++++ common/src/app/common/pages/common.cljc | 2 +- common/src/app/common/pages/migrations.cljc | 17 +++++++ frontend/src/app/main/data/workspace.cljs | 6 ++- .../shapes/frame/thumbnail_render.cljs | 2 +- .../workspace/shapes/text/viewport_texts.cljs | 44 ++++++++----------- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/common/src/app/common/geom/shapes/text.cljc b/common/src/app/common/geom/shapes/text.cljc index 9fdb0b34bd..af23e0c15f 100644 --- a/common/src/app/common/geom/shapes/text.cljc +++ b/common/src/app/common/geom/shapes/text.cljc @@ -28,3 +28,11 @@ [shape] (gpr/points->selrect (position-data-points shape))) +(defn overlaps-position-data? + "Checks if the given position data is inside the shape" + [{:keys [points]} position-data] + (let [bounding-box (gpr/points->selrect points) + fix-rect #(assoc % :y (- (:y %) (:height %)))] + (->> position-data + (some #(gpr/overlaps-rects? bounding-box (fix-rect %))) + (boolean)))) diff --git a/common/src/app/common/pages/common.cljc b/common/src/app/common/pages/common.cljc index 8255582592..c412176f64 100644 --- a/common/src/app/common/pages/common.cljc +++ b/common/src/app/common/pages/common.cljc @@ -9,7 +9,7 @@ [app.common.colors :as clr] [app.common.uuid :as uuid])) -(def file-version 18) +(def file-version 19) (def default-color clr/gray-20) (def root uuid/zero) diff --git a/common/src/app/common/pages/migrations.cljc b/common/src/app/common/pages/migrations.cljc index eb0f1c77f8..d2beb3ce04 100644 --- a/common/src/app/common/pages/migrations.cljc +++ b/common/src/app/common/pages/migrations.cljc @@ -10,6 +10,7 @@ [app.common.geom.matrix :as gmt] [app.common.geom.shapes :as gsh] [app.common.geom.shapes.path :as gsp] + [app.common.geom.shapes.text :as gsht] [app.common.logging :as l] [app.common.math :as mth] [app.common.pages :as cp] @@ -415,5 +416,21 @@ (update :pages-index d/update-vals update-container) (update :components d/update-vals update-container)))) +(defmethod migrate 19 + [data] + (letfn [(update-object [object] + (cond-> object + (and (cph/text-shape? object) + (d/not-empty? (:position-data object)) + (not (gsht/overlaps-position-data? object (:position-data object)))) + (dissoc :position-data))) + + (update-container [container] + (update container :objects d/update-vals update-object))] + + (-> data + (update :pages-index d/update-vals update-container) + (update :components d/update-vals update-container)))) + ;; TODO: pending to do a migration for delete already not used fill ;; and stroke props. This should be done for >1.14.x version. diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 8266507316..951f5dae95 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -150,7 +150,11 @@ :workspace-undo {} :workspace-project project :workspace-file (assoc file :initialized true) - :workspace-data (:data file) + :workspace-data (-> (:data file) + ;; DEBUG: Uncomment this to try out migrations in local without changing + ;; the version number + #_(assoc :version 17) + #_(app.common.pages.migrations/migrate-data 19)) :workspace-libraries (d/index-by :id libraries))) ptk/WatchEvent diff --git a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs index 5fd7d4eeb3..1e763a462f 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame/thumbnail_render.cljs @@ -144,7 +144,7 @@ (mf/use-effect (mf/deps @render-frame? thumbnail-data) (fn [] - (if (and (some? thumbnail-data) @render-frame?) + (when (and (some? thumbnail-data) @render-frame?) (reset! render-frame? false)))) (mf/use-effect 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 f823fb8808..3aca3072c9 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 @@ -9,7 +9,7 @@ [app.common.data :as d] [app.common.data.macros :as dm] [app.common.geom.shapes :as gsh] - [app.common.geom.shapes.rect :as gshr] + [app.common.geom.shapes.text :as gsht] [app.common.math :as mth] [app.common.pages.helpers :as cph] [app.common.text :as txt] @@ -22,7 +22,7 @@ [app.util.dom :as dom] [app.util.object :as obj] [app.util.text-editor :as ted] - [app.util.text-svg-position :as utp] + [app.util.text-svg-position :as tsp] [app.util.timers :as ts] [promesa.core :as p] [rumext.alpha :as mf])) @@ -62,38 +62,30 @@ (and (some? shape) (some? editor-content)) (assoc :content (d/txt-merge content editor-content))))) -(defn overlaps-position-data? - [bounding-box position-data] - (let [fix-rect #(assoc % :y (- (:y %) (:height %)))] - (->> position-data - (some #(gshr/overlaps-rects? bounding-box (fix-rect %))) - (boolean)))) - (defn- update-text-shape - [{:keys [grow-type id migrate points]} node] + [{:keys [grow-type id migrate] :as shape} node] ;; Check if we need to update the size because it's auto-width or auto-height ;; Update the position-data of every text fragment - (p/let [position-data (utp/calc-position-data node)] - (let [bounding-box (gsh/points->selrect points)] - ;; At least one paragraph needs to be inside the bounding box - (when (overlaps-position-data? bounding-box position-data) - (st/emit! (dwt/update-position-data id position-data))) + (p/let [position-data (tsp/calc-position-data node)] + ;; At least one paragraph needs to be inside the bounding box + (when (gsht/overlaps-position-data? shape position-data) + (st/emit! (dwt/update-position-data id position-data))) - (when (contains? #{:auto-height :auto-width} grow-type) - (let [{:keys [width height]} - (-> (dom/query node ".paragraph-set") - (dom/get-client-size)) - width (mth/ceil width) - height (mth/ceil height)] - (when (and (not (mth/almost-zero? width)) - (not (mth/almost-zero? height)) - (not migrate)) - (st/emit! (dwt/resize-text id width height)))))) + (when (contains? #{:auto-height :auto-width} grow-type) + (let [{:keys [width height]} + (-> (dom/query node ".paragraph-set") + (dom/get-client-size)) + width (mth/ceil width) + height (mth/ceil height)] + (when (and (not (mth/almost-zero? width)) + (not (mth/almost-zero? height)) + (not migrate)) + (st/emit! (dwt/resize-text id width height))))) (st/emit! (dwt/clean-text-modifier id)))) (defn- update-text-modifier [{:keys [grow-type id]} node] - (p/let [position-data (utp/calc-position-data node) + (p/let [position-data (tsp/calc-position-data node) props {:position-data position-data} props