Browse Source

enhance(mobile): () supports toggle between block reference/embed

1. (| denotes cursor postion),
   - insert `((|))` after first click
   - change `((|))` to `{{embed ((|))}}`
   - later click toggles between `((|))` and `{{embed ((|))}}`
2. if cursor at `((uuid-uuid-uuid|))`, the reference will be changed to`{{embed
((uuid-uuid-uuid))}}|` when clcking the icon, and vice versa.
leizhe 4 years ago
parent
commit
c317836857
2 changed files with 33 additions and 17 deletions
  1. 1 6
      src/main/frontend/components/editor.cljs
  2. 32 11
      src/main/frontend/handler/editor.cljs

+ 1 - 6
src/main/frontend/components/editor.cljs

@@ -294,12 +294,7 @@
       [:button.bottom-action
        {:on-mouse-down (fn [e]
                          (util/stop e)
-                         (commands/simple-insert!
-                          parent-id "(())"
-                          {:backward-pos 2
-                           :check-fn     (fn [_ _ new-pos]
-                                           (reset! commands/*slash-caret-pos new-pos)
-                                           (commands/handle-step [:editor/search-block]))})
+                         (editor-handler/toggle-block-reference-embed parent-id)
                          (when-let [input (gdom/getElement parent-id)]
                            (.focus input)))}
        (ui/icon "parentheses"

+ 32 - 11
src/main/frontend/handler/editor.cljs

@@ -2473,9 +2473,9 @@
   (let [{:keys [block]} (get-state)]
     (when block
       (let [input (state/get-input)
-            page-ref-fn (fn [] (commands/simple-insert!
-                                parent-id "[[]]"
-                                {:backward-pos 2
+            page-ref-fn (fn [bounds backward-pos] (commands/simple-insert!
+                                parent-id bounds
+                                {:backward-pos backward-pos
                                  :check-fn     (fn [_ _ new-pos]
                                                  (reset! commands/*slash-caret-pos new-pos)
                                                  (commands/handle-step [:editor/search-page]))}))]
@@ -2489,20 +2489,41 @@
               (let [{:keys [raw-content start end]} embed-ref]
                 (delete-and-update input start end)
                 (if (= 5 (count raw-content))
-                  (page-ref-fn)
+                  (page-ref-fn "[[]]" 2)
                   (insert raw-content)))
               (if-let [page-ref (thingatpt/page-ref-at-point input)]
                 (let [{:keys [start end link full-content raw-content]} page-ref]
                   (delete-and-update input start end)
                   (if (= raw-content "")
-                    (commands/simple-insert!
-                     parent-id "{{embed [[]]}}"
-                     {:backward-pos 4
-                      :check-fn     (fn [_ _ new-pos]
-                                      (reset! commands/*slash-caret-pos new-pos)
-                                      (commands/handle-step [:editor/search-page]))})
+                    (page-ref-fn "{{embed [[]]}}" 4)
                     (insert (util/format "{{embed %s}}" full-content))))
-                (page-ref-fn)))))))))
+                (page-ref-fn "[[]]" 2)))))))))
+
+(defn toggle-block-reference-embed
+  [parent-id]
+  (let [{:keys [block]} (get-state)]
+    (when block
+      (let [input (state/get-input)
+            block-ref-fn (fn [bounds backward-pos] (commands/simple-insert!
+                                parent-id bounds
+                                {:backward-pos backward-pos
+                                 :check-fn     (fn [_ _ new-pos]
+                                                 (reset! commands/*slash-caret-pos new-pos)
+                                                 (commands/handle-step [:editor/search-block]))}))]
+        (state/set-editor-show-block-search! false)
+        (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 "(())" 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 "(())" 2)))))))
 
 (defn- keydown-new-block
   [state]