0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-02 20:39:09 -05:00
penpot/frontend/uxbox/ui/workspace/base.cljs

144 lines
4.2 KiB
Text
Raw Normal View History

2015-12-17 12:51:36 +02:00
(ns uxbox.ui.workspace.base
2015-12-17 17:32:06 +02:00
(:require [beicon.core :as rx]
2015-12-17 12:51:36 +02:00
[cats.labs.lens :as l]
[uxbox.rstore :as rs]
[uxbox.state :as s]
[uxbox.data.projects :as dp]
2015-12-28 20:06:59 +02:00
[uxbox.data.workspace :as dw]
2015-12-18 11:18:00 +02:00
[uxbox.util.lens :as ul]
[uxbox.ui.util :as util]
[goog.events :as events])
(:import goog.events.EventType))
2015-12-17 12:51:36 +02:00
2015-12-17 16:43:58 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-17 12:51:36 +02:00
;; Lenses
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:static project-state
2015-12-17 16:43:58 +02:00
(as-> (ul/dep-in [:projects-by-id] [:workspace :project]) $
2015-12-17 12:51:36 +02:00
(l/focus-atom $ s/state)))
(def ^:static page-state
2015-12-17 16:43:58 +02:00
(as-> (ul/dep-in [:pages-by-id] [:workspace :page]) $
2015-12-17 12:51:36 +02:00
(l/focus-atom $ s/state)))
(def ^:static pages-state
2015-12-17 16:43:58 +02:00
(as-> (ul/getter #(let [pid (get-in % [:workspace :project])]
2015-12-17 12:51:36 +02:00
(dp/project-pages % pid))) $
(l/focus-atom $ s/state)))
2015-12-29 15:51:47 +02:00
(def ^:static shapes-state
(as-> (ul/getter (fn [state]
(let [pid (get-in state [:workspace :page])
shapes (->> (vals (:shapes-by-id state))
(filter #(= (:page %) pid)))]
(into [] shapes)))) $
(l/focus-atom $ s/state)))
2015-12-17 12:51:36 +02:00
(def ^:static workspace-state
(as-> (l/in [:workspace]) $
(l/focus-atom $ s/state)))
2015-12-28 20:39:46 +02:00
(def ^:static selected-state
(as-> (l/in [:workspace :selected]) $
(l/focus-atom $ s/state)))
2015-12-17 12:51:36 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Scroll Stream
2015-12-17 12:51:36 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-18 11:18:00 +02:00
(defonce ^:private scroll-bus (rx/bus))
(defonce scroll-s (rx/dedupe scroll-bus))
(defonce top-scroll-s
(->> scroll-bus
(rx/map :top)
(rx/dedupe)))
(defonce left-scroll-s
(->> scroll-bus
(rx/map :left)
(rx/dedupe)))
2015-12-17 12:51:36 +02:00
(defonce top-scroll (rx/to-atom top-scroll-s))
(defonce left-scroll (rx/to-atom left-scroll-s))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mouse Position Stream
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-18 11:18:00 +02:00
(defonce shapes-dragging? (atom false))
2015-12-28 20:06:59 +02:00
(defonce mouse-b (rx/bus))
(defonce mouse-s (rx/dedupe mouse-b))
;; Deltas
(defn- coords-delta
[[old new]]
(let [[oldx oldy] old
[newx newy] new]
[(- newx oldx)
(- newy oldy)]))
(defonce mouse-delta-s
(->> mouse-s
(rx/sample 10)
(rx/buffer 2 1)
(rx/map coords-delta)))
(defonce $$drag-subscription$$
(as-> mouse-delta-s $
(rx/filter #(deref shapes-dragging?) $)
(rx/on-value $ (fn [delta]
(doseq [id @selected-state]
(rs/emit! (dw/apply-delta id delta)))))))
2015-12-28 20:06:59 +02:00
;; Materialized views
(defonce mouse-position (rx/to-atom (rx/sample 50 mouse-s)))
;; (defonce mouse-position2 (rx/to-atom mouse-s))
2015-12-18 11:18:00 +02:00
2015-12-22 20:25:38 +02:00
(defn- mouse-mixin-did-mount
[own]
2015-12-22 20:56:58 +02:00
(letfn [(on-mousemove [event]
(let [canvas (util/get-ref-dom own "canvas")
brect (.getBoundingClientRect canvas)
offset-x (.-left brect)
offset-y (.-top brect)
x (.-clientX event)
y (.-clientY event)]
2015-12-28 20:06:59 +02:00
(rx/push! mouse-b [(- x offset-x)
(- y offset-y)])))]
2015-12-22 21:33:50 +02:00
(let [key (events/listen js/document EventType.MOUSEMOVE on-mousemove)]
(js/console.log "mouse-mixin-did-mount" key)
(assoc own ::eventkey key))))
2015-12-22 20:25:38 +02:00
(defn- mouse-mixin-will-unmount
[own]
(let [key (::eventkey own)]
(events/unlistenByKey key)
(dissoc own ::eventkey)))
2015-12-22 21:33:50 +02:00
(defn- mouse-mixin-transfer-state
[old-own own]
(let [key (::eventkey old-own)]
(assoc own ::eventkey key)))
2015-12-22 20:25:38 +02:00
(def ^:static mouse-mixin
{:did-mount mouse-mixin-did-mount
2015-12-22 21:33:50 +02:00
:will-unmount mouse-mixin-will-unmount
:transfer-state mouse-mixin-transfer-state})
2015-12-18 11:18:00 +02:00
2015-12-17 15:44:05 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def viewport-height 2048)
(def viewport-width 2048)
2015-12-17 15:44:05 +02:00
(def document-start-x 50)
(def document-start-y 50)