Browse Source

enhance: hide built-in properties when editing

Tienson Qin 4 years ago
parent
commit
310f69203b

+ 5 - 3
src/main/frontend/components/block.cljs

@@ -1286,7 +1286,7 @@
 (rum/defc properties-cp
   [config block]
   (let [properties (walk/keywordize-keys (:block/properties block))
-        properties (apply dissoc properties text/hidden-properties)
+        properties (apply dissoc properties text/built-in-properties)
         pre-block? (:block/pre-block? block)
         properties (if pre-block?
                      (dissoc properties :title :filters)
@@ -1355,7 +1355,9 @@
         (editor-handler/clear-selection! nil)
         (editor-handler/unhighlight-blocks!)
         (let [block (or (db/pull [:block/uuid (:block/uuid block)]) block)
-              f #(let [cursor-range (util/caret-range (gdom/getElement block-id))]
+              f #(let [cursor-range (util/caret-range (gdom/getElement block-id))
+                       content (text/remove-built-in-properties! (:block/format block)
+                                                                 content)]
                    (state/set-editing!
                     edit-input-id
                     content
@@ -1437,7 +1439,7 @@
           (timestamp-cp block "SCHEDULED" scheduled-ast)))
 
       (when (and (seq properties)
-                 (let [hidden? (text/properties-hidden? properties)]
+                 (let [hidden? (text/properties-built-in? properties)]
                    (not hidden?))
                  (not (:slide? config)))
         (properties-cp config block))

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

@@ -222,7 +222,9 @@
                           content
 
                           :else
-                          (subs content 0 pos))]
+                          (subs content 0 pos))
+             content (text/remove-built-in-properties! (:block/format block)
+                                                       content)]
          (do
            (clear-selection! nil)
            (state/set-editing! edit-input-id content block text-range move-cursor?)))))))
@@ -277,8 +279,12 @@
     block))
 
 (defn- wrap-parse-block
-  [{:block/keys [content format parent left page uuid pre-block?] :as block}]
-  (let [first-block? (= left page)
+  [{:block/keys [content format parent left page uuid pre-block? properties] :as block}]
+  (let [real-content (:block/content (db/entity (:db/id block)))
+        content (if (and (seq properties) real-content (not= real-content content))
+                  (text/with-built-in-properties properties content format)
+                  content)
+        first-block? (= left page)
         ast (mldoc/->edn (string/trim content) (mldoc/default-config format))
         first-elem-type (first (ffirst ast))
         properties? (contains? #{"Property_Drawer" "Properties"} first-elem-type)

+ 20 - 0
src/main/frontend/modules/shortcut/binding.cljc

@@ -58,6 +58,26 @@
    :editor/delete "delete"
    :editor/delete-selection ["backspace" "delete"]
 
+   ;; clear the block content
+   :editor/clear-block "alt+l"
+   ;; kill the line before the cursor position
+   :editor/kill-line-before "alt+u"
+   ;; kill the line after the cursor position
+   :editor/kill-line-after "alt+k"
+   ;; go to the beginning of the block
+   :editor/beginning-of-block "alt+a"
+   ;; go to the end of the block
+   :editor/end-of-block "alt+e"
+   ;; forward one word
+   :editor/forward-word "alt+f"
+   ;; backward one word
+   :editor/backward-word "alt+b"
+   ;; kill one word backward
+   :editor/backward-kill-word "alt+w"
+   ;; kill one word forward
+   :editor/forward-kill-word "alt+d"
+
+
    :editor/selection-up "up"
    :editor/selection-down "down"
 

+ 2 - 3
src/main/frontend/state.cljs

@@ -719,12 +719,11 @@
                    (assoc block
                           :block/container (gobj/get container "id"))
                    block)
-           content (or content "")]
-
+           content (string/trim (or content ""))]
        (swap! state
               (fn [state]
                 (-> state
-                    (assoc-in [:editor/content edit-input-id] (string/trim content))
+                    (assoc-in [:editor/content edit-input-id] content)
                     (assoc
                      :editor/block block
                      :editor/editing? {edit-input-id true}

+ 31 - 4
src/main/frontend/text.cljs

@@ -139,16 +139,16 @@
 
 ;; properties
 
-(def hidden-properties
+(def built-in-properties
   (set/union
    #{:id :custom-id :background-color :heading :collapsed}
-   config/markers))
+   (set (map keyword config/markers))))
 
-(defn properties-hidden?
+(defn properties-built-in?
   [properties]
   (and (seq properties)
        (let [ks (map (comp keyword string/lower-case name) (keys properties))]
-         (every? hidden-properties ks))))
+         (every? built-in-properties ks))))
 
 (defn contains-properties?
   [content]
@@ -205,6 +205,28 @@
                                   (string/join "\n"))]
       (util/format full-format properties-content))))
 
+;; title properties body
+(defn with-built-in-properties
+  [properties content format]
+  (let [org? (= format :org)
+        properties (filter (fn [[k v]] (built-in-properties k)) properties)]
+    (if (seq properties)
+      (let [[title & body] (string/split-lines content)
+            properties-in-content? (and title (= (string/upper-case title) properties-start))
+            no-title? (or (simplified-property? title) properties-in-content?)
+            built-in-properties-area (map (fn [[k v]] (if org?
+                                              (str ":" (name k) ": " v)
+                                              (str (name k) ":: " v))) properties)
+            body (concat (if no-title? nil [title])
+                         (when (and org? properties-in-content?) [properties-start])
+                         built-in-properties-area
+                         (if (and no-title? (not org?)) [title])
+                         (if (and org? properties-in-content?)
+                           (rest body)
+                           body))]
+        (string/join "\n" body))
+      content)))
+
 ;; FIXME:
 (defn front-matter?
   [s]
@@ -301,6 +323,11 @@
   [format content]
   (remove-property! format "id" content false))
 
+(defn remove-built-in-properties!
+  [format content]
+  (reduce (fn [content key]
+            (remove-property! format key content)) content built-in-properties))
+
 (defn ->new-properties
   "New syntax: key:: value"
   [content]