0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 21:09:00 -05:00

Minor improvements on worker impl.

This commit is contained in:
Andrey Antukh 2020-05-11 08:11:14 +02:00 committed by Alonso Torres
parent d9c459e877
commit 16c3520587
2 changed files with 21 additions and 47 deletions

View file

@ -11,9 +11,6 @@
(:require (:require
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[okulary.core :as l] [okulary.core :as l]
[promesa.core :as p]
[beicon.core :as rx]
[cuerdas.core :as str]
[uxbox.common.exceptions :as ex] [uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us] [uxbox.common.spec :as us]
[uxbox.common.pages :as cp] [uxbox.common.pages :as cp]
@ -24,20 +21,16 @@
(defonce state (l/atom {})) (defonce state (l/atom {}))
(declare resolve-object)
(declare index-object) (declare index-object)
(declare calculate-bounds)
(declare create-index) (declare create-index)
(defmethod impl/handler :selection/create-index (defmethod impl/handler :selection/create-index
[{:keys [file-id pages] :as message}] [{:keys [file-id pages] :as message}]
(letfn [(index-page [state page] (letfn [(index-page [state page]
(let [id (:id page) (let [id (:id page)
objects (get-in page [:data :objects]) objects (get-in page [:data :objects])]
objects (reduce resolve-object {} (vals objects)) (assoc state id (create-index objects))))
index (create-index objects)]
(assoc state id {:index index
:objects objects})))
(update-state [state] (update-state [state]
(reduce index-page state pages))] (reduce index-page state pages))]
@ -46,52 +39,35 @@
(defmethod impl/handler :selection/update-index (defmethod impl/handler :selection/update-index
[{:keys [page-id objects] :as message}] [{:keys [page-id objects] :as message}]
(letfn [(update-page [_] (let [index (create-index objects)]
(let [objects (reduce resolve-object {} (vals objects)) (swap! state update page-id (constantly index))
index (create-index objects)]
{:index index :objects objects}))]
(swap! state update page-id update-page)
nil)) nil))
(defmethod impl/handler :selection/query (defmethod impl/handler :selection/query
[{:keys [page-id rect] :as message}] [{:keys [page-id rect] :as message}]
(when-let [{:keys [index objects]} (get @state page-id)] (when-let [index (get @state page-id)]
(let [lookup #(get objects %) (let [result (-> (qdt/search index (clj->js rect))
result (-> (qdt/search index (clj->js rect))
(es6-iterator-seq)) (es6-iterator-seq))
matches? #(geom/overlaps? % rect)] matches? #(geom/overlaps? % rect)]
(into #{} (comp (map #(unchecked-get % "data")) (into #{} (comp (map #(unchecked-get % "data"))
(filter matches?) (filter matches?)
(map :id)) (map :id))
result)))) result))))
(defn- calculate-bounds
[objects]
#js {:x 0
:y 0
:width (::width objects)
:height (::height objects)})
(defn- create-index (defn- create-index
[objects] [objects]
(let [bounds (calculate-bounds objects)] (let [shapes (->> (cp/select-toplevel-shapes objects)
(map #(merge % (select-keys % [:x :y :width :height]))))
bounds (geom/shapes->rect-shape shapes)
bounds #js {:x (:x bounds)
:y (:y bounds)
:width (:width bounds)
:height (:height bounds)}]
(reduce index-object (reduce index-object
(qdt/create bounds) (qdt/create bounds)
(cp/select-toplevel-shapes objects)))) shapes)))
(defn- index-object (defn- index-object
[index {:keys [id x y width height] :as obj}] [index {:keys [id x y width height] :as obj}]
(let [rect #js {:x x :y y :width width :height height}] (let [rect #js {:x x :y y :width width :height height}]
(qdt/insert index rect obj))) (qdt/insert index rect obj)))
(defn- resolve-object
[state {:keys [id] :as item}]
(let [selection-rect (geom/selection-rect-shape item)
item (merge item (select-keys selection-rect [:x :y :width :height]))
width (+ (:x item 0) (:width item 0))
height (+ (:y item 0) (:height item 0))
max (fnil max 0)]
(-> state
(assoc id item)
(update ::width max width)
(update ::height max height))))

View file

@ -11,6 +11,7 @@
(:require (:require
[okulary.core :as l] [okulary.core :as l]
[uxbox.common.uuid :as uuid] [uxbox.common.uuid :as uuid]
[uxbox.common.pages :as cp]
[uxbox.worker.impl :as impl] [uxbox.worker.impl :as impl]
[uxbox.util.range-tree :as rt] [uxbox.util.range-tree :as rt]
[uxbox.util.geom.snap :as snap])) [uxbox.util.geom.snap :as snap]))
@ -39,13 +40,10 @@
"Initialize the snap information with the current workspace information" "Initialize the snap information with the current workspace information"
[objects] [objects]
(let [shapes (vals objects) (let [shapes (vals objects)
frame-shapes (->> shapes frame-shapes (->> (vals objects)
(filter (comp not nil? :frame-id)) (filter :frame-id)
(group-by :frame-id)) (group-by :frame-id))
frame-shapes (->> (cp/select-frames objects)
frame-shapes (->> shapes
(filter #(= :frame (:type %)))
(remove #(= uuid/zero (:id %)))
(reduce #(update %1 (:id %2) conj %2) frame-shapes))] (reduce #(update %1 (:id %2) conj %2) frame-shapes))]
(mapm (fn [shapes] {:x (create-coord-data shapes :x) (mapm (fn [shapes] {:x (create-coord-data shapes :x)
:y (create-coord-data shapes :y)}) :y (create-coord-data shapes :y)})