Bladeren bron

fix: #tag and page ref on mobile

Tienson Qin 8 maanden geleden
bovenliggende
commit
cfbc002f3e
2 gewijzigde bestanden met toevoegingen van 23 en 74 verwijderingen
  1. 0 61
      src/main/frontend/handler/editor.cljs
  2. 23 13
      src/main/frontend/mobile/mobile_bar.cljs

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

@@ -2369,67 +2369,6 @@
                       (state/set-edit-content! (state/get-edit-input-id) value')
                       (state/set-edit-content! (state/get-edit-input-id) value')
                       (cursor/move-cursor-to input cursor'))))))))))))
                       (cursor/move-cursor-to input cursor'))))))))))))
 
 
-(defn toggle-page-reference-embed
-  [parent-id]
-  (let [{:keys [block]} (get-state)]
-    (when block
-      (let [input (state/get-input)
-            new-pos (cursor/get-caret-pos input)
-            page-ref-fn (fn [bounds backward-pos]
-                          (commands/simple-insert!
-                           parent-id bounds
-                           {:backward-pos backward-pos
-                            :check-fn (fn [_ _ _]
-                                        (state/set-editor-action-data! {:pos new-pos})
-                                        (commands/handle-step [:editor/search-page]))}))]
-        (state/clear-editor-action!)
-        (let [selection (get-selection-and-format)
-              {:keys [selection-start selection-end selection]} selection]
-          (if selection
-            (do (delete-and-update input selection-start selection-end)
-                (insert (ref/->page-ref selection)))
-            (if-let [embed-ref (thingatpt/embed-macro-at-point input)]
-              (let [{:keys [raw-content start end]} embed-ref]
-                (delete-and-update input start end)
-                (if (= 5 (count raw-content))
-                  (page-ref-fn page-ref/left-and-right-brackets 2)
-                  (insert raw-content)))
-              (if-let [page-ref (thingatpt/page-ref-at-point input)]
-                (let [{:keys [start end full-content raw-content]} page-ref]
-                  (delete-and-update input start end)
-                  (if (= raw-content "")
-                    (page-ref-fn "{{embed [[]]}}" 4)
-                    (insert (util/format "{{embed %s}}" full-content))))
-                (page-ref-fn page-ref/left-and-right-brackets 2)))))))))
-
-(defn toggle-block-reference-embed
-  [parent-id]
-  (let [{:keys [block]} (get-state)]
-    (when block
-      (let [input (state/get-input)
-            new-pos (cursor/get-caret-pos input)
-            block-ref-fn (fn [bounds backward-pos]
-                           (commands/simple-insert!
-                            parent-id bounds
-                            {:backward-pos backward-pos
-                             :check-fn     (fn [_ _ _]
-                                             (state/set-editor-action-data! {:pos new-pos})
-                                             (commands/handle-step [:editor/search-block]))}))]
-        (state/clear-editor-action!)
-        (if-let [embed-ref (thingatpt/embed-macro-at-point input)]
-          (let [{:keys [raw-content start end]} embed-ref]
-            (delete-and-update input start end)
-            (if (= 5 (count raw-content))
-              (block-ref-fn block-ref/left-and-right-parens 2)
-              (insert raw-content)))
-          (if-let [page-ref (thingatpt/block-ref-at-point input)]
-            (let [{:keys [start end full-content raw-content]} page-ref]
-              (delete-and-update input start end)
-              (if (= raw-content "")
-                (block-ref-fn "{{embed (())}}" 4)
-                (insert (util/format "{{embed %s}}" full-content))))
-            (block-ref-fn block-ref/left-and-right-parens 2)))))))
-
 (defn- keydown-new-block
 (defn- keydown-new-block
   [state]
   [state]
   (when-not (auto-complete?)
   (when-not (auto-complete?)

+ 23 - 13
src/main/frontend/mobile/mobile_bar.cljs

@@ -10,7 +10,9 @@
             [frontend.state :as state]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.ui :as ui]
             [frontend.util :as util]
             [frontend.util :as util]
+            [frontend.util.cursor :as cursor]
             [goog.dom :as gdom]
             [goog.dom :as gdom]
+            [logseq.common.util.page-ref :as page-ref]
             [rum.core :as rum]))
             [rum.core :as rum]))
 
 
 (defn- blur-if-compositing
 (defn- blur-if-compositing
@@ -71,21 +73,29 @@
                      (commands/simple-insert! parent-id timestamp {}))
                      (commands/simple-insert! parent-id timestamp {}))
                   "Time")]]))
                   "Time")]]))
 
 
+(defn- insert-text
+  [text opts]
+  (when-let [parent-id (state/get-edit-input-id)]
+    (let [input (gdom/getElement parent-id)
+          pos (cursor/pos input)
+          c (when (> pos 0)
+              (str (nth (.-value input) (dec pos))))
+          text' (if (and c (not= c " "))
+                  (str " " text)
+                  text)]
+      (commands/simple-insert! parent-id text' opts))))
+
 (defn commands
 (defn commands
   []
   []
-  [(command #(let [parent-id (state/get-edit-input-id)]
-               ;; TODO: set tags
-               )
-            {:icon "hash"}
-            true)
-   (command #(let [parent-id (state/get-edit-input-id)]
-               (editor-handler/toggle-page-reference-embed parent-id))
-            {:icon "brackets"}
-            true)
-   (command #(let [parent-id (state/get-edit-input-id)]
-               (commands/simple-insert! parent-id " /" {}))
-            {:icon "command"}
-            true)])
+  [(command #(insert-text "#" {}) {:icon "hash"} true)
+   (command #(let [input (state/get-input)
+                   new-pos (cursor/get-caret-pos input)]
+               (insert-text page-ref/left-and-right-brackets
+                            {:backward-pos 2
+                             :check-fn (fn [_ _ _]
+                                         (state/set-editor-action-data! {:pos new-pos})
+                                         (commands/handle-step [:editor/search-page]))})) {:icon "brackets"} true)
+   (command #(insert-text "/" {}) {:icon "command"} true)])
 
 
 (rum/defc mobile-bar < rum/reactive
 (rum/defc mobile-bar < rum/reactive
   []
   []