🐛 Fix selection handlers reactivity.

This commit is contained in:
Andrey Antukh 2019-08-22 20:18:35 +02:00
parent 5a820b4f9e
commit f0230c346c
3 changed files with 23 additions and 44 deletions

View file

@ -565,7 +565,6 @@
;; --- Outer Rect ;; --- Outer Rect
(declare selection-rect-generic) (declare selection-rect-generic)
(declare selection-rect-group)
(defn rotation-matrix (defn rotation-matrix
"Generate a rotation matrix from shape." "Generate a rotation matrix from shape."
@ -590,25 +589,15 @@
([shape] ([shape]
(selection-rect @st/state shape)) (selection-rect @st/state shape))
([state shape] ([state shape]
(let [{:keys [displacement resize]} (:modifiers shape)] (let [modifier (:modifier-mtx shape)]
(-> (shape->rect-shape shape) (-> (shape->rect-shape shape)
(assoc :type :rect :id (:id shape)) (assoc :type :rect :id (:id shape))
(transform (or resize (gmt/matrix))) (transform (or modifier (gmt/matrix)))
(transform (or displacement (gmt/matrix)))
(rotate-shape) (rotate-shape)
(size))))) (size)))))
;; --- Helpers ;; --- Helpers
(defn resolve-parent
"Recursively resolve the real shape parent."
([shape]
(resolve-parent @st/state shape))
([state {:keys [group] :as shape}]
(if group
(resolve-parent state (get-in state [:shapes group]))
shape)))
(defn contained-in? (defn contained-in?
"Check if a shape is contained in the "Check if a shape is contained in the
provided selection rect." provided selection rect."

View file

@ -14,7 +14,8 @@
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.shapes.attrs :as attrs] [uxbox.main.ui.shapes.attrs :as attrs]
[uxbox.main.ui.shapes.common :as common] [uxbox.main.ui.shapes.common :as common]
[uxbox.util.data :refer [classnames normalize-props]])) [uxbox.util.data :refer [classnames normalize-props]]
[uxbox.util.geom.matrix :as gmt]))
;; --- Path Component ;; --- Path Component
@ -22,8 +23,7 @@
(mf/defc path-component (mf/defc path-component
[{:keys [shape] :as props}] [{:keys [shape] :as props}]
(let [modifiers (mf/deref (refs/selected-modifiers (:id shape))) (let [selected (mf/deref refs/selected-shapes)
selected (mf/deref refs/selected-shapes)
selected? (contains? selected (:id shape))] selected? (contains? selected (:id shape))]
(letfn [(on-mouse-down [event] (letfn [(on-mouse-down [event]
(common/on-mouse-down event shape selected)) (common/on-mouse-down event shape selected))
@ -35,7 +35,6 @@
:on-double-click on-double-click :on-double-click on-double-click
:on-mouse-down on-mouse-down} :on-mouse-down on-mouse-down}
[:& path-shape {:shape shape [:& path-shape {:shape shape
:modifiers modifiers
:background? true}]]))) :background? true}]])))
;; --- Path Shape ;; --- Path Shape
@ -62,12 +61,13 @@
(recur buffer (inc index))))))) (recur buffer (inc index)))))))
(mf/defc path-shape (mf/defc path-shape
[{:keys [shape modifiers background?] :as props}] [{:keys [shape background?] :as props}]
(let [{:keys [resize displacement]} modifiers (let [modifier-mtx (:modifier-mtx shape)
shape (cond-> shape shape (cond
displacement (geom/transform displacement) (gmt/matrix? modifier-mtx) (geom/transform shape modifier-mtx)
resize (geom/transform resize)) :else shape)
moving? (boolean displacement)
moving? (boolean modifier-mtx)
pdata (render-path shape) pdata (render-path shape)
props {:id (str (:id shape)) props {:id (str (:id shape))

View file

@ -190,9 +190,8 @@
:style {:cursor "pointer"}}])]))) :style {:cursor "pointer"}}])])))
(mf/defc multiple-selection-handlers (mf/defc multiple-selection-handlers
[{:keys [shapes modifiers zoom] :as props}] [{:keys [shapes zoom] :as props}]
(let [shape (->> shapes (let [shape (->> shapes
(map #(assoc % :modifiers (get modifiers (:id %))))
(map #(geom/selection-rect %)) (map #(geom/selection-rect %))
(geom/shapes->rect-shape) (geom/shapes->rect-shape)
(geom/selection-rect)) (geom/selection-rect))
@ -202,18 +201,9 @@
:zoom zoom :zoom zoom
:on-click on-click}])) :on-click on-click}]))
(mf/defc single-selection-handlers
[{:keys [shape zoom modifiers] :as props}]
(let [on-click #(do (dom/stop-propagation %2)
(start-resize %1 #{(:id shape)} shape))
shape (-> (assoc shape :modifiers modifiers)
(geom/selection-rect))]
[:& controls {:shape shape :zoom zoom :on-click on-click}]))
(mf/defc text-edition-selection-handlers (mf/defc text-edition-selection-handlers
[{:keys [shape modifiers zoom] :as props}] [{:keys [shape zoom] :as props}]
(let [{:keys [x1 y1 width height] :as shape} (-> (assoc shape :modifiers modifiers) (let [{:keys [x1 y1 width height] :as shape} (geom/selection-rect shape)]
(geom/selection-rect))]
[:g.controls [:g.controls
[:rect.main {:x x1 :y y1 [:rect.main {:x x1 :y y1
:width width :width width
@ -224,6 +214,13 @@
:stroke-opacity "0.5" :stroke-opacity "0.5"
:fill "transparent"}}]])) :fill "transparent"}}]]))
(mf/defc single-selection-handlers
[{:keys [shape zoom] :as props}]
(let [on-click #(do (dom/stop-propagation %2)
(start-resize %1 #{(:id shape)} shape))
shape (geom/selection-rect shape)]
[:& controls {:shape shape :zoom zoom :on-click on-click}]))
(def ^:private shapes-map-iref (def ^:private shapes-map-iref
(-> (l/key :shapes) (-> (l/key :shapes)
(l/derive st/state))) (l/derive st/state)))
@ -233,34 +230,27 @@
(let [shapes-map (mf/deref shapes-map-iref) (let [shapes-map (mf/deref shapes-map-iref)
shapes (map #(get shapes-map %) (:selected wst)) shapes (map #(get shapes-map %) (:selected wst))
edition? (:edition wst) edition? (:edition wst)
modifiers (:modifiers wst)
zoom (:zoom wst 1) zoom (:zoom wst 1)
num (count shapes) num (count shapes)
{:keys [id type] :as shape} (first shapes)] {:keys [id type] :as shape} (first shapes)]
(cond (cond
(zero? num) (zero? num)
nil nil
(> num 1) (> num 1)
[:& multiple-selection-handlers {:shapes shapes [:& multiple-selection-handlers {:shapes shapes
:modifiers modifiers
:zoom zoom}] :zoom zoom}]
(and (= type :text) (and (= type :text)
(= edition? (:id shape))) (= edition? (:id shape)))
[:& text-edition-selection-handlers {:shape shape [:& text-edition-selection-handlers {:shape shape
:modifiers (get modifiers id)
:zoom zoom}] :zoom zoom}]
(and (= type :path) (and (= type :path)
(= edition? (:id shape))) (= edition? (:id shape)))
[:& path-edition-selection-handlers {:shape shape [:& path-edition-selection-handlers {:shape shape
:zoom zoom :zoom zoom}]
:modifiers (get modifiers id)}]
:else :else
[:& single-selection-handlers {:shape shape [:& single-selection-handlers {:shape shape
:modifiers (get modifiers id)
:zoom zoom}]))) :zoom zoom}])))