From 229c7f2594df6bb9bd9a68c7b4c4a68cb16bf7e7 Mon Sep 17 00:00:00 2001 From: leizhe Date: Sat, 4 Sep 2021 15:11:07 +0900 Subject: [PATCH] enhance(property): remove empty properties drawer --- src/main/frontend/handler/editor.cljs | 3 ++- src/main/frontend/util/drawer.cljs | 10 ++++++++++ src/main/frontend/util/property.cljs | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index dffd2269d..2825d6089 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -792,7 +792,7 @@ content (drawer/insert-drawer format content "logbook" (util/format (str (if (= :org format) "-" "*") - " State \"DONE\" from \"%s\" [%s]") + " State \"DONE\" from \"%s\" [%s]") marker (date/get-date-time-string-3)))] content) @@ -952,6 +952,7 @@ content (if (nil? value) (property/remove-property format key content) (property/insert-property format content key value)) + content (property/remove-empty-properties content) block (outliner-core/block {:block/uuid block-id :block/properties properties :block/content content}) diff --git a/src/main/frontend/util/drawer.cljs b/src/main/frontend/util/drawer.cljs index 75057d886..2b9ac220f 100644 --- a/src/main/frontend/util/drawer.cljs +++ b/src/main/frontend/util/drawer.cljs @@ -75,6 +75,16 @@ (js/console.error e) content)))) +(defn empty-drawer-typ? + [format content typ] + (when-let [drawer-ast (get-drawer-ast format content typ)] + (empty? (last drawer-ast)))) + +(defn remove-empty-drawer + [format content typ] + (if (empty-drawer-typ? format content typ) + (string/replace content (build-drawer-str typ) ""))) + (defn contains-logbook? [content] (and (util/safe-re-find (re-pattern (str "(?i)" logbook-start)) content) diff --git a/src/main/frontend/util/property.cljs b/src/main/frontend/util/property.cljs index db6b99c9f..ebbb5b45d 100644 --- a/src/main/frontend/util/property.cljs +++ b/src/main/frontend/util/property.cljs @@ -38,6 +38,14 @@ (and (string/includes? content properties-start) (util/safe-re-find properties-end-pattern content))) +(defn remove-empty-properties + [content] + (if (contains-properties? content) + (string/replace content + (re-pattern ":PROPERTIES:\n:END:\n*") + "") + content)) + (defn simplified-property? [line] (boolean