0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 17:00:36 -05:00
penpot/frontend/uxbox/data/projects.cljs

132 lines
4 KiB
Text
Raw Normal View History

2015-12-14 07:17:18 -05:00
(ns uxbox.data.projects
(:require [uxbox.rstore :as rs]
2015-12-14 13:31:21 -05:00
[uxbox.router :as r]
2015-12-14 07:17:18 -05:00
[uxbox.state :as st]
[uxbox.schema :as sc]
[uxbox.time :as time]
2015-12-14 07:17:18 -05:00
[bouncer.validators :as v]))
2015-12-14 13:31:21 -05:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schemas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-14 07:17:18 -05:00
(def ^:static +project-schema+
{:name [v/required v/string]
:width [v/required v/integer]
:height [v/required v/integer]
2015-12-14 13:31:21 -05:00
:layout [v/required sc/keyword]})
2015-12-14 07:17:18 -05:00
2015-12-14 13:31:21 -05:00
(def ^:static +page-schema+
{:name [v/required v/string]
:width [v/required v/integer]
:height [v/required v/integer]
:project [v/required sc/uuid]})
2015-12-14 16:57:47 -05:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn assoc-project
"A reduce function for assoc the project
to the state map."
[state proj]
(let [uuid (:id proj)]
(update-in state [:projects-by-id] assoc uuid proj)))
(defn assoc-page
"A reduce function for assoc the page
to the state map."
[state page]
(let [uuid (:id page)]
(update-in state [:pages-by-id] assoc uuid page)))
(defn project-pages
"Get a ordered list of pages that
belongs to a specified project."
[state projectid]
(->> (vals (:pages-by-id state))
(filter #(= projectid (:project %)))
(sort-by :created)
(into [])))
2015-12-14 16:57:47 -05:00
2015-12-14 13:31:21 -05:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Events
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn create-page
[{:keys [name width height project] :as data}]
(sc/validate! +page-schema+ data)
2015-12-14 07:17:18 -05:00
(reify
rs/UpdateEvent
(-apply-update [_ state]
2015-12-14 16:57:47 -05:00
(let [page {:id (random-uuid)
2015-12-14 13:31:21 -05:00
:project project
:created (time/now :unix)
2015-12-14 07:17:18 -05:00
:name name
:width width
2015-12-14 13:31:21 -05:00
:height height}]
2015-12-14 16:57:47 -05:00
(assoc-page state page)))
2015-12-14 07:17:18 -05:00
IPrintWithWriter
(-pr-writer [mv writer _]
2015-12-14 13:31:21 -05:00
(-write writer "#<event:u.s.p/create-page>"))))
(defn create-project
[{:keys [name width height layout] :as data}]
(sc/validate! +project-schema+ data)
(let [uuid (random-uuid)]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(let [proj {:id uuid
:name name
:width width
:created (time/now :unix)
2015-12-14 13:31:21 -05:00
:height height
2015-12-14 16:57:47 -05:00
:layout layout}]
(assoc-project state proj)))
2015-12-14 13:31:21 -05:00
rs/EffectEvent
(-apply-effect [_ state]
(rs/emit! (create-page {:name "Page 1"
:width width
:height height
:project uuid})))
IPrintWithWriter
(-pr-writer [mv writer _]
(-write writer "#<event:u.s.p/create-project>")))))
(defn initialize-workspace
[projectid pageid]
(reify
rs/UpdateEvent
(-apply-update [_ state]
(assoc state :workspace {:project projectid
:page pageid
:toolboxes {}}))
2015-12-14 13:31:21 -05:00
IPrintWithWriter
(-pr-writer [mv writer _]
2015-12-14 16:57:47 -05:00
(-write writer "#<event:u.s.p/initialize-workspace>"))))
2015-12-14 13:31:21 -05:00
(defn go-to-project
"A shortcut event that redirects the user to the
first page of the project."
([projectid]
(go-to-project projectid nil))
([projectid pageid]
(reify
rs/EffectEvent
(-apply-effect [_ state]
(if pageid
(rs/emit! (r/navigate :main/page {:project-uuid projectid
:page-uuid pageid}))
(let [pages (project-pages state projectid)
pageid (:id (first pages))]
2015-12-14 13:31:21 -05:00
(rs/emit! (r/navigate :main/page {:project-uuid projectid
:page-uuid pageid})))))
2015-12-14 13:31:21 -05:00
IPrintWithWriter
(-pr-writer [mv writer _]
(-write writer "#<event:u.s.p/go-to-project")))))