0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 16:00:19 -05:00

Improvements to grid alignment impl.

This commit is contained in:
Andrey Antukh 2016-04-11 17:32:16 +03:00
parent 8916a9b7ac
commit 4f0a74b1a3
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
7 changed files with 58 additions and 50 deletions

15
src/uxbox/data/core.cljs Normal file
View file

@ -0,0 +1,15 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.data.core
"Worker related api and initialization events."
(:require [beicon.core :as rx]
[uxbox.rstore :as rs]
[uxbox.constants :as c]
[uxbox.util.workers :as uw]))
(defonce worker (uw/init "/js/worker.js"))

View file

@ -1,32 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.data.worker
"Worker related api and initialization events."
(:require [beicon.core :as rx]
[uxbox.rstore :as rs]
[uxbox.constants :as c]
[uxbox.util.workers :as uw]))
(defonce worker (uw/init "/js/worker.js"))
;; --- Worker Initialization
(defrecord InitializeWorker [id]
rs/EffectEvent
(-apply-effect [_ state]
(let [page (get-in state [:pages-by-id id])
opts (:options page)
message {:cmd :grid/init
:width c/viewport-width
:height c/viewport-height
:x-axis (:grid/x-axis opts c/grid-x-axis)
:y-axis (:grid/y-axis opts c/grid-y-axis)}]
(uw/send! worker message))))
(defn initialize
[id]
(InitializeWorker. id))

View file

@ -8,18 +8,22 @@
(ns uxbox.data.workspace
(:require [bouncer.validators :as v]
[beicon.core :as rx]
[uxbox.constants :as c]
[uxbox.shapes :as sh]
[uxbox.rstore :as rs]
[uxbox.state.shapes :as stsh]
[uxbox.schema :as sc]
[uxbox.data.core :refer (worker)]
[uxbox.data.pages :as udp]
[uxbox.data.shapes :as uds]
[uxbox.data.worker :as wrk]
[uxbox.util.datetime :as dt]
[uxbox.util.math :as mth]
[uxbox.util.geom.point :as gpt]))
[uxbox.util.geom.point :as gpt]
[uxbox.util.workers :as uw]))
;; --- Workspace Initialization
;; --- Initialize Workspace
(declare initialize-alignment-index)
(defrecord InitializeWorkspace [project page]
rs/UpdateEvent
@ -41,10 +45,10 @@
rs/WatchEvent
(-apply-watch [_ state s]
(if (get-in state [:pages-by-id page])
(rx/of (wrk/initialize page))
(rx/of (initialize-alignment-index page))
(->> (rx/filter udp/pages-fetched? s)
(rx/take 1)
(rx/map #(wrk/initialize page))))))
(rx/map #(initialize-alignment-index page))))))
(defn initialize
"Initialize the workspace state."
@ -146,3 +150,22 @@
[]
(ResetZoom.))
;; --- Initialize Alignment Index
(defrecord InitializeAlignmentIndex [id]
rs/WatchEvent
(-apply-watch [_ state s]
(let [page (get-in state [:pages-by-id id])
opts (:options page)
message {:cmd :grid/init
:width c/viewport-width
:height c/viewport-height
:x-axis (:grid/x-axis opts c/grid-x-axis)
:y-axis (:grid/y-axis opts c/grid-y-axis)}]
(->> (uw/send! worker message)
(rx/map #(toggle-flag :alignment/indexed))))))
(defn initialize-alignment-index
[id]
(InitializeAlignmentIndex. id))

View file

@ -10,7 +10,7 @@
[lentes.core :as l]
[uxbox.state :as st]
[uxbox.shapes :as sh]
[uxbox.data.worker :refer (worker)]
[uxbox.data.core :refer (worker)]
[uxbox.ui.workspace.base :as wb]
[uxbox.util.geom.point :as gpt]
[uxbox.util.workers :as uw]))

View file

@ -86,17 +86,20 @@
[]
(let [shapes @selected-shapes-l
options @page-options-l
flags @wb/flags-l
indexed? (:alignment/indexed flags)
stoper (->> uuc/actions-s
(rx/map :type)
(rx/filter empty?)
(rx/take 1))]
(as-> wb/mouse-delta-s $
(rx/take-until stoper $)
(rx/map #(gpt/divide % @wb/zoom-l) $)
(rx/scan (fn [acc delta]
(let [xf (map #(sh/move % delta))]
(into [] xf acc))) shapes $)
(rx/mapcat (fn [items]
(if (:grid/align options)
(if (and (:grid/align options) indexed?)
(->> (apply rx/of items)
(rx/mapcat align/translate)
(rx/reduce conj []))

View file

@ -7,28 +7,28 @@
(ns uxbox.worker.align
"Workspace aligment indexes worker."
(:require [beicon.core :as rx]
[kdtree :as kd]
[kdtree.core :as kd]
[uxbox.worker.core :as wrk]
[uxbox.util.geom.point :as gpt]))
(defonce state (volatile! nil))
(defmethod wrk/handler :grid/init
[{:keys [width height x-axis y-axis] :as opts}]
(println ":grid/init" opts)
(let [points (into-array
(for [x (range 0 width (or x-axis 10))
y (range 0 height (or y-axis 10))]
[{:keys [sender width height x-axis y-axis] :as opts}]
(time
(let [points (into-array
(for [x (range 0 width (or x-axis 10))
y (range 0 height (or y-axis 10))]
#js [x y]))
tree (kd/create2d points)]
(vreset! state tree)))
tree (kd/create2d points)]
(vreset! state tree)
(wrk/reply! sender nil))))
(defmethod wrk/handler :grid/align
[{:keys [sender point] :as message}]
(println "request" point)
(let [point #js [(:x point) (:y point)]
results (js->clj (.nearest @state point 1))
[[x y] d] (first results)
result (gpt/point x y)]
(println "result:" result)
(wrk/reply! sender {:point (gpt/point x y)})))

View file

@ -27,5 +27,4 @@
(defn reply!
[sender message]
(let [message (assoc message :reply-to sender)]
(println "replying " message)
(.postMessage js/self (t/encode message))))