Skip to content

Commit

Permalink
enhance(editor): reorder page search autocompletion items
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Feb 10, 2022
1 parent fef8c39 commit e2ee417
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main/frontend/components/editor.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns frontend.components.editor
(:require [clojure.string :as string]
[goog.string :as gstring]
[frontend.commands :as commands
:refer [*angle-bracket-caret-pos *first-command-group *matched-block-commands *matched-commands *show-block-commands *show-commands *slash-caret-pos]]
[frontend.components.block :as block]
Expand Down Expand Up @@ -97,7 +98,7 @@

(rum/defc page-search < rum/reactive
{:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
"Editor embedded page searching"
"Embedded page searching popup"
[id format]
(when (state/sub :editor/show-page-search?)
(let [pos (:editor/last-saved-cursor @state/state)
Expand All @@ -119,20 +120,23 @@
(contains? (set (map util/page-name-sanity-lc matched-pages)) (util/page-name-sanity-lc (string/trim q))) ;; if there's a page name fully matched
matched-pages

(string/blank? q)
nil

(empty? matched-pages)
matched-pages
(cons (str "New page: " q) matched-pages)

;; reorder, shortest and starts-with first.
:else
(let [matched-pages (remove nil? matched-pages)
matched-pages (sort-by
(fn [m]
[(not (string/starts-with? m q)) (count m)])
[(not (gstring/caseInsensitiveStartsWith m q)) (count m)])
matched-pages)]
(cons (first matched-pages)
(cons
(str "New page: " q)
(rest matched-pages)))))]
(if (gstring/caseInsensitiveStartsWith (first matched-pages) q)
(cons (first matched-pages)
(cons (str "New page: " q) (rest matched-pages)))
(cons (str "New page: " q) matched-pages))))]
(ui/auto-complete
matched-pages
{:on-chosen (page-handler/on-chosen-handler input id q pos format)
Expand Down

0 comments on commit e2ee417

Please sign in to comment.