Skip to content

Commit

Permalink
fix cycle-todo! on # heading
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed May 8, 2021
1 parent f1f9800 commit bfae935
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/main/frontend/commands.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@

(defn compute-pos-delta-when-change-marker
[current-input edit-content new-value marker pos]
(let [old-marker (some->> (first (re-find format/bare-marker-pattern edit-content))
(let [old-marker (some->> (first (re-find util/bare-marker-pattern edit-content))
(string/trim))
old-marker (if old-marker old-marker "")
pos-delta (- (count marker)
Expand All @@ -444,7 +444,7 @@
(count (re-find re-pattern prefix))))
new-value (str (subs edit-content 0 pos)
(string/replace-first (subs edit-content pos)
format/marker-pattern
util/marker-pattern
(str marker " ")))]
(state/set-edit-content! input-id new-value)
(let [new-pos (compute-pos-delta-when-change-marker
Expand All @@ -467,8 +467,8 @@
(string/replace-first (subs edit-content pos)
priority-pattern
new-priority))
(re-find format/marker-pattern edit-content)
(string/replace-first edit-content format/marker-pattern
(re-find util/marker-pattern edit-content)
(string/replace-first edit-content util/marker-pattern
(fn [marker] (str marker new-priority " ")))

:else
Expand Down
6 changes: 0 additions & 6 deletions src/main/frontend/format.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,3 @@
[format]
(when-let [record (get-format-record format)]
(protocol/loaded? record)))

(def marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS)?\s?")

(def bare-marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS){1}\s+")
7 changes: 5 additions & 2 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@

(defn- with-timetracking-properties
[block value]
(let [new-marker (first (re-find format/bare-marker-pattern (or value "")))
(let [new-marker (first (re-find util/bare-marker-pattern (or value "")))
new-marker (if new-marker (string/lower-case (string/trim new-marker)))
time-properties (if (and
new-marker
Expand Down Expand Up @@ -594,6 +594,8 @@
(let [edit-input-id (state/get-edit-input-id)
current-input (gdom/getElement edit-input-id)
content (state/get-edit-content)
format (or (db/get-page-format (state/get-current-page))
(state/get-preferred-format))
[new-content marker] (cond
(util/starts-with? content "TODO")
[(string/replace-first content "TODO" "DOING") "DOING"]
Expand All @@ -609,13 +611,14 @@
(let [marker (if (= :now (state/get-preferred-workflow))
"LATER"
"TODO")]
[(str marker " " (string/triml content)) marker]))
[(util/add-or-update-marker (string/triml content) format marker) marker]))
new-content (string/triml new-content)]
(let [new-pos (commands/compute-pos-delta-when-change-marker
current-input content new-content marker (util/get-input-pos current-input))]
(state/set-edit-content! edit-input-id new-content)
(util/set-caret-pos! current-input new-pos)))))


(defn set-marker
[{:block/keys [uuid marker content dummy? properties] :as block} new-marker]
(let [new-content (string/replace-first content marker new-marker)]
Expand Down
24 changes: 24 additions & 0 deletions src/main/frontend/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,30 @@
"DONE" "WAIT" "WAITING" "CANCELED" "CANCELLED" "STARTED" "IN-PROGRESS"}
(string/upper-case s)))

(def marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS)?\s?")

(def bare-marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS){1}\s+")

(defn add-or-update-marker
[content format marker]
(let [[re-pattern new-line-re-pattern]
(if (= :org format)
[#"\*+\s" #"\n\*+\s"]
[#"#+\s" #"\n#+\s"])
pos
(if-let [matches (seq (re-pos new-line-re-pattern content))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
(count (re-find re-pattern content)))
new-content
(str (subs content 0 pos)
(string/replace-first (subs content pos)
marker-pattern
(str marker " ")))]
new-content))

(defn pp-str [x]
(with-out-str (clojure.pprint/pprint x)))

Expand Down

0 comments on commit bfae935

Please sign in to comment.