Forráskód Böngészése

feat: Improve text editing across blocks

Related thread at
https://discuss.logseq.com/t/improve-text-editing-across-blocks/148

Related to #557
Tienson Qin 4 éve
szülő
commit
11ee002e2a

+ 4 - 0
src/main/frontend/components/editor.cljs

@@ -321,6 +321,10 @@
     38 (editor-handler/keydown-up-down-handler input true)
     ;; down
     40 (editor-handler/keydown-up-down-handler input false)
+    ;; left
+    37 (editor-handler/keydown-arrow-handler input :left)
+    ;; right
+    39 (editor-handler/keydown-arrow-handler input :right)
     ;; backspace
     8 (editor-handler/keydown-backspace-handler repo input input-id)
     ;; tab

+ 41 - 0
src/main/frontend/handler/editor.cljs

@@ -2340,6 +2340,47 @@
            (not (in-auto-complete? input)))
       (on-up-down state e up?))))
 
+(defn- move-to-block-when-cross-boundrary
+  [state e direction]
+  (let [up? (= :left direction)
+        pos (if up? :max 0)
+        {:keys [id block-id block block-parent-id dummy? value format] :as block-state} (get-state state)
+        element (gdom/getElement id)]
+    (when block-id
+      (let [f (if up? get-prev-block-non-collapsed get-next-block-non-collapsed)
+            sibling-block (f (gdom/getElement block-parent-id))]
+        (when sibling-block
+          (when-let [sibling-block-id (d/attr sibling-block "blockid")]
+            (let [state (get-state state)
+                  content (:block/content block)
+                  value (:value state)]
+              (when (not= (-> content
+                              (text/remove-level-spaces format)
+                              text/remove-properties!
+                              string/trim)
+                          (string/trim value))
+                (save-block! state (:value state))))
+            (let [block (db/pull (state/get-current-repo) '[*] [:block/uuid (uuid sibling-block-id)])]
+              (edit-block! block pos format id)
+              (util/stop e))))))))
+
+(defn- on-arrow-move-to-boundray
+  [state input e direction]
+  (when (or (and (= :left direction) (util/input-start? input))
+            (and (= :right direction) (util/input-end? input)))
+    (move-to-block-when-cross-boundrary state e direction)))
+
+(defn keydown-arrow-handler
+  [input direction]
+  (fn [state e]
+    (when (and
+           input
+           (not (gobj/get e "shiftKey"))
+           (not (gobj/get e "ctrlKey"))
+           (not (gobj/get e "metaKey"))
+           (not (in-auto-complete? input)))
+      (on-arrow-move-to-boundray state input e direction))))
+
 (defn keydown-backspace-handler
   [repo input id]
   (fn [state e]

+ 12 - 0
src/main/frontend/util.cljc

@@ -749,6 +749,18 @@
       [input]
       (and input (.-selectionStart input))))
 
+#?(:cljs
+   (defn input-start?
+     [input]
+     (and input (zero? (.-selectionStart input)))))
+
+#?(:cljs
+   (defn input-end?
+     [input]
+     (and input
+          (= (count (.-value input))
+             (.-selectionStart input)))))
+
 #?(:cljs
     (defn get-selected-text
       []