Skip to content

Commit

Permalink
feat: add to page
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed May 7, 2022
1 parent d4b3f55 commit d421f2a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 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
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
:at 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 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
34 changes: 33 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
: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 @@ -302,6 +304,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 start-idx start)
(replace-selection-with (str "[[" expansion "]]"))
(swap! state assoc :search/type nil)
(dispatch [:add-to (:string/local @state) (:block/uid @state) expansion]))))))


;; Arrow Keys


Expand Down Expand Up @@ -492,7 +516,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 @@ -760,6 +785,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)
;; plus: 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 +828,11 @@
:search/query ""
:search/type :template
:search/results [])
(and (= key "+") (nil? type)) (swap! state assoc
:search/index 0
:search/query ""
:search/type :add-to
:search/results [])
type (update-query state head key type))))


Expand Down

0 comments on commit d421f2a

Please sign in to comment.