Skip to content

Commit 0b833e3

Browse files
committed
feat: add reference at
1 parent d4b3f55 commit 0b833e3

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

src/cljc/athens/common_events/bfs.cljc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@
117117
;; - else the parent is the current block's parent
118118
current-block-parent? (and children
119119
open)
120+
is-page? (common-db/get-page-title db uid)
120121
empty-block? (and (string/blank? local-str)
121-
(empty? children))
122+
(empty? children)
123+
(not is-page?))
122124
new-block-str? (not= local-str string)
123125
;; If block has a new local-str, write that
124126
block-save-op (when new-block-str?
@@ -135,8 +137,11 @@
135137
empty-block? current-block-parent-uid
136138
current-block-parent? uid
137139
:else current-block-parent-uid)
138-
default-position (common-db/compat-position db {:block/uid block-position
139-
:relation new-block-order})
140+
default-position (common-db/compat-position db (if is-page?
141+
{:page/title (common-db/get-page-title db uid)
142+
:relation :last}
143+
{:block/uid block-position
144+
:relation new-block-order}))
140145
ir-ops (internal-representation->atomic-ops db internal-representation default-position)
141146
remove-op (when empty-block?
142147
(graph-ops/build-block-remove-op db uid))]

src/cljs/athens/events.cljs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,21 +1625,22 @@
16251625
(reg-event-fx
16261626
:paste-internal
16271627
[(interceptors/sentry-span-no-new-tx "paste-internal")]
1628-
(fn [_ [_ uid local-str internal-representation]]
1628+
(fn [_ [_ uid local-str internal-representation target-uid skip-focus?]]
16291629
(when (seq internal-representation)
16301630
(let [[uid] (db/uid-and-embed-id uid)
16311631
op (bfs/build-paste-op @db/dsdb
1632-
uid
1632+
(or target-uid uid)
16331633
local-str
16341634
internal-representation)
16351635
new-titles (graph-ops/ops->new-page-titles op)
16361636
new-uids (graph-ops/ops->new-block-uids op)
16371637
[_rm add] (graph-ops/structural-diff @db/dsdb op)
16381638
event (common-events/build-atomic-event op)
1639-
focus-uid (-> (graph-ops/contains-op? op :block/new)
1640-
first
1641-
:op/args
1642-
:block/uid)]
1639+
focus-uid (when-not skip-focus?
1640+
(-> (graph-ops/contains-op? op :block/new)
1641+
first
1642+
:op/args
1643+
:block/uid))]
16431644
(log/debug "paste internal event is" (pr-str event))
16441645
{:fx [[:async-flow {:id :paste-internal-async-flow
16451646
:db-path [:async-flow :paste-internal]
@@ -1764,3 +1765,13 @@
17641765
(atomic-graph-ops/make-block-open-op block-uid open?))]
17651766
{:fx [[:dispatch [:resolve-transact-forward event]]]})))
17661767

1768+
1769+
(rf/reg-event-fx
1770+
:add-to
1771+
[(interceptors/sentry-span-no-new-tx "add-to")]
1772+
(fn [_ [_ string uid page-title]]
1773+
(log/debug ":add-to args" uid page-title)
1774+
(let [page-uid (common-db/get-page-uid @db/dsdb page-title)
1775+
ref-ir [{:block/uid (common.utils/gen-block-uid)
1776+
:block/string (str "((" uid "))")}]]
1777+
{:fx [[:dispatch [:paste-internal uid string ref-ir page-uid true]]]})))

src/cljs/athens/views/blocks/autocomplete_search.cljs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
f (case (:search/type @state)
1414
:hashtag textarea-keydown/auto-complete-hashtag
1515
:template textarea-keydown/auto-complete-template
16+
:at textarea-keydown/auto-complete-add-to
1617
textarea-keydown/auto-complete-inline)]
1718
(f state target expansion)))
1819

@@ -22,7 +23,7 @@
2223
(fn [block state]
2324
(let [{:keys [last-e]} @state
2425
{:search/keys [index results type query]} @state
25-
is-open (some #(= % type) [:page :block :hashtag :template])]
26+
is-open (some #(= % type) [:page :block :hashtag :template :add-to])]
2627
[:> Autocomplete {:event last-e
2728
:isOpen is-open
2829
:onClose #(swap! state assoc :search/type false)}
@@ -32,7 +33,9 @@
3233
[:> Text {:py "0.4rem"
3334
:px "0.8rem"
3435
:fontStyle "italics"}
35-
(str "Search for a " (symbol type))]
36+
(str "Search for a " (if (= type :add-to)
37+
"page to add to"
38+
(symbol type)))]
3639
(doall
3740
(for [[i {:keys [node/title block/string block/uid]}] (map-indexed list results)]
3841
[:> AutocompleteButton {:key (str "inline-search-item" uid)

src/cljs/athens/views/blocks/core.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
{:block/keys [uid original-uid]} block
291291
state (r/atom {:string/local nil
292292
:string/previous nil
293-
;; one of #{:page :block :slash :hashtag :template}
293+
;; one of #{:page :block :slash :hashtag :template :add-to}
294294
:search/type nil
295295
:search/results nil
296296
:search/query nil

src/cljs/athens/views/blocks/textarea_keydown.cljs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@
133133
:page db/search-in-node-title
134134
:hashtag db/search-in-node-title
135135
:template db/search-in-block-content
136+
:add-to db/search-in-node-title
136137
:slash filter-slash-options)
137138
regex (case type
138139
:block #"(?s).*\(\("
139140
:page #"(?s).*\[\["
140141
:hashtag #"(?s).*#"
141142
:template #"(?s).*;;"
143+
:add-to #"(?s).*\+"
142144
:slash #"(?s).*/")
143145
find (re-find regex head)
144146
query-start-idx (count find)
@@ -302,6 +304,28 @@
302304
(swap! state assoc :search/type nil))))))
303305

304306

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+
305329
;; Arrow Keys
306330

307331

@@ -492,7 +516,8 @@
492516
:page (auto-complete-inline state e)
493517
:block (auto-complete-inline state e)
494518
: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))
496521
;; shift-enter: add line break to textarea and move cursor to the next line.
497522
shift (replace-selection-with "\n")
498523
;; cmd-enter: cycle todo states, then move cursor to the end of the line.
@@ -760,6 +785,8 @@
760785
(and (= "#" look-behind-char) (= type :hashtag)) (swap! state assoc :search/type nil)
761786
;; semicolon: close dropdown
762787
(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)
763790
;; dropdown is open: update query
764791
type (update-query state head "" type))))
765792

@@ -801,6 +828,11 @@
801828
:search/query ""
802829
:search/type :template
803830
: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 [])
804836
type (update-query state head key type))))
805837

806838

0 commit comments

Comments
 (0)