mirror of
https://github.com/penpot/penpot.git
synced 2025-03-14 16:51:18 -05:00
Reorganize viewport positioning related code.
This commit is contained in:
parent
c906a4e46f
commit
137af0e14d
6 changed files with 108 additions and 65 deletions
|
@ -25,3 +25,10 @@
|
||||||
:grid-color "#cccccc"
|
:grid-color "#cccccc"
|
||||||
:grid-alignment true
|
:grid-alignment true
|
||||||
:background "#ffffff"})
|
:background "#ffffff"})
|
||||||
|
|
||||||
|
(def zoom-levels
|
||||||
|
[0.20 0.21 0.22 0.23 0.24 0.25 0.27 0.28 0.30 0.32 0.34
|
||||||
|
0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.51 0.54 0.57 0.60
|
||||||
|
0.63 0.66 0.69 0.73 0.77 0.81 0.85 0.90 0.95 1.00 1.05
|
||||||
|
1.10 1.15 1.21 1.27 1.33 1.40 1.47 1.54 1.62 1.70 1.78
|
||||||
|
1.87 1.96 2.06 2.16 2.27 2.38 2.50 2.62 2.75 2.88 3.00])
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
[uxbox.main.data.shapes-impl :as shimpl]
|
[uxbox.main.data.shapes-impl :as shimpl]
|
||||||
[uxbox.main.data.lightbox :as udl]
|
[uxbox.main.data.lightbox :as udl]
|
||||||
[uxbox.main.data.history :as udh]
|
[uxbox.main.data.history :as udh]
|
||||||
|
|
||||||
|
[uxbox.main.data.workspace.scroll :as wscroll]
|
||||||
[uxbox.util.uuid :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.util.spec :as us]
|
[uxbox.util.spec :as us]
|
||||||
[uxbox.util.forms :as sc]
|
[uxbox.util.forms :as sc]
|
||||||
|
@ -29,14 +31,10 @@
|
||||||
[uxbox.util.math :as mth]
|
[uxbox.util.math :as mth]
|
||||||
[uxbox.util.data :refer (index-of)]))
|
[uxbox.util.data :refer (index-of)]))
|
||||||
|
|
||||||
;; --- Constants
|
;; --- Expose inner functions
|
||||||
|
|
||||||
(def zoom-levels
|
(def start-viewport-positioning wscroll/start-viewport-positioning)
|
||||||
[0.20 0.21 0.22 0.23 0.24 0.25 0.27 0.28 0.30 0.32 0.34
|
(def stop-viewport-positioning wscroll/stop-viewport-positioning)
|
||||||
0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.51 0.54 0.57 0.60
|
|
||||||
0.63 0.66 0.69 0.73 0.77 0.81 0.85 0.90 0.95 1.00 1.05
|
|
||||||
1.10 1.15 1.21 1.27 1.33 1.40 1.47 1.54 1.62 1.70 1.78
|
|
||||||
1.87 1.96 2.06 2.16 2.27 2.38 2.50 2.62 2.75 2.88 3.00])
|
|
||||||
|
|
||||||
;; --- Initialize Workspace
|
;; --- Initialize Workspace
|
||||||
|
|
||||||
|
@ -192,7 +190,7 @@
|
||||||
[]
|
[]
|
||||||
(InitializeIconsToolbox.))
|
(InitializeIconsToolbox.))
|
||||||
|
|
||||||
;; --- Copy to Clipboard
|
;; --- Clipboard Management
|
||||||
|
|
||||||
(defrecord CopyToClipboard []
|
(defrecord CopyToClipboard []
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
|
@ -213,8 +211,6 @@
|
||||||
[]
|
[]
|
||||||
(CopyToClipboard.))
|
(CopyToClipboard.))
|
||||||
|
|
||||||
;; --- Paste from Clipboard
|
|
||||||
|
|
||||||
(defrecord PasteFromClipboard [id]
|
(defrecord PasteFromClipboard [id]
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
|
@ -231,37 +227,33 @@
|
||||||
([] (PasteFromClipboard. nil))
|
([] (PasteFromClipboard. nil))
|
||||||
([id] (PasteFromClipboard. id)))
|
([id] (PasteFromClipboard. id)))
|
||||||
|
|
||||||
;; --- Increase Zoom
|
;; --- Zoom Management
|
||||||
|
|
||||||
(defrecord IncreaseZoom []
|
(deftype IncreaseZoom []
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(let [increase #(nth zoom-levels
|
(let [increase #(nth c/zoom-levels
|
||||||
(+ (index-of zoom-levels %) 1)
|
(+ (index-of c/zoom-levels %) 1)
|
||||||
(last zoom-levels))]
|
(last c/zoom-levels))]
|
||||||
(update-in state [:workspace :zoom] (fnil increase 1)))))
|
(update-in state [:workspace :zoom] (fnil increase 1)))))
|
||||||
|
|
||||||
(defn increase-zoom
|
(defn increase-zoom
|
||||||
[]
|
[]
|
||||||
(IncreaseZoom.))
|
(IncreaseZoom.))
|
||||||
|
|
||||||
;; --- Decrease Zoom
|
(deftype DecreaseZoom []
|
||||||
|
|
||||||
(defrecord DecreaseZoom []
|
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(let [decrease #(nth zoom-levels
|
(let [decrease #(nth c/zoom-levels
|
||||||
(- (index-of zoom-levels %) 1)
|
(- (index-of c/zoom-levels %) 1)
|
||||||
(first zoom-levels))]
|
(first c/zoom-levels))]
|
||||||
(update-in state [:workspace :zoom] (fnil decrease 1)))))
|
(update-in state [:workspace :zoom] (fnil decrease 1)))))
|
||||||
|
|
||||||
(defn decrease-zoom
|
(defn decrease-zoom
|
||||||
[]
|
[]
|
||||||
(DecreaseZoom.))
|
(DecreaseZoom.))
|
||||||
|
|
||||||
;; --- Reset Zoom
|
(deftype ResetZoom []
|
||||||
|
|
||||||
(defrecord ResetZoom []
|
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(assoc-in state [:workspace :zoom] 1)))
|
(assoc-in state [:workspace :zoom] 1)))
|
||||||
|
@ -270,7 +262,7 @@
|
||||||
[]
|
[]
|
||||||
(ResetZoom.))
|
(ResetZoom.))
|
||||||
|
|
||||||
;; --- Set tooltip
|
;; --- Tooltips
|
||||||
|
|
||||||
(defrecord SetTooltip [text]
|
(defrecord SetTooltip [text]
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
|
@ -281,7 +273,7 @@
|
||||||
[text]
|
[text]
|
||||||
(SetTooltip. text))
|
(SetTooltip. text))
|
||||||
|
|
||||||
;; --- Initialize Alignment Index
|
;; --- Grid Alignment
|
||||||
|
|
||||||
(declare initialize-alignment?)
|
(declare initialize-alignment?)
|
||||||
|
|
||||||
|
|
69
frontend/src/uxbox/main/data/workspace/scroll.cljs
Normal file
69
frontend/src/uxbox/main/data/workspace/scroll.cljs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
|
||||||
|
(ns uxbox.main.data.workspace.scroll
|
||||||
|
"Workspace scroll related events. Mostly or all events
|
||||||
|
are related to UI logic."
|
||||||
|
(:require [beicon.core :as rx]
|
||||||
|
[potok.core :as ptk]
|
||||||
|
[uxbox.main.ui.workspace.base :as wb]
|
||||||
|
[uxbox.util.mixins :as mx :include-macros true]
|
||||||
|
[uxbox.util.rlocks :as rlocks]
|
||||||
|
[uxbox.util.dom :as dom]
|
||||||
|
[uxbox.util.geom.point :as gpt]))
|
||||||
|
|
||||||
|
;; --- Start Viewport Positioning
|
||||||
|
|
||||||
|
(declare stop-viewport-positioning?)
|
||||||
|
|
||||||
|
(defn run-viewport-positioning
|
||||||
|
[stream]
|
||||||
|
(let [stoper (->> (rx/filter stop-viewport-positioning? stream)
|
||||||
|
(rx/take 1))
|
||||||
|
reference @wb/mouse-viewport-a
|
||||||
|
dom (dom/get-element "workspace-canvas")]
|
||||||
|
(->> wb/mouse-viewport-s
|
||||||
|
(rx/take-until stoper)
|
||||||
|
(rx/map (fn [point]
|
||||||
|
(let [{:keys [x y]} (gpt/subtract point reference)
|
||||||
|
cx (.-scrollLeft dom)
|
||||||
|
cy (.-scrollTop dom)]
|
||||||
|
(set! (.-scrollLeft dom) (- cx x))
|
||||||
|
(set! (.-scrollTop dom) (- cy y)))))
|
||||||
|
(rx/ignore))))
|
||||||
|
|
||||||
|
(deftype StartViewportPositioning [id]
|
||||||
|
ptk/UpdateEvent
|
||||||
|
(update [_ state]
|
||||||
|
(update-in state [:workspace :viewport-positionig] #(if (nil? %) id %)))
|
||||||
|
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ state stream]
|
||||||
|
(let [cid (get-in state [:workspace :viewport-positionig])]
|
||||||
|
(if (= cid id)
|
||||||
|
(run-viewport-positioning stream)
|
||||||
|
(rx/empty)))))
|
||||||
|
|
||||||
|
(defn start-viewport-positioning
|
||||||
|
[]
|
||||||
|
(StartViewportPositioning. (gensym "viewport-positioning")))
|
||||||
|
|
||||||
|
;; --- Stop Viewport positioning
|
||||||
|
|
||||||
|
(deftype StopViewportPositioning []
|
||||||
|
ptk/UpdateEvent
|
||||||
|
(update [_ state]
|
||||||
|
(update state :workspace dissoc :viewport-positionig)))
|
||||||
|
|
||||||
|
(defn stop-viewport-positioning
|
||||||
|
[]
|
||||||
|
(StopViewportPositioning.))
|
||||||
|
|
||||||
|
(defn stop-viewport-positioning?
|
||||||
|
[v]
|
||||||
|
(instance? StopViewportPositioning v))
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@
|
||||||
(let [[projectid pageid] (:rum/args own)
|
(let [[projectid pageid] (:rum/args own)
|
||||||
dom (mx/ref-node own "workspace-canvas")
|
dom (mx/ref-node own "workspace-canvas")
|
||||||
scroll-to-page-center #(scroll/scroll-to-page-center dom @wb/page-ref)
|
scroll-to-page-center #(scroll/scroll-to-page-center dom @wb/page-ref)
|
||||||
sub1 (scroll/watch-scroll-interactions own)
|
;; sub1 (scroll/watch-scroll-interactions own)
|
||||||
sub2 (rx/subscribe wb/page-id-ref-s scroll-to-page-center)]
|
sub2 (rx/subscribe wb/page-id-ref-s scroll-to-page-center)]
|
||||||
|
|
||||||
(scroll-to-page-center)
|
(scroll-to-page-center)
|
||||||
|
@ -55,12 +55,14 @@
|
||||||
(udu/watch-page-changes pageid)
|
(udu/watch-page-changes pageid)
|
||||||
(udh/watch-page-changes pageid))
|
(udh/watch-page-changes pageid))
|
||||||
|
|
||||||
(assoc own ::sub1 sub1 ::sub2 sub2)))
|
(assoc own
|
||||||
|
;; ::sub1 sub1
|
||||||
|
::sub2 sub2)))
|
||||||
|
|
||||||
(defn- workspace-will-unmount
|
(defn- workspace-will-unmount
|
||||||
[own]
|
[own]
|
||||||
(st/emit! ::udp/stop-page-watcher)
|
(st/emit! ::udp/stop-page-watcher)
|
||||||
(.close (::sub1 own))
|
;; (.close (::sub1 own))
|
||||||
(.close (::sub2 own))
|
(.close (::sub2 own))
|
||||||
(dissoc own ::sub1 ::sub2))
|
(dissoc own ::sub1 ::sub2))
|
||||||
|
|
||||||
|
|
|
@ -133,12 +133,15 @@
|
||||||
:ctrl? (kbd/ctrl? event)}]
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
(rx/push! wb/events-b [:key/down opts])
|
(rx/push! wb/events-b [:key/down opts])
|
||||||
(when (kbd/space? event)
|
(when (kbd/space? event)
|
||||||
(rlocks/acquire! :workspace/scroll))))
|
(st/emit! (dw/start-viewport-positioning)))))
|
||||||
|
#_(rlocks/acquire! :workspace/scroll)
|
||||||
|
|
||||||
(on-key-up [event]
|
(on-key-up [event]
|
||||||
(let [opts {:key (.-keyCode event)
|
(let [opts {:key (.-keyCode event)
|
||||||
:shift? (kbd/shift? event)
|
:shift? (kbd/shift? event)
|
||||||
:ctrl? (kbd/ctrl? event)}]
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
|
(when (kbd/space? event)
|
||||||
|
(st/emit! (dw/stop-viewport-positioning)))
|
||||||
(rx/push! wb/events-b [:key/up opts])))
|
(rx/push! wb/events-b [:key/up opts])))
|
||||||
|
|
||||||
(on-mousemove [event]
|
(on-mousemove [event]
|
||||||
|
|
|
@ -2,49 +2,19 @@
|
||||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
|
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
|
||||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
;; Copyright (c) 2015-2017 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||||
|
|
||||||
(ns uxbox.main.ui.workspace.scroll
|
(ns uxbox.main.ui.workspace.scroll
|
||||||
"Workspace scroll events handling."
|
"Workspace scroll events handling."
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uxbox.util.mixins :as mx :include-macros true]
|
[potok.core :as ptk]
|
||||||
[uxbox.main.ui.workspace.base :as wb]
|
[uxbox.main.ui.workspace.base :as wb]
|
||||||
|
[uxbox.util.mixins :as mx :include-macros true]
|
||||||
[uxbox.util.rlocks :as rlocks]
|
[uxbox.util.rlocks :as rlocks]
|
||||||
|
[uxbox.util.dom :as dom]
|
||||||
[uxbox.util.geom.point :as gpt]))
|
[uxbox.util.geom.point :as gpt]))
|
||||||
|
|
||||||
(defn watch-scroll-interactions
|
|
||||||
[own]
|
|
||||||
(letfn [(is-space-up? [[type {:keys [key]}]]
|
|
||||||
(and (= 32 key) (= :key/up type)))
|
|
||||||
|
|
||||||
(on-start []
|
|
||||||
(let [stoper (->> wb/events-s
|
|
||||||
(rx/filter is-space-up?)
|
|
||||||
(rx/take 1))
|
|
||||||
local (:rum/local own)
|
|
||||||
initial @wb/mouse-viewport-a
|
|
||||||
stream (rx/take-until stoper wb/mouse-viewport-s)]
|
|
||||||
(swap! local assoc :scrolling true)
|
|
||||||
(rx/subscribe stream #(on-scroll % initial) nil on-scroll-end)))
|
|
||||||
|
|
||||||
(on-scroll-end []
|
|
||||||
(rlocks/release! :workspace/scroll)
|
|
||||||
(let [local (:rum/local own)]
|
|
||||||
(swap! local assoc :scrolling false)))
|
|
||||||
|
|
||||||
(on-scroll [pt initial]
|
|
||||||
(let [{:keys [x y]} (gpt/subtract pt initial)
|
|
||||||
el (mx/ref-node own "workspace-canvas")
|
|
||||||
cx (.-scrollLeft el)
|
|
||||||
cy (.-scrollTop el)]
|
|
||||||
(set! (.-scrollLeft el) (- cx x))
|
|
||||||
(set! (.-scrollTop el) (- cy y))))]
|
|
||||||
|
|
||||||
(let [stream (->> (rx/map first rlocks/stream)
|
|
||||||
(rx/filter #(= % :workspace/scroll)))]
|
|
||||||
(rx/subscribe stream on-start))))
|
|
||||||
|
|
||||||
(defn set-scroll-position
|
(defn set-scroll-position
|
||||||
[dom position]
|
[dom position]
|
||||||
(set! (.-scrollLeft dom) (:x position))
|
(set! (.-scrollLeft dom) (:x position))
|
||||||
|
|
Loading…
Add table
Reference in a new issue