Skip to content

Commit

Permalink
refactor: move block/page/template searches to editor handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Mar 1, 2021
1 parent 2071de2 commit b0f48a4
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 99 deletions.
103 changes: 6 additions & 97 deletions src/main/frontend/components/editor.cljs
Expand Up @@ -32,7 +32,6 @@
[medley.core :as medley]
[cljs-drag-n-drop.core :as dnd]
[frontend.text :as text]
[frontend.template :as template]
[frontend.date :as date]
[frontend.handler.notification :as notification]
["/frontend/utils" :as utils]))
Expand Down Expand Up @@ -93,45 +92,11 @@
(when (> (count edit-content) current-pos)
(util/safe-subs edit-content pos current-pos)))
matched-pages (when-not (string/blank? q)
(editor-handler/get-matched-pages q))
chosen-handler (if (state/sub :editor/show-page-search-hashtag?)
(fn [chosen _click?]
(state/set-editor-show-page-search! false)
(let [chosen (if (re-find #"\s+" chosen)
(util/format "[[%s]]" chosen)
chosen)]
(editor-handler/insert-command! id
(str "#" chosen)
format
{:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})))
(fn [chosen _click?]
(state/set-editor-show-page-search! false)
(let [page-ref-text (page-handler/get-page-ref-text chosen)]
(editor-handler/insert-command! id
page-ref-text
format
{:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
:postfix-fn (fn [s] (util/replace-first "]]" s ""))}))))
non-exist-page-handler (fn [_state]
(state/set-editor-show-page-search! false)
(if (state/org-mode-file-link? (state/get-current-repo))
(let [page-ref-text (page-handler/get-page-ref-text q)
value (gobj/get input "value")
old-page-ref (util/format "[[%s]]" q)
new-value (string/replace value
old-page-ref
page-ref-text)]
(state/set-edit-content! id new-value)
(let [new-pos (+ current-pos
(- (count page-ref-text)
(count old-page-ref))
2)]
(util/move-cursor-to input new-pos)))
(util/cursor-move-forward input 2)))]
(editor-handler/get-matched-pages q))]
(ui/auto-complete
matched-pages
{:on-chosen chosen-handler
:on-enter non-exist-page-handler
{:on-chosen (page-handler/on-chosen-handler input id q pos format)
:on-enter #(page-handler/page-not-exists-handler input id q current-pos)
:empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a page"]
:class "black"}))))))

Expand All @@ -155,27 +120,8 @@
matched-blocks (when-not (string/blank? q)
(editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
(when input
(let [chosen-handler (fn [chosen _click?]
(state/set-editor-show-block-search! false)
(let [uuid-string (str (:block/uuid chosen))]

;; block reference
(editor-handler/insert-command! id
(util/format "((%s))" uuid-string)
format
{:last-pattern (str "((" (if @editor-handler/*selected-text "" q))
:postfix-fn (fn [s] (util/replace-first "))" s ""))})

;; Save it so it'll be parsed correctly in the future
(editor-handler/set-block-property! (:block/uuid chosen)
"ID"
uuid-string)

(when-let [input (gdom/getElement id)]
(.focus input))))
non-exist-block-handler (fn [_state]
(state/set-editor-show-block-search! false)
(util/cursor-move-forward input 2))]
(let [chosen-handler (editor-handler/block-on-chosen-handler input id q format)
non-exist-block-handler (editor-handler/block-non-exist-handler input)]
(ui/auto-complete
matched-blocks
{:on-chosen chosen-handler
Expand All @@ -200,48 +146,11 @@
(subs edit-content pos current-pos))
"")
matched-templates (editor-handler/get-matched-templates q)
chosen-handler (fn [[template db-id] _click?]
(if-let [block (db/entity db-id)]
(let [new-level (:block/level edit-block)
properties (:block/properties block)
block-uuid (:block/uuid block)
including-parent? (not= (get properties "including-parent") "false")
template-parent-level (:block/level block)
pattern (config/get-block-pattern format)
content
(block-handler/get-block-full-content
(state/get-current-repo)
(:block/uuid block)
(fn [{:block/keys [uuid level content properties] :as block}]
(let [parent? (= uuid block-uuid)
ignore-parent? (and parent? (not including-parent?))]
(if ignore-parent?
""
(let [new-level (+ new-level
(- level template-parent-level
(if (not including-parent?) 1 0)))
properties' (dissoc (into {} properties) "id" "custom_id" "template" "including-parent")]
(-> content
(string/replace-first (apply str (repeat level pattern))
(apply str (repeat new-level pattern)))
text/remove-properties!
(text/rejoin-properties properties')))))))
content (if (string/includes? (string/trim edit-content) "\n")
content
(text/remove-level-spaces content format))
content (template/resolve-dynamic-template! content)]
(state/set-editor-show-template-search! false)
(editor-handler/insert-command! id
content
format
{})))
(when-let [input (gdom/getElement id)]
(.focus input)))
non-exist-handler (fn [_state]
(state/set-editor-show-template-search! false))]
(ui/auto-complete
matched-templates
{:on-chosen chosen-handler
{:on-chosen (editor-handler/template-on-chosen-handler input id q format edit-block edit-content)
:on-enter non-exist-handler
:empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a template"]
:item-render (fn [[template _block-db-id]]
Expand Down
66 changes: 66 additions & 0 deletions src/main/frontend/handler/editor.cljs
Expand Up @@ -12,6 +12,7 @@
[frontend.handler.notification :as notification]
[frontend.handler.draw :as draw]
[frontend.handler.expand :as expand]
[frontend.handler.block :as block-handler]
[frontend.format.mldoc :as mldoc]
[frontend.format :as format]
[frontend.format.block :as block]
Expand Down Expand Up @@ -44,6 +45,7 @@
[frontend.text :as text]
[frontend.date :as date]
[frontend.handler.repeated :as repeated]
[frontend.template :as template]
[clojure.core.async :as async]
[lambdaisland.glogi :as log]))

Expand Down Expand Up @@ -2283,3 +2285,67 @@
(reset! commands/*angle-bracket-caret-pos (util/get-caret-pos input))
(reset! commands/*show-block-commands true))
nil))))

(defn block-on-chosen-handler
[input id q format]
(fn [chosen _click?]
(state/set-editor-show-block-search! false)
(let [uuid-string (str (:block/uuid chosen))]

;; block reference
(insert-command! id
(util/format "((%s))" uuid-string)
format
{:last-pattern (str "((" (if @*selected-text "" q))
:postfix-fn (fn [s] (util/replace-first "))" s ""))})

;; Save it so it'll be parsed correctly in the future
(set-block-property! (:block/uuid chosen)
"ID"
uuid-string)

(when-let [input (gdom/getElement id)]
(.focus input)))))

(defn block-non-exist-handler
[input]
(fn []
(state/set-editor-show-block-search! false)
(util/cursor-move-forward input 2)))

(defn template-on-chosen-handler
[input id q format edit-block edit-content]
(fn [[template db-id] _click?]
(if-let [block (db/entity db-id)]
(let [new-level (:block/level edit-block)
properties (:block/properties block)
block-uuid (:block/uuid block)
including-parent? (not= (get properties "including-parent") "false")
template-parent-level (:block/level block)
pattern (config/get-block-pattern format)
content
(block-handler/get-block-full-content
(state/get-current-repo)
(:block/uuid block)
(fn [{:block/keys [uuid level content properties] :as block}]
(let [parent? (= uuid block-uuid)
ignore-parent? (and parent? (not including-parent?))]
(if ignore-parent?
""
(let [new-level (+ new-level
(- level template-parent-level
(if (not including-parent?) 1 0)))
properties' (dissoc (into {} properties) "id" "custom_id" "template" "including-parent")]
(-> content
(string/replace-first (apply str (repeat level pattern))
(apply str (repeat new-level pattern)))
text/remove-properties!
(text/rejoin-properties properties')))))))
content (if (string/includes? (string/trim edit-content) "\n")
content
(text/remove-level-spaces content format))
content (template/resolve-dynamic-template! content)]
(state/set-editor-show-template-search! false)
(insert-command! id content format {})))
(when-let [input (gdom/getElement id)]
(.focus input))))
52 changes: 51 additions & 1 deletion src/main/frontend/handler/page.cljs
Expand Up @@ -25,7 +25,8 @@
[lambdaisland.glogi :as log]
[frontend.format.mldoc :as mldoc]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]))
[cljs-time.coerce :as tc]
[goog.object :as gobj]))

(defn- get-directory
[journal?]
Expand Down Expand Up @@ -519,3 +520,52 @@
[page-name]
(when page-name
(db/entity [:page/name page-name])))

;; Editor
(defn page-not-exists-handler
[input id q current-pos]
(state/set-editor-show-page-search! false)
(if (state/org-mode-file-link? (state/get-current-repo))
(let [page-ref-text (get-page-ref-text q)
value (gobj/get input "value")
old-page-ref (util/format "[[%s]]" q)
new-value (string/replace value
old-page-ref
page-ref-text)]
(state/set-edit-content! id new-value)
(let [new-pos (+ current-pos
(- (count page-ref-text)
(count old-page-ref))
2)]
(util/move-cursor-to input new-pos)))
(util/cursor-move-forward input 2)))

(defn on-chosen-handler
[input id q pos format]
(let [current-pos (:pos (util/get-caret-pos input))
edit-content (state/sub [:editor/content id])
edit-block (state/sub :editor/block)
q (or
@editor-handler/*selected-text
(when (state/sub :editor/show-page-search-hashtag?)
(util/safe-subs edit-content pos current-pos))
(when (> (count edit-content) current-pos)
(util/safe-subs edit-content pos current-pos)))]
(if (state/sub :editor/show-page-search-hashtag?)
(fn [chosen _click?]
(state/set-editor-show-page-search! false)
(let [chosen (if (re-find #"\s+" chosen)
(util/format "[[%s]]" chosen)
chosen)]
(editor-handler/insert-command! id
(str "#" chosen)
format
{:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})))
(fn [chosen _click?]
(state/set-editor-show-page-search! false)
(let [page-ref-text (get-page-ref-text chosen)]
(editor-handler/insert-command! id
page-ref-text
format
{:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
:postfix-fn (fn [s] (util/replace-first "]]" s ""))}))))))
2 changes: 1 addition & 1 deletion src/main/frontend/ui.css
Expand Up @@ -62,7 +62,7 @@
}

&-overlay div {
background: var(--ls-quaternary-background-color);
background: var(--ls-tertiary-background-color);
}

&-panel {
Expand Down

0 comments on commit b0f48a4

Please sign in to comment.