0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-02 04:19:08 -05:00

Decouple shape moving logic.

This commit is contained in:
Andrey Antukh 2015-12-30 01:25:26 +02:00
parent e07ada5ad4
commit 7a71b088f4
3 changed files with 25 additions and 12 deletions

View file

@ -1,10 +1,11 @@
(ns uxbox.data.workspace (ns uxbox.data.workspace
(:require [uxbox.rstore :as rs] (:require [bouncer.validators :as v]
[uxbox.rstore :as rs]
[uxbox.router :as r] [uxbox.router :as r]
[uxbox.state :as st] [uxbox.state :as st]
[uxbox.schema :as sc] [uxbox.schema :as sc]
[uxbox.time :as time] [uxbox.time :as time]
[bouncer.validators :as v])) [uxbox.shapes :as shapes]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schemas ;; Schemas
@ -127,14 +128,11 @@
(-pr-writer [mv writer _] (-pr-writer [mv writer _]
(-write writer "#<event:u.d.w/initialize>")))) (-write writer "#<event:u.d.w/initialize>"))))
(defn apply-delta (defn move-shape
"Mark a shape selected for drawing in the canvas." "Mark a shape selected for drawing in the canvas."
[sid [dx dy :as delta]] [sid [dx dy :as delta]]
(reify (reify
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes-by-id sid])]
(update-in state [:shapes-by-id sid] merge (update-in state [:shapes-by-id sid] shapes/-move {:dx dx :dy dy})))))
{:x (+ (:x shape) dx)
:y (+ (:y shape) dy)})))))

View file

@ -21,9 +21,7 @@
;; Api ;; Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti -render (declare -render)
(fn [shape attrs]
(:type shape)))
(defn render (defn render
([shape] (-render shape nil)) ([shape] (-render shape nil))
@ -33,6 +31,18 @@
;; Implementation ;; Implementation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- dispatch-by-type
[shape props]
(:type shape))
(defmulti -render
dispatch-by-type
:hierarchy #'+hierarchy+)
(defmulti -move
dispatch-by-type
:hierarchy #'+hierarchy+)
(defn transform-attrs (defn transform-attrs
[{:keys [view-box] :as data}] [{:keys [view-box] :as data}]
(if view-box (if view-box
@ -65,3 +75,8 @@
[:svg attrs [:svg attrs
[:image image]]))) [:image image]])))
(defmethod -move ::shape
[shape {:keys [dx dy] :as opts}]
(assoc shape
:x (+ (:x shape) dx)
:y (+ (:y shape) dy)))

View file

@ -91,8 +91,8 @@
(as-> mouse-delta-s $ (as-> mouse-delta-s $
(rx/filter #(deref shapes-dragging?) $) (rx/filter #(deref shapes-dragging?) $)
(rx/on-value $ (fn [delta] (rx/on-value $ (fn [delta]
(doseq [id @selected-state] (doseq [sid @selected-state]
(rs/emit! (dw/apply-delta id delta))))))) (rs/emit! (dw/move-shape sid delta)))))))
;; Materialized views ;; Materialized views