Skip to content

Commit

Permalink
enhance(dwim): re-number its items when dwim in an ordered list (logs…
Browse files Browse the repository at this point in the history
…eq#3358)

* enhance(dwim): re-number list if dwim in ordered list

* fix: regexp for matching a list item

* enhance: don't enable dwim in list if cursor is at beginning of item

Co-authored-by: leizhe <[email protected]>
  • Loading branch information
llcc and leizhe authored Dec 16, 2021
1 parent f94e01d commit a8c9daa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
46 changes: 32 additions & 14 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
[frontend.util.page-property :as page-property]
[frontend.util.property :as property]
[frontend.util.thingatpt :as thingatpt]
[frontend.util.list :as list]
[goog.dom :as gdom]
[goog.dom.classes :as gdom-classes]
[goog.object :as gobj]
Expand Down Expand Up @@ -2422,6 +2423,34 @@
(cursor/move-cursor-backward input move-to-pos)))
(insert "\n")))))))

(defn- dwim-in-list
[state]
(when-not (auto-complete?)
(let [{:keys [block]} (get-state)]
(when block
(let [input (state/get-input)]
(when-let [item (thingatpt/list-item-at-point input)]
(let [{:keys [full-content indent bullet checkbox ordered _]} item
current-bullet (cljs.reader/read-string bullet)
next-bullet (if ordered (str (inc current-bullet) ".") bullet)
checkbox (when checkbox "[ ] ")]
(if (= (count full-content)
(+ (if ordered (+ (count bullet) 2) 2) (when checkbox (count checkbox))))
(delete-and-update input (cursor/line-beginning-pos input) (cursor/line-end-pos input))
(do (cursor/move-cursor-to-line-end input)
(insert (str "\n" indent next-bullet " " checkbox))
(when ordered
(let [bullet-atom (atom (inc current-bullet))]
(while (when-let [next-item (list/get-next-item input)]
(swap! bullet-atom inc)
(let [{:keys [full-content start end]} next-item
new-bullet @bullet-atom]
(delete-and-update input start end)
(insert (string/replace-first full-content (:bullet next-item) new-bullet))
true))
nil)
(cursor/move-cursor-to input (+ (:end item) (count next-bullet) 2)))))))))))))

(defn- keydown-new-block
[state]
(when-not (auto-complete?)
Expand All @@ -2445,7 +2474,7 @@
(when (thingatpt/get-setting :properties?)
(thingatpt/properties-at-point input))
(when (thingatpt/get-setting :list?)
(and (cursor/end-of-line? input) ;; only apply DWIM when cursor at EOL
(and (not (cursor/beginning-of-line? input))
(thingatpt/list-item-at-point input))))]
(cond
thing-at-point
Expand All @@ -2466,19 +2495,8 @@
"page-ref" (when-not (string/blank? (:link thing-at-point))
(insert-first-page-block-if-not-exists! (:link thing-at-point))
(route-handler/redirect-to-page! (:link thing-at-point)))
"list-item"
(let [{:keys [full-content indent bullet checkbox ordered _]} thing-at-point
next-bullet (if ordered
(str (inc (cljs.reader/read-string bullet)) ".")
bullet)
checkbox (when checkbox "[ ] ")]
(if (= (count full-content)
(+ (if ordered (+ (count bullet) 2) 2) (when checkbox (count checkbox))))
(delete-and-update input (cursor/line-beginning-pos input) (cursor/line-end-pos input))
(do (cursor/move-cursor-to-line-end input)
(insert (str "\n" indent next-bullet " " checkbox)))))
"properties-drawer"
(dwim-in-properties state))
"list-item" (dwim-in-list state)
"properties-drawer" (dwim-in-properties state))

(and
(string/blank? content)
Expand Down
20 changes: 20 additions & 0 deletions src/main/frontend/util/list.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns frontend.util.list
(:require [frontend.util.thingatpt :as thingatpt]
[frontend.util.cursor :as cursor]))

(defn- get-prev-item [& [input]]
(when-let [item (thingatpt/list-item-at-point input)]
(let [{:keys [bullet ordered]} item]
(when-not (or (cursor/textarea-cursor-first-row? input)
(and ordered
(= bullet "1")))
(cursor/move-cursor-up input)
(thingatpt/list-item-at-point input)))))

(defn- get-next-item [& [input]]
(when-let [item (thingatpt/list-item-at-point input)]
(let [{:keys [_bullet _ordered]} item]
(when-not (cursor/textarea-cursor-last-row? input)
(cursor/move-cursor-down input)
(thingatpt/list-item-at-point input)))))

4 changes: 2 additions & 2 deletions src/main/frontend/util/thingatpt.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@

(defn- get-list-item-indent&bullet [line]
(when-not (string/blank? line)
(or (re-matches #"^([ \t\r]*)(\+|\*|-) (\[[X ]\])*.*$" line)
(re-matches #"^([\s]*)(\d+)\. (\[[X ]\])*.*$" line))))
(or (re-matches #"^([ \t\r]*)(\+|\*|-){1} (\[[X ]\])?.*$" line)
(re-matches #"^([\s]*)(\d+){1}\. (\[[X ]\])?.*$" line))))

(defn list-item-at-point [& [input]]
(when-let [line (line-at-point input)]
Expand Down

0 comments on commit a8c9daa

Please sign in to comment.