Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created February 19, 2016 14:55
Show Gist options
  • Select an option

  • Save selfsame/92869e535cd7741626eb to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/92869e535cd7741626eb to your computer and use it in GitHub Desktop.
(ns commands.dom
(:require
[om.next :as om]
[commands.core :refer-macros [defcommand]]
[pdfn.core :refer [and* or* not* is*] :refer-macros [defpdfn pdfn inspect compile!]]
[fast.util] [fast.core :refer [-o -parent -next -prev -nodes]]
[fp.data][fp.selection][fp.util][fp.refs][fp.flow][sync.core]))
(defn walk-selection [k]
(let [So (filter -o (fp.refs/doc :selection/uids))
f (cond (commands.core/key-modifier? :additive) fp.selection/add
(commands.core/key-modifier? :subtractive) fp.selection/subtract
:else fp.selection/reset)
found (case k
:parent (remove nil? (mapv -parent So))
:children (mapcat #(let [v (-nodes %)] (if (first v) v [%])) So)
:prev (remove nil? (mapv -prev So))
:next (remove nil? (mapv -next So))
'())]
(when (not= found '())
(f found))))
(def non-special-context (not* (comp #{:dragging :dialogue :text-editing} last)))
(defn dom-context [[a b c & _]] (and (:edit b) (:dom c)))
(defcommand :dom/select-parent
{:doc "select parent elements"
:key-codes [[:ctrl :up]]
:menu [:edit :select] :icon [-112 -64]
:context (and* dom-context non-special-context)
:behaviour #(walk-selection :parent)})
(defcommand :dom/select-children
{:doc "select child elements"
:key-codes [[:ctrl :down]]
:menu [:edit :select] :icon [-112 -80]
:context (and* dom-context non-special-context)
:behaviour #(walk-selection :children)})
(defcommand :dom/select-previous
{:doc "select previous elements"
:key-codes [[:ctrl :left]]
:menu [:edit :select] :icon [-112 -96]
:context (and* dom-context non-special-context)
:behaviour #(walk-selection :prev)})
(defcommand :dom/select-next
{:doc "select next elements"
:key-codes [[:ctrl :right]]
:menu [:edit :select] :icon [-112 -112]
:context (and* dom-context non-special-context)
:behaviour #(walk-selection :next)})
(defcommand :dom/delete-selection
{:doc "deletes selected elements"
:key-codes [[:ctrl :x]]
:menu [:edit] :icon [-112 -16]
:behaviour
#(do (fast.util/delete-selection)
(fp.flow/uncache-tracking))})
(defcommand :dom/duplicate-selection
{:doc "duplicates selected elements"
:key-codes [[:ctrl :d]]
:menu [:edit] :icon [-112 0]
:behaviour
#(do (fast.util/duplicate-selection)
(fp.flow/uncache-tracking))})
(defcommand :dom/insert-html
{:doc "dialogue for pasting html into the document"
:key-codes [[:ctrl :alt :i]]
:menu [:workspace] :icon [-64 -80]
:behaviour #(commands.core/show-dialogue {:title "insert html:" :component :insert-html} prn)})
(defcommand :dom/add-class
{:doc "adds a class to selected elements in current context"
:key-codes [[:alt :a] :c]
:behaviour (fn [] (prn "dom/add-class invoked!"))})
(defcommand :dom/layout-style
{:key-codes [[:shift :l]]
:menu [:workspace]
:behaviour
#(do (sync.core/inject-css "_outline_mode"
(if (fp.refs/doc :options/layout-style) "" fp.data/LAYOUT-CSS))
(om/transact! @fp.data/RECONCILER
`[(std/update-in ~{:path [:options/layout-style] :fn
not})]))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment