0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 12:59:12 -05:00
penpot/frontend/uxbox/data/workspace.cljs

139 lines
4.1 KiB
Text
Raw Normal View History

2015-12-16 18:08:28 +02:00
(ns uxbox.data.workspace
2015-12-30 01:25:26 +02:00
(:require [bouncer.validators :as v]
[uxbox.rstore :as rs]
2015-12-16 18:08:28 +02:00
[uxbox.router :as r]
[uxbox.state :as st]
[uxbox.schema :as sc]
[uxbox.time :as time]
2015-12-30 01:25:26 +02:00
[uxbox.shapes :as shapes]))
2015-12-16 18:08:28 +02:00
2015-12-28 16:33:14 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schemas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:static +shape-props-schema+
{:x [v/required v/integer]
:y [v/required v/integer]
:width [v/required v/integer]
:height [v/required v/integer]})
2015-12-29 23:38:09 +02:00
(def ^:static +shape-schema+
{:x [v/integer]
:y [v/integer]
:width [v/integer]
:height [v/integer]
:type [v/required sc/shape-type]})
2015-12-16 18:08:28 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Events
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn toggle-tool
2015-12-28 16:33:14 +02:00
"Toggle the enabled flag of the specified tool."
[toolname]
2015-12-16 18:08:28 +02:00
(reify
rs/UpdateEvent
(-apply-update [_ state]
(let [key (keyword (str (name toolname) "-enabled"))]
(update-in state [:workspace key] (fnil not false))))
2015-12-16 18:08:28 +02:00
IPrintWithWriter
(-pr-writer [mv writer _]
2015-12-28 16:33:14 +02:00
(-write writer "#<event:u.d.w/toggle-tool>"))))
(defn toggle-toolbox
2015-12-28 16:33:14 +02:00
"Toggle the visibility flag of the specified toolbox."
[toolname]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(let [key (keyword (str (name toolname) "-toolbox-enabled"))
val (get-in state [:workspace key] false)
state (assoc-in state [:workspace key] (not val))]
(if val
(update-in state [:workspace :toolboxes] disj toolname)
(update-in state [:workspace :toolboxes] conj toolname))))
IPrintWithWriter
(-pr-writer [mv writer _]
2015-12-28 16:33:14 +02:00
(-write writer "#<event:u.d.w/toggle-toolbox>"))))
(defn select-for-drawing
"Mark a shape selected for drawing in the canvas."
[shape]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(println "select-for-drawing" shape)
(if shape
(assoc-in state [:workspace :drawing] shape)
(update-in state [:workspace] dissoc :drawing)))
IPrintWithWriter
(-pr-writer [mv writer _]
(-write writer "#<event:u.d.w/select-for-drawing>"))))
2015-12-28 20:39:24 +02:00
(defn select-shape
"Mark a shape selected for drawing in the canvas."
[id]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(let [selected (get-in state [:workspace :selected])]
(if (contains? selected id)
(update-in state [:workspace :selected] disj id)
(update-in state [:workspace :selected] conj id))))))
(defn deselect-all
"Mark a shape selected for drawing in the canvas."
[]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(assoc-in state [:workspace :selected] #{}))))
2015-12-28 20:39:24 +02:00
2015-12-28 16:33:14 +02:00
(defn add-shape
"Mark a shape selected for drawing in the canvas."
[shape props]
2015-12-29 23:40:01 +02:00
(sc/validate! +shape-schema+ shape)
2015-12-28 16:33:14 +02:00
(sc/validate! +shape-props-schema+ props)
(reify
rs/UpdateEvent
(-apply-update [_ state]
2015-12-29 15:51:47 +02:00
(let [sid (random-uuid)
pid (get-in state [:workspace :page])
shape (merge shape props {:id sid :page pid})]
2015-12-28 20:06:59 +02:00
(as-> state $
2015-12-29 15:51:47 +02:00
(update-in $ [:pages-by-id pid :shapes] conj sid)
(assoc-in $ [:shapes-by-id sid] shape))))
2015-12-28 16:33:14 +02:00
IPrintWithWriter
(-pr-writer [mv writer _]
(-write writer "#<event:u.d.w/add-shape>"))))
(defn initialize
2015-12-28 16:33:14 +02:00
"Initialize the workspace state."
[projectid pageid]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(let [s {:project projectid
:toolboxes #{}
2015-12-28 14:31:33 +02:00
:drawing nil
:selected #{}
:page pageid}]
(assoc state :workspace s)))
IPrintWithWriter
(-pr-writer [mv writer _]
2015-12-28 16:33:14 +02:00
(-write writer "#<event:u.d.w/initialize>"))))
2015-12-30 01:25:26 +02:00
(defn move-shape
2015-12-28 20:06:59 +02:00
"Mark a shape selected for drawing in the canvas."
2015-12-29 15:51:47 +02:00
[sid [dx dy :as delta]]
2015-12-28 20:06:59 +02:00
(reify
rs/UpdateEvent
(-apply-update [_ state]
2015-12-29 15:51:47 +02:00
(let [shape (get-in state [:shapes-by-id sid])]
2015-12-30 01:25:26 +02:00
(update-in state [:shapes-by-id sid] shapes/-move {:dx dx :dy dy})))))