From 7d85770a10fd2d86f059266b1c0ca95a7fd587d5 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 25 Aug 2021 17:37:03 +0800 Subject: [PATCH] fix: /scheduled doesn't work well if the content has extra spaces at the end. --- src/main/frontend/handler/editor.cljs | 18 +++--------------- src/main/frontend/text.cljs | 18 ++++++++++++++++++ src/test/frontend/text_test.cljs | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 4f7002ab69..f91ec653b2 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -987,21 +987,9 @@ (when-let [block (db/pull [:block/uuid block-id])] (let [{:block/keys [content scheduled deadline format]} block content (or (state/get-edit-content) content) - new-line (str (string/upper-case key) ": " value) - new-content (let [lines (string/split-lines content) - new-lines (map (fn [line] - (string/trim - (if (string/starts-with? (string/lower-case line) key) - new-line - line))) - lines) - new-lines (if (not= lines new-lines) - new-lines - (cons (first new-lines) ;; title - (cons - new-line - (rest new-lines))))] - (string/join "\n" new-lines))] + new-content (text/add-timestamp content key value)] + (prn {:new-content new-content + :content content}) (when (not= content new-content) (if-let [input-id (state/get-edit-input-id)] (state/set-edit-content! input-id new-content) diff --git a/src/main/frontend/text.cljs b/src/main/frontend/text.cljs index 695727c0a7..5141dfc18d 100644 --- a/src/main/frontend/text.cljs +++ b/src/main/frontend/text.cljs @@ -222,3 +222,21 @@ (when-let [last-part (last (string/split p #"/"))] ;; a file (string/includes? last-part "."))))) + +(defn add-timestamp + [content key value] + (let [new-line (str (string/upper-case key) ": " value) + lines (string/split-lines content) + new-lines (map (fn [line] + (string/trim + (if (string/starts-with? (string/lower-case line) key) + new-line + line))) + lines) + new-lines (if (not= (map string/trim lines) new-lines) + new-lines + (cons (first new-lines) ;; title + (cons + new-line + (rest new-lines))))] + (string/join "\n" new-lines))) diff --git a/src/test/frontend/text_test.cljs b/src/test/frontend/text_test.cljs index ef44175a89..b725321f8c 100644 --- a/src/test/frontend/text_test.cljs +++ b/src/test/frontend/text_test.cljs @@ -83,4 +83,22 @@ "**foobar" "foobar" "*********************foobar" "foobar"))) +(deftest test-add-timestamp + [] + (are [x y] (= x y) + (text/add-timestamp "LATER hello world\nhello" + "scheduled" + "<2021-08-25 Wed>") + "LATER hello world\nSCHEDULED: <2021-08-25 Wed>\nhello" + + (text/add-timestamp "LATER hello world " + "scheduled" + "<2021-08-25 Wed>") + "LATER hello world\nSCHEDULED: <2021-08-25 Wed>" + + (text/add-timestamp "LATER hello world\nfoo:: bar\ntest" + "scheduled" + "<2021-08-25 Wed>") + "LATER hello world\nSCHEDULED: <2021-08-25 Wed>\nfoo:: bar\ntest")) + #_(cljs.test/test-ns 'frontend.text-test)