0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 23:49:45 -05:00

Refactor shapes movement related code.

This commit is contained in:
Andrey Antukh 2016-04-06 19:47:39 +03:00
parent 4185849102
commit 45255ab932
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 70 additions and 53 deletions

View file

@ -24,7 +24,7 @@
[uxbox.ui.mixins :as mx]
[uxbox.ui.workspace.base :as uuwb]
[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.ruler :refer (ruler)]
[uxbox.ui.workspace.canvas.selection :refer (shapes-selection)]
@ -155,23 +155,28 @@
(let [key1 (events/listen js/document EventType.MOUSEMOVE on-mousemove)
key2 (events/listen js/document EventType.KEYDOWN on-key-down)
key3 (events/listen js/document EventType.KEYUP on-key-up)]
(assoc own ::key1 key1 ::key2 key2 ::key3 key3))))
key3 (events/listen js/document EventType.KEYUP on-key-up)
sub1 (cmov/watch-move-actions)]
(assoc own
::sub1 sub1
::key1 key1
::key2 key2
::key3 key3))))
(defn- viewport-will-unmount
[own]
(let [key1 (::key1 own)
key2 (::key2 own)
key3 (::key3 own)]
(events/unlistenByKey key1)
(events/unlistenByKey key2)
(events/unlistenByKey key3)
(dissoc own ::key1 ::key2 ::key3)))
(events/unlistenByKey (::key1 own))
(events/unlistenByKey (::key2 own))
(events/unlistenByKey (::key3 own))
(.close (::sub1 own))
(dissoc own ::key1 ::key2 ::key3 ::sub1))
(defn- viewport-transfer-state
[old-own own]
(let [data (select-keys old-own [::key1 ::key2 ::key3])]
(merge own data)))
(->> [::key1 ::key2 ::key3
::sub1]
(select-keys old-own)
(merge own)))
(def viewport
(mx/component

View file

@ -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))))

View 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)))))