ソースを参照

fix: remove-properties!

Tienson Qin 4 年 前
コミット
dc6acbab6c

+ 1 - 1
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!

+ 10 - 12
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)])]

+ 39 - 28
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)))

+ 4 - 4
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