Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relocate codemirror specific logic onto demo #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
43 changes: 37 additions & 6 deletions demo/src/nextjournal/command_bar/demo/editor.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns nextjournal.command-bar.demo.editor
(:require ["@codemirror/commands" :refer [history historyKeymap]]
["@codemirror/language" :refer [syntaxHighlighting HighlightStyle]]
["@codemirror/state" :refer [Compartment EditorState]]
["@codemirror/view" :as view :refer [EditorView]]
["@codemirror/state" :refer [Compartment EditorState StateField]]
["@codemirror/view" :as view :refer [keymap EditorView ViewPlugin]]
["@lezer/highlight" :refer [tags]]
["react" :as react]
["react-dom/client" :as react-client]
Expand All @@ -21,6 +21,37 @@
[reagent.core :as r]
[shadow.resource :as rc]))

(defn get-codemirror-bindings [^js state]
;; Removing everything that doesn't have a fn name for now. Not sure about this yet.
(->> (.. state (facet keymap) flat (filter #(and (or (.-key %) (.-mac %)) (.-run %) (.. % -run -name))))
(map
(fn [binding]
(let [{:keys [key mac run shift]} (j/lookup binding)
key (or key mac)]
(concat
[{:local? true
:spec (command-bar/normalize-spec key)
:run run
:name (command-bar/get-fn-name run)}]
(when shift
[{:local? true
:spec (command-bar/normalize-spec (str "Shift-" key))
:run shift
:name (command-bar/get-fn-name run)}])))))
flatten
(remove nil?)))

(def extension
#js [(.-extension (.define StateField
(j/lit {:create #(reset! command-bar/!local-bindings (get-codemirror-bindings %))
:update (fn [_value tr]
#(reset! command-bar/!local-bindings (get-codemirror-bindings (.-state tr))))})))
(.define ViewPlugin (fn [view]
#js {:update (fn [^js update]
(when (and (.-focusChanged update) (.. update -view -hasFocus))
(reset! command-bar/!local-bindings (get-codemirror-bindings (.-state update)))
(reset! command-bar/!local-view (.-view update))))}))])

(def highlight-style
(.define HighlightStyle
(clj->js [{:tag (.-meta tags) :class "cmt-meta"}
Expand Down Expand Up @@ -95,7 +126,7 @@
(test-utils/make-state
#js [theme
(history)
command-bar/extension
extension
clojure-mode/default-extensions
(syntaxHighlighting highlight-style)
(eval-region/extension {:modifier "Meta"})
Expand Down Expand Up @@ -123,18 +154,18 @@
{:class "px-[3px] py-[1px] min-w-[16px]"} k]))
(str/split (command-bar/get-pretty-spec spec) #" ")))

(defn key-description [{:keys [codemirror? run spec var]}]
(defn key-description [{:keys [local? run spec var]}]
[:div.font-mono {:class "text-[12px]"}
[:div
[keys-view spec]
" is bound to "
(when codemirror?
(when local?
[:span "CodeMirror's "])
[:span
(when-let [ns (some-> var meta :ns)]
[:span ns "/"])
[:span.font-bold (command-bar/get-fn-name run)]]
(when codemirror?
(when local?
" command")]
(when-let [docs (some-> var meta :doc)]
[:div.mt-3 docs])])
Expand Down
56 changes: 11 additions & 45 deletions src/nextjournal/command_bar.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
(ns nextjournal.command-bar
(:require ["@codemirror/state" :refer [StateField]]
["@codemirror/view" :refer [keymap EditorView ViewPlugin]]
[applied-science.js-interop :as j]
[clojure.string :as str]
(:require [clojure.string :as str]
[nextjournal.clerk.render.hooks :as hooks]
[nextjournal.command-bar.fuzzy :as fuzzy]
[nextjournal.command-bar.keybind :as keybind]
[reagent.core :as reagent]))

(defonce !bindings (reagent/atom []))
(defonce !global-bindings (reagent/atom []))
(defonce !codemirror-bindings (reagent/atom []))
(defonce !codemirror-view (reagent/atom nil))
(defonce !local-bindings (reagent/atom []))
(defonce !local-view (reagent/atom nil))
(defonce !state (reagent/atom {}))

(defn chord->spec [{:keys [shift ctrl meta alt button]}]
Expand Down Expand Up @@ -67,44 +64,13 @@
(swap! !fn-names assoc n fn-name)
fn-name))))

(defn get-codemirror-bindings [^js state]
;; Removing everything that doesn't have a fn name for now. Not sure about this yet.
(->> (.. state (facet keymap) flat (filter #(and (or (.-key %) (.-mac %)) (.-run %) (.. % -run -name))))
(map
(fn [binding]
(let [{:keys [key mac run shift]} (j/lookup binding)
key (or key mac)]
(concat
[{:codemirror? true
:spec (normalize-spec key)
:run run
:name (get-fn-name run)}]
(when shift
[{:codemirror? true
:spec (normalize-spec (str "Shift-" key))
:run shift
:name (get-fn-name run)}])))))
flatten
(remove nil?)))

(def extension
#js [(.-extension (.define StateField
(j/lit {:create #(reset! !codemirror-bindings (get-codemirror-bindings %))
:update (fn [_value tr]
#(reset! !codemirror-bindings (get-codemirror-bindings (.-state tr))))})))
(.define ViewPlugin (fn [view]
#js {:update (fn [^js update]
(when (and (.-focusChanged update) (.. update -view -hasFocus))
(reset! !codemirror-bindings (get-codemirror-bindings (.-state update)))
(reset! !codemirror-view (.-view update))))}))])

(defn use-watches []
(hooks/use-effect (fn []
(let [reset-bindings! #(reset! !bindings (concat @!global-bindings @!codemirror-bindings))]
(let [reset-bindings! #(reset! !bindings (concat @!global-bindings @!local-bindings))]
(add-watch !global-bindings :global reset-bindings!)
(add-watch !codemirror-bindings :codemirror reset-bindings!)
(add-watch !local-bindings :codemirror reset-bindings!)
#(do (remove-watch !global-bindings :global)
(remove-watch !codemirror-bindings :codemirror))))))
(remove-watch !local-bindings :codemirror))))))

(defn get-pretty-spec [spec]
(->> (str/split spec #"-")
Expand All @@ -127,16 +93,16 @@
"delete" "Del"} (str/lower-case k) (str/capitalize k))))
(str/join " ")))

(defn run-binding [{:keys [codemirror? run]}]
(if codemirror?
(defn run-binding [{:keys [local? run]}]
(if local?
(do
(.focus @!codemirror-view)
(js/requestAnimationFrame #(run @!codemirror-view)))
(.focus @!local-view)
(js/requestAnimationFrame #(run @!local-view)))
(run)))

(defn kill-interactive! []
(swap! !state dissoc :interactive :input/query :pick-list/filtered-items :pick-list/selected-index)
(when-let [cm @!codemirror-view]
(when-let [cm @!local-view]
(.focus cm)))

(defn make-interactive! [interactive-fn]
Expand Down