From dc6acbab6c39ed9a8a092f878538bfc908519cca Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Fri, 30 Apr 2021 19:10:49 +0800 Subject: [PATCH] fix: remove-properties! --- src/main/frontend/components/block.cljs | 2 +- src/main/frontend/handler/editor.cljs | 22 ++++---- src/main/frontend/text.cljs | 67 ++++++++++++++----------- src/test/frontend/text_test.cljs | 8 +-- 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 702ee8d28..4a5320b67 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -1362,7 +1362,7 @@ (editor-handler/clear-selection! nil) (editor-handler/unhighlight-blocks!) (let [properties-hidden? (text/properties-hidden? properties) - content (if properties-hidden? (text/remove-properties! content) content) + content (if properties-hidden? (text/remove-properties! format content) content) block (db/pull [:block/uuid (:block/uuid block)]) f #(let [cursor-range (util/caret-range (gdom/getElement block-id))] (state/set-editing! diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 0b92f0a20..5676507a8 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1109,6 +1109,13 @@ (catch js/Error error (log/error :save-block-failed error)))))))) +(defn- clean-content! + [format content] + (->> content + (text/remove-level-spaces format) + (text/remove-properties! format) + string/trim)) + (defn on-up-down [direction] (when (state/editing?) @@ -1126,10 +1133,7 @@ (when sibling-block (when-let [sibling-block-id (dom/attr sibling-block "blockid")] (let [value (state/get-edit-content)] - (when (not= (-> content - (text/remove-level-spaces format) - text/remove-properties! - string/trim) + (when (not= (clean-content! format content) (string/trim value)) (save-block! repo uuid value))) (let [block (db/pull repo '[*] [:block/uuid (cljs.core/uuid sibling-block-id)])] @@ -1967,10 +1971,7 @@ (when sibling-block (when-let [sibling-block-id (dom/attr sibling-block "blockid")] (let [value (state/get-edit-content)] - (when (not= (-> content - (text/remove-level-spaces format) - text/remove-properties! - string/trim) + (when (not= (clean-content! format content) (string/trim value)) (save-block! repo uuid value))) @@ -2017,10 +2018,7 @@ (when-let [sibling-block-id (dom/attr sibling-block "blockid")] (let [content (:block/content block) value (state/get-edit-content)] - (when (not= (-> content - (text/remove-level-spaces format) - text/remove-properties! - string/trim) + (when (not= (clean-content! format content) (string/trim value)) (save-block! repo uuid value))) (let [block (db/pull repo '[*] [:block/uuid (cljs.core/uuid sibling-block-id)])] diff --git a/src/main/frontend/text.cljs b/src/main/frontend/text.cljs index a1e38a490..e8ee4bc3b 100644 --- a/src/main/frontend/text.cljs +++ b/src/main/frontend/text.cljs @@ -149,26 +149,46 @@ (let [ks (map (comp keyword string/lower-case name) (keys properties))] (every? hidden-properties ks)))) -(defn remove-properties! +(defn contains-properties? [content] - (let [lines (string/split-lines content) - [title-lines properties-and-body] (split-with (fn [l] (not (string/starts-with? (string/upper-case (string/triml l)) ":PROPERTIES:"))) lines) - body (drop-while (fn [l] - (let [l' (string/lower-case (string/trim l))] - (or - (and (string/starts-with? l' ":") - (not (string/starts-with? l' ":end:"))) - (string/blank? l)))) - properties-and-body) - body (if (and (seq body) - (string/starts-with? (string/lower-case (string/triml (first body))) ":end:")) - (let [line (string/replace (first body) #"(?i):end:\s?" "")] - (if (string/blank? line) - (rest body) - (cons line (rest body)))) - body)] - (->> (concat title-lines body) - (string/join "\n")))) + (and (string/includes? content properties-start) + (string/includes? content properties-end))) + +(defn simplified-property? + [line] + (some? (first (util/split-first ":: " line)))) + +(defn remove-properties! + [format content] + (let [org? (= format :org)] + (cond + (contains-properties? content) + (let [lines (string/split-lines content) + [title-lines properties-and-body] (split-with (fn [l] (not (string/starts-with? (string/upper-case (string/triml l)) ":PROPERTIES:"))) lines) + body (drop-while (fn [l] + (let [l' (string/lower-case (string/trim l))] + (or + (and (string/starts-with? l' ":") + (not (string/starts-with? l' ":end:"))) + (string/blank? l)))) + properties-and-body) + body (if (and (seq body) + (string/starts-with? (string/lower-case (string/triml (first body))) ":end:")) + (let [line (string/replace (first body) #"(?i):end:\s?" "")] + (if (string/blank? line) + (rest body) + (cons line (rest body)))) + body)] + (->> (concat title-lines body) + (string/join "\n"))) + + (not org?) + (let [lines (string/split-lines content) + non-properties (get (group-by simplified-property? lines) false)] + (string/join "\n" non-properties)) + + :else + content))) (defn build-properties-str [format properties] @@ -180,15 +200,6 @@ (string/join "\n"))] (util/format full-format properties-content)))) -(defn contains-properties? - [content] - (and (string/includes? content properties-start) - (string/includes? content properties-end))) - -(defn simplified-property? - [line] - (some? (first (util/split-first ":: " line)))) - (defn insert-property! [format content key value] (when (and (not (string/blank? (name key))) diff --git a/src/test/frontend/text_test.cljs b/src/test/frontend/text_test.cljs index 11f5bdd20..3b0d5d28c 100644 --- a/src/test/frontend/text_test.cljs +++ b/src/test/frontend/text_test.cljs @@ -110,17 +110,17 @@ [] (testing "properties with non-blank lines" (are [x y] (= x y) - (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\n:END:\n") + (text/remove-properties! :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n") "** hello" - (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\na:b\n:END:\n") + (text/remove-properties! :org "** hello\n:PROPERTIES:\n:x: y\na:b\n:END:\n") "** hello")) (testing "properties with blank lines" (are [x y] (= x y) - (text/remove-properties! "** hello\n:PROPERTIES:\n\n:x: y\n:END:\n") + (text/remove-properties! :org "** hello\n:PROPERTIES:\n\n:x: y\n:END:\n") "** hello" - (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\n\na:b\n:END:\n") + (text/remove-properties! :org "** hello\n:PROPERTIES:\n:x: y\n\na:b\n:END:\n") "** hello"))) (defn test-insert-property