Browse Source

feat: add including-parent property for children-only templates

Tienson Qin 4 years ago
parent
commit
e862cdf081
2 changed files with 39 additions and 10 deletions
  1. 22 2
      src/main/frontend/components/content.cljs
  2. 17 8
      src/main/frontend/components/editor.cljs

+ 22 - 2
src/main/frontend/components/content.cljs

@@ -72,12 +72,28 @@
    "#264c9b"
    "#793e3e"])
 
-(rum/defcs block-template <
+(defonce *including-parent? (atom nil))
+
+(rum/defc template-checkbox
+  [including-parent?]
+  [:div.flex.flex-row
+   [:span.text-medium.mr-2 "Including the parent block in the template?"]
+   (ui/toggle including-parent?
+              #(swap! *including-parent? not))])
+
+(rum/defcs block-template < rum/reactive
   (rum/local false ::edit?)
   (rum/local "" ::input)
   [state block-id]
   (let [edit? (get state ::edit?)
-        input (get state ::input)]
+        input (get state ::input)
+        including-parent? (rum/react *including-parent?)
+        block-id (if (string? block-id) (uuid block-id) block-id)
+        block (db/entity [:block/uuid block-id])
+        has-children? (seq (:block/children block))]
+    (when (and (nil? including-parent?) has-children?)
+      (reset! *including-parent? true))
+
     (if @edit?
       (do
         (state/clear-edit!)
@@ -87,6 +103,8 @@
           {:auto-focus true
            :on-change (fn [e]
                         (reset! input (util/evalue e)))}]
+         (when has-children?
+           (template-checkbox including-parent?))
          (ui/button "Submit"
                     :on-click (fn []
                                 (let [title (string/trim @input)]
@@ -97,6 +115,8 @@
                                        :error)
                                       (do
                                         (editor-handler/set-block-property! block-id "template" title)
+                                        (when-not including-parent?
+                                          (editor-handler/set-block-property! block-id "including-parent" false))
                                         (state/hide-custom-context-menu!)))))))])
       (ui/menu-link
        {:key "Make template"

+ 17 - 8
src/main/frontend/components/editor.cljs

@@ -202,20 +202,29 @@
               chosen-handler (fn [[template db-id] _click?]
                                (if-let [block (db/entity db-id)]
                                  (let [new-level (:block/level edit-block)
+                                       properties (:block/properties block)
+                                       block-uuid (:block/uuid block)
+                                       including-parent? (not= (get properties "including-parent") "false")
                                        template-parent-level (:block/level block)
                                        pattern (config/get-block-pattern format)
                                        content
                                        (block-handler/get-block-full-content
                                         (state/get-current-repo)
                                         (:block/uuid block)
-                                        (fn [{:block/keys [level content properties] :as block}]
-                                          (let [new-level (+ new-level (- level template-parent-level))
-                                                properties' (dissoc (into {} properties) "id" "custom_id" "template")]
-                                            (-> content
-                                                (string/replace-first (apply str (repeat level pattern))
-                                                                      (apply str (repeat new-level pattern)))
-                                                text/remove-properties!
-                                                (text/rejoin-properties properties')))))
+                                        (fn [{:block/keys [uuid level content properties] :as block}]
+                                          (let [parent? (= uuid block-uuid)
+                                                ignore-parent? (and parent? (not including-parent?))]
+                                            (if ignore-parent?
+                                              ""
+                                              (let [new-level (+ new-level
+                                                                 (- level template-parent-level
+                                                                    (if (not including-parent?) 1 0)))
+                                                    properties' (dissoc (into {} properties) "id" "custom_id" "template")]
+                                                (-> content
+                                                   (string/replace-first (apply str (repeat level pattern))
+                                                                         (apply str (repeat new-level pattern)))
+                                                   text/remove-properties!
+                                                   (text/rejoin-properties properties')))))))
                                        content (if (string/includes? (string/trim edit-content) "\n")
                                                  content
                                                  (text/remove-level-spaces content format))