feat: add hashtag autocomplete

pull/645/head
Haoji Xu 2020-08-31 18:11:57 +08:00
parent ebb30a79ab
commit d91dd76a2a
5 changed files with 65 additions and 12 deletions

View File

@ -214,6 +214,7 @@
new-pos (- (+ (count prefix)
(or forward-pos 0))
(or backward-pos 0))]
(println edit-content current-pos prefix value)
(state/set-block-content-and-last-pos! id new-value new-pos)
(util/move-cursor-to input
(if (or backward-pos forward-pos)
@ -373,6 +374,9 @@
(defmethod handle-step :editor/search-page [[_]]
(state/set-editor-show-page-search true))
(defmethod handle-step :editor/search-page-hashtag [[_]]
(state/set-editor-show-page-search-hashtag true))
(defmethod handle-step :editor/search-block [[_ type]]
(state/set-editor-show-block-search true))

View File

@ -80,17 +80,28 @@
edit-content (state/sub [:editor/content id])
q (or
@editor-handler/*selected-text
(when (state/sub :editor/show-page-search-hashtag?)
(subs edit-content pos current-pos))
(when (> (count edit-content) current-pos)
(subs edit-content pos current-pos)))
matched-pages (when-not (string/blank? q)
(editor-handler/get-matched-pages q))
chosen-handler (fn [chosen _click?]
chosen-handler (if (state/sub :editor/show-page-search-hashtag?)
(fn [chosen _click?]
(state/set-editor-show-page-search false)
(editor-handler/insert-command! id
(util/format "#%s" (if (string/includes? chosen " ")
(str "[[" chosen "]]")
chosen))
format
{:last-pattern (str "#" (if @editor-handler/*selected-text "" q))}))
(fn [chosen _click?]
(state/set-editor-show-page-search false)
(editor-handler/insert-command! id
(util/format "[[%s]]" chosen)
format
{:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
:postfix-fn (fn [s] (util/replace-first "]]" s ""))}))
:postfix-fn (fn [s] (util/replace-first "]]" s ""))})))
non-exist-page-handler (fn [_state]
(state/set-editor-show-page-search false)
(util/cursor-move-forward input 2))]
@ -436,6 +447,9 @@
(and (= deleted "(") (state/get-editor-show-block-search))
(state/set-editor-show-block-search false)
(and (= deleted "#") (state/get-editor-show-page-search-hashtag))
(state/set-editor-show-page-search-hashtag false)
:else
nil))
@ -463,6 +477,19 @@
(commands/handle-step [:editor/search-page])
(reset! commands/*slash-caret-pos (util/get-caret-pos input)))
(and
(not= key-code 8) ;; backspace
(or
(editor-handler/surround-by? input "#" " ")
(editor-handler/surround-by? input "#" :end)
(and (= key "#") (editor-handler/surround-by? input "#" :end))
(and (= key "#") (editor-handler/surround-by? input " " :end))
(and (= key "#") (editor-handler/surround-by? input :start :end))))
(do
(commands/handle-step [:editor/search-page-hashtag])
(state/set-last-pos! (:pos (util/get-caret-pos input)))
(reset! commands/*slash-caret-pos (util/get-caret-pos input)))
(editor-handler/surround-by? input "((" "))")
(do
(commands/handle-step [:editor/search-block :reference])
@ -587,7 +614,6 @@
:display "flex"
:flex "1 1 0%"}
:class (if block "block-editor" "non-block-editor")}
(println id)
(when config/mobile? (mobile-bar state id))
(ui/textarea
{:id id

View File

@ -1145,10 +1145,21 @@
(when input
(let [value (gobj/get input "value")
pos (util/get-input-pos input)
start-pos (- pos (count before))
end-pos (+ pos (count after))]
start-pos (if (= :start before) 0 (- pos (count before)))
end-pos (if (= :end after) (count value) (+ pos (count after)))]
(when (>= (count value) end-pos)
(= (str before after)
(= (cond
(and (= :end after) (= :start before))
""
(= :end after)
before
(= :start before)
after
:else
(str before after))
(subs value start-pos end-pos))))))
(defn get-matched-pages

View File

@ -49,6 +49,7 @@
:github/contents {}
:config {}
:editor/show-page-search? false
:editor/show-page-search-hashtag? false
:editor/show-date-picker? false
;; With label or other data
:editor/show-input nil
@ -280,9 +281,16 @@
(defn set-editor-show-page-search
[value]
(set-state! :editor/show-page-search? value))
(defn set-editor-show-page-search-hashtag
[value]
(set-state! :editor/show-page-search? value)
(set-state! :editor/show-page-search-hashtag? value))
(defn get-editor-show-page-search
[]
(get @state :editor/show-page-search?))
(defn get-editor-show-page-search-hashtag
[]
(get @state :editor/show-page-search-hashtag?))
(defn set-editor-show-block-search
[value]
(set-state! :editor/show-block-search? value))
@ -478,6 +486,10 @@
[]
(get @state :editor/block))
(defn set-last-pos!
[new-pos]
(reset! state (assoc @state :editor/last-saved-cursor new-pos)))
(defn set-block-content-and-last-pos!
[edit-input-id content new-pos]
(when edit-input-id

View File

@ -260,7 +260,7 @@
item-render
class]}]
(let [current-idx (get state ::current-idx)]
[:div.py-1.rounded-md.shadow-xs.bg-base-2 {:class class}
[:div.py-1.rounded-md.shadow-xs.bg-base-3 {:class class}
(if (seq matched)
[:div#ac-inner
{:style {:max-height 400