Skip to content

Commit

Permalink
feat: add reference at
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed May 6, 2022
1 parent d4b3f55 commit cff0880
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/cljc/athens/common_events/bfs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@
;; - else the parent is the current block's parent
current-block-parent? (and children
open)
is-page? (common-db/get-page-title db uid)
empty-block? (and (string/blank? local-str)
(empty? children))
(empty? children)
(not is-page?))
new-block-str? (not= local-str string)
;; If block has a new local-str, write that
block-save-op (when new-block-str?
Expand All @@ -135,8 +137,11 @@
empty-block? current-block-parent-uid
current-block-parent? uid
:else current-block-parent-uid)
default-position (common-db/compat-position db {:block/uid block-position
:relation new-block-order})
default-position (common-db/compat-position db (if is-page?
{:page/title (common-db/get-page-title db uid)
:relation :last}
{:block/uid block-position
:relation new-block-order}))
ir-ops (internal-representation->atomic-ops db internal-representation default-position)
remove-op (when empty-block?
(graph-ops/build-block-remove-op db uid))]
Expand Down
4 changes: 2 additions & 2 deletions src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,11 +1625,11 @@
(reg-event-fx
:paste-internal
[(interceptors/sentry-span-no-new-tx "paste-internal")]
(fn [_ [_ uid local-str internal-representation]]
(fn [_ [_ uid local-str internal-representation target-uid]]
(when (seq internal-representation)
(let [[uid] (db/uid-and-embed-id uid)
op (bfs/build-paste-op @db/dsdb
uid
(or target-uid uid)
local-str
internal-representation)
new-titles (graph-ops/ops->new-page-titles op)
Expand Down
3 changes: 2 additions & 1 deletion src/cljs/athens/views/blocks/autocomplete_search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
f (case (:search/type @state)
:hashtag textarea-keydown/auto-complete-hashtag
:template textarea-keydown/auto-complete-template
:at textarea-keydown/auto-complete-at
textarea-keydown/auto-complete-inline)]
(f state target expansion)))

Expand All @@ -22,7 +23,7 @@
(fn [block state]
(let [{:keys [last-e]} @state
{:search/keys [index results type query]} @state
is-open (some #(= % type) [:page :block :hashtag :template])]
is-open (some #(= % type) [:page :block :hashtag :template :at])]
[:> Autocomplete {:event last-e
:isOpen is-open
:onClose #(swap! state assoc :search/type false)}
Expand Down
2 changes: 1 addition & 1 deletion src/cljs/athens/views/blocks/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
{:block/keys [uid original-uid]} block
state (r/atom {:string/local nil
:string/previous nil
;; one of #{:page :block :slash :hashtag :template}
;; one of #{:page :block :slash :hashtag :template :at}
:search/type nil
:search/results nil
:search/query nil
Expand Down
38 changes: 37 additions & 1 deletion src/cljs/athens/views/blocks/textarea_keydown.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@
:page db/search-in-node-title
:hashtag db/search-in-node-title
:template db/search-in-block-content
:at db/search-in-node-title
:slash filter-slash-options)
regex (case type
:block #"(?s).*\(\("
:page #"(?s).*\[\["
:hashtag #"(?s).*#"
:template #"(?s).*;;"
:at #"(?s).*@"
:slash #"(?s).*/")
find (re-find regex head)
query-start-idx (count find)
Expand Down Expand Up @@ -302,6 +304,32 @@
(swap! state assoc :search/type nil))))))


;; see `auto-complete-slash` for how this arity-overloaded
;; function is used.
(defn auto-complete-at
([state e]
(let [{:search/keys [index results]} @state
target (.. e -target)
{:keys [node/title]} (nth results index nil)
expansion title]
(auto-complete-at state target expansion)))

([state target expansion]
(let [{:keys [start head]} (destruct-target target)
start-idx (count (re-find #"(?s).*@" head))
uid (:block/uid @state)
page-uid (common-db/get-page-uid @db/dsdb expansion)
ref-ir [{:block/uid (common.utils/gen-block-uid)
:block/string (str "((" uid "))")}]]
(if (nil? expansion)
(swap! state assoc :search/type nil)
(do
(set-selection target start-idx start)
(replace-selection-with (str "[[" expansion "]]"))
(swap! state assoc :search/type nil)
(dispatch [:paste-internal uid (:string/local @state) ref-ir page-uid]))))))


;; Arrow Keys


Expand Down Expand Up @@ -492,7 +520,8 @@
:page (auto-complete-inline state e)
:block (auto-complete-inline state e)
:hashtag (auto-complete-hashtag state e)
:template (auto-complete-template state e))
:template (auto-complete-template state e)
:at (auto-complete-at state e))
;; shift-enter: add line break to textarea and move cursor to the next line.
shift (replace-selection-with "\n")
;; cmd-enter: cycle todo states, then move cursor to the end of the line.
Expand Down Expand Up @@ -760,6 +789,8 @@
(and (= "#" look-behind-char) (= type :hashtag)) (swap! state assoc :search/type nil)
;; semicolon: close dropdown
(and (= ";" look-behind-char) (= type :template)) (swap! state assoc :search/type nil)
;; at symbol: close dropdown
(and (= "@" look-behind-char) (= type :at)) (swap! state assoc :search/type nil)
;; dropdown is open: update query
type (update-query state head "" type))))

Expand Down Expand Up @@ -801,6 +832,11 @@
:search/query ""
:search/type :template
:search/results [])
(and (= key "@") (nil? type)) (swap! state assoc
:search/index 0
:search/query ""
:search/type :at
:search/results [])
type (update-query state head key type))))


Expand Down

0 comments on commit cff0880

Please sign in to comment.