0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-14 00:31:34 -05:00

Rename shapes api.

This commit is contained in:
Andrey Antukh 2016-02-26 19:52:48 +02:00
parent 1572c4cdd3
commit 1a44b75790
6 changed files with 58 additions and 58 deletions
src/uxbox

View file

@ -119,7 +119,7 @@
(filter #(= (:page %) pageid)) (filter #(= (:page %) pageid))
(remove :hidden) (remove :hidden)
(remove :blocked) (remove :blocked)
(map sh/-outer-rect) (map sh/outer-rect')
(filter #(sh/contained-in? % selrect)) (filter #(sh/contained-in? % selrect))
(map :id))] (map :id))]
(->> (into #{} xf (vals (:shapes-by-id state))) (->> (into #{} xf (vals (:shapes-by-id state)))
@ -152,7 +152,7 @@
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] sh/-move delta))))) (update-in state [:shapes-by-id sid] sh/move delta)))))
(defn update-line-attrs (defn update-line-attrs
[sid {:keys [x1 y1 x2 y2] :as opts}] [sid {:keys [x1 y1 x2 y2] :as opts}]
@ -163,7 +163,7 @@
(let [shape (get-in state [:shapes-by-id sid]) (let [shape (get-in state [:shapes-by-id sid])
props (select-keys opts [:x1 :y1 :x2 :y2]) props (select-keys opts [:x1 :y1 :x2 :y2])
props' (select-keys shape [:x1 :y1 :x2 :y2])] props' (select-keys shape [:x1 :y1 :x2 :y2])]
(update-in state [:shapes-by-id sid] sh/-initialize (update-in state [:shapes-by-id sid] sh/initialize
(merge props' props)))))) (merge props' props))))))
(defn update-rotation (defn update-rotation
@ -175,7 +175,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] (update-in state [:shapes-by-id sid]
sh/-rotate rotation)))) sh/rotate rotation))))
(defn update-size (defn update-size
"A helper event just for update the position "A helper event just for update the position
@ -190,8 +190,8 @@
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])
size (merge (sh/-size shape) opts)] size (merge (sh/size shape) opts)]
(update-in state [:shapes-by-id sid] sh/-resize' size))))) (update-in state [:shapes-by-id sid] sh/resize' size)))))
(defn update-position (defn update-position
"Update the start position coordenate of the shape." "Update the start position coordenate of the shape."
@ -200,7 +200,7 @@
(reify (reify
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] sh/-move' opts)))) (update-in state [:shapes-by-id sid] sh/move' opts))))
(defn update-fill-attrs (defn update-fill-attrs
[sid {:keys [color opacity] :as opts}] [sid {:keys [color opacity] :as opts}]

View file

@ -31,27 +31,27 @@
[shape & params] [shape & params]
(:type shape)) (:type shape))
(defmulti -move (defmulti move
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
(defmulti -move' (defmulti move'
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
(defmulti -resize (defmulti resize
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
(defmulti -resize' (defmulti resize'
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
(defmulti -size (defmulti size
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
(defmulti -rotate (defmulti rotate
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
@ -60,7 +60,7 @@
;; for calculate the shape or shapes selection ;; for calculate the shape or shapes selection
;; rectangle. ;; rectangle.
(defmulti -outer-rect (defmulti outer-rect'
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
@ -68,7 +68,7 @@
;; from initial shape data structure and final ;; from initial shape data structure and final
;; canvas position. ;; canvas position.
(defmulti -initialize (defmulti initialize
dispatch-by-type dispatch-by-type
:hierarchy #'+hierarchy+) :hierarchy #'+hierarchy+)
@ -82,7 +82,7 @@
;; Initialize ;; Initialize
(defmethod -initialize ::shape (defmethod initialize ::shape
[shape {:keys [x1 y1 x2 y2]}] [shape {:keys [x1 y1 x2 y2]}]
(assoc shape (assoc shape
:x1 x1 :x1 x1
@ -92,11 +92,11 @@
;; :width (- x2 x1) ;; :width (- x2 x1)
;; :height (- y2 y1))) ;; :height (- y2 y1)))
(defmethod -initialize :builtin/group (defmethod initialize :builtin/group
[shape {:keys [x1 y1 x2 y2] :as props}] [shape {:keys [x1 y1 x2 y2] :as props}]
(assoc shape ::initial props)) (assoc shape ::initial props))
(defmethod -initialize :builtin/line (defmethod initialize :builtin/line
[shape {:keys [x1 y1 x2 y2]}] [shape {:keys [x1 y1 x2 y2]}]
(assoc shape (assoc shape
:x1 x1 :x1 x1
@ -104,7 +104,7 @@
:x2 x2 :x2 x2
:y2 y2)) :y2 y2))
(defmethod -initialize :builtin/circle (defmethod initialize :builtin/circle
[shape {:keys [x1 y1 x2 y2]}] [shape {:keys [x1 y1 x2 y2]}]
(assoc shape (assoc shape
:cx x1 :cx x1
@ -114,11 +114,11 @@
;; FIXME: lock mode ;; FIXME: lock mode
(defmethod -resize :builtin/line (defmethod resize :builtin/line
[shape {:keys [x y] :as pos}] [shape {:keys [x y] :as pos}]
(assoc shape :x2 x :y2 y)) (assoc shape :x2 x :y2 y))
(defmethod -resize :builtin/circle (defmethod resize :builtin/circle
[shape {:keys [x y lock] :as pos}] [shape {:keys [x y lock] :as pos}]
(let [cx (:cx shape) (let [cx (:cx shape)
cy (:cy shape) cy (:cy shape)
@ -129,17 +129,17 @@
(assoc shape :rx rx :ry rx) (assoc shape :rx rx :ry rx)
(assoc shape :rx rx :ry ry)))) (assoc shape :rx rx :ry ry))))
(defmethod -resize :builtin/rect (defmethod resize :builtin/rect
[shape {:keys [x y lock] :as pos}] [shape {:keys [x y lock] :as pos}]
(if lock (if lock
(assoc shape :x2 x :y2 x) (assoc shape :x2 x :y2 x)
(assoc shape :x2 x :y2 y))) (assoc shape :x2 x :y2 y)))
(defmethod -resize :default (defmethod resize :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
(defmethod -resize' ::rect (defmethod resize' ::rect
[shape {:keys [width height] :as size}] [shape {:keys [width height] :as size}]
(let [x1 (:x1 shape) (let [x1 (:x1 shape)
y1 (:y1 shape)] y1 (:y1 shape)]
@ -147,22 +147,22 @@
:x2 (+ x1 width) :x2 (+ x1 width)
:y2 (+ y1 height)))) :y2 (+ y1 height))))
(defmethod -resize' :default (defmethod resize' :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
(defmethod -size ::rect (defmethod size ::rect
[{:keys [x1 y1 x2 y2] :as shape}] [{:keys [x1 y1 x2 y2] :as shape}]
{:width (- x2 x1) {:width (- x2 x1)
:height (- y2 y1)}) :height (- y2 y1)})
(defmethod -size :default (defmethod size :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
;; Move ;; Move
(defmethod -move ::rect (defmethod move ::rect
[shape {dx :x dy :y}] [shape {dx :x dy :y}]
(assoc shape (assoc shape
:x1 (+ (:x1 shape) dx) :x1 (+ (:x1 shape) dx)
@ -170,13 +170,13 @@
:x2 (+ (:x2 shape) dx) :x2 (+ (:x2 shape) dx)
:y2 (+ (:y2 shape) dy))) :y2 (+ (:y2 shape) dy)))
(defmethod -move :builtin/group (defmethod move :builtin/group
[shape {dx :x dy :y}] [shape {dx :x dy :y}]
(assoc shape (assoc shape
:dx (+ (:dx shape 0) dx) :dx (+ (:dx shape 0) dx)
:dy (+ (:dy shape 0) dy))) :dy (+ (:dy shape 0) dy)))
(defmethod -move :builtin/line (defmethod move :builtin/line
[shape {dx :x dy :y}] [shape {dx :x dy :y}]
(assoc shape (assoc shape
:x1 (+ (:x1 shape) dx) :x1 (+ (:x1 shape) dx)
@ -184,54 +184,54 @@
:x2 (+ (:x2 shape) dx) :x2 (+ (:x2 shape) dx)
:y2 (+ (:y2 shape) dy))) :y2 (+ (:y2 shape) dy)))
(defmethod -move :builtin/circle (defmethod move :builtin/circle
[shape {dx :x dy :y}] [shape {dx :x dy :y}]
(assoc shape (assoc shape
:cx (+ (:cx shape) dx) :cx (+ (:cx shape) dx)
:cy (+ (:cy shape) dy))) :cy (+ (:cy shape) dy)))
(defmethod -move :default (defmethod move :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
(defmethod -move' ::rect (defmethod move' ::rect
[shape {:keys [x y] :as pos}] [shape {:keys [x y] :as pos}]
(let [dx (if x (- (:x1 shape) x) 0) (let [dx (if x (- (:x1 shape) x) 0)
dy (if y (- (:y1 shape) y) 0)] dy (if y (- (:y1 shape) y) 0)]
(-move shape (gpt/point dx dy)))) (move shape (gpt/point dx dy))))
(defmethod -move' :builtin/line (defmethod move' :builtin/line
[shape {:keys [x y] :as pos}] [shape {:keys [x y] :as pos}]
(let [dx (if x (- (:x1 shape) x) 0) (let [dx (if x (- (:x1 shape) x) 0)
dy (if y (- (:y1 shape) y) 0)] dy (if y (- (:y1 shape) y) 0)]
(-move shape (gpt/point dx dy)))) (move shape (gpt/point dx dy))))
(defmethod -move' :builtin/circle (defmethod move' :builtin/circle
[shape {:keys [x y] :as pos}] [shape {:keys [x y] :as pos}]
(let [dx (if x (- (:cx shape) x) 0) (let [dx (if x (- (:cx shape) x) 0)
dy (if y (- (:cy shape) y) 0)] dy (if y (- (:cy shape) y) 0)]
(-move shape (gpt/point dx dy)))) (move shape (gpt/point dx dy))))
(defmethod -move' :default (defmethod move' :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
(defmethod -rotate ::shape (defmethod rotate ::shape
[shape rotation] [shape rotation]
(assoc shape :rotation rotation)) (assoc shape :rotation rotation))
(declare container-rect) (declare container-rect)
(defmethod -outer-rect ::shape (defmethod outer-rect' ::shape
[{:keys [group] :as shape}] [{:keys [group] :as shape}]
(let [group (get-in @st/state [:shapes-by-id group])] (let [group (get-in @st/state [:shapes-by-id group])]
(as-> shape $ (as-> shape $
(assoc $ :x (+ (:x1 shape) (:dx group 0))) (assoc $ :x (+ (:x1 shape) (:dx group 0)))
(assoc $ :y (+ (:y1 shape) (:dy group 0))) (assoc $ :y (+ (:y1 shape) (:dy group 0)))
(merge $ (-size $)) (merge $ (size $))
(container-rect $)))) (container-rect $))))
(defmethod -outer-rect :builtin/line (defmethod outer-rect' :builtin/line
[{:keys [x1 y1 x2 y2 group] :as shape}] [{:keys [x1 y1 x2 y2 group] :as shape}]
(let [group (get-in @st/state [:shapes-by-id group]) (let [group (get-in @st/state [:shapes-by-id group])
props {:x (+ x1 (:dx group 0)) props {:x (+ x1 (:dx group 0))
@ -241,7 +241,7 @@
(-> (merge shape props) (-> (merge shape props)
(container-rect)))) (container-rect))))
(defmethod -outer-rect :builtin/circle (defmethod outer-rect' :builtin/circle
[{:keys [cx cy rx ry group] :as shape}] [{:keys [cx cy rx ry group] :as shape}]
(let [group (get-in @st/state [:shapes-by-id group]) (let [group (get-in @st/state [:shapes-by-id group])
props {:x (+ (- cx rx) (:dx group 0)) props {:x (+ (- cx rx) (:dx group 0))
@ -251,11 +251,11 @@
(-> (merge shape props) (-> (merge shape props)
(container-rect)))) (container-rect))))
(defmethod -outer-rect :builtin/group (defmethod outer-rect' :builtin/group
[{:keys [id group rotation dx dy] :as shape}] [{:keys [id group rotation dx dy] :as shape}]
(let [shapes (->> (:items shape) (let [shapes (->> (:items shape)
(map #(get-in @st/state [:shapes-by-id %])) (map #(get-in @st/state [:shapes-by-id %]))
(map -outer-rect)) (map outer-rect'))
x (apply min (map :x shapes)) x (apply min (map :x shapes))
y (apply min (map :y shapes)) y (apply min (map :y shapes))
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes)) x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))
@ -271,13 +271,13 @@
}) })
(container-rect $))))) (container-rect $)))))
(defmethod -outer-rect :default (defmethod outer-rect' :default
[shape _] [shape _]
(throw (ex-info "Not implemented" (select-keys shape [:type])))) (throw (ex-info "Not implemented" (select-keys shape [:type]))))
(defmethod -transformation :builtin/icon (defmethod -transformation :builtin/icon
[{:keys [x1 y1 rotation view-box] :or {rotation 0} :as shape}] [{:keys [x1 y1 rotation view-box] :or {rotation 0} :as shape}]
(let [{:keys [width height]} (-size shape) (let [{:keys [width height]} (size shape)
orig-width (nth view-box 2) orig-width (nth view-box 2)
orig-height (nth view-box 3) orig-height (nth view-box 3)
scale-x (/ width orig-width) scale-x (/ width orig-width)
@ -293,7 +293,7 @@
(defmethod -transformation :builtin/rect (defmethod -transformation :builtin/rect
[{:keys [x1 y1 rotation] :or {rotation 0} :as shape}] [{:keys [x1 y1 rotation] :or {rotation 0} :as shape}]
(let [{:keys [width height]} (-size shape) (let [{:keys [width height]} (size shape)
center-x (+ x1 (/ width 2)) center-x (+ x1 (/ width 2))
center-y (+ y1 (/ height 2))] center-y (+ y1 (/ height 2))]
(-> (gmt/matrix) (-> (gmt/matrix)
@ -408,7 +408,7 @@
(defn outer-rect (defn outer-rect
[shapes] [shapes]
{:pre [(seq shapes)]} {:pre [(seq shapes)]}
(let [shapes (map -outer-rect shapes) (let [shapes (map outer-rect' shapes)
x (apply min (map :x shapes)) x (apply min (map :x shapes))
y (apply min (map :y shapes)) y (apply min (map :y shapes))
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes)) x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))

View file

@ -88,7 +88,7 @@
(defn- handlers-render (defn- handlers-render
[own shape] [own shape]
(let [{:keys [x y width height]} (sh/-outer-rect shape)] (let [{:keys [x y width height]} (sh/outer-rect' shape)]
(html (html
[:g.controls [:g.controls
[:rect {:x x :y y :width width :height height :stroke-dasharray "5,5" [:rect {:x x :y y :width width :height height :stroke-dasharray "5,5"

View file

@ -16,7 +16,7 @@
[{:keys [id x1 y1 x2 y2] :as shape}] [{:keys [id x1 y1 x2 y2] :as shape}]
(let [key (str id) (let [key (str id)
rfm (ush/-transformation shape) rfm (ush/-transformation shape)
size (ush/-size shape) size (ush/size shape)
props {:x x1 :y y1 :id key :key key :transform (str rfm)} props {:x x1 :y y1 :id key :key key :transform (str rfm)}
attrs (-> (uusc/extract-style-attrs shape) attrs (-> (uusc/extract-style-attrs shape)
(merge props size))] (merge props size))]

View file

@ -26,7 +26,7 @@
(let [shape (rum/react +drawing-shape+) (let [shape (rum/react +drawing-shape+)
position (rum/react +drawing-position+)] position (rum/react +drawing-position+)]
(when shape (when shape
(-> (ush/-resize shape position) (-> (ush/resize shape position)
(uusc/render-shape identity))))) (uusc/render-shape identity)))))
(def ^:static draw-area (def ^:static draw-area
@ -45,7 +45,7 @@
(define-once :drawing-subscriptions (define-once :drawing-subscriptions
(letfn [(init-shape [shape] (letfn [(init-shape [shape]
(let [{:keys [x y] :as point} @wb/mouse-canvas-a (let [{:keys [x y] :as point} @wb/mouse-canvas-a
shape (ush/-initialize shape {:x1 x :y1 y :x2 x :y2 y})] shape (ush/initialize shape {:x1 x :y1 y :x2 x :y2 y})]
(reset! +drawing-shape+ shape) (reset! +drawing-shape+ shape)
(reset! +drawing-position+ (assoc point :lock false)) (reset! +drawing-position+ (assoc point :lock false))
@ -63,7 +63,7 @@
(on-complete [] (on-complete []
(let [shape @+drawing-shape+ (let [shape @+drawing-shape+
shpos @+drawing-position+ shpos @+drawing-position+
shape (ush/-resize shape shpos)] shape (ush/resize shape shpos)]
(rs/emit! (dw/add-shape shape) (rs/emit! (dw/add-shape shape)
(dw/select-for-drawing nil)) (dw/select-for-drawing nil))
(reset! +drawing-position+ nil) (reset! +drawing-position+ nil)
@ -72,7 +72,7 @@
(init-icon [shape] (init-icon [shape]
(let [{:keys [x y]} @wb/mouse-canvas-a (let [{:keys [x y]} @wb/mouse-canvas-a
props {:x1 x :y1 y :x2 (+ x 100) :y2 (+ y 100)} props {:x1 x :y1 y :x2 (+ x 100) :y2 (+ y 100)}
shape (ush/-initialize shape props)] shape (ush/initialize shape props)]
(rs/emit! (dw/add-shape shape) (rs/emit! (dw/add-shape shape)
(dw/select-for-drawing nil)))) (dw/select-for-drawing nil))))
(init [] (init []

View file

@ -190,7 +190,7 @@
sid (:id shape) sid (:id shape)
props {attr value}] props {attr value}]
(rs/emit! (dw/update-radius-attrs sid props))))] (rs/emit! (dw/update-radius-attrs sid props))))]
(let [size (sh/-size shape)] (let [size (sh/size shape)]
(html (html
[:div.element-set {:key (str (:id menu))} [:div.element-set {:key (str (:id menu))}
[:div.element-set-title (:name menu)] [:div.element-set-title (:name menu)]
@ -282,7 +282,7 @@
sid (:id shape) sid (:id shape)
props {attr value}] props {attr value}]
(rs/emit! (dw/update-position sid props))))] (rs/emit! (dw/update-position sid props))))]
(let [size (sh/-size shape)] (let [size (sh/size shape)]
(html (html
[:div.element-set {:key (str (:id menu))} [:div.element-set {:key (str (:id menu))}
[:div.element-set-title (:name menu)] [:div.element-set-title (:name menu)]