Przeglądaj źródła

enhance(ux): enter to edit the code type block

charlie 1 rok temu
rodzic
commit
7d1d6f49da

+ 7 - 3
src/main/frontend/components/block.cljs

@@ -4033,16 +4033,20 @@
         (case (:logseq.property.node/display-type editing-block)
           :code
           (let [_cursor-pos (some-> (:editor/cursor-range @state/state) (deref) (count))
-                direction (:block.tmp/direction editing-block)
+                direction (:block.editing/direction editing-block)
+                pos (:block.editing/pos editing-block)
                 target (js/document.querySelector
                          (util/format "a.select-language[blockid=\"%s\"][containerid=\"%s\"]" uuid' container-id))]
             (when-let [cm (get-cm-instance target)]
               (let [to-line (case direction
-                              :up (.lastLine cm) 0)]
+                              :up (.lastLine cm)
+                              (case pos
+                                :max (.lastLine cm)
+                                0))]
                 ;; move to friendly cursor
                 (doto cm
                   (.focus)
-                  (.setCursor to-line 0)))))
+                  (.setCursor to-line (or _cursor-pos 0))))))
           :dune))
       [editing-block]))
   nil)

+ 3 - 3
src/main/frontend/handler/block.cljs

@@ -171,13 +171,13 @@
     (state/set-editor-last-input-time! repo (util/time-ms))))
 
 (defn- edit-block-aux
-  [repo block content text-range {:keys [container-id direction]}]
+  [repo block content text-range {:keys [container-id direction event pos]}]
   (when block
     (let [container-id (or container-id
                            (state/get-current-editor-container-id)
                            :unknown-container)]
       (state/set-editing! (str "edit-block-" (:block/uuid block)) content block text-range
-        {:container-id container-id :direction direction}))
+        {:container-id container-id :direction direction :event event :pos pos}))
     (mark-last-input-time! repo)))
 
 (defn sanity-block-content
@@ -215,7 +215,7 @@
                           (subs content 0 pos))
              content (sanity-block-content repo (:block/format block) content)]
          (state/clear-selection!)
-         (edit-block-aux repo block content text-range opts))))))
+         (edit-block-aux repo block content text-range (assoc opts :pos pos)))))))
 
 (defn- get-original-block-by-dom
   [node]

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -3285,7 +3285,8 @@
       (util/stop e)
       (let [block {:block/uuid block-id}
             left? (= direction :left)
-            opts {:container-id (some-> node (dom/attr "containerid") (parse-long))}]
+            opts {:container-id (some-> node (dom/attr "containerid") (parse-long))
+                  :event e}]
         (edit-block! block (if left? 0 :max) opts)))))
 
 (defn shortcut-left-right [direction]

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

@@ -2034,7 +2034,7 @@ Similar to re-frame subscriptions"
    (set-selection-blocks! blocks direction)))
 
 (defn set-editing!
-  [edit-input-id content block cursor-range & {:keys [move-cursor? container-id property-block direction]
+  [edit-input-id content block cursor-range & {:keys [move-cursor? container-id property-block direction event pos]
                                                :or {move-cursor? true}}]
   (when-not (exists? js/process)
     (if (> (count content)
@@ -2053,7 +2053,9 @@ Similar to re-frame subscriptions"
                         (assoc block
                                :block.temp/container (gobj/get container "id"))
                         block)
-                block (assoc block :block.tmp/direction direction)
+                block (assoc block :block.editing/direction direction
+                        :block.editing/event event
+                        :block.editing/pos pos)
                 content (string/trim (or content ""))]
             (assert (and container-id (:block/uuid block))
                     "container-id or block uuid is missing")