فهرست منبع

fix(editor): put content after properties for any block without title

Tienson Qin 5 سال پیش
والد
کامیت
0875af2b7d
4فایلهای تغییر یافته به همراه66 افزوده شده و 39 حذف شده
  1. 9 8
      src/main/frontend/components/content.cljs
  2. 21 16
      src/main/frontend/handler/editor.cljs
  3. 15 7
      src/main/frontend/text.cljs
  4. 21 8
      src/test/frontend/text_test.cljs

+ 9 - 8
src/main/frontend/components/content.cljs

@@ -62,14 +62,15 @@
       :on-click editor-handler/bulk-make-todos}
      (str "Make " (state/get-preferred-todo) "s"))]])
 
+;; FIXME: Make it configurable
 (def block-background-colors
-  ["rgb(83, 62, 125)"
-   "rgb(73, 125, 70)"
-   "rgb(120, 127, 151)"
-   "rgb(151, 134, 38)"
-   "rgb(73, 118, 123)"
-   "rgb(38, 76, 155)"
-   "rgb(121, 62, 62)"])
+  ["#533e7d"
+   "#497d46"
+   "#787f97"
+   "#978626"
+   "#49767b"
+   "#264c9b"
+   "#793e3e"])
 
 (rum/defcs block-template <
   (rum/local false ::edit?)
@@ -147,7 +148,7 @@
                                     content (:block/content block)
                                     content (cond
                                               empty-properties?
-                                              (text/rejoin-properties content {"" ""} false)
+                                              (text/rejoin-properties content {"" ""} {:remove-blank? false})
                                               all-hidden?
                                               (let [idx (string/index-of content "\n:END:")]
                                                 (str

+ 21 - 16
src/main/frontend/handler/editor.cljs

@@ -324,19 +324,23 @@
      (mapv (fn [ref] [:db/retract eid :block/ref-pages ref]) retracted-pages)
      (mapv (fn [ref] [:db/retract eid :block/ref-blocks ref]) retracted-blocks))))
 
-(defn- rebuild-block-content
-  "We'll create an empty heading if the first parsed ast element is not a paragraph, definition list or some hiccup."
+(defn- block-with-title
   [content format]
   (let [content-without-level-spaces (text/remove-level-spaces content format)
         first-block (-> content-without-level-spaces
                         (format/to-edn format)
                         ffirst)]
-    (if (or (block/heading-block? first-block)
-            (block/paragraph-block? first-block)
-            (block/hiccup-block? first-block)
-            (block/definition-list-block? first-block))
-      content
-      (text/append-newline-after-level-spaces content format))))
+    (or (block/heading-block? first-block)
+        (block/paragraph-block? first-block)
+        (block/hiccup-block? first-block)
+        (block/definition-list-block? first-block))))
+
+(defn- rebuild-block-content
+  "We'll create an empty heading if the first parsed ast element is not a paragraph, definition list or some hiccup."
+  [content format]
+  (if (block-with-title content format)
+    content
+    (text/append-newline-after-level-spaces content format)))
 
 (defn- get-edit-input-id-with-block-id
   [block-id]
@@ -407,10 +411,13 @@
     properties))
 
 (defn- block-text-with-time
-  [block format value]
-  (let [value (text/remove-level-spaces value (keyword format))
-        properties (with-time-properties block {})]
-    (text/re-construct-block-properties format value properties)))
+  ([block format value]
+   (block-text-with-time block format value {}))
+  ([block format value properties]
+   (let [properties (with-time-properties block properties)
+         block-with-title? (boolean (block-with-title value format))]
+     (text/re-construct-block-properties format value properties
+                                         block-with-title?))))
 
 (defn save-block-if-changed!
   ([block value]
@@ -455,12 +462,11 @@
                           new-properties)
          text-properties (text/extract-properties value)
          properties (->> custom-properties
-                         (with-time-properties block)
                          (merge text-properties))
          properties (if (and (seq properties) (seq remove-properties))
                       (medley/remove-keys (fn [k] (contains? (set remove-properties) k)) properties)
                       properties)
-         value (text/re-construct-block-properties format value properties)
+         value (block-text-with-time block format value properties)
          content-changed? (not= (text/remove-timestamp-property! (string/trim content))
                                 (text/remove-timestamp-property! (string/trim value)))]
      (cond
@@ -647,8 +653,7 @@
                                      (str fst-block-text "\n" snd-block-text)
                                      value)
                              text-properties (text/extract-properties fst-block-text)
-                             properties (with-time-properties block text-properties)
-                             value (text/re-construct-block-properties format value properties)
+                             value (block-text-with-time block format value text-properties)
                              value (rebuild-block-content value format)
                              [new-content value] (new-file-content block file-content value)
                              parse-result (block/parse-block (assoc block :block/content value) format)

+ 15 - 7
src/main/frontend/text.cljs

@@ -141,17 +141,23 @@
 
 (defn rejoin-properties
   ([content properties]
-   (rejoin-properties content properties true))
-  ([content properties remove-blank?]
+   (rejoin-properties content properties {}))
+  ([content properties {:keys [remove-blank? block-with-title?]
+                        :or {remove-blank? true
+                             block-with-title? true}}]
    (let [properties (if (= (get properties "heading") "false")
                       (dissoc properties "heading")
                       properties)
          properties (if remove-blank?
                       (remove (fn [[k _v]] (string/blank? k)) properties)
                       properties)
-         [title body] (util/safe-split-first "\n" content)
+         [title body] (if block-with-title?
+                        (util/safe-split-first "\n" content)
+                        ["" content])
          properties (build-properties-str properties)]
-     (str title "\n" properties body))))
+     (if block-with-title?
+       (str title "\n" properties body)
+       (str properties body)))))
 
 (defn contains-properties?
   [s]
@@ -184,14 +190,16 @@
          (into {}))))
 
 (defn re-construct-block-properties
-  [format content properties]
+  [format content properties block-with-title?]
   (let [format (keyword format)
         level-spaces (extract-level-spaces content format)
         result (-> content
                    (remove-level-spaces format true)
                    (remove-properties!)
-                   (rejoin-properties properties))]
-    (str level-spaces (string/triml result))))
+                   (rejoin-properties properties {:block-with-title? block-with-title?}))]
+    (str level-spaces
+         (when-not block-with-title? "\n")
+         (string/triml result))))
 
 (defn insert-property
   [content key value]

+ 21 - 8
src/test/frontend/text_test.cljs

@@ -6,26 +6,39 @@
   []
   (testing "block content without a title"
     (are [x y] (= x y)
-      (text/re-construct-block-properties :org "** :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"})
-      "** :PROPERTIES:\n:x: y\n:END:\n"
+      (text/re-construct-block-properties :org "** :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} false)
+      "** \n:PROPERTIES:\n:x: y\n:END:\n"
 
-      (text/re-construct-block-properties :markdown "## :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"})
-      "## :PROPERTIES:\n:x: y\n:END:\n"))
+      (text/re-construct-block-properties :markdown "## :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} false)
+      "## \n:PROPERTIES:\n:x: y\n:END:\n"))
+
+  (testing "query block without a title"
+    (are [x y] (= x y)
+      (text/re-construct-block-properties :org "** #+BEGIN_QUERY
+test
+#+END_QUERY" {"created_at" 1609332958103} false)
+      "** \n:PROPERTIES:\n:created_at: 1609332958103\n:END:\n#+BEGIN_QUERY\ntest\n#+END_QUERY"))
+
+  (testing "table without a title"
+    (are [x y] (= x y)
+      (text/re-construct-block-properties :org "** |x|y|
+|1|2|" {"created_at" 1609332958103} false)
+      "** \n:PROPERTIES:\n:created_at: 1609332958103\n:END:\n|x|y|\n|1|2|"))
 
   (testing "block content with a title"
     (are [x y] (= x y)
-      (text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"})
+      (text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} true)
       "** hello\n:PROPERTIES:\n:x: y\n:END:\n"
 
-      (text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"})
+      (text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} true)
       "## hello\n:PROPERTIES:\n:x: y\n:END:\n"))
 
   (testing "block content with custom properties"
     (are [x y] (= x y)
-      (text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "z"})
+      (text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "z"} true)
       "** hello\n:PROPERTIES:\n:x: z\n:END:\n"
 
-      (text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y" "a" "b"})
+      (text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y" "a" "b"} true)
       "## hello\n:PROPERTIES:\n:x: y\n:a: b\n:END:\n")))
 
 #_(cljs.test/test-ns 'frontend.text-test)