mirror of
https://github.com/penpot/penpot.git
synced 2025-08-06 11:28:28 +02:00
Refactor shapes movement related code.
This commit is contained in:
parent
4185849102
commit
45255ab932
3 changed files with 70 additions and 53 deletions
|
@ -24,7 +24,7 @@
|
||||||
[uxbox.ui.mixins :as mx]
|
[uxbox.ui.mixins :as mx]
|
||||||
[uxbox.ui.workspace.base :as uuwb]
|
[uxbox.ui.workspace.base :as uuwb]
|
||||||
[uxbox.ui.workspace.drawarea :refer (draw-area)]
|
[uxbox.ui.workspace.drawarea :refer (draw-area)]
|
||||||
[uxbox.ui.workspace.canvas.movement]
|
[uxbox.ui.workspace.movement :as cmov]
|
||||||
[uxbox.ui.workspace.canvas.resize]
|
[uxbox.ui.workspace.canvas.resize]
|
||||||
[uxbox.ui.workspace.canvas.ruler :refer (ruler)]
|
[uxbox.ui.workspace.canvas.ruler :refer (ruler)]
|
||||||
[uxbox.ui.workspace.canvas.selection :refer (shapes-selection)]
|
[uxbox.ui.workspace.canvas.selection :refer (shapes-selection)]
|
||||||
|
@ -155,23 +155,28 @@
|
||||||
|
|
||||||
(let [key1 (events/listen js/document EventType.MOUSEMOVE on-mousemove)
|
(let [key1 (events/listen js/document EventType.MOUSEMOVE on-mousemove)
|
||||||
key2 (events/listen js/document EventType.KEYDOWN on-key-down)
|
key2 (events/listen js/document EventType.KEYDOWN on-key-down)
|
||||||
key3 (events/listen js/document EventType.KEYUP on-key-up)]
|
key3 (events/listen js/document EventType.KEYUP on-key-up)
|
||||||
(assoc own ::key1 key1 ::key2 key2 ::key3 key3))))
|
sub1 (cmov/watch-move-actions)]
|
||||||
|
(assoc own
|
||||||
|
::sub1 sub1
|
||||||
|
::key1 key1
|
||||||
|
::key2 key2
|
||||||
|
::key3 key3))))
|
||||||
|
|
||||||
(defn- viewport-will-unmount
|
(defn- viewport-will-unmount
|
||||||
[own]
|
[own]
|
||||||
(let [key1 (::key1 own)
|
(events/unlistenByKey (::key1 own))
|
||||||
key2 (::key2 own)
|
(events/unlistenByKey (::key2 own))
|
||||||
key3 (::key3 own)]
|
(events/unlistenByKey (::key3 own))
|
||||||
(events/unlistenByKey key1)
|
(.close (::sub1 own))
|
||||||
(events/unlistenByKey key2)
|
(dissoc own ::key1 ::key2 ::key3 ::sub1))
|
||||||
(events/unlistenByKey key3)
|
|
||||||
(dissoc own ::key1 ::key2 ::key3)))
|
|
||||||
|
|
||||||
(defn- viewport-transfer-state
|
(defn- viewport-transfer-state
|
||||||
[old-own own]
|
[old-own own]
|
||||||
(let [data (select-keys old-own [::key1 ::key2 ::key3])]
|
(->> [::key1 ::key2 ::key3
|
||||||
(merge own data)))
|
::sub1]
|
||||||
|
(select-keys old-own)
|
||||||
|
(merge own)))
|
||||||
|
|
||||||
(def viewport
|
(def viewport
|
||||||
(mx/component
|
(mx/component
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
;; 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-2016 Andrey Antukh <niwi@niwi.nz>
|
|
||||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
|
||||||
|
|
||||||
(ns uxbox.ui.workspace.canvas.movement
|
|
||||||
(:require-macros [uxbox.util.syntax :refer [define-once]])
|
|
||||||
(:require [beicon.core :as rx]
|
|
||||||
[lentes.core :as l]
|
|
||||||
[uxbox.rstore :as rs]
|
|
||||||
[uxbox.state :as st]
|
|
||||||
[uxbox.ui.core :as uuc]
|
|
||||||
[uxbox.ui.workspace.base :as wb]
|
|
||||||
[uxbox.data.shapes :as uds]
|
|
||||||
[uxbox.util.geom.point :as gpt]))
|
|
||||||
|
|
||||||
(define-once :movement-subscription
|
|
||||||
(letfn [(on-value [delta]
|
|
||||||
(let [pageid (get-in @st/state [:workspace :page])
|
|
||||||
selected (get-in @st/state [:workspace :selected])
|
|
||||||
shapes (->> (vals @wb/shapes-by-id-l)
|
|
||||||
(filter #(= (:page %) pageid))
|
|
||||||
(filter (comp selected :id)))
|
|
||||||
delta (gpt/divide delta @wb/zoom-l)]
|
|
||||||
(doseq [{:keys [id group]} shapes]
|
|
||||||
(rs/emit! (uds/move-shape id delta)))))
|
|
||||||
|
|
||||||
(init []
|
|
||||||
(let [stoper (->> uuc/actions-s
|
|
||||||
(rx/map :type)
|
|
||||||
(rx/filter empty?)
|
|
||||||
(rx/take 1))]
|
|
||||||
(as-> wb/mouse-delta-s $
|
|
||||||
(rx/take-until stoper $)
|
|
||||||
(rx/on-value $ on-value))))]
|
|
||||||
|
|
||||||
(as-> uuc/actions-s $
|
|
||||||
(rx/filter #(= "ui.shape.move" (:type %)) $)
|
|
||||||
(rx/on-value $ init))))
|
|
53
src/uxbox/ui/workspace/movement.cljs
Normal file
53
src/uxbox/ui/workspace/movement.cljs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
;; 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-2016 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||||
|
|
||||||
|
(ns uxbox.ui.workspace.movement
|
||||||
|
"Shape movement in workspace logic."
|
||||||
|
(:require-macros [uxbox.util.syntax :refer [define-once]])
|
||||||
|
(:require [beicon.core :as rx]
|
||||||
|
[lentes.core :as l]
|
||||||
|
[uxbox.rstore :as rs]
|
||||||
|
[uxbox.state :as st]
|
||||||
|
[uxbox.ui.core :as uuc]
|
||||||
|
[uxbox.ui.workspace.base :as wb]
|
||||||
|
[uxbox.data.shapes :as uds]
|
||||||
|
[uxbox.util.geom.point :as gpt]))
|
||||||
|
|
||||||
|
(declare initialize)
|
||||||
|
(declare handle-movement)
|
||||||
|
|
||||||
|
;; --- Public Api
|
||||||
|
|
||||||
|
(defn watch-move-actions
|
||||||
|
[]
|
||||||
|
(as-> uuc/actions-s $
|
||||||
|
(rx/filter #(= "ui.shape.move" (:type %)) $)
|
||||||
|
(rx/on-value $ initialize)))
|
||||||
|
|
||||||
|
;; --- Implementation
|
||||||
|
|
||||||
|
(defn- initialize
|
||||||
|
[]
|
||||||
|
(let [stoper (->> uuc/actions-s
|
||||||
|
(rx/map :type)
|
||||||
|
(rx/filter empty?)
|
||||||
|
(rx/take 1))]
|
||||||
|
(as-> wb/mouse-delta-s $
|
||||||
|
(rx/take-until stoper $)
|
||||||
|
(rx/on-value $ handle-movement))))
|
||||||
|
|
||||||
|
(defn- handle-movement
|
||||||
|
[delta]
|
||||||
|
(let [pageid (get-in @st/state [:workspace :page])
|
||||||
|
selected (get-in @st/state [:workspace :selected])
|
||||||
|
shapes (->> (vals @wb/shapes-by-id-l)
|
||||||
|
(filter #(= (:page %) pageid))
|
||||||
|
(filter (comp selected :id)))
|
||||||
|
delta (gpt/divide delta @wb/zoom-l)]
|
||||||
|
(doseq [{:keys [id group]} shapes]
|
||||||
|
(rs/emit! (uds/move-shape id delta)))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue