|
133 | 133 | :page db/search-in-node-title |
134 | 134 | :hashtag db/search-in-node-title |
135 | 135 | :template db/search-in-block-content |
| 136 | + :add-to db/search-in-node-title |
136 | 137 | :slash filter-slash-options) |
137 | 138 | regex (case type |
138 | 139 | :block #"(?s).*\(\(" |
139 | 140 | :page #"(?s).*\[\[" |
140 | 141 | :hashtag #"(?s).*#" |
141 | 142 | :template #"(?s).*;;" |
| 143 | + :add-to #"(?s).*\+" |
142 | 144 | :slash #"(?s).*/") |
143 | 145 | find (re-find regex head) |
144 | 146 | query-start-idx (count find) |
|
302 | 304 | (swap! state assoc :search/type nil)))))) |
303 | 305 |
|
304 | 306 |
|
| 307 | +;; see `auto-complete-slash` for how this arity-overloaded |
| 308 | +;; function is used. |
| 309 | +(defn auto-complete-add-to |
| 310 | + ([state e] |
| 311 | + (let [{:search/keys [index results]} @state |
| 312 | + target (.. e -target) |
| 313 | + {:keys [node/title]} (nth results index nil) |
| 314 | + expansion title] |
| 315 | + (auto-complete-add-to state target expansion))) |
| 316 | + |
| 317 | + ([state target expansion] |
| 318 | + (let [{:keys [start head]} (destruct-target target) |
| 319 | + start-idx (count (re-find #"(?s).*\+" head))] |
| 320 | + (if (nil? expansion) |
| 321 | + (swap! state assoc :search/type nil) |
| 322 | + (do |
| 323 | + (set-selection target start-idx start) |
| 324 | + (replace-selection-with (str "[[" expansion "]]")) |
| 325 | + (swap! state assoc :search/type nil) |
| 326 | + (dispatch [:add-to (:string/local @state) (:block/uid @state) expansion])))))) |
| 327 | + |
| 328 | + |
305 | 329 | ;; Arrow Keys |
306 | 330 |
|
307 | 331 |
|
|
492 | 516 | :page (auto-complete-inline state e) |
493 | 517 | :block (auto-complete-inline state e) |
494 | 518 | :hashtag (auto-complete-hashtag state e) |
495 | | - :template (auto-complete-template state e)) |
| 519 | + :template (auto-complete-template state e) |
| 520 | + :add-to (auto-complete-add-to state e)) |
496 | 521 | ;; shift-enter: add line break to textarea and move cursor to the next line. |
497 | 522 | shift (replace-selection-with "\n") |
498 | 523 | ;; cmd-enter: cycle todo states, then move cursor to the end of the line. |
|
760 | 785 | (and (= "#" look-behind-char) (= type :hashtag)) (swap! state assoc :search/type nil) |
761 | 786 | ;; semicolon: close dropdown |
762 | 787 | (and (= ";" look-behind-char) (= type :template)) (swap! state assoc :search/type nil) |
| 788 | + ;; at symbol: close dropdown |
| 789 | + (and (= "+" look-behind-char) (= type :at)) (swap! state assoc :search/type nil) |
763 | 790 | ;; dropdown is open: update query |
764 | 791 | type (update-query state head "" type)))) |
765 | 792 |
|
|
801 | 828 | :search/query "" |
802 | 829 | :search/type :template |
803 | 830 | :search/results []) |
| 831 | + (and (= key "+") (nil? type)) (swap! state assoc |
| 832 | + :search/index 0 |
| 833 | + :search/query "" |
| 834 | + :search/type :add-to |
| 835 | + :search/results []) |
804 | 836 | type (update-query state head key type)))) |
805 | 837 |
|
806 | 838 |
|
|
0 commit comments