瀏覽代碼

Fix: Auto heading on markdown mode (#8327)

* fix: markdown auto heading

* fix: do not remove heading property

* fix: always clear heading before setting

* fix: auto heading preview on edit mode

* fix: inc

* fix: remove console log

* fix: remove numeric heading and revert changes

* disable current state button
Konstantinos 2 年之前
父節點
當前提交
206844d2a8

+ 5 - 8
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -538,13 +538,10 @@
     (with-path-refs blocks)))
 
 (defn- with-heading-property
-  [properties markdown-heading? size level]
-  (let [properties (if markdown-heading?
-                     (assoc properties :heading size)
-                     properties)]
-    (if (true? (:heading properties))
-      (assoc properties :heading (min 6 level))
-      properties)))
+  [properties markdown-heading? size]
+  (if markdown-heading?
+    (assoc properties :heading size)
+    properties))
 
 (defn- construct-block
   [block properties timestamps body encoded-content format pos-meta with-id? {:keys [block-pattern supported-formats db date-formatter]}]
@@ -566,7 +563,7 @@
                            :meta pos-meta)
                     (dissoc :size))
                 (or (seq (:properties properties)) markdown-heading?)
-                (assoc :properties (with-heading-property (:properties properties) markdown-heading? (:size block) (:level block))
+                (assoc :properties (with-heading-property (:properties properties) markdown-heading? (:size block))
                        :properties-text-values (:properties-text-values properties)
                        :properties-order (vec (:properties-order properties)))
 

+ 6 - 5
src/main/frontend/components/content.cljs

@@ -135,7 +135,8 @@
 (rum/defc ^:large-vars/cleanup-todo block-context-menu-content
   [_target block-id]
     (when-let [block (db/entity [:block/uuid block-id])]
-      (let [format (:block/format block)]
+      (let [format (:block/format block)
+            heading (-> block :block/properties :heading)]
         [:.menu-links-wrapper
          [:div.flex.flex-row.justify-between.py-1.px-2.items-center
           [:div.flex.flex-row.justify-between.flex-1.mx-2.mt-2
@@ -156,6 +157,7 @@
            (for [i (range 1 7)]
              (ui/button
               ""
+              :disabled (= heading i)
               :icon (str "h-" i)
               :title (t :heading i)
               :class "to-heading-button"
@@ -166,12 +168,10 @@
            (ui/button
             ""
             :icon "h-auto"
+            :disabled (= heading true)
             :icon-props {:extension? true}
             :class "to-heading-button"
-            :title (if (= format :markdown)
-                     (str (t :auto-heading) " - " (t :not-available-in-mode format))
-                     (t :auto-heading))
-            :disabled (= format :markdown)
+            :title (t :auto-heading)
             :on-click (fn [_e]
                         (editor-handler/set-heading! block-id format true))
             :intent "link"
@@ -179,6 +179,7 @@
            (ui/button
             ""
             :icon "heading-off"
+            :disabled (not heading)
             :icon-props {:extension? true}
             :class "to-heading-button"
             :title (t :remove-heading)

+ 9 - 3
src/main/frontend/components/editor.cljs

@@ -450,8 +450,12 @@
 
 (defn get-editor-style-class
   "Get textarea css class according to it's content"
-  [content format]
-  (let [content (if content (str content) "")]
+  [block content format]
+  (let [content (if content (str content) "")
+        heading (-> block :block/properties :heading)
+        heading (if (true? heading)
+                  (min (inc (:block/level block)) 6)
+                  heading)]
     ;; as the function is binding to the editor content, optimization is welcome
     (str
      (if (or (> (.-length content) 1000)
@@ -462,6 +466,7 @@
      (case format
        :markdown
        (cond
+         heading (str "h" heading)
          (string/starts-with? content "# ") "h1"
          (string/starts-with? content "## ") "h2"
          (string/starts-with? content "### ") "h3"
@@ -472,6 +477,7 @@
          :else "normal-block")
        ;; other formats
        (cond
+         heading (str "h" heading)
          (and (string/starts-with? content "---\n") (.endsWith content "\n---")) "page-properties"
          :else "normal-block")))))
 
@@ -585,7 +591,7 @@
   lifecycle/lifecycle
   [state {:keys [format block]} id _config]
   (let [content (state/sub-edit-content id)
-        heading-class (get-editor-style-class content format)]
+        heading-class (get-editor-style-class block content format)]
     [:div.editor-inner {:class (if block "block-editor" "non-block-editor")}
 
      (ui/ls-textarea

+ 0 - 8
src/main/frontend/dicts.cljc

@@ -354,7 +354,6 @@
         :remove-heading "Remove heading"
         :heading "Heading {1}"
         :auto-heading "Auto heading"
-        :not-available-in-mode "Not available in {1} mode"
         :open "Open"
         :open-a-directory "Open a local directory"
         :user/delete-account "Delete account"
@@ -1225,7 +1224,6 @@
         :new-graph "Ajouter un nouveau graphe"
         :new-whiteboard "Nouveau tableau blanc"
         :no "Non"
-        :not-available-in-mode "Indisponible en mode {1}"
         :open "Ouvrir"
         :open-a-directory "Ouvrir un dossier local"
         :open-new-window "Nouvelle fenêtre"
@@ -1801,7 +1799,6 @@
            :new-whiteboard "新建白板"
            :all-whiteboards "所有白板"
            :go-to-whiteboard "前往白板"
-           :not-available-in-mode "在{1}模式下不可用"
            :user/delete-account "删除账号"
            :user/delete-your-account "删除你的账号"
            :user/delete-account-notice "你在 logseq.com 发布的页面(假如有的话)也会被删除。"
@@ -3215,7 +3212,6 @@
            :go-to-whiteboard "Ir para o quadro branco"
            :heading "Título {1}"
            :new-whiteboard "Novo quadro branco"
-           :not-available-in-mode "Não disponível no modo {1}"
            :remove-heading "Remover título"
            :untitled "Sem título"
            :accessibility/skip-to-main-content "Ir para o conteúdo principal"
@@ -3644,7 +3640,6 @@
            :remove-heading "Remover título"
            :heading "Título {1}"
            :auto-heading "Título automático"
-           :not-available-in-mode "Não disponível no modo {1}"
            :open "Abrir"
            :open-a-directory "Abrir uma pasta local"
            :user/delete-account "Apagar conta"
@@ -4316,7 +4311,6 @@
         :remove-heading "見出しを消去"
         :heading "見出し {1}"
         :auto-heading "自動見出し"
-        :not-available-in-mode "{1} モードでは利用できません"
         :open "開く"
         :open-a-directory "ローカルディレクトリを開く"
         :user/delete-account "アカウント削除"
@@ -5056,7 +5050,6 @@
         :remove-heading "Başlığı kaldır"
         :heading "Başlık {1}"
         :auto-heading "Otomatik başlık"
-        :not-available-in-mode "{1} modunda kullanılamıyor"
         :open "Aç"
         :open-a-directory "Yerel bir dizin aç"
         :user/delete-account "Hesabı sil"
@@ -6119,7 +6112,6 @@
         :remove-heading                                   "Odstrániť nadpis"
         :heading                                          "Nadpis {1}"
         :auto-heading                                     "Automatický nadpis"
-        :not-available-in-mode                            "Nie je k dispozícii v režime {1}"
         :open                                             "Otvoriť"
         :open-a-directory                                 "Otvoriť lokálny adresár"
         :user/delete-account                              "Odstrániť účet"

+ 17 - 14
src/main/frontend/handler/editor.cljs

@@ -310,8 +310,11 @@
   (let [block (or (and (:db/id block) (db/pull (:db/id block))) block)
         block (merge block
                      (block/parse-title-and-body uuid format pre-block? (:block/content block)))
-        properties (-> (:block/properties block)
-                       (dissoc :heading))
+        properties (:block/properties block)
+        properties (if (and (= format :markdown) 
+                            (number? (:heading properties)))
+                     (dissoc properties :heading)
+                     properties)
         real-content (:block/content block)
         content (if (and (seq properties) real-content (not= real-content content))
                   (property/with-built-in-properties properties content format)
@@ -3553,18 +3556,6 @@
     (first (:block/_parent (db/entity (:db/id block)))))
    (util/collapsed? block)))
 
-(defn set-heading!
-  [block-id format heading]
-  (if (= format :markdown)
-    (let [repo (state/get-current-repo)
-          block (db/entity [:block/uuid block-id])
-          heading (if (true? heading) 2 heading)
-          content' (commands/set-markdown-heading (:block/content block) heading)]
-      (save-block! repo block-id content'))
-    (do
-      (save-current-block!)
-      (set-block-property! block-id "heading" heading))))
-
 (defn remove-heading!
   [block-id format]
   (remove-block-property! block-id "heading")
@@ -3574,6 +3565,18 @@
           content' (commands/clear-markdown-heading (:block/content block))]
       (save-block! repo block-id content'))))
 
+(defn set-heading!
+  [block-id format heading]
+  (remove-heading! block-id format)
+  (if (or (true? heading) (not= format :markdown))
+    (do
+      (save-current-block!)
+      (set-block-property! block-id "heading" heading))
+    (let [repo (state/get-current-repo)
+          block (db/entity [:block/uuid block-id])
+          content' (commands/set-markdown-heading (:block/content block) heading)]
+      (save-block! repo block-id content'))))
+
 (defn block->data-transfer!
   "Set block or page name to the given event's dataTransfer. Used in dnd."
   [block-or-page-name event]