From 4f0a74b1a37e7b6140e03ee3bc97eebdc2b422e7 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 11 Apr 2016 17:32:16 +0300 Subject: [PATCH] Improvements to grid alignment impl. --- src/uxbox/data/core.cljs | 15 +++++++++++++ src/uxbox/data/worker.cljs | 32 --------------------------- src/uxbox/data/workspace.cljs | 33 +++++++++++++++++++++++----- src/uxbox/ui/workspace/align.cljs | 2 +- src/uxbox/ui/workspace/movement.cljs | 5 ++++- src/uxbox/worker/align.cljs | 20 ++++++++--------- src/uxbox/worker/core.cljs | 1 - 7 files changed, 58 insertions(+), 50 deletions(-) create mode 100644 src/uxbox/data/core.cljs delete mode 100644 src/uxbox/data/worker.cljs diff --git a/src/uxbox/data/core.cljs b/src/uxbox/data/core.cljs new file mode 100644 index 0000000000..89ed9ca6af --- /dev/null +++ b/src/uxbox/data/core.cljs @@ -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 + +(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")) + diff --git a/src/uxbox/data/worker.cljs b/src/uxbox/data/worker.cljs deleted file mode 100644 index 6a549362ce..0000000000 --- a/src/uxbox/data/worker.cljs +++ /dev/null @@ -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 - -(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)) diff --git a/src/uxbox/data/workspace.cljs b/src/uxbox/data/workspace.cljs index e4f8694956..9855ffc883 100644 --- a/src/uxbox/data/workspace.cljs +++ b/src/uxbox/data/workspace.cljs @@ -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)) + diff --git a/src/uxbox/ui/workspace/align.cljs b/src/uxbox/ui/workspace/align.cljs index d57eca2ce7..0eb28517f1 100644 --- a/src/uxbox/ui/workspace/align.cljs +++ b/src/uxbox/ui/workspace/align.cljs @@ -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])) diff --git a/src/uxbox/ui/workspace/movement.cljs b/src/uxbox/ui/workspace/movement.cljs index 364994cada..1d3b49020b 100644 --- a/src/uxbox/ui/workspace/movement.cljs +++ b/src/uxbox/ui/workspace/movement.cljs @@ -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 [])) diff --git a/src/uxbox/worker/align.cljs b/src/uxbox/worker/align.cljs index 704fb908cd..af28f97e87 100644 --- a/src/uxbox/worker/align.cljs +++ b/src/uxbox/worker/align.cljs @@ -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)}))) diff --git a/src/uxbox/worker/core.cljs b/src/uxbox/worker/core.cljs index 43988fb93d..d5f30b7909 100644 --- a/src/uxbox/worker/core.cljs +++ b/src/uxbox/worker/core.cljs @@ -27,5 +27,4 @@ (defn reply! [sender message] (let [message (assoc message :reply-to sender)] - (println "replying " message) (.postMessage js/self (t/encode message))))