Repository: penpot/penpot
File: frontend/src/app/main/data/workspace/viewport.cljs (ClojureScript)

Code before:
                (fn [local]
                  (setup state local)))))))

(defn calculate-centered-viewbox
  "Updates the viewbox coordinates for a given center position"
  [local position]
  (let [vbox (:vbox local)
        nw   (/ (:width vbox) 2)
        nh   (/ (:height vbox) 2)
        nx   (- (:x position) nw)
        ny   (- (:y position) nh)]
    (update local :vbox assoc :x nx :y ny)))

(defn update-viewport-position-center
  [position]

  (dm/assert!
   "expected a point instance for `position` param"
   (gpt/point? position))

  (ptk/reify ::update-viewport-position-center
    ptk/UpdateEvent
    (update [_ state]
      (update state :workspace-local
              (fn [local]
                (calculate-centered-viewbox local position))))))

(defn update-viewport-position
  [{:keys [x y] :or {x identity y identity}}]

  (dm/assert!
   "expected function for `x`"
   (fn? x))
  (dm/assert!
   "expected function for `y`"
   (fn? y))

  (ptk/reify ::update-viewport-position
    ptk/UpdateEvent
    (update [_ state]
      (update-in state [:workspace-local :vbox]
                 (fn [vbox]
                   (-> vbox
                       (update :x x)
                       (update :y y)))))))

(defn update-viewport-size
  [resize-type {:keys [width height] :as size}]
  (ptk/reify ::update-viewport-size
    ptk/UpdateEvent
    (update [_ state]

Review comment (suggestion):
This can be expressed this way, without crating an additional anonymous function:

```clojure
(update state :workspace-local calculate-centered-viewbox position)
```

Code after:
                (fn [local]
                  (setup state local)))))))

(defn calculate-centered-viewbox
  "Updates the viewbox coordinates for a given center position"
  [local position]
  (let [vbox (:vbox local)
        nw   (/ (:width vbox) 2)
        nh   (/ (:height vbox) 2)
        nx   (- (:x position) nw)
        ny   (- (:y position) nh)]
    (update local :vbox assoc :x nx :y ny)))

(defn update-viewport-position-center
  [position]
  (assert (gpt/point? position) "expected a point instance for `position` param")

  (ptk/reify ::update-viewport-position-center
    ptk/UpdateEvent
    (update [_ state]
      (update state :workspace-local calculate-centered-viewbox position))))

(defn update-viewport-position
  [{:keys [x y] :or {x identity y identity}}]

  (dm/assert!
   "expected function for `x`"
   (fn? x))
  (dm/assert!
   "expected function for `y`"
   (fn? y))

  (ptk/reify ::update-viewport-position
    ptk/UpdateEvent
    (update [_ state]
      (update-in state [:workspace-local :vbox]
                 (fn [vbox]
                   (-> vbox
                       (update :x x)
                       (update :y y)))))))

(defn update-viewport-size
  [resize-type {:keys [width height] :as size}]
  (ptk/reify ::update-viewport-size
    ptk/UpdateEvent
    (update [_ state]
      (update state :workspace-local
              (fn [{:keys [vport] :as local}]
                (if (or (nil? vport)
                        (mth/almost-zero? width)
                        (mth/almost-zero? height))