Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reference to page #2184

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 17 additions & 6 deletions src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,21 +1625,22 @@
(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 skip-focus?]]
(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)
new-uids (graph-ops/ops->new-block-uids op)
[_rm add] (graph-ops/structural-diff @db/dsdb op)
event (common-events/build-atomic-event op)
focus-uid (-> (graph-ops/contains-op? op :block/new)
first
:op/args
:block/uid)]
focus-uid (when-not skip-focus?
(-> (graph-ops/contains-op? op :block/new)
first
:op/args
:block/uid))]
(log/debug "paste internal event is" (pr-str event))
{:fx [[:async-flow {:id :paste-internal-async-flow
:db-path [:async-flow :paste-internal]
Expand Down Expand Up @@ -1764,3 +1765,13 @@
(atomic-graph-ops/make-block-open-op block-uid open?))]
{:fx [[:dispatch [:resolve-transact-forward event]]]})))


(rf/reg-event-fx
:add-to
[(interceptors/sentry-span-no-new-tx "add-to")]
(fn [_ [_ string uid page-title]]
(log/debug ":add-to args" uid page-title)
(let [page-uid (common-db/get-page-uid @db/dsdb page-title)
ref-ir [{:block/uid (common.utils/gen-block-uid)
:block/string (str "((" uid "))")}]]
{:fx [[:dispatch [:paste-internal uid string ref-ir page-uid true]]]})))
7 changes: 5 additions & 2 deletions 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
:add-to textarea-keydown/auto-complete-add-to
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 :add-to])]
[:> Autocomplete {:event last-e
:isOpen is-open
:onClose #(swap! state assoc :search/type false)}
Expand All @@ -32,7 +33,9 @@
[:> Text {:py "0.4rem"
:px "0.8rem"
:fontStyle "italics"}
(str "Search for a " (symbol type))]
(str "Search for a " (if (= type :add-to)
"page to add a reference to"
(symbol type)))]
(doall
(for [[i {:keys [node/title block/string block/uid]}] (map-indexed list results)]
[:> AutocompleteButton {:key (str "inline-search-item" uid)
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 :add-to}
:search/type nil
:search/results nil
:search/query nil
Expand Down
39 changes: 35 additions & 4 deletions src/cljs/athens/views/blocks/textarea_keydown.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns athens.views.blocks.textarea-keydown
(:require
["/components/Icons/Icons" :refer [TimeNowIcon PersonIcon CheckboxIcon CalendarNowIcon CalendarTomorrowIcon CalendarYesterdayIcon BlockEmbedIcon TemplateIcon HTMLEmbedIcon YoutubeIcon]]
["/components/Icons/Icons" :refer [TimeNowIcon PersonIcon CheckboxIcon CalendarNowIcon CalendarTomorrowIcon CalendarYesterdayIcon BlockEmbedIcon TemplateIcon HTMLEmbedIcon YoutubeIcon ArrowRightOnBoxIcon]]
[athens.common-db :as common-db]
[athens.common.utils :as common.utils]
[athens.dates :as dates]
Expand Down Expand Up @@ -101,7 +101,8 @@
["YouTube Embed" YoutubeIcon "{{[[youtube]]: }}" nil 2]
["iframe Embed" HTMLEmbedIcon "{{iframe: }}" nil 2]
["Block Embed" BlockEmbedIcon "{{[[embed]]: (())}}" nil 4]
["Template" TemplateIcon ";;" nil nil]]
["Template" TemplateIcon ";;" nil nil]
["Add reference to page" ArrowRightOnBoxIcon "#" nil nil]]
@(subscribe [:db-picker/remote-db?])
(conj (let [username (:username @(rf/subscribe [:presence/current-user]))]
[(str "Me (" username ")") PersonIcon (fn [] (str "[[" username "]]")) nil nil]))))
Expand Down Expand Up @@ -133,12 +134,14 @@
:page db/search-in-node-title
:hashtag db/search-in-node-title
:template db/search-in-block-content
:add-to db/search-in-node-title
:slash filter-slash-options)
regex (case type
:block #"(?s).*\(\("
:page #"(?s).*\[\["
:hashtag #"(?s).*#"
:template #"(?s).*;;"
:add-to #"(?s).*#"
:slash #"(?s).*/")
find (re-find regex head)
query-start-idx (count find)
Expand Down Expand Up @@ -211,6 +214,11 @@
(swap! state assoc
:search/type :template
:search/query ""
:search/results []))
(when (= caption "Add reference to page")
(swap! state assoc
:search/type :add-to
:search/query ""
:search/results [])))))


Expand Down Expand Up @@ -302,6 +310,28 @@
(swap! state assoc :search/type nil))))))


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

([state target expansion]
(let [{:keys [start head]} (destruct-target target)
start-idx (count (re-find #"(?s).*#" head))]
(if (nil? expansion)
(swap! state assoc :search/type nil)
(do
(set-selection target (dec start-idx) start)
(replace-selection-with "")
(swap! state assoc :search/type nil)
(dispatch [:add-to (:string/local @state) (:block/uid @state) expansion]))))))


;; Arrow Keys


Expand Down Expand Up @@ -492,7 +522,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)
:add-to (auto-complete-add-to 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 @@ -757,7 +788,7 @@
;; slash: close dropdown
(and (= "/" look-behind-char) (= type :slash)) (swap! state assoc :search/type nil)
;; hashtag: close dropdown
(and (= "#" look-behind-char) (= type :hashtag)) (swap! state assoc :search/type nil)
(and (= "#" look-behind-char) (#{:hashtag :add-to} type)) (swap! state assoc :search/type nil)
;; semicolon: close dropdown
(and (= ";" look-behind-char) (= type :template)) (swap! state assoc :search/type nil)
;; dropdown is open: update query
Expand Down