From 623043d3634bfa5064be7659e5fe159cf8c05a6f Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 28 Feb 2024 21:30:49 +0800 Subject: [PATCH] enhance: `j` jump to a property key or value (selection mode) We can support jump to any block in the future. More enhancements need to be done including: 1. still focus on the block or property key/value after any property operation 2. up/down/left/right support maybe? --- src/main/frontend/components/container.cljs | 3 + src/main/frontend/components/jump.cljs | 45 ++++++++++ src/main/frontend/components/property.cljs | 5 +- src/main/frontend/components/property.css | 6 +- .../frontend/components/property/value.cljs | 82 ++++++++++--------- src/main/frontend/components/select.cljs | 3 +- src/main/frontend/handler/jump.cljs | 28 +++++++ .../frontend/modules/shortcut/config.cljs | 6 +- src/main/frontend/state.cljs | 1 + src/resources/dicts/en.edn | 1 + 10 files changed, 136 insertions(+), 44 deletions(-) create mode 100644 src/main/frontend/components/jump.cljs create mode 100644 src/main/frontend/handler/jump.cljs diff --git a/src/main/frontend/components/container.cljs b/src/main/frontend/components/container.cljs index cea79c814..c40311c94 100644 --- a/src/main/frontend/components/container.cljs +++ b/src/main/frontend/components/container.cljs @@ -48,6 +48,7 @@ [frontend.util :as util] [frontend.util.cursor :as cursor] [frontend.components.window-controls :as window-controls] + [frontend.components.jump :as jump] [medley.core :as medley] [goog.dom :as gdom] [goog.object :as gobj] @@ -920,6 +921,8 @@ (when (util/electron?) (find-in-page/search)) + (jump/jump) + (main {:route-match route-match :margin-less-pages? margin-less-pages? :logged? logged? diff --git a/src/main/frontend/components/jump.cljs b/src/main/frontend/components/jump.cljs new file mode 100644 index 000000000..58310c873 --- /dev/null +++ b/src/main/frontend/components/jump.cljs @@ -0,0 +1,45 @@ +(ns frontend.components.jump + "Jump to" + (:require [frontend.ui :as ui] + [frontend.state :as state] + [rum.core :as rum] + [frontend.util :as util] + [frontend.handler.jump :as jump-handler])) + +(defn- exit! + [] + (state/set-state! :editor/jump-data nil) + (jump-handler/clear-jump-hints!)) + +(rum/defcs input < + (rum/local "" ::q) + [state {:keys [triggers _mode]}] ; TODO: jump to block + (let [*q (::q state)] + [:div.flex.w-full.relative + [:input.form-input.block.sm:text-sm.my-2.border-none.outline-none + {:auto-focus true + :placeholder "Jump to" + :aria-label "Jump to" + :value @*q + :on-change (fn [e] (reset! *q (util/evalue e))) + :on-key-down (fn [e] + (util/stop-propagation e) + (case (util/ekey e) + "Enter" + (when-let [idx (util/safe-parse-int @*q)] + (when (> idx 0) + (when-let [elem (nth triggers (dec idx))] + (state/clear-selection!) + (exit!) + (.click elem)))) + "Escape" + (exit!) + nil))}]])) + +(rum/defc jump < rum/reactive + [] + (let [data (state/sub :editor/jump-data)] + (when data + [:div#bottom-console.flex.flex-1.flex-row.absolute.top-10.right-2.shadow-lg.px-2.py-1.faster-fade-in.items-center + + (input data)]))) diff --git a/src/main/frontend/components/property.cljs b/src/main/frontend/components/property.cljs index e23ab102f..138f31ab9 100644 --- a/src/main/frontend/components/property.cljs +++ b/src/main/frontend/components/property.cljs @@ -587,8 +587,9 @@ {:on-click #(route-handler/redirect-to-page! (:block/name property))} [:div {:style {:padding-left 6}} (:block/original-name property)]] - [:a.property-k.select-none - {:title (str "Configure property: " (:block/original-name property)) + [:a.property-k.flex.select-none.jtrigger + {:tabIndex 0 + :title (str "Configure property: " (:block/original-name property)) :on-mouse-down (fn [^js e] (when (util/meta-key? e) (route-handler/redirect-to-page! (:block/name property)) diff --git a/src/main/frontend/components/property.css b/src/main/frontend/components/property.css index a3bfc1e65..bd4528b17 100644 --- a/src/main/frontend/components/property.css +++ b/src/main/frontend/components/property.css @@ -241,4 +241,8 @@ a.control-link { &.no-mode { @apply hidden; } -} \ No newline at end of file +} + +.property-k:focus { + @apply pr-1; +} diff --git a/src/main/frontend/components/property/value.cljs b/src/main/frontend/components/property/value.cljs index d50472f29..0f6661642 100644 --- a/src/main/frontend/components/property/value.cljs +++ b/src/main/frontend/components/property/value.cljs @@ -98,7 +98,7 @@ (shui/popup-hide! id) (when-let [toggle (:toggle-fn opts)] (toggle)))))}))] - [:a.w-fit.flex.items-center + [:a.w-fit.flex.items-center.jtrigger {:tabIndex "0" ;; meta-click or just click in publishing to navigate to date's page :on-click (if config/publishing? @@ -587,10 +587,11 @@ (select-f) (ui/dropdown (fn [{:keys [toggle-fn]}] - [:a.control-link - {:on-mouse-down (if config/publishing? - (constantly nil) - toggle-fn) + [:a.control-link.jtrigger + {:tabIndex 0 + :on-click (if config/publishing? + (constantly nil) + toggle-fn) :class "flex flex-1"} (if (and (string/blank? value) (not editing?)) [:div.opacity-50.pointer.text-sm "Empty"] @@ -631,6 +632,7 @@ (let [add-property! (fn [] ( (d/create-element :span) + (d/set-attr! :class "jtrigger-id text-sm border rounded ml-2 px-1 shadow-xs") + (d/set-text! (str (inc id)))))) + triggers)))) + + :else ; add block jump support + nil))) diff --git a/src/main/frontend/modules/shortcut/config.cljs b/src/main/frontend/modules/shortcut/config.cljs index a24bcd086..c4d9e8e52 100644 --- a/src/main/frontend/modules/shortcut/config.cljs +++ b/src/main/frontend/modules/shortcut/config.cljs @@ -18,6 +18,7 @@ [frontend.handler.whiteboard :as whiteboard-handler] [frontend.handler.plugin-config :as plugin-config-handler] [frontend.handler.window :as window-handler] + [frontend.handler.jump :as jump-handler] [frontend.modules.editor.undo-redo :as undo-redo] [frontend.dicts :as dicts] [frontend.modules.shortcut.before :as m] @@ -507,6 +508,8 @@ :command/toggle-favorite {:binding "mod+shift+f" :fn page-handler/toggle-favorite!} + :editor/jump {:binding "j" + :fn jump-handler/jump-to} :editor/open-file-in-default-app {:binding "mod+d mod+a" :inactive (not (util/electron?)) :file-graph? true @@ -711,7 +714,8 @@ :editor/copy :editor/copy-text :editor/cut - :command/toggle-favorite]) + :command/toggle-favorite + :editor/jump]) (with-meta {:before m/enable-when-not-component-editing!})) :shortcut.handler/global-prevent-default diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 48a50c119..77d9d1ae7 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -121,6 +121,7 @@ :block/component-editing-mode? false :editor/start-pos (atom nil) :editor/op (atom nil) + :editor/jump-data (atom nil) :editor/hidden-editors #{} ;; page names :editor/draw-mode? false :editor/action (atom nil) diff --git a/src/resources/dicts/en.edn b/src/resources/dicts/en.edn index 5ccb8eecc..08c0f4927 100644 --- a/src/resources/dicts/en.edn +++ b/src/resources/dicts/en.edn @@ -730,6 +730,7 @@ :editor/toggle-undo-redo-mode "Toggle undo redo mode (global or page only)" :editor/toggle-number-list "Toggle number list" :editor/add-property "Add property" + :editor/jump "Jump to a property key or value (selection mode)" :whiteboard/select "Select tool" :whiteboard/pan "Pan tool" :whiteboard/portal "Portal tool"