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

fix(blocks): prevent indent when already a leaf block, prevent indent and unindent when already a top-level child #228

Merged
25 changes: 15 additions & 10 deletions src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,11 @@
db/get-block)
new-block {:db/id (:db/id block) :block/order (count (:block/children older-sib))}
reindex-blocks (->> (dec-after (:db/id parent) (:block/order block)))]
{:transact! [[:db/retract (:db/id parent) :block/children (:db/id block)]
{:db/id (:db/id older-sib) :block/children [new-block]} ;; becomes child of older sibling block — same parent but order-1
{:db/id (:db/id parent) :block/children reindex-blocks}]}))
(if (zero? (:block/order block)) ; If the block is already the top in it's level, do not indent
tangjeff0 marked this conversation as resolved.
Show resolved Hide resolved
{}
tangjeff0 marked this conversation as resolved.
Show resolved Hide resolved
{:transact! [[:db/retract (:db/id parent) :block/children (:db/id block)]
{:db/id (:db/id older-sib) :block/children [new-block]} ;; becomes child of older sibling block — same parent but order-1
{:db/id (:db/id parent) :block/children reindex-blocks}]})))


(reg-event-fx
Expand All @@ -497,23 +499,26 @@
(indent uid)))


;; TODO: no-op when user tries to unindent to a child out of current context
(defn unindent
[uid]
[uid context-root]
(let [parent (db/get-parent [:block/uid uid])
grandpa (db/get-parent (:db/id parent))
new-block {:block/uid uid :block/order (inc (:block/order parent))}
reindex-grandpa (->> (inc-after (:db/id grandpa) (:block/order parent))
(concat [new-block]))]
(when (and parent grandpa)
{:transact! [[:db/retract (:db/id parent) :block/children [:block/uid uid]]
{:db/id (:db/id grandpa) :block/children reindex-grandpa}]})))
(if (= (:block/uid parent) context-root) ; if the parent node is the context-root, prevent unindent
{}
(when (and parent grandpa)
{:transact! [[:db/retract (:db/id parent) :block/children [:block/uid uid]]
{:db/id (:db/id grandpa) :block/children reindex-grandpa}]}))))
tangjeff0 marked this conversation as resolved.
Show resolved Hide resolved


(reg-event-fx
:unindent
(fn [_ [_ uid]]
(unindent uid)))
; Pass in the reframe db as a cofx to the :unindent event handler
tangjeff0 marked this conversation as resolved.
Show resolved Hide resolved
(fn [{rfdb :db} [_ uid]]
(let [context-root (get-in rfdb [:current-route :path-params :id])]
tangjeff0 marked this conversation as resolved.
Show resolved Hide resolved
(unindent uid context-root))))


(defn target-child
Expand Down
48 changes: 25 additions & 23 deletions src/cljs/athens/views/blocks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@
(and (= key-code KeyCodes.RIGHT) block-end?) (dispatch [:right uid])

;; -- Tab ----------------------------------------------------------------
(and shift (= key-code KeyCodes.TAB)) (dispatch [:unindent uid])
(= key-code KeyCodes.TAB) (dispatch [:indent uid])
(and shift (= key-code KeyCodes.TAB)) (do (.. e preventDefault)
(dispatch [:unindent uid]))
(= key-code KeyCodes.TAB) (do (.. e preventDefault)
(dispatch [:indent uid]))

;; -- Enter --------------------------------------------------------------

Expand Down Expand Up @@ -455,30 +457,30 @@
:on-change (fn [_] (db-on-change (:atom-string @state) uid))
:on-key-down (fn [e] (on-key-down e uid state))}]
[parse-and-render string]


;; Slash menu
(when (:slash? @state)
[slash-menu-component {:style {:position "absolute"
:top "100%"
:left "-0.125em"}}])
(when (:slash? @state)
[slash-menu-component {:style {:position "absolute"
:top "100%"
:left "-0.125em"}}])

;; Page search menu
(when (:search/page @state)
(let [query (:search/query @state)
results (when (not (str/blank? query))
(db/search-in-node-title query))]
[dropdown {:style {:position "absolute"
:top "100%"
:left "-0.125em"}
:content
(if (not query)
[:div "Start Typing!"]
(for [{:keys [node/title block/uid]} results]
^{:key uid}
[:div {:on-click #(navigate-uid uid)} title]))}]))
(when (:search/page @state)
(let [query (:search/query @state)
results (when (not (str/blank? query))
(db/search-in-node-title query))]
[dropdown {:style {:position "absolute"
:top "100%"
:left "-0.125em"}
:content
(if (not query)
[:div "Start Typing!"]
(for [{:keys [node/title block/uid]} results]
^{:key uid}
[:div {:on-click #(navigate-uid uid)} title]))}]))



;; Drop Indicator
(when (and (= closest-uid uid)
Expand Down