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

Reimplement shape displacement using keyboard shortcuts.

This commit is contained in:
Andrey Antukh 2016-12-27 23:40:02 +01:00
parent bbaf80bedf
commit f82c534df4
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 67 additions and 9 deletions

View file

@ -6,8 +6,8 @@
(ns uxbox.main.data.shapes
(:require [cljs.spec :as s :include-macros true]
[lentes.core :as l]
[beicon.core :as rx]
[uxbox.util.uuid :as uuid]
[potok.core :as ptk]
[uxbox.store :as st]
[uxbox.main.constants :as c]
@ -21,6 +21,7 @@
[uxbox.util.geom.matrix :as gmt]
[uxbox.util.router :as r]
[uxbox.util.rlocks :as rlocks]
[uxbox.util.uuid :as uuid]
[uxbox.util.workers :as uw]))
(s/def ::x1 number?)
@ -142,6 +143,8 @@
;; --- Apply Displacement
;; TODO: move to shapes-impl ns.
(deftype ApplyDisplacement [id]
udp/IPageUpdate
ptk/UpdateEvent
@ -664,6 +667,61 @@
{:pre [(us/valid? ::direction loc)]}
(MoveSelectedLayer. loc))
;; --- Move Selected
(defn alignment-activated?
[state]
(let [flags (get-in state [:workspace :flags])]
(and (contains? flags :grid-indexed)
(contains? flags :grid-alignment)
(contains? flags :grid))))
(def selected-shapes-lens (l/in [:workspace :selected]))
(def selected-page-lens (l/in [:workspace :page]))
(defn- calculate-displacement
[direction speed distance]
(case direction
:up (gpt/point 0 (- (get-in distance [speed :y])))
:down (gpt/point 0 (get-in distance [speed :y]))
:left (gpt/point (- (get-in distance [speed :x])) 0)
:right (gpt/point (get-in distance [speed :x]) 0)))
(defn- calculate-displacement-distance
[metadata align?]
(let [gx (:grid-x-axis metadata)
gy (:grid-y-axis metadata)]
{:std (gpt/point (if align? gx 1)
(if align? gy 1))
:fast (gpt/point (if align? (* 3 gx) 10)
(if align? (* 3 gy) 10))}))
(deftype MoveSelected [direction speed]
ptk/WatchEvent
(watch [_ state stream]
(let [align? (alignment-activated? state)
selected (l/focus selected-shapes-lens state)
page (l/focus selected-page-lens state)
metadata (merge c/page-metadata (get-in state [:pages page :metadata]))
distance (calculate-displacement-distance metadata align?)
displacement (calculate-displacement direction speed distance)]
(rx/concat
(when align?
(rx/concat
(rx/from-coll (map initial-align-shape selected))
(rx/from-coll (map apply-displacement selected))))
(rx/from-coll (map #(apply-temporal-displacement % displacement) selected))
(rx/from-coll (map apply-displacement selected))))))
(s/def ::direction #{:up :down :right :left})
(s/def ::speed #{:std :fast})
(defn move-selected
[direction speed]
{:pre [(us/valid? ::direction direction)
(us/valid? ::speed speed)]}
(MoveSelected. direction speed))
;; --- Point Alignment (with Grid)
(defn align-point

View file

@ -50,14 +50,14 @@
:ctrl+down #(st/emit! (uds/move-selected-layer :down))
:ctrl+shift+up #(st/emit! (uds/move-selected-layer :top))
:ctrl+shift+down #(st/emit! (uds/move-selected-layer :bottom))
;; :shift+up #(move-selected :up :fast)
;; :shift+down #(move-selected :down :fast)
;; :shift+right #(move-selected :right :fast)
;; :shift+left #(move-selected :left :fast)
;; :up #(move-selected :up :std)
;; :down #(move-selected :down :std)
;; :right #(move-selected :right :std)
;; :left #(move-selected :left :std)
:shift+up #(st/emit! (uds/move-selected :up :fast))
:shift+down #(st/emit! (uds/move-selected :down :fast))
:shift+right #(st/emit! (uds/move-selected :right :fast))
:shift+left #(st/emit! (uds/move-selected :left :fast))
:up #(st/emit! (uds/move-selected :up :std))
:down #(st/emit! (uds/move-selected :down :std))
:right #(st/emit! (uds/move-selected :right :std))
:left #(st/emit! (uds/move-selected :left :std))
})
;; --- Shortcuts Setup Functions