🐛 Fix initial auto-fit of viewport.

This commit is contained in:
Andrey Antukh 2020-06-02 11:54:15 +02:00 committed by Alonso Torres
parent fbd6e395a4
commit bee4e5177c
2 changed files with 51 additions and 23 deletions

View file

@ -398,12 +398,11 @@
"Returns a rect that contains all the shapes and is aware of the "Returns a rect that contains all the shapes and is aware of the
rotation of each shape. Mainly used for multiple selection." rotation of each shape. Mainly used for multiple selection."
[shapes] [shapes]
(let [xf-resolve-shape (map :selrect) (let [shapes (map :selrect shapes)
shapes (into [] xf-resolve-shape shapes) minx (transduce (map :x1) min ##Inf shapes)
minx (transduce (map :x1) min ##Inf shapes) miny (transduce (map :y1) min ##Inf shapes)
miny (transduce (map :y1) min ##Inf shapes) maxx (transduce (map :x2) max ##-Inf shapes)
maxx (transduce (map :x2) max ##-Inf shapes) maxy (transduce (map :y2) max ##-Inf shapes)]
maxy (transduce (map :y2) max ##-Inf shapes)]
{:x1 minx {:x1 minx
:y1 miny :y1 miny
:x2 maxx :x2 maxx

View file

@ -9,7 +9,6 @@
(ns uxbox.main.data.workspace (ns uxbox.main.data.workspace
(:require (:require
[uxbox.util.debug :refer [logjs]]
[beicon.core :as rx] [beicon.core :as rx]
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[clojure.set :as set] [clojure.set :as set]
@ -197,21 +196,53 @@
(declare zoom-to-fit-all) (declare zoom-to-fit-all)
;; TODO: add-spec
(defn initialize-viewport (defn initialize-viewport
[{:keys [width height] :as size}] [{:keys [width height] :as size}]
(ptk/reify ::initialize-viewport (letfn [(update* [{:keys [vbox vport] :as local}]
ptk/UpdateEvent (let [wprop (/ (:width vport) width)
(update [_ state] hprop (/ (:height vport) height)]
(update state :workspace-local (-> local
(fn [local] (assoc :vport size)
(-> local (update :vbox (fn [vbox]
(assoc :vport size) (-> vbox
(update :vbox (fn [vbox] (update :width #(/ % wprop))
(if (nil? vbox) (update :height #(/ % hprop))))))))
(assoc size :x 0 :y 0)
vbox))))))))) (initialize [state local]
(let [page-id (get-in state [:workspace-page :id])
objects (get-in state [:workspace-data page-id :objects])
shapes (cp/select-toplevel-shapes objects {:include-frames? true})
srect (geom/selection-rect shapes)
local (assoc local :vport size)]
(cond
(or (not (mth/finite? (:width srect)))
(not (mth/finite? (:height srect))))
(assoc local :vbox (assoc size :x 0 :y 0))
(or (> (:width srect) width)
(> (:height srect) height))
(let [srect (geom/adjust-to-viewport size srect {:padding 40})
zoom (/ (:width size) (:width srect))]
(-> local
(assoc :zoom zoom)
(update :vbox merge srect)))
:else
(assoc local :vbox (assoc size
:x (- (:x srect) 40)
:y (- (:y srect) 40))))))
(setup [state local]
(if (:vbox local)
(update* local)
(initialize state local)))]
(ptk/reify ::initialize-viewport
ptk/UpdateEvent
(update [_ state]
(update state :workspace-local
(fn [local]
(setup state local)))))))
(defn update-viewport-position (defn update-viewport-position
[{:keys [x y] :or {x identity y identity}}] [{:keys [x y] :or {x identity y identity}}]
@ -226,8 +257,6 @@
(update :x x) (update :x x)
(update :y y))))))) (update :y y)))))))
;; TODO: add spec
(defn update-viewport-size (defn update-viewport-size
[{:keys [width height] :as size}] [{:keys [width height] :as size}]
(ptk/reify ::update-viewport-size (ptk/reify ::update-viewport-size
@ -368,7 +397,7 @@
(let [page-id (get-in state [:workspace-page :id]) (let [page-id (get-in state [:workspace-page :id])
objects (get-in state [:workspace-data page-id :objects]) objects (get-in state [:workspace-data page-id :objects])
shapes (cp/select-toplevel-shapes objects {:include-frames? true}) shapes (cp/select-toplevel-shapes objects {:include-frames? true})
srect (geom/shapes->rect-shape shapes)] srect (geom/selection-rect shapes)]
(if (or (mth/nan? (:width srect)) (if (or (mth/nan? (:width srect))
(mth/nan? (:height srect))) (mth/nan? (:height srect)))