refactor: cycle-marker

pull/2169/head
Tienson Qin 2021-06-11 11:31:19 +08:00
parent fcd42dfaf1
commit 8faeea1188
2 changed files with 31 additions and 24 deletions

View File

@ -728,30 +728,7 @@
content (state/get-edit-content)
format (or (db/get-page-format (state/get-current-page))
(state/get-preferred-format))
markdown? (= :markdown format)
cond-fn (fn [marker] (or (and markdown? (util/safe-re-find (re-pattern (str "^" "[# ]*" marker)) content))
(util/starts-with? content "TODO")))
marker-pattern (fn [marker] (re-pattern (str "^" (if markdown? "[# ]*") marker)))
replace-marker (fn [old-marker new-marker]
(string/replace-first content (marker-pattern old-marker)
(fn [match]
(string/replace match old-marker new-marker))))
[new-content marker] (cond
(cond-fn "TODO")
[(replace-marker "TODO" "DOING") "DOING"]
(cond-fn "DOING")
[(replace-marker "DOING" "DONE") "DONE"]
(cond-fn "LATER")
[(replace-marker "LATER" "NOW") "NOW"]
(cond-fn "NOW")
[(replace-marker "NOW" "DONE") "DONE"]
(cond-fn "DONE")
[(replace-marker "DONE" "") nil]
:else
(let [marker (if (= :now (state/get-preferred-workflow))
"LATER"
"TODO")]
[(marker/add-or-update-marker (string/triml content) format marker) marker]))
[new-content marker] (marker/cycle-marker content format (state/get-preferred-workflow))
new-content (string/triml new-content)]
(let [new-pos (commands/compute-pos-delta-when-change-marker
current-input content new-content marker (cursor/pos current-input))]

View File

@ -33,3 +33,33 @@
marker-pattern
(str marker " ")))]
new-content))
(defn header-marker-pattern
[markdown? marker]
(re-pattern (str "^" (if markdown? "#*\\s*") marker)))
(defn replace-marker
[content markdown? old-marker new-marker]
(string/replace-first content (header-marker-pattern markdown? old-marker)
(fn [match]
(string/replace match old-marker new-marker))))
(defn cycle-marker
[content format preferred-workflow]
(let [markdown? (= :markdown format)
match-fn (fn [marker] (util/safe-re-find (header-marker-pattern markdown? marker)
content))]
(cond
(match-fn "TODO")
[(replace-marker content markdown? "TODO" "DOING") "DOING"]
(match-fn "DOING")
[(replace-marker content markdown? "DOING" "DONE") "DONE"]
(match-fn "LATER")
[(replace-marker content markdown? "LATER" "NOW") "NOW"]
(match-fn "NOW")
[(replace-marker content markdown? "NOW" "DONE") "DONE"]
(match-fn "DONE")
[(replace-marker content markdown? "DONE" "") nil]
:else
(let [marker (if (= :now preferred-workflow) "LATER" "TODO")]
[(add-or-update-marker (string/triml content) format marker) marker]))))