diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 225fd788f..f98fd31b8 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -16,6 +16,7 @@ [app.common.pages.common :as cpc] [app.common.pages.helpers :as cph] [app.common.spec :as us] + [app.common.types.component :as ctk] [app.common.types.container :as ctn] [app.common.types.shape-tree :as ctst] [app.main.data.workspace.changes :as dch] @@ -31,6 +32,110 @@ [cljs.spec.alpha :as s] [potok.core :as ptk])) +;; --- copies --------------------------- + +(defn- get-copies + "If one or more of the shapes belongs to a component's main instance, find all copies of + the component in the same page. + + Return a map { [ [ ...]] ...}" + [shapes objects modifiers] + (letfn [(get-copies-one [shape] + (let [root-shape (ctn/get-root-shape objects shape)] + (when (:main-instance? root-shape) + (let [children (->> root-shape + :shapes + (map #(get objects %)) + (map #(gsh/apply-modifiers % (get-in modifiers [(:id %) :modifiers])))) + root-shape (gsh/update-group-selrect root-shape children)] + [(:id root-shape) [root-shape (ctn/get-instances objects root-shape)]]))))] + + (into {} (map get-copies-one shapes)))) + +(defn- reposition-shape + [shape origin-root dest-root] + (let [shape-pos (fn [shape] + (gpt/point (get-in shape [:selrect :x]) + (get-in shape [:selrect :y]))) + + origin-root-pos (shape-pos origin-root) + dest-root-pos (shape-pos dest-root) + delta (gpt/subtract dest-root-pos origin-root-pos)] + (gsh/move shape delta))) + +(defn- sync-shape + [main-shape copy-shape copy-root main-root] + ;; (js/console.log "+++") + ;; (js/console.log "main-shape" (clj->js main-shape)) + ;; (js/console.log "copy-shape" (clj->js copy-shape)) + (if (ctk/touched-group? copy-shape :geometry-group) + {} + (let [main-shape (reposition-shape main-shape main-root copy-root) + + translation (gpt/subtract (gsh/orig-pos main-shape) + (gsh/orig-pos copy-shape)) + + center (gsh/orig-pos copy-shape) + mult-w (/ (gsh/width main-shape) (gsh/width copy-shape)) + mult-h (/ (gsh/height main-shape) (gsh/height copy-shape)) + resize (gpt/point mult-w mult-h)] + + (cond-> {} + (not (gpt/almost-zero? translation)) + (assoc :displacement (gmt/translate-matrix translation)) + + (not (gpt/close? resize (gpt/point 1 1))) + (assoc :resize-vector resize + :resize-origin center))))) + +(defn- process-text-modifiers + "For texts we only use the displacement because resize + needs to recalculate the text layout" + [shape modifiers] + modifiers) + ;; (cond-> modifiers + ;; (= :text (:type shape)) + ;; (select-keys [:displacement :rotation]))) + +(defn- add-copies-modifiers + "Add modifiers to all necessary shapes inside the copies" + [copies objects modifiers] + (letfn [(add-copy-modifiers-one [modifiers copy-shape copy-root main-root main-shapes main-shapes-modif] + (let [main-shape-modif (d/seek #(ctk/is-main-of? % copy-shape) main-shapes-modif) + modifier (cond-> (sync-shape main-shape-modif copy-shape copy-root main-root) + (some? (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers]))) + (assoc :rotation (:rotation (get-in modifiers [(:id main-shape-modif) :modifiers]))) + )] + (if (seq modifier) + (assoc-in modifiers [(:id copy-shape) :modifiers] modifier) + modifiers))) + + (add-copy-modifiers [modifiers copy-root main-root main-shapes main-shapes-modif] + (let [copy-shapes (into [copy-root] (cph/get-children objects (:id copy-root)))] + (reduce #(add-copy-modifiers-one %1 %2 copy-root main-root main-shapes main-shapes-modif) + modifiers + copy-shapes))) + + (add-copies-modifiers-one [modifiers [main-root copy-roots]] + (let [main-shapes (into [main-root] (cph/get-children objects (:id main-root))) + main-shapes-modif (map (fn [shape] + (let [; shape (cond-> shape + ; (some? (:transform-inverse shape)) + ; (gsh/apply-transform (:transform-inverse shape))) + ] + (->> (get-in modifiers [(:id shape) :modifiers]) + (process-text-modifiers shape) + (gsh/apply-modifiers shape)))) + main-shapes)] + (reduce #(add-copy-modifiers %1 %2 main-root main-shapes main-shapes-modif) + modifiers + copy-roots)))] + + (reduce add-copies-modifiers-one + modifiers + (vals copies)))) + + ;; -- Helpers -------------------------------------------------------- ;; For each of the 8 handlers gives the multiplier for resize @@ -138,16 +243,21 @@ (contains? (:workspace-layout state) :snap-pixel-grid)) modif-tree - (gsh/set-objects-modifiers ids objects (constantly modifiers) ignore-constraints snap-pixel?)] + (gsh/set-objects-modifiers ids objects (constantly modifiers) ignore-constraints snap-pixel?) + + copies (get-copies (mapv (d/getf objects) ids) objects modif-tree) + + ;; TODO: mark new modifiers to be ignored in apply-modifiers + modif-tree (add-copies-modifiers copies objects modif-tree)] (update state :workspace-modifiers merge modif-tree)))))) -(defn set-modifiers-raw - [modifiers] - (ptk/reify ::set-modifiers-raw - ptk/UpdateEvent - (update [_ state] - (update state :workspace-modifiers merge modifiers)))) +;; (defn set-modifiers-raw +;; [modifiers] +;; (ptk/reify ::set-modifiers-raw +;; ptk/UpdateEvent +;; (update [_ state] +;; (update state :workspace-modifiers merge modifiers)))) ;; Rotation use different algorithm to calculate children modifiers (and do not use child constraints). (defn- set-rotation-modifiers diff --git a/frontend/src/app/main/ui/workspace/shapes/frame/dynamic_modifiers.cljs b/frontend/src/app/main/ui/workspace/shapes/frame/dynamic_modifiers.cljs index 0378dd06e..9184f1585 100644 --- a/frontend/src/app/main/ui/workspace/shapes/frame/dynamic_modifiers.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/frame/dynamic_modifiers.cljs @@ -446,36 +446,36 @@ (let [prev-shapes (mf/use-var nil) prev-modifiers (mf/use-var nil) prev-transforms (mf/use-var nil) - prev-copies (mf/use-var nil) + ;; prev-copies (mf/use-var nil) - copies - (mf/use-memo ; TODO: ojo estas deps hay que revisarlas - (mf/deps modifiers (and (d/not-empty? @prev-modifiers) (d/not-empty? modifiers))) - (fn [] - (let [shapes (->> (keys modifiers) - (mapv (d/getf objects)))] - (get-copies shapes objects modifiers)))) + ;; copies + ;; (mf/use-memo ; TODO: ojo estas deps hay que revisarlas + ;; (mf/deps modifiers (and (d/not-empty? @prev-modifiers) (d/not-empty? modifiers))) + ;; (fn [] + ;; (let [shapes (->> (keys modifiers) + ;; (mapv (d/getf objects)))] + ;; (get-copies shapes objects modifiers)))) - modifiers - (mf/use-memo - (mf/deps objects modifiers copies @prev-copies) - (fn [] - (if (= (count copies) (count @prev-copies)) - modifiers - (let [new-modifiers (add-copies-modifiers copies objects modifiers)] - (js/console.log "==================") - (js/console.log "modifiers (antes)" (clj->js modifiers)) - (js/console.log "copies" (clj->js copies)) - (js/console.log "modifiers (después)" (clj->js new-modifiers)) - (when (seq new-modifiers) - (tm/schedule #(st/emit! (dwt/set-modifiers-raw new-modifiers)))) - new-modifiers)))) + ;; modifiers + ;; (mf/use-memo + ;; (mf/deps objects modifiers copies @prev-copies) + ;; (fn [] + ;; (if (= (count copies) (count @prev-copies)) + ;; modifiers + ;; (let [new-modifiers (add-copies-modifiers copies objects modifiers)] + ;; (js/console.log "==================") + ;; (js/console.log "modifiers (antes)" (clj->js modifiers)) + ;; (js/console.log "copies" (clj->js copies)) + ;; (js/console.log "modifiers (después)" (clj->js new-modifiers)) + ;; (when (seq new-modifiers) + ;; (tm/schedule #(st/emit! (dwt/set-modifiers-raw new-modifiers)))) + ;; new-modifiers)))) transforms (mf/use-memo (mf/deps modifiers) (fn [] - (js/console.log "****modifiers" (clj->js modifiers)) + ;; (js/console.log "****modifiers" (clj->js modifiers)) (when (seq modifiers) (d/mapm (fn [id {modifiers :modifiers}] (let [shape (get objects id) @@ -492,7 +492,7 @@ (mf/use-memo (mf/deps transforms) (fn [] - (js/console.log "transforms" (clj->js transforms)) + ;; (js/console.log "transforms" (clj->js transforms)) (->> (keys transforms) (map (d/getf objects))))) @@ -699,4 +699,5 @@ (reset! prev-modifiers modifiers) (reset! prev-transforms transforms) (reset! prev-shapes shapes) - (reset! prev-copies copies)))))) + ;; (reset! prev-copies copies) + )))))