Browse Source

enhance(mobile): [] supports toggle between page reference/embed

1. if text selected, toggle the text to a page reference `[[text]]`
2. if no text selected (| denotes cursor postion),
   - insert `[[|]]` after first click
   - change `[[]]` to `{{embed [[|]]}}`
   - later click toggles between `[[|]]` and `{{embed [[|]]}}`
3. if cursor at [[text|]], The reference will be changed to`{{embed
[[text]]}}|` when clcking the icon, and vice versa.
leizhe 4 years ago
parent
commit
f39df32872

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

@@ -285,12 +285,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-page]))})
+                         (editor-handler/toggle-page-reference-embed parent-id)
                          (when-let [input (gdom/getElement parent-id)]
                            (.focus input)))}
        (ui/icon "brackets"

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

@@ -2468,6 +2468,42 @@
                           nil)
                         (cursor/move-cursor-to input (+ (:end item) (count next-bullet) 2)))))))))))))
 
+(defn toggle-page-reference-embed
+  [parent-id]
+  (let [{:keys [block]} (get-state)]
+    (when block
+      (let [input (state/get-input)
+            page-ref-fn (fn [] (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-page]))}))]
+        (state/set-editor-show-page-search! false)
+        (let [selection (get-selection-and-format)
+              {:keys [selection-start selection-end value]} selection]
+          (if (not= selection-start selection-end)
+            (do (delete-and-update selection-start selection-end)
+                (insert (util/format "[[%s]]" value)))
+            (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)
+                  (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]))})
+                    (insert (util/format "{{embed %s}}" full-content))))
+                (page-ref-fn)))))))))
+
 (defn- keydown-new-block
   [state]
   (when-not (auto-complete?)

+ 1 - 1
src/main/frontend/util/thingatpt.cljs

@@ -59,7 +59,7 @@
                   (:full-content page-ref)))))
 
 (defn embed-macro-at-point [& [input]]
-  (when-let [macro (thing-at-point ["{{embed" "}}"] (first input) " ")]
+  (when-let [macro (thing-at-point ["{{embed" "}}"] input)]
     (assoc macro :type "macro")))
 
 (defn properties-at-point [& [input]]