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.
This commit is contained in:
leizhe
2022-01-04 15:18:32 +08:00
committed by Andelf
parent 4ede1dee37
commit f39df32872
3 changed files with 38 additions and 7 deletions

View File

@@ -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?)