Bladeren bron

enhance: unify advanced commands with slash commands

fixes LOG-3206
Gabriel Horner 1 jaar geleden
bovenliggende
commit
1401bb1dd9

+ 29 - 55
src/main/frontend/commands.cljs

@@ -29,7 +29,6 @@
 
 ;; TODO: move to frontend.handler.editor.commands
 
-(defonce angle-bracket "<")
 (defonce hashtag "#")
 (defonce colon ":")
 (defonce command-trigger "/")
@@ -230,46 +229,15 @@
                         (+ 1 (count right))
                         (count right))]
      [[:editor/input template {:type "block"
-                               :last-pattern angle-bracket
+                               :last-pattern command-trigger
                                :backward-pos backward-pos}]])))
 
 (defn ->properties
   []
-  [[:editor/clear-current-bracket]
+  [[:editor/clear-current-slash]
    [:editor/insert-properties]
    [:editor/move-cursor-to-properties]])
 
-;; https://orgmode.org/manual/Structure-Templates.html
-(defn block-commands-map
-  []
-  (->>
-   (concat
-    [["Quote" (->block "quote")
-      "Add a quote"
-      "More"]
-     ["Src" (->block "src" "")]
-     ["Query" (->block "query")]
-     ["Latex export" (->block "export" "latex")]
-     ;; FIXME: current page's format
-     (when (= :org (state/get-preferred-format))
-       ["Properties" (->properties)])
-     ["Note" (->block "note")]
-     ["Tip" (->block "tip")]
-     ["Important" (->block "important")]
-     ["Caution" (->block "caution")]
-     ["Pinned" (->block "pinned")]
-     ["Warning" (->block "warning")]
-     ["Example" (->block "example")]
-     ["Export" (->block "export")]
-     ["Verse" (->block "verse")]
-     ["Ascii" (->block "export" "ascii")]
-     ["Center" (->block "center")]
-     ["Comment" (->block "comment")]]
-
-    ;; Allow user to modify or extend, should specify how to extend.
-    (state/get-commands))
-   (remove nil?)
-   (util/distinct-by-last-wins first)))
 
 (defn ^:large-vars/cleanup-todo commands-map
   [get-page-ref-text]
@@ -346,6 +314,32 @@
         "Number children"
         :icon/numberedChildren]]
 
+      ;; https://orgmode.org/manual/Structure-Templates.html
+      (cond->
+       [["Quote" (->block "quote")
+         "Create a quote block"
+         :icon/quote-block
+         "BLOCK TYPE"]
+        ;; Should this be replaced by "Code block"?
+        ["Src" (->block "src") "Create a code block"]
+        ["Advanced Query" (->block "query") "Create an advanced query block"]
+        ["Latex export" (->block "export" "latex") "Create a latex block"]
+        ["Note" (->block "note") "Create a note block"]
+        ["Tip" (->block "tip") "Create a tip block"]
+        ["Important" (->block "important") "Create an important block"]
+        ["Caution" (->block "caution") "Create a caution block"]
+        ["Pinned" (->block "pinned") "Create a pinned block"]
+        ["Warning" (->block "warning") "Create a warning block"]
+        ["Example" (->block "example") "Create an example block"]
+        ["Export" (->block "export") "Create an export block"]
+        ["Verse" (->block "verse") "Create a verse block"]
+        ["Ascii" (->block "export" "ascii") "Create an ascii block"]
+        ["Center" (->block "center") "Create a center block"]]
+
+        ;; FIXME: current page's format
+        (= :org (state/get-preferred-format))
+        (conj ["Properties" (->properties)]))
+
       ;; advanced
       [["Query"
         [[:editor/input "{{query }}" {:backward-pos 2}]
@@ -417,21 +411,14 @@
     (reset! *initial-commands commands)
     (reset! *matched-commands commands)))
 
-(defonce *matched-block-commands (atom (block-commands-map)))
-
 (defn reinit-matched-commands!
   []
   (reset! *matched-commands @*initial-commands))
 
-(defn reinit-matched-block-commands!
-  []
-  (reset! *matched-block-commands (block-commands-map)))
-
 (defn restore-state
   []
   (state/clear-editor-action!)
-  (reinit-matched-commands!)
-  (reinit-matched-block-commands!))
+  (reinit-matched-commands!))
 
 (defn insert!
   [id value
@@ -646,19 +633,6 @@
                                                new-value
                                                (count prefix))))))
 
-(defmethod handle-step :editor/clear-current-bracket [[_ space?]]
-  (when-let [input-id (state/get-edit-input-id)]
-    (when-let [current-input (gdom/getElement input-id)]
-      (let [edit-content (gobj/get current-input "value")
-            current-pos (cursor/pos current-input)
-            prefix (subs edit-content 0 current-pos)
-            prefix (util/replace-last angle-bracket prefix "" (boolean space?))
-            new-value (str prefix
-                           (subs edit-content current-pos))]
-        (state/set-block-content-and-last-pos! input-id
-                                               new-value
-                                               (count prefix))))))
-
 (defn compute-pos-delta-when-change-marker
   [edit-content marker pos]
   (let [old-marker (some->> (first (util/safe-re-find file-based-status/bare-marker-pattern edit-content))

+ 7 - 27
src/main/frontend/components/editor.cljs

@@ -1,7 +1,6 @@
 (ns frontend.components.editor
   (:require [clojure.string :as string]
-            [frontend.commands :as commands
-             :refer [*matched-block-commands *matched-commands]]
+            [frontend.commands :as commands :refer [*matched-commands]]
             [frontend.components.datetime :as datetime-comp]
             [frontend.components.svg :as svg]
             [frontend.components.search :as search]
@@ -101,19 +100,6 @@
        :class
        "cp__commands-slash"})))
 
-(rum/defc block-commands < rum/reactive
-  [id format]
-  (when (= :block-commands (state/get-editor-action))
-    (let [matched (util/react *matched-block-commands)]
-      (ui/auto-complete
-        (map first matched)
-        {:on-chosen (fn [chosen]
-                      (editor-handler/insert-command! id (get (into {} matched) chosen)
-                        format
-                        {:last-pattern commands/angle-bracket
-                         :command :block-commands}))
-         :class "black"}))))
-
 (defn- page-on-chosen-handler
   [embed? input id q pos format]
   (if embed?
@@ -500,11 +486,11 @@
    {:not-matched-handler (editor-handler/keydown-not-matched-handler format)}))
 
 (defn- set-up-key-up!
-  [state input' input-id]
+  [state input']
   (mixins/on-key-up
    state
    {}
-   (editor-handler/keyup-handler state input' input-id)))
+   (editor-handler/keyup-handler state input')))
 
 (def search-timeout (atom nil))
 
@@ -514,7 +500,7 @@
         input-id id
         input' (gdom/getElement input-id)]
     (set-up-key-down! state format)
-    (set-up-key-up! state input' input-id)))
+    (set-up-key-up! state input')))
 
 (defn get-editor-style-class
   "Get textarea css class according to it's content"
@@ -624,11 +610,6 @@
                     (commands id format)
                     {:content-props {:withoutAnimation false}})
 
-                  :block-commands
-                  (open-editor-popup! :block-commands
-                    (block-commands id format)
-                    {:content-props {:withoutAnimation true}})
-
                   (:block-search :page-search :page-search-hashtag)
                   (open-editor-popup! action
                     (if (= :block-search action)
@@ -698,10 +679,9 @@
       nil
 
       (or (contains?
-           #{:commands :block-commands
-             :page-search :page-search-hashtag :block-search :template-search
-             :property-search :property-value-search
-             :datepicker} action)
+           #{:commands :page-search :page-search-hashtag :block-search :template-search
+             :property-search :property-value-search :datepicker}
+           action)
           (and (keyword? action)
                (= (namespace action) "editor.action")))
       (when e (util/stop e))

+ 4 - 63
src/main/frontend/handler/editor.cljs

@@ -1725,25 +1725,6 @@
       (js/console.error e)
       nil)))
 
-(defn get-matched-block-commands
-  [input]
-  (try
-    (let [edit-content (gobj/get input "value")
-          pos (cursor/pos input)
-          last-command (subs edit-content
-                             (:pos (:pos (state/get-editor-action-data)))
-                             pos)]
-      (when (> pos 0)
-        (or
-         (and (= \< (util/nth-safe edit-content (dec pos)))
-              (commands/block-commands-map))
-         (and last-command
-              (commands/get-matched-commands
-               last-command
-               (commands/block-commands-map))))))
-    (catch :default _error
-      nil)))
-
 (defn auto-complete?
   []
   (or @*asset-uploading?
@@ -1936,12 +1917,6 @@
         (commands/reinit-matched-commands!)
         (state/set-editor-show-commands!))
 
-      (and (not db-based?) (= last-input-char commands/angle-bracket))
-      (do
-        (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
-        (commands/reinit-matched-block-commands!)
-        (state/set-editor-show-block-commands!))
-
       (and (= last-input-char last-prev-input-char commands/colon)
            (or (nil? prev-prev-input-char)
                (= prev-prev-input-char "\n"))
@@ -2792,7 +2767,7 @@
                 (delete-block! repo))))
 
           (and (> current-pos 0)
-            (contains? #{commands/command-trigger commands/angle-bracket commands/command-ask}
+            (contains? #{commands/command-trigger commands/command-ask}
               (util/nth-safe value (dec current-pos))))
           (do
             (util/stop e)
@@ -2989,7 +2964,7 @@
        (> current-pos 0)))
 
 (defn- default-case-for-keyup-handler
-  [input current-pos k code is-processed? c]
+  [input current-pos k code is-processed?]
   (let [last-key-code (state/get-last-key-code)
         blank-selected? (string/blank? (util/get-selected-text))
         non-enter-processed? (and is-processed? ;; #3251
@@ -3040,21 +3015,11 @@
             (commands/handle-step [:editor/search-block :reference])
             (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}))
 
-         ;; Handle non-ascii angle brackets
-          (and (= "〈" c)
-               (= "《" (util/nth-safe (gobj/get input "value") (dec (dec current-pos))))
-               (> current-pos 0))
-          (do
-            (commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
-                                                                         :backward-pos 0}])
-            (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
-            (state/set-editor-show-block-commands!))
-
           :else
           nil)))))
 
 (defn keyup-handler
-  [_state input input-id]
+  [_state input]
   (fn [e key-code]
     (when-not (util/goog-event-is-composing? e)
       (let [current-pos (cursor/pos input)
@@ -3094,32 +3059,8 @@
                 (reset! commands/*matched-commands matched-commands)
                 (state/clear-editor-action!))))
 
-          ;; When you type search text after < (and when you release shift after typing <)
-          (and (not (config/db-based-graph? (state/get-current-repo))) (= :block-commands (state/get-editor-action)) (not= key-code 188)) ; not <
-          (let [matched-block-commands (get-matched-block-commands input)
-                format (:format (get-state))]
-            (if (seq matched-block-commands)
-              (cond
-                (= key-code 9)          ;tab
-                (do
-                  (util/stop e)
-                  (insert-command! input-id
-                                   (last (first matched-block-commands))
-                                   format
-                                   {:last-pattern commands/angle-bracket
-                                    :command :block-commands}))
-
-                :else
-                (reset! commands/*matched-block-commands matched-block-commands))
-              (state/clear-editor-action!)))
-
-          ;; When you type two spaces after a command character (may always just be handled by the above instead?)
-          (and (contains? #{:block-commands} (state/get-editor-action))
-               (= c (util/nth-safe value (dec (dec current-pos))) " "))
-          (state/clear-editor-action!)
-
           :else
-          (default-case-for-keyup-handler input current-pos k code is-processed? c))
+          (default-case-for-keyup-handler input current-pos k code is-processed?))
 
         (close-autocomplete-if-outside input)
 

+ 0 - 4
src/main/frontend/state.cljs

@@ -1112,10 +1112,6 @@ Similar to re-frame subscriptions"
   []
   (when-not (get-editor-action) (set-editor-action! :commands)))
 
-(defn set-editor-show-block-commands!
-  []
-  (when-not (get-editor-action) (set-editor-action! :block-commands)))
-
 (defn clear-editor-action!
   []
   (set-state! :editor/action nil))

+ 1 - 1
src/test/frontend/handler/editor_test.cljs

@@ -87,7 +87,7 @@
                   ;; Ignore as none of its behaviors are tested
                   editor/default-case-for-keyup-handler (constantly nil)
                   cursor/pos (constantly pos)]
-      ((editor/keyup-handler nil input nil)
+      ((editor/keyup-handler nil input)
        #js {:key (subs value (dec (count value)))}
        nil))))